-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
317 lines (300 loc) · 9.1 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
317
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Item Selector</title>
<!-- Include jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery-ui.min.js"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/jquery-ui.min.css"
/>
<!-- Kontent Styling -->
<link rel="stylesheet" href="custom-element.css" />
<!-- Kentico Custom elements API-->
<script src="https://app.kontent.ai/js-api/custom-element/v1/custom-element.min.js"></script>
<!-- Custom element CSS styles -->
<style>
/* We recommended you always set margin to zero to avoid problems when displaying the element in UI */
@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic");
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
body {
margin: 0;
overflow-y: hidden;
overflow-x: hidden;
margin: 10px;
}
.external-item {
background-color: #ffffff;
border: 2px solid #e0e0e0;
display: flex;
justify-content: space-between;
text-align: left;
width: 100%;
height: 44px;
}
.external-item.selected {
background-color: #f5f5f5;
margin-bottom: 10px;
}
.external-item .item-name {
width: 50%;
padding: 8px;
font-weight: bold;
}
.external-item .item-type {
padding: 8px;
}
.select-item {
width: calc(100vw - 20px);
}
.bar-item__action,
.bar-item__status-icon {
display: -webkit-box;
display: flex;
width: 40px;
height: 40px;
-webkit-box-align: center;
align-items: center;
justify-content: space-around;
color: #9e9e9e;
cursor: pointer;
-webkit-transition: all 0.15s cubic-bezier(0.23, 1, 0.32, 1) 0ms;
transition: all 0.15s cubic-bezier(0.23, 1, 0.32, 1) 0ms;
}
.bar-item__action--is-destructive:hover,
.bar-item__status-icon--is-destructive:hover {
color: #fff;
background-color: #ef5350;
}
li {
margin-top: 5px;
}
.external-item.selected:hover {
background-color: #e3f2fe;
}
.ui-autocomplete {
max-height: 300px;
overflow-y: auto;
overflow-x: hidden;
}
.disabled_overlay {
position: fixed;
background-color: #777;
z-index: 10;
cursor: not-allowed;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0.1;
}
</style>
</head>
<body>
<div class="disabled_overlay"></div>
<div id="selectedItems"></div>
<div id="item-selector" class="item-selector">
<input
id="selectItem"
class="select-item"
placeholder="Search for an item here..."
/>
<div id="item"></div>
</div>
<script>
var projectid = "";
var filter = "";
var items = [];
var selectedItems = [];
function updateDisabled(disabled) {
if (disabled) {
$(".disabled_overlay").show();
} else {
$(".disabled_overlay").hide();
}
}
function setup(value) {
var urlfindItems =
"https://deliver.kontent.ai/" +
projectid +
"/items?depth=0&elements=null";
if (filter) {
urlfindItems += "&" + filter;
}
$.ajax({
url: urlfindItems,
dataType: "text",
success: function(data) {
data = JSON.parse(data);
if (data.items.length > 0) {
processItems(data.items);
loadSelector(value);
} else {
showError(
"There are no items in specified project that would match the configuration."
);
}
}
});
}
function loadSelector(value) {
var storedItems = JSON.parse(value);
loadItems(storedItems);
$("#selectItem")
.autocomplete({
minLength: 1,
source: items,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
addItem(ui.item.value, ui.item.label, ui.item.lang, ui.item.type);
updateSize();
return false;
}
})
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li>")
.append(
"<div class='external-item'><div class='item-name'>" +
item.label +
"</div><div class='item-type'>" +
item.type +
"</div></div>"
)
.appendTo(ul);
};
updateSize();
}
function processItems(data) {
for (var i = 0; i < data.length; i++) {
items.push({
value: data[i].system.codename,
label: data[i].system.name,
lang: data[i].system.language,
type: data[i].system.type
});
}
}
function addItem(codename, name, lang, type) {
if (!checkItems(codename)) {
$("#selectedItems").append(
"<div class='external-item selected' id='" +
codename +
"'><div class='item-name' title='Item name'>" +
name +
"</div><div class='item-type' title='Content type'>" +
type +
"</div><div codename='" +
codename +
"' class='bar-item__action bar-item__action--is-destructive remove-item'><i aria-hidden='true' class='icon-remove'></i></div></div>"
);
$("#selectItem").val("");
$(".remove-iteme").off();
$(".remove-item").click(function(e) {
removeItem($(this).attr("codename"));
});
selectedItems.push({
codename: codename,
name: name,
language: lang,
type: type
});
CustomElement.setValue(JSON.stringify(selectedItems));
} else {
highlightItem(codename);
}
}
function removeItem(codename) {
for (var i = 0; i < selectedItems.length; i++) {
if (selectedItems[i].codename == codename) {
selectedItems.splice(i, 1);
}
}
CustomElement.setValue(JSON.stringify(selectedItems));
$("#" + codename).fadeOut(200);
updateSize();
}
function loadItems(storedItems) {
for (var i = 0; i < storedItems.length; i++) {
addItem(
storedItems[i].codename,
storedItems[i].name,
storedItems[i].language,
storedItems[i].type
);
}
}
function checkItems(codename) {
for (var i = 0; i < selectedItems.length; i++) {
if (selectedItems[i].codename == codename) {
return true;
}
}
return false;
}
function highlightItem(codename) {
$("#" + codename).effect("highlight", { color: "#e3f2fe" }, 2000);
}
function updateSize() {
var height = 500;
try {
height = parseInt($("html").height()) + 300;
} catch (err) {
console.error(err);
}
CustomElement.setHeight(height);
}
function initCustomElement() {
try {
CustomElement.init((element, _context) => {
// Setup with initial value and disabled state
var configError = false;
if (element.config) {
if (element.config.projectid)
projectid = element.config.projectid;
else {
configError = true;
}
if (element.config.filter) filter = element.config.filter;
} else {
configError = true;
}
if (!configError) {
if (element.value) {
setup(element.value);
} else {
setup("[]");
}
} else {
showError(
"Your configuration doesn't contain the projectid value. Please check the documentation of this element."
);
}
updateDisabled(element.disabled);
updateSize();
});
// React when the disabled state changes (e.g. when publishing the item)
CustomElement.onDisabledChanged(updateDisabled);
} catch (err) {
// Initialization with the Custom elements API failed
// (page displayed outside of the Kentico Kontent UI)
console.error(err);
updateDisabled(true);
}
}
initCustomElement();
function showError(message) {
$("#item-selector").html(
'<span class="item-status item-status--failed">' + message + "</span>"
);
}
</script>
</body>
</html>