2021-08-25 20:22:17 -04:00
|
|
|
const $$ = function(selector) {
|
|
|
|
return document.querySelectorAll(selector) || [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeEl = function(html) {
|
2021-08-25 02:08:30 -04:00
|
|
|
const template = document.createElement('template');
|
|
|
|
|
|
|
|
template.innerHTML = html.trim();
|
|
|
|
|
|
|
|
return template.content.firstChild;
|
2021-08-25 20:22:17 -04:00
|
|
|
};
|
2021-08-25 02:08:30 -04:00
|
|
|
|
2021-08-25 20:22:17 -04:00
|
|
|
const escape = function(unsafe) {
|
2021-08-25 02:08:30 -04:00
|
|
|
return unsafe
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
2021-08-25 20:22:17 -04:00
|
|
|
};
|
2021-08-25 02:08:30 -04:00
|
|
|
|
|
|
|
class TagsInput {
|
|
|
|
constructor(element, options = {}) {
|
|
|
|
this.element = element;
|
|
|
|
this.tags = [];
|
|
|
|
this.options = options;
|
|
|
|
|
|
|
|
this.maxTags = options.maxTags || 10;
|
|
|
|
this.inputNode = null;
|
|
|
|
this.containerNode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
attach() {
|
|
|
|
this.element.style.display = 'none';
|
|
|
|
|
2021-08-25 20:22:17 -04:00
|
|
|
this.containerNode = makeEl('<div class="tags-input"></div>');
|
|
|
|
this.inputNode = makeEl('<input class="input" type="text" placeholder="10 tags maximum" value="" />');
|
2021-08-25 02:08:30 -04:00
|
|
|
this.containerNode.appendChild(this.inputNode);
|
|
|
|
|
|
|
|
this.element.parentNode.insertBefore(this.containerNode, this.element.nextSibling);
|
|
|
|
|
2021-08-25 20:22:17 -04:00
|
|
|
/* Load existing tags from input */
|
|
|
|
if (this.element.value) {
|
|
|
|
for (const tag of this.element.value.split(',')) {
|
|
|
|
this.addTag(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 02:08:30 -04:00
|
|
|
/* Handle addition and removal of tags via key-presses */
|
|
|
|
this.containerNode.addEventListener('keydown', this._handleInputKeyUp.bind(this));
|
|
|
|
|
|
|
|
/* Handle deletions by clicking the delete button */
|
|
|
|
this.containerNode.addEventListener('click', this._handleContainerClick.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
detach() {
|
|
|
|
this.tags.clear();
|
|
|
|
this.containerNode.remove();
|
|
|
|
this.element.style.display = 'inline-block';
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHiddenInputValue() {
|
|
|
|
this.element.value = this.tags.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteTagNode(node) {
|
|
|
|
this.tags.splice(this.tags.indexOf(node.dataset.value.toLowerCase()), 1);
|
|
|
|
node.remove();
|
|
|
|
|
|
|
|
/* Below the limit? Make sure the input is enabled. */
|
|
|
|
if (this.tags.length < this.maxTags) {
|
|
|
|
this.inputNode.disabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addTag(tagValue) {
|
|
|
|
tagValue = tagValue.trim();
|
|
|
|
|
|
|
|
/* Tag value is probably not empty and we don't already have the same tag. */
|
|
|
|
if (tagValue !== '' && this.tags.indexOf(tagValue.toLowerCase()) === -1) {
|
|
|
|
this.tags.push(tagValue.toLowerCase());
|
|
|
|
|
|
|
|
this.inputNode.parentNode.insertBefore(
|
2021-08-25 20:22:17 -04:00
|
|
|
makeEl('<span class="tag is-info" data-value="' + escape(tagValue) + '">' + escape(tagValue) + '<span class="delete is-small" /></span>'),
|
2021-08-25 02:08:30 -04:00
|
|
|
this.inputNode
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Too many tags, disable the input for now. */
|
|
|
|
if (this.tags.length >= this.maxTags) {
|
|
|
|
this.inputNode.disabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleInputKeyUp(evt) {
|
|
|
|
let tagValue = this.inputNode.value;
|
|
|
|
|
|
|
|
if (evt.key === 'Backspace' && tagValue === '') {
|
|
|
|
// Remove the child
|
|
|
|
if (this.inputNode.previousSibling) {
|
|
|
|
this.deleteTagNode(this.inputNode.previousSibling);
|
|
|
|
|
|
|
|
this.updateHiddenInputValue();
|
|
|
|
}
|
|
|
|
} else if (evt.key === ',') {
|
|
|
|
this.addTag(tagValue);
|
|
|
|
|
|
|
|
this.inputNode.value = '';
|
|
|
|
this.updateHiddenInputValue();
|
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleContainerClick(evt) {
|
|
|
|
if (evt.target && evt.target.classList.contains('delete')) {
|
|
|
|
this.deleteTagNode(evt.target.closest('.tag'));
|
|
|
|
this.updateHiddenInputValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 20:22:17 -04:00
|
|
|
const setupSite = function() {
|
|
|
|
Array.prototype.forEach.call($$('.js-tag-input'), (el) => {
|
|
|
|
new TagsInput(el).attach();
|
|
|
|
});
|
|
|
|
};
|
2021-08-25 02:08:30 -04:00
|
|
|
|
2021-08-25 20:22:17 -04:00
|
|
|
if (document.readyState !== 'loading') {
|
|
|
|
setupSite();
|
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', setupSite);
|
2021-08-25 02:08:30 -04:00
|
|
|
}
|