-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunique_url_slug.html
253 lines (231 loc) · 7.31 KB
/
unique_url_slug.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unique URL Slug</title>
<!-- Include jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<!-- Include the Custom Elements API-->
<script src="https://app.kontent.ai/js-api/custom-element.js"></script>
<link rel="stylesheet" href="./custom-element.css" />
<!-- 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;
}
[contenteditable=true]:empty:before{
content: attr(placeholder);
color: #aaa;
display: block; /* For Firefox */
}
.status-checking {
color: #ff9800;
}
.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="text-field text-field--has-button" id="element">
<div class="text-field__input u-transparent-border u-no-left-padding">
<div id="unique" class="notranslate public-DraftEditor-content" contenteditable="true" placeholder="Enter slug here..." role="textbox" spellcheck="false" style="outline: none; user-select: text; overflow-wrap: break-word;"></div>
</div>
<div id="regenerate" class="text-field__button-pane">
<div class="text-field__button">
<div class="text-field__button-icon">
<i class="icon-autogenerate" id="autogenerate" title="Autogenerate"></i>
</div>
</div>
</div>
</div>
<div id="unique-status" class="status-checking item-status">checking</div>
<script>
var repeater = '';
var codename = '';
var itemid = '';
var generates_from = '';
var autogenerate = '[autogenerated]';
var force_uniqueness = "false";
var restricted_chars = '[^a-zA-Z0-9]';
function updateDisabled(disabled) {
if (disabled) {
$('.disabled_overlay').show();
}
else {
$('.disabled_overlay').hide();
}
}
function setup(value) {
setValue(value);
$('body').on('input', '[contenteditable]', function() {
autogenerate = '[manual]';
isUnique($('#unique').text());
});
$("#autogenerate").on( "click", function() {
autogenerate = '[autogenerated]';
returnValue(generates_from);
});
}
function updateSize() {
var height = 100;
try {
height = parseInt($("html").height());
} catch (err) {
console.error(err);
}
CustomElement.setHeight(height);
}
function initCustomElement() {
try {
CustomElement.init((element, _context) => {
itemid = _context.item.id;
// Setup with initial value and disabled state
if (element.config) {
if (element.config.repeater) {
repeater = element.config.repeater;
}
else {
showError("Missing 'Repeater' URL for proxying requests from this element to Preview API");
}
if (element.config.codename) {
codename = element.config.codename;
}
else {
showError("Missing codename of 'Custom URL Slug' element");
}
if (element.config.generates_from) {
generates_from = element.config.generates_from;
}
else {
showError("Missing source 'Text' element codename");
}
if (element.config.force_uniqueness) {
force_uniqueness = element.config.force_uniqueness;
}
if (element.config.restricted_chars) {
restricted_chars = element.config.restricted_chars;
}
}
if (element.value) {
autogenerate = JSON.parse(element.value)[1];
setup(JSON.parse(element.value)[0]);
}
else {
setup("");
}
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 Cloud UI)
console.error(err);
updateDisabled(true);
}
}
initCustomElement();
CustomElement.observeElementChanges([], (changedElementCodenames) => {
if (changedElementCodenames[0] == generates_from && autogenerate != '[manual]') {
returnValue(changedElementCodenames[0]);
}
})
function returnValue(codename) {
CustomElement.getElementValue(codename, (value) => {
setValue(value)
});
}
function setValue(value) {
var re = new RegExp(restricted_chars,"g");
value = value.replace(re,'-');
$('#unique').text(value);
isUnique(value);
}
function isUnique(text) {
$('#unique').css("color","unset");
$('#unique-status').removeClass("item-status--is-successful");
$('#unique-status').removeClass("item-status--failed");
$('#unique-status').addClass("status-checking");
$('#unique-status').text("checking");
// HERE comes your own server proxy sevice that forwards JSON from Preview API based on "/items?elements.<codename>[contains]=<value>&depth=0" query
var urlUnique = repeater+"?codename="+codename+"&value="+text;
$.ajax({
url: urlUnique,
dataType: 'text',
success: function (data) {
data = JSON.parse(data);
if (text == $('#unique').text()) {
$('#unique-status').removeClass("status-checking");
$('#unique-status').removeClass("item-status--is-successful");
$('#unique-status').removeClass("item-status--failed");
var name = otherThanCurrent(data.items, text);
if (data.items.length > 0 && name) {
if (force_uniqueness == "true") {
var uniqueValue = text+'-'+makeid(6);
setValue(uniqueValue);
}
else {
$('#unique').css("color","red");
$('#unique-status').addClass("item-status--failed");
$('#unique-status').html("Not unique - value is used in <strong>"+name+"</strong> item " + autogenerate);
}
}
else {
$('#unique').css("color","green");
$('#unique-status').addClass("item-status--is-successful");
$('#unique-status').text("Unique value " + autogenerate);
}
}
var value = [text,autogenerate];
CustomElement.setValue(JSON.stringify(value));
}
});
}
function otherThanCurrent(items, text) {
if (items.length > 0) {
for(var x=0;x<items.length;x++) {
if(items[x].system.id != itemid) {
return items[x].system.name;
}
}
}
return null;
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function showError(message) {
$('#element').hide();
$('#unique-status').css("color","#ef5350");
$('#unique-status').addClass("item-status--failed");
$('#unique-status').text(message);
throw new Error(message);
}
</script>
</body>
</html>