-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_editor.js
154 lines (126 loc) · 3.24 KB
/
tag_editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { el } from './utils.js';
export function TagEditor(allTags, initSelectedTags) {
const dom = el('div');
dom.classList.add('tag-editor');
const title = el('div');
title.innerText = "Tags:";
dom.appendChild(title);
let tagList = createTagList(initSelectedTags);
dom.appendChild(tagList);
let selectedTags = [];
let select = TagSelect(allTags);
wireSelect();
dom.appendChild(select);
function notifyChanged() {
select.dispatchEvent(new CustomEvent('tags-changed', {
bubbles: true,
detail: {
tags: selectedTags,
},
}));
}
function wireSelect() {
select.addEventListener('tag-selected', (e) => {
selectTag(e.detail.tagName);
});
}
function selectTag(tagName) {
if (tagName && !selectedTags.includes(tagName)) {
selectedTags.push(tagName);
replaceTagList();
notifyChanged();
}
}
function createTagList(tags) {
const tagList = TagList(tags);
tagList.addEventListener('tag-clicked', (e) => {
selectedTags = selectedTags.filter(t => t !== e.detail.tag);
replaceTagList();
notifyChanged();
});
return tagList;
}
function replaceTagList() {
const newTagList = createTagList(selectedTags);
dom.replaceChild(newTagList, tagList);
tagList = newTagList;
}
function updateTags(newTags) {
const newSelect = TagSelect(newTags);
dom.replaceChild(newSelect, select);
select = newSelect;
wireSelect();
}
for (const tag of initSelectedTags) {
selectTag(tag);
}
return {
dom,
updateTags,
};
}
function TagSelect(allTags) {
const select = el('select', {
onchange: (e) => {
let tagName = select.value;
select.value = 'choose';
if (tagName === 'create') {
const newTag = prompt("Enter a tag");
if (newTag && !allTags[newTag]) {
select.dispatchEvent(new CustomEvent('create-tag', {
bubbles: true,
detail: {
tag: newTag,
},
}));
tagName = newTag;
}
else {
alert("Tag already exists");
return;
}
}
select.dispatchEvent(new CustomEvent('tag-selected', {
bubbles: true,
detail: {
tagName,
},
}));
},
});
const firstOption = el('option');
firstOption.value = 'choose';
firstOption.innerText = "Select a tag";
select.appendChild(firstOption);
for (const tag of Object.keys(allTags).sort()) {
const option = el('option');
option.setAttribute('value', tag);
option.innerText = tag;
select.appendChild(option);
}
const addTagOption = el('option');
addTagOption.setAttribute('value', 'create');
addTagOption.innerText = "New tag";
select.appendChild(addTagOption);
return select;
}
export function TagList(tags) {
const tagList = el('div');
tagList.classList.add('tag-list');
for (const tag of tags) {
const tagEl = el('div', {
onclick: (e) => {
tagList.dispatchEvent(new CustomEvent('tag-clicked', {
bubbles: true,
detail: {
tag,
}
}));
},
});
tagEl.classList.add('tag-list__tag');
tagEl.innerText = tag;
tagList.appendChild(tagEl);
}
return tagList;
}