-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
316 lines (248 loc) · 11 KB
/
index.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Core extensions CSS from Contentful. -->
<link rel="stylesheet" href="//contentful.github.io/ui-extensions-sdk/cf-extension.css">
<style>
.tag-field {
z-index: 2
}
ul.suggestion-text {
visibility: hidden;
background-color: rgba(255, 255, 255, 0.7);
padding: 5px 5px;
border-radius: 6px;
margin-top: -33px;
margin-left: -3px;
position: absolute;
}
span.known {
color: #2b502b;
font-weight: bolder;
font-size: 16px;
}
span.guess {
color: #858585;
font-weight: bold;
font-family: monospace;
}
span.instruction {
color: #9cb883;
padding-right: 1em;
padding-left: 1em;
font-style: italic;
}
ul {
list-style: none;
}
ul.tag-list {
display: flex;
flex-wrap: wrap;
padding-left: 0px;
margin-top: 1rem;
margin-bottom: 0px;
}
.tag-item {
background: #eef2f4;
line-height: 2rem;
border-radius: 2px;
padding: 1px 2ex 0 2ex;
text-overflow: ellipsis;
color: #536171;
margin-bottom: 0.643em;
margin-right: 1rem;
}
.tag-item-remove {
height: 2rem;
float: right;
margin-right: -2ex;
outline: 0;
border: 0;
cursor: pointer;
background: none;
color: inherit;
}
</style>
</head>
<body>
<template id="tag">
<li class="tag-item">
<span class="tag-label"></span>
<button aria-label="remove" type="button" class="tag-item-remove">
<cf-icon name="close">
<svg width="18" height="14" viewBox="-1 -1 20 20" xmlns="http://www.w3.org/2000/svg">
<g fill="none" fill-rule="evenodd">
<g fill="#21304A">
<path d="M6.23 9L1.386 4.154 0 2.769 2.77 0l1.384 1.385L9 6.23l4.846-4.846L15.231 0 18 2.77l-1.385 1.384L11.77 9l4.846 4.846L18 15.231 15.23 18l-1.384-1.385L9 11.77l-4.846 4.846L2.769 18 0 15.23l1.385-1.384L6.23 9z"></path>
</g>
</g>
</svg>
</cf-icon>
</button>
</li>
</template>
<template id="suggestion-template">
<li class="suggestion">
<span class="known"></span><span class="guess"></span><span class="instruction"></span>
</li>
</template>
<template id="alert-text">
<p>Field Empty... (Confirm an entry with `Enter` key)</p>
</template>
<!-- Extension DOM Elements -->
<div class="cf-form-field">
<input type="text" class="cf-form-input tag-field" id="tag-field">
<ul class="suggestion-text" id="suggestion-text" style="pointer-events: none">
</ul>
<div class="cf-form-hint">
<ul class="tag-list" id="tag-list">
</ul>
</div>
</div>
<!-- Contenful UI Extensions SDK API -->
<script src="//contentful.github.io/ui-extensions-sdk/cf-extension-api.js"></script>
<!-- Javascript event-listeners -->
<script>
document.getElementById("tag-field").onblur = function() {
document.getElementById("suggestion-text").style.visibility = 'hidden';
document.querySelector('#tag-field').placeholder = document.querySelector('#tag-field').value;
document.querySelector('#tag-field').value = '';
}
document.getElementById("tag-field").onfocus = function() {
document.getElementById("suggestion-text").style.visibility = 'visible';
document.querySelector('#tag-field').value = document.querySelector('#tag-field').placeholder;
document.querySelector('#tag-field').placeholder = '';
}
</script>
<script>
// Set this to 'TRUE', before publising the extention, in order to restrict matches to within the same content-type.
var restrictMatchesToWithinContentTypes = false;
function tagExtension(extensionsApi) {
extensionsApi.window.startAutoResizer();
// Get the DOM elements
var tagFieldEl = document.querySelector('#tag-field');
var tagListEl = document.querySelector('#tag-list');
var suggestionTextEl = document.querySelector('.suggestion-text');
// Disable the field whilst we fetch Tags
tagFieldEl.disabled = true;
var entry = extensionsApi.entry;
var entryContentType = entry.getSys().contentType.sys.id;
var field = extensionsApi.field;
var fieldName = field.id
// Get all entries in the space...
var allEntries = extensionsApi.space.getEntries({});
var uniqueTags = null;
var suggestions = [];
var _thisFieldIsSymbol = field.type == 'Symbol';
var _tagValues = !field.getValue() ? [] : ( _thisFieldIsSymbol ? [field.getValue()] : field.getValue());
renderTags();
allEntries.then(function (entries) {
var allTags = [];
var eItems = entries.items;
for (var i = 0, len = eItems.length; i < len; i++) {
if ( (!restrictMatchesToWithinContentTypes || eItems[i].sys.contentType.sys.id == entryContentType) &&
eItems[i].fields.hasOwnProperty(fieldName) &&
eItems[i].fields[fieldName].hasOwnProperty(field.locale) )
{
allTags = allTags.concat(eItems[i].fields[fieldName][field.locale]);
}
};
uniqueTags = allTags.filter(function (item, pos) {
return allTags.indexOf(item) == pos;
});
tagFieldEl.disabled = false;
});
window.addEventListener('onbeforeunload', onBeforeUnloadHandler);
tagFieldEl.addEventListener('input', keyboardInputHandler);
tagFieldEl.addEventListener('keydown', addTagHandler);
function keyboardInputHandler(event) {
renderSuggestions();
}
function addTagHandler(event) {
var tagValue = tagFieldEl.value.trim()
var key = event.which || event.keyCode;
if ( (key === 39 || key === 9) && suggestions[0] ) { // 9 is Tab // 39 is Right Arrow Key
event.preventDefault();
tagFieldEl.value = suggestions[0];
renderSuggestions();
return false;
} else if ( key === 13 && tagValue ) { // 13 is Enter
event.preventDefault();
if ( _thisFieldIsSymbol ) { _tagValues = [tagValue]; }
else { _tagValues.splice(0,0,tagValue); }
tagFieldEl.value = '';
renderTags();
renderSuggestions();
return false;
}
}
function onBeforeUnloadHandler(event) {
window.removeEventListener('onbeforeunload', onBeforeUnloadHandler);
tagFieldEl.removeEventListener('input', keyboardInputHandler);
tagFieldEl.removeEventListener('keypress', addTagHandler);
}
function findSuggestions() {
if ( tagFieldEl.value == '' ) return [];
var suggestionsList = [];
var matcher = new RegExp('^' + tagFieldEl.value);
for (var i = 0; i < uniqueTags.length; i++) {
var x = uniqueTags[i]
if ( matcher.test(x) ) suggestionsList.push(x);
}
return suggestionsList.sort(function (a, b) {
return a.length - b.length;
});
}
function renderSuggestions() {
suggestionTextEl.style.visibility = 'hidden';
suggestions = findSuggestions();
while (suggestionTextEl.firstChild) suggestionTextEl.removeChild(suggestionTextEl.firstChild);
// Do not render if there are no tags.
if ( !uniqueTags || suggestions.length == 0 ) {
console.log('No tags found!');
return;
}
for (var i = 0, len = suggestions.length; i < len; i++) {
var suggestionTemplate = document.querySelector('#suggestion-template').content;
suggestionTemplate.querySelector('.known').innerHTML = tagFieldEl.value;
suggestionTemplate.querySelector('.guess').innerHTML = suggestions[i].slice(tagFieldEl.value.length);
if ( i.toString() == "0" ) { suggestionTemplate.querySelector('.instruction').innerHTML = "→ Tab"; }
else { suggestionTemplate.querySelector('.instruction').innerHTML = ""; }
var thisSuggestion = document.importNode(suggestionTemplate, true);
document.querySelector('#suggestion-text').appendChild(thisSuggestion);
}
suggestionTextEl.style.visibility = 'visible';
}
function removeTag(index) {
if ( index > -1 &&
_tagValues &&
_tagValues.length > index )
{
_tagValues.splice(index, 1);
}
renderTags();
}
function renderTags() {
if ( _thisFieldIsSymbol ) { field.setValue(_tagValues[0]); }
else { field.setValue(_tagValues); }
while (tagListEl.firstChild) tagListEl.removeChild(tagListEl.firstChild);
_tagValues.filter(function (item, pos) {
return _tagValues.indexOf(item) == pos;
});
_tagValues.forEach(function (tag, index) {
var tagTemplate = document.querySelector('#tag').content;
tagTemplate.querySelector('.tag-item span.tag-label').textContent = tag;
var thisTag = document.importNode(tagTemplate, true);
document.querySelector('#tag-list').appendChild(thisTag);
document.querySelector('#tag-list li:last-child button').onclick = function () { removeTag(index); }
});
if ( !tagListEl.firstChild ) {
document.querySelector('#tag-list').appendChild( document.importNode( document.querySelector('#alert-text').content , true ) )
}
}
}
window.contentfulExtension.init(tagExtension);
</script>
</body>
</html>