2019-09-13 22:47:07 +12:00
|
|
|
(function(window) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
window.ls.container.get("view").add({
|
|
|
|
selector: "data-forms-tags",
|
|
|
|
controller: function(element) {
|
|
|
|
let array = [];
|
|
|
|
let tags = window.document.createElement("div");
|
|
|
|
let preview = window.document.createElement("ul");
|
|
|
|
let add = window.document.createElement("input");
|
|
|
|
|
2020-05-26 17:10:13 +12:00
|
|
|
let listen = function(event) {
|
2019-09-13 22:47:07 +12:00
|
|
|
if (
|
2019-12-24 18:43:15 +13:00
|
|
|
(event.key === "Enter" || event.key === " " || event.key === "Tab") &&
|
2019-09-13 22:47:07 +12:00
|
|
|
add.value.length > 0
|
|
|
|
) {
|
|
|
|
array.push(add.value);
|
|
|
|
|
|
|
|
add.value = "";
|
|
|
|
|
|
|
|
element.value = JSON.stringify(array);
|
|
|
|
|
|
|
|
check();
|
|
|
|
|
2019-12-24 18:43:15 +13:00
|
|
|
if(event.key !== "Tab") { // Don't lock accessibility
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2019-09-13 22:47:07 +12:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
(event.key === "Backspace" || event.key === "Delete") &&
|
|
|
|
add.value === ""
|
|
|
|
) {
|
|
|
|
array.splice(-1, 1);
|
|
|
|
|
|
|
|
element.value = JSON.stringify(array);
|
|
|
|
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-05-26 17:10:13 +12:00
|
|
|
};
|
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
let check = function() {
|
|
|
|
try {
|
|
|
|
array = JSON.parse(element.value) || [];
|
|
|
|
} catch (error) {
|
|
|
|
array = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(array)) {
|
|
|
|
array = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.innerHTML = "";
|
|
|
|
|
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
let value = array[index];
|
|
|
|
let tag = window.document.createElement("li");
|
|
|
|
|
2020-04-28 06:34:52 +12:00
|
|
|
if(!value || value === ' ') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
tag.className = "tag";
|
|
|
|
tag.textContent = value;
|
|
|
|
|
|
|
|
tag.addEventListener("click", function() {
|
|
|
|
array.splice(index, 1);
|
|
|
|
|
|
|
|
element.value = JSON.stringify(array);
|
|
|
|
|
|
|
|
check();
|
|
|
|
});
|
|
|
|
|
|
|
|
preview.appendChild(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.required && array.length === 0) {
|
|
|
|
add.setCustomValidity("Please add permissions");
|
|
|
|
} else {
|
|
|
|
add.setCustomValidity("");
|
2019-08-22 17:56:01 +12:00
|
|
|
}
|
2019-09-13 22:47:07 +12:00
|
|
|
};
|
|
|
|
|
2020-05-26 17:10:13 +12:00
|
|
|
tags.className = "tags";
|
|
|
|
|
|
|
|
preview.className = "tags-list";
|
|
|
|
|
|
|
|
add.type = "text";
|
|
|
|
add.className = "add";
|
|
|
|
add.placeholder = element.placeholder;
|
|
|
|
|
|
|
|
//add.required = element.required;
|
|
|
|
|
|
|
|
tags.addEventListener("click", function() {
|
|
|
|
add.focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
add.addEventListener("keydown", listen);
|
|
|
|
add.addEventListener("blur", function(event) {
|
|
|
|
if(add.value !== '') {
|
|
|
|
array.push(add.value);
|
|
|
|
|
|
|
|
add.value = "";
|
|
|
|
|
|
|
|
element.value = JSON.stringify(array);
|
|
|
|
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
tags.appendChild(preview);
|
|
|
|
tags.appendChild(add);
|
|
|
|
|
|
|
|
element.parentNode.insertBefore(tags, element);
|
|
|
|
|
|
|
|
element.addEventListener("change", check);
|
2019-08-22 17:56:01 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
check();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})(window);
|