tissue/resources/assets/js/checkin.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

function updateTags() {
$('input[name=tags]').val(
$('#tags')
.find('li')
.map(function () {
return $(this).data('value');
})
.get()
.join(' ')
);
}
2019-05-24 01:08:47 +09:00
function insertTag(value: string) {
2019-04-21 17:12:18 +09:00
$('<li class="list-inline-item badge badge-primary" style="cursor: pointer;"><span class="oi oi-tag"></span> <span></span> | x</li>')
.data('value', value)
.children(':last-child')
.text(value)
.end()
.appendTo('#tags');
}
2019-05-24 01:08:47 +09:00
const initTags = $('input[name=tags]').val() as string;
if (initTags.trim() !== '') {
initTags.split(' ').forEach(function (value) {
insertTag(value);
});
}
2019-05-24 01:08:47 +09:00
$('#tagInput').on('keydown', function (ev: JQuery.KeyDownEvent) {
const $this = $(this);
let value = $this.val() as string;
if (value.trim() !== '') {
switch (ev.key) {
case 'Tab':
case 'Enter':
case ' ':
2019-05-24 01:08:47 +09:00
if ((ev.originalEvent as any).isComposing !== true) {
insertTag(value.trim());
$this.val('');
updateTags();
}
ev.preventDefault();
break;
}
} else if (ev.key === 'Enter') {
// 誤爆防止
ev.preventDefault();
}
});
$('#tags')
.on('click', 'li', function (ev) {
$(this).remove();
updateTags();
})
.parent()
.on('click', function (ev) {
$('#tagInput').focus();
});