1
0
Fork 0
mirror of synced 2024-07-20 13:55:53 +12:00
appwrite/public/scripts/views/forms/custom-id.js

153 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-08-03 22:29:33 +12:00
(function (window) {
"use strict";
window.ls.container.get("view").add({
selector: "data-custom-id",
2021-08-05 22:33:22 +12:00
controller: function (element, sdk, console, window) {
2021-08-04 20:36:37 +12:00
let prevData = "";
2021-08-05 22:33:22 +12:00
let idType = element.getAttribute('data-id-type');
2021-08-09 17:12:21 +12:00
let disableSwitch = element.getAttribute('data-disable-switch');
2021-08-03 22:29:33 +12:00
2021-08-04 20:36:37 +12:00
const div = window.document.createElement("div");
2021-08-09 17:12:21 +12:00
if(disableSwitch !== "true") {
div.className = "input-copy";
}
2021-08-03 22:29:33 +12:00
2021-08-04 20:36:37 +12:00
const button = window.document.createElement("i");
2021-08-03 22:29:33 +12:00
button.type = "button";
button.style.cursor = "pointer";
2021-08-05 22:33:22 +12:00
const writer = window.document.createElement("input");
2021-08-03 22:29:33 +12:00
writer.type = "text";
writer.setAttribute("maxlength", element.getAttribute("maxlength"));
2021-08-04 20:36:37 +12:00
const placeholder = element.getAttribute("placeholder");
2021-08-03 22:29:33 +12:00
if (placeholder) {
writer.setAttribute("placeholder", placeholder);
}
2021-08-05 22:33:22 +12:00
const info = window.document.createElement("div");
2021-08-03 22:29:33 +12:00
info.className = "text-fade text-size-xs margin-top-negative-small margin-bottom";
div.appendChild(writer);
2021-08-09 17:12:21 +12:00
if(disableSwitch !== "true") {
div.appendChild(button);
}
2021-08-03 22:29:33 +12:00
element.parentNode.insertBefore(div, element);
element.parentNode.insertBefore(info, div.nextSibling);
2021-07-26 19:24:29 +12:00
2021-08-04 20:36:37 +12:00
const switchType = function (event) {
2021-08-03 22:29:33 +12:00
if (idType == "custom") {
idType = "auto";
setIdType(idType);
} else {
idType = "custom";
setIdType(idType);
2021-07-26 19:24:29 +12:00
}
2021-08-03 22:29:33 +12:00
}
2021-07-26 19:24:29 +12:00
2021-08-04 20:36:37 +12:00
const validate = function (event) {
const [service, method] = element.dataset["validator"].split('.');
2021-08-03 23:23:13 +12:00
const value = event.target.value;
if (value.length < 1) {
event.target.setCustomValidity("ID is required");
} else {
2021-08-05 22:33:22 +12:00
switch (service) {
2021-08-04 20:36:37 +12:00
case 'projects':
2021-08-06 21:26:53 +12:00
setValidity(console[service][method](value), event.target);
2021-08-04 20:36:37 +12:00
break;
default:
setValidity(sdk[service][method](value), event.target);
}
2021-08-03 23:23:13 +12:00
}
}
2021-08-04 20:36:37 +12:00
const setValidity = async function (promise, target) {
try {
await promise;
target.setCustomValidity("ID already exists");
} catch (e) {
target.setCustomValidity("");
}
}
const setIdType = function (idType) {
2021-08-03 22:29:33 +12:00
if (idType == "custom") {
2021-08-05 22:33:22 +12:00
element.setAttribute("data-id-type", idType);
2021-12-30 06:09:21 +13:00
info.innerHTML = "Allowed Characters A-Z, a-z, 0-9, and non-leading underscore, hyphen and dot";
2021-08-05 22:33:22 +12:00
if (prevData === 'auto-generated') {
prevData = ""
}
writer.setAttribute("value", prevData);
2021-08-03 22:29:33 +12:00
writer.value = prevData;
element.value = prevData;
2021-08-05 22:33:22 +12:00
writer.removeAttribute("disabled");
2021-08-03 22:29:33 +12:00
writer.focus();
2021-08-03 23:23:13 +12:00
writer.addEventListener('blur', validate);
2021-08-03 22:29:33 +12:00
} else {
2021-08-05 22:33:22 +12:00
idType = 'auto'
element.setAttribute("data-id-type", idType);
info.innerHTML = "Appwrite will generate a unique ID";
2021-08-03 22:29:33 +12:00
prevData = writer.value;
2021-08-05 22:33:22 +12:00
writer.setAttribute("disabled", true);
writer.setAttribute("value", "auto-generated");
writer.value = "auto-generated";
2021-08-03 22:29:33 +12:00
element.value = 'unique()';
2021-07-26 22:05:31 +12:00
}
2021-08-04 20:53:05 +12:00
button.className = idType == "custom" ? "icon-shuffle copy" : "icon-edit copy";
2021-08-03 22:29:33 +12:00
}
2021-07-26 19:24:29 +12:00
2021-08-04 21:01:24 +12:00
const syncEditorWithID = function (event) {
2021-08-05 17:24:41 +12:00
if (element.value !== 'unique()' || idType != 'auto') {
2021-08-03 22:29:33 +12:00
writer.value = element.value;
2021-08-03 21:49:41 +12:00
}
2021-08-05 22:33:22 +12:00
if (idType == 'auto') {
2021-08-05 17:24:41 +12:00
element.value = 'unique()';
}
2021-08-03 22:29:33 +12:00
}
2021-08-05 16:11:08 +12:00
const keypress = function (e) {
// which key is pressed, keyPressed = e.which || e.keyCode;
const key = e.which || e.keyCode;
const ZERO = 48;
const NINE = 57;
const SMALL_A = 97;
const SMALL_Z = 122;
const CAPITAL_A = 65;
const CAPITAL_Z = 90;
const UNDERSCORE = 95;
const HYPHEN = 45;
const PERIOD = 46;
2021-08-05 16:11:08 +12:00
const isNotValidDigit = key < ZERO || key > NINE;
const isNotValidSmallAlphabet = key < SMALL_A || key > SMALL_Z;
const isNotValidCapitalAlphabet = key < CAPITAL_A || key > CAPITAL_Z;
const isNotValidFirstChar = (key === UNDERSCORE || key === HYPHEN || key === PERIOD);
2021-08-05 16:11:08 +12:00
//Leading underscore is prevented
if ( isNotValidFirstChar && e.target.value.length == 0) {
2021-08-05 16:11:08 +12:00
e.preventDefault();
}
if (key != UNDERSCORE && key != HYPHEN && key != PERIOD && isNotValidDigit && isNotValidSmallAlphabet && isNotValidCapitalAlphabet) {
2021-08-05 16:11:08 +12:00
e.preventDefault();
}
}
2021-08-04 21:03:02 +12:00
syncEditorWithID();
2021-08-03 22:29:33 +12:00
setIdType(idType);
2021-08-04 21:03:02 +12:00
writer.addEventListener("change", function (event) {
element.value = writer.value;
});
2021-08-05 22:33:22 +12:00
writer.form.addEventListener('reset', function (event) {
const resetEvent = new Event('reset');
element.dispatchEvent(resetEvent);
});
element.addEventListener('reset', function (event) {
idType = element.getAttribute('data-id-type');
setIdType(idType);
});
2021-08-03 22:29:33 +12:00
writer.addEventListener('keypress', keypress);
button.addEventListener("click", switchType);
}
});
})(window);