]>
Commit | Line | Data |
---|---|---|
a33c5653 | 1 | import Awesomplete from 'awesomplete'; |
b9b41d25 | 2 | |
82e3bb5f A |
3 | /** |
4 | * Find a parent element according to its tag and its attributes | |
5 | * | |
6 | * @param element Element where to start the search | |
7 | * @param tagName Expected parent tag name | |
8 | * @param attributes Associative array of expected attributes (name=>value). | |
9 | * | |
10 | * @returns Found element or null. | |
11 | */ | |
a33c5653 A |
12 | function findParent(element, tagName, attributes) { |
13 | const parentMatch = key => attributes[key] !== '' && element.getAttribute(key).indexOf(attributes[key]) !== -1; | |
14 | while (element) { | |
15 | if (element.tagName.toLowerCase() === tagName) { | |
16 | if (Object.keys(attributes).find(parentMatch)) { | |
17 | return element; | |
18 | } | |
aa4797ba | 19 | } |
a33c5653 A |
20 | element = element.parentElement; |
21 | } | |
22 | return null; | |
aa4797ba A |
23 | } |
24 | ||
82e3bb5f A |
25 | /** |
26 | * Ajax request to refresh the CSRF token. | |
27 | */ | |
cd10bc23 | 28 | function refreshToken(basePath, callback) { |
a33c5653 | 29 | const xhr = new XMLHttpRequest(); |
764d34a7 | 30 | xhr.open('GET', `${basePath}/admin/token`); |
a33c5653 | 31 | xhr.onload = () => { |
301c7ab1 A |
32 | const elements = document.querySelectorAll('input[name="token"]'); |
33 | [...elements].forEach((element) => { | |
301c7ab1 A |
34 | element.setAttribute('value', xhr.responseText); |
35 | }); | |
cd10bc23 A |
36 | |
37 | if (callback) { | |
38 | callback(xhr.response); | |
39 | } | |
a33c5653 A |
40 | }; |
41 | xhr.send(); | |
42 | } | |
43 | ||
44 | function createAwesompleteInstance(element, tags = []) { | |
45 | const awesome = new Awesomplete(Awesomplete.$(element)); | |
46 | // Tags are separated by a space | |
47 | awesome.filter = (text, input) => Awesomplete.FILTER_CONTAINS(text, input.match(/[^ ]*$/)[0]); | |
48 | // Insert new selected tag in the input | |
49 | awesome.replace = (text) => { | |
50 | const before = awesome.input.value.match(/^.+ \s*|/)[0]; | |
51 | awesome.input.value = `${before}${text} `; | |
52 | }; | |
53 | // Highlight found items | |
54 | awesome.item = (text, input) => Awesomplete.ITEM(text, input.match(/[^ ]*$/)[0]); | |
55 | // Don't display already selected items | |
56 | const reg = /(\w+) /g; | |
57 | let match; | |
58 | awesome.data = (item, input) => { | |
59 | while ((match = reg.exec(input))) { | |
60 | if (item === match[1]) { | |
61 | return ''; | |
62 | } | |
63 | } | |
64 | return item; | |
65 | }; | |
66 | awesome.minChars = 1; | |
67 | if (tags.length) { | |
68 | awesome.list = tags; | |
69 | } | |
70 | ||
71 | return awesome; | |
aa4797ba A |
72 | } |
73 | ||
82e3bb5f A |
74 | /** |
75 | * Update awesomplete list of tag for all elements matching the given selector | |
76 | * | |
77 | * @param selector CSS selector | |
78 | * @param tags Array of tags | |
79 | * @param instances List of existing awesomplete instances | |
80 | */ | |
a33c5653 A |
81 | function updateAwesompleteList(selector, tags, instances) { |
82 | if (instances.length === 0) { | |
82e3bb5f | 83 | // First load: create Awesomplete instances |
a33c5653 A |
84 | const elements = document.querySelectorAll(selector); |
85 | [...elements].forEach((element) => { | |
86 | instances.push(createAwesompleteInstance(element, tags)); | |
87 | }); | |
88 | } else { | |
89 | // Update awesomplete tag list | |
90 | instances.map((item) => { | |
91 | item.list = tags; | |
92 | return item; | |
93 | }); | |
94 | } | |
95 | return instances; | |
82e3bb5f A |
96 | } |
97 | ||
aa4797ba A |
98 | /** |
99 | * html_entities in JS | |
100 | * | |
101 | * @see http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript | |
102 | */ | |
a33c5653 A |
103 | function htmlEntities(str) { |
104 | return str.replace(/[\u00A0-\u9999<>&]/gim, i => `&#${i.charCodeAt(0)};`); | |
aa4797ba A |
105 | } |
106 | ||
a0737313 A |
107 | /** |
108 | * Add the class 'hidden' to city options not attached to the current selected continent. | |
109 | * | |
110 | * @param cities List of <option> elements | |
111 | * @param currentContinent Current selected continent | |
112 | * @param reset Set to true to reset the selected value | |
113 | */ | |
a33c5653 A |
114 | function hideTimezoneCities(cities, currentContinent, reset = null) { |
115 | let first = true; | |
116 | if (reset == null) { | |
117 | reset = false; | |
118 | } | |
119 | [...cities].forEach((option) => { | |
120 | if (option.getAttribute('data-continent') !== currentContinent) { | |
121 | option.className = 'hidden'; | |
122 | } else { | |
123 | option.className = ''; | |
124 | if (reset === true && first === true) { | |
125 | option.setAttribute('selected', 'selected'); | |
126 | first = false; | |
127 | } | |
aa4797ba | 128 | } |
a33c5653 A |
129 | }); |
130 | } | |
131 | ||
132 | /** | |
133 | * Retrieve an element up in the tree from its class name. | |
134 | */ | |
135 | function getParentByClass(el, className) { | |
136 | const p = el.parentNode; | |
137 | if (p == null || p.classList.contains(className)) { | |
138 | return p; | |
139 | } | |
140 | return getParentByClass(p, className); | |
141 | } | |
142 | ||
143 | function toggleHorizontal() { | |
144 | [...document.getElementById('shaarli-menu').querySelectorAll('.menu-transform')].forEach((el) => { | |
145 | el.classList.toggle('pure-menu-horizontal'); | |
146 | }); | |
147 | } | |
148 | ||
149 | function toggleMenu(menu) { | |
150 | // set timeout so that the panel has a chance to roll up | |
151 | // before the menu switches states | |
152 | if (menu.classList.contains('open')) { | |
153 | setTimeout(toggleHorizontal, 500); | |
154 | } else { | |
155 | toggleHorizontal(); | |
156 | } | |
157 | menu.classList.toggle('open'); | |
158 | document.getElementById('menu-toggle').classList.toggle('x'); | |
159 | } | |
160 | ||
161 | function closeMenu(menu) { | |
162 | if (menu.classList.contains('open')) { | |
163 | toggleMenu(menu); | |
164 | } | |
165 | } | |
166 | ||
167 | function toggleFold(button, description, thumb) { | |
168 | // Switch fold/expand - up = fold | |
169 | if (button.classList.contains('fa-chevron-up')) { | |
170 | button.title = document.getElementById('translation-expand').innerHTML; | |
171 | if (description != null) { | |
172 | description.style.display = 'none'; | |
173 | } | |
174 | if (thumb != null) { | |
175 | thumb.style.display = 'none'; | |
176 | } | |
177 | } else { | |
178 | button.title = document.getElementById('translation-fold').innerHTML; | |
179 | if (description != null) { | |
180 | description.style.display = 'block'; | |
181 | } | |
182 | if (thumb != null) { | |
183 | thumb.style.display = 'block'; | |
184 | } | |
185 | } | |
186 | button.classList.toggle('fa-chevron-down'); | |
187 | button.classList.toggle('fa-chevron-up'); | |
188 | } | |
189 | ||
190 | function removeClass(element, classname) { | |
191 | element.className = element.className.replace(new RegExp(`(?:^|\\s)${classname}(?:\\s|$)`), ' '); | |
192 | } | |
193 | ||
194 | function init(description) { | |
195 | function resize() { | |
196 | /* Fix jumpy resizing: https://stackoverflow.com/a/18262927/1484919 */ | |
197 | const scrollTop = window.pageYOffset || | |
198 | (document.documentElement || document.body.parentNode || document.body).scrollTop; | |
199 | ||
200 | description.style.height = 'auto'; | |
201 | description.style.height = `${description.scrollHeight + 10}px`; | |
202 | ||
203 | window.scrollTo(0, scrollTop); | |
204 | } | |
205 | ||
206 | /* 0-timeout to get the already changed text */ | |
207 | function delayedResize() { | |
208 | window.setTimeout(resize, 0); | |
209 | } | |
210 | ||
211 | const observe = (element, event, handler) => { | |
212 | element.addEventListener(event, handler, false); | |
213 | }; | |
214 | observe(description, 'change', resize); | |
215 | observe(description, 'cut', delayedResize); | |
216 | observe(description, 'paste', delayedResize); | |
217 | observe(description, 'drop', delayedResize); | |
218 | observe(description, 'keydown', delayedResize); | |
219 | ||
220 | resize(); | |
221 | } | |
222 | ||
223 | (() => { | |
818b3193 A |
224 | const basePath = document.querySelector('input[name="js_base_path"]').value; |
225 | ||
a33c5653 A |
226 | /** |
227 | * Handle responsive menu. | |
228 | * Source: http://purecss.io/layouts/tucked-menu-vertical/ | |
229 | */ | |
230 | const menu = document.getElementById('shaarli-menu'); | |
231 | const WINDOW_CHANGE_EVENT = ('onorientationchange' in window) ? 'orientationchange' : 'resize'; | |
232 | ||
233 | const menuToggle = document.getElementById('menu-toggle'); | |
234 | if (menuToggle != null) { | |
235 | menuToggle.addEventListener('click', () => toggleMenu(menu)); | |
236 | } | |
237 | ||
238 | window.addEventListener(WINDOW_CHANGE_EVENT, () => closeMenu(menu)); | |
239 | ||
240 | /** | |
241 | * Fold/Expand shaares description and thumbnail. | |
242 | */ | |
243 | const foldAllButtons = document.getElementsByClassName('fold-all'); | |
244 | const foldButtons = document.getElementsByClassName('fold-button'); | |
245 | ||
246 | [...foldButtons].forEach((foldButton) => { | |
247 | // Retrieve description | |
248 | let description = null; | |
249 | let thumbnail = null; | |
250 | const linklistItem = getParentByClass(foldButton, 'linklist-item'); | |
251 | if (linklistItem != null) { | |
252 | description = linklistItem.querySelector('.linklist-item-description'); | |
253 | thumbnail = linklistItem.querySelector('.linklist-item-thumbnail'); | |
254 | if (description != null || thumbnail != null) { | |
255 | foldButton.style.display = 'inline'; | |
256 | } | |
257 | } | |
258 | ||
259 | foldButton.addEventListener('click', (event) => { | |
260 | event.preventDefault(); | |
261 | toggleFold(event.target, description, thumbnail); | |
262 | }); | |
263 | }); | |
264 | ||
265 | if (foldAllButtons != null) { | |
266 | [].forEach.call(foldAllButtons, (foldAllButton) => { | |
267 | foldAllButton.addEventListener('click', (event) => { | |
268 | event.preventDefault(); | |
269 | const state = foldAllButton.firstElementChild.getAttribute('class').indexOf('down') !== -1 ? 'down' : 'up'; | |
270 | [].forEach.call(foldButtons, (foldButton) => { | |
271 | if ((foldButton.firstElementChild.classList.contains('fa-chevron-up') && state === 'down') | |
272 | || (foldButton.firstElementChild.classList.contains('fa-chevron-down') && state === 'up') | |
273 | ) { | |
274 | return; | |
275 | } | |
276 | // Retrieve description | |
277 | let description = null; | |
278 | let thumbnail = null; | |
279 | const linklistItem = getParentByClass(foldButton, 'linklist-item'); | |
280 | if (linklistItem != null) { | |
281 | description = linklistItem.querySelector('.linklist-item-description'); | |
282 | thumbnail = linklistItem.querySelector('.linklist-item-thumbnail'); | |
283 | if (description != null || thumbnail != null) { | |
284 | foldButton.style.display = 'inline'; | |
285 | } | |
286 | } | |
287 | ||
288 | toggleFold(foldButton.firstElementChild, description, thumbnail); | |
289 | }); | |
290 | foldAllButton.firstElementChild.classList.toggle('fa-chevron-down'); | |
291 | foldAllButton.firstElementChild.classList.toggle('fa-chevron-up'); | |
292 | foldAllButton.title = state === 'down' | |
293 | ? document.getElementById('translation-fold-all').innerHTML | |
294 | : document.getElementById('translation-expand-all').innerHTML; | |
295 | }); | |
296 | }); | |
297 | } | |
298 | ||
299 | /** | |
300 | * Confirmation message before deletion. | |
301 | */ | |
302 | const deleteLinks = document.querySelectorAll('.confirm-delete'); | |
303 | [...deleteLinks].forEach((deleteLink) => { | |
304 | deleteLink.addEventListener('click', (event) => { | |
305 | if (!confirm(document.getElementById('translation-delete-link').innerHTML)) { | |
306 | event.preventDefault(); | |
307 | } | |
308 | }); | |
309 | }); | |
310 | ||
311 | /** | |
312 | * Close alerts | |
313 | */ | |
314 | const closeLinks = document.querySelectorAll('.pure-alert-close'); | |
315 | [...closeLinks].forEach((closeLink) => { | |
316 | closeLink.addEventListener('click', (event) => { | |
317 | const alert = getParentByClass(event.target, 'pure-alert-closable'); | |
318 | alert.style.display = 'none'; | |
319 | }); | |
320 | }); | |
321 | ||
322 | /** | |
323 | * New version dismiss. | |
324 | * Hide the message for one week using localStorage. | |
325 | */ | |
326 | const newVersionDismiss = document.getElementById('new-version-dismiss'); | |
327 | const newVersionMessage = document.querySelector('.new-version-message'); | |
328 | if (newVersionMessage != null | |
329 | && localStorage.getItem('newVersionDismiss') != null | |
330 | && parseInt(localStorage.getItem('newVersionDismiss'), 10) + (7 * 24 * 60 * 60 * 1000) > (new Date()).getTime() | |
331 | ) { | |
332 | newVersionMessage.style.display = 'none'; | |
333 | } | |
334 | if (newVersionDismiss != null) { | |
335 | newVersionDismiss.addEventListener('click', () => { | |
336 | localStorage.setItem('newVersionDismiss', (new Date()).getTime().toString()); | |
337 | }); | |
338 | } | |
339 | ||
340 | const hiddenReturnurl = document.getElementsByName('returnurl'); | |
341 | if (hiddenReturnurl != null) { | |
342 | hiddenReturnurl.value = window.location.href; | |
343 | } | |
344 | ||
345 | /** | |
346 | * Autofocus text fields | |
347 | */ | |
348 | const autofocusElements = document.querySelectorAll('.autofocus'); | |
349 | let breakLoop = false; | |
350 | [].forEach.call(autofocusElements, (autofocusElement) => { | |
351 | if (autofocusElement.value === '' && !breakLoop) { | |
352 | autofocusElement.focus(); | |
353 | breakLoop = true; | |
354 | } | |
355 | }); | |
356 | ||
357 | /** | |
358 | * Handle sub menus/forms | |
359 | */ | |
360 | const openers = document.getElementsByClassName('subheader-opener'); | |
361 | if (openers != null) { | |
362 | [...openers].forEach((opener) => { | |
363 | opener.addEventListener('click', (event) => { | |
364 | event.preventDefault(); | |
365 | ||
366 | const id = opener.getAttribute('data-open-id'); | |
367 | const sub = document.getElementById(id); | |
368 | ||
369 | if (sub != null) { | |
370 | [...document.getElementsByClassName('subheader-form')].forEach((element) => { | |
371 | if (element !== sub) { | |
372 | removeClass(element, 'open'); | |
a0737313 | 373 | } |
a33c5653 A |
374 | }); |
375 | ||
376 | sub.classList.toggle('open'); | |
a0737313 | 377 | } |
a33c5653 | 378 | }); |
a0737313 | 379 | }); |
a33c5653 A |
380 | } |
381 | ||
382 | /** | |
383 | * Remove CSS target padding (for fixed bar) | |
384 | */ | |
385 | if (location.hash !== '') { | |
386 | const anchor = document.getElementById(location.hash.substr(1)); | |
387 | if (anchor != null) { | |
388 | const padsize = anchor.clientHeight; | |
389 | window.scroll(0, window.scrollY - padsize); | |
390 | anchor.style.paddingTop = '0'; | |
391 | } | |
392 | } | |
393 | ||
394 | /** | |
395 | * Text area resizer | |
396 | */ | |
397 | const description = document.getElementById('lf_description'); | |
398 | ||
399 | if (description != null) { | |
400 | init(description); | |
401 | // Submit editlink form with CTRL + Enter in the text area. | |
402 | description.addEventListener('keydown', (event) => { | |
403 | if (event.ctrlKey && event.keyCode === 13) { | |
404 | document.getElementById('button-save-edit').click(); | |
405 | } | |
406 | }); | |
407 | } | |
408 | ||
409 | /** | |
410 | * Bookmarklet alert | |
411 | */ | |
412 | const bookmarkletLinks = document.querySelectorAll('.bookmarklet-link'); | |
413 | const bkmMessage = document.getElementById('bookmarklet-alert'); | |
414 | [].forEach.call(bookmarkletLinks, (link) => { | |
415 | link.addEventListener('click', (event) => { | |
416 | event.preventDefault(); | |
417 | alert(bkmMessage.value); | |
418 | }); | |
419 | }); | |
420 | ||
a33c5653 A |
421 | const continent = document.getElementById('continent'); |
422 | const city = document.getElementById('city'); | |
423 | if (continent != null && city != null) { | |
424 | continent.addEventListener('change', () => { | |
425 | hideTimezoneCities(city, continent.options[continent.selectedIndex].value, true); | |
426 | }); | |
427 | hideTimezoneCities(city, continent.options[continent.selectedIndex].value, false); | |
428 | } | |
429 | ||
430 | /** | |
431 | * Bulk actions | |
432 | */ | |
fc574e64 | 433 | const linkCheckboxes = document.querySelectorAll('.link-checkbox'); |
a33c5653 A |
434 | const bar = document.getElementById('actions'); |
435 | [...linkCheckboxes].forEach((checkbox) => { | |
436 | checkbox.style.display = 'inline-block'; | |
fc574e64 A |
437 | checkbox.addEventListener('change', () => { |
438 | const linkCheckedCheckboxes = document.querySelectorAll('.link-checkbox:checked'); | |
a33c5653 A |
439 | const count = [...linkCheckedCheckboxes].length; |
440 | if (count === 0 && bar.classList.contains('open')) { | |
441 | bar.classList.toggle('open'); | |
442 | } else if (count > 0 && !bar.classList.contains('open')) { | |
443 | bar.classList.toggle('open'); | |
444 | } | |
445 | }); | |
446 | }); | |
447 | ||
448 | const deleteButton = document.getElementById('actions-delete'); | |
449 | const token = document.getElementById('token'); | |
450 | if (deleteButton != null && token != null) { | |
451 | deleteButton.addEventListener('click', (event) => { | |
452 | event.preventDefault(); | |
453 | ||
454 | const links = []; | |
fc574e64 | 455 | const linkCheckedCheckboxes = document.querySelectorAll('.link-checkbox:checked'); |
a33c5653 A |
456 | [...linkCheckedCheckboxes].forEach((checkbox) => { |
457 | links.push({ | |
458 | id: checkbox.value, | |
459 | title: document.querySelector(`.linklist-item[data-id="${checkbox.value}"] .linklist-link`).innerHTML, | |
460 | }); | |
461 | }); | |
462 | ||
463 | let message = `Are you sure you want to delete ${links.length} links?\n`; | |
464 | message += 'This action is IRREVERSIBLE!\n\nTitles:\n'; | |
465 | const ids = []; | |
466 | links.forEach((item) => { | |
467 | message += ` - ${item.title}\n`; | |
468 | ids.push(item.id); | |
469 | }); | |
470 | ||
471 | if (window.confirm(message)) { | |
9c75f877 | 472 | window.location = `${basePath}/admin/shaare/delete?id=${ids.join('+')}&token=${token.value}`; |
a33c5653 A |
473 | } |
474 | }); | |
475 | } | |
476 | ||
8d03f705 A |
477 | const changeVisibilityButtons = document.querySelectorAll('.actions-change-visibility'); |
478 | if (changeVisibilityButtons != null && token != null) { | |
479 | [...changeVisibilityButtons].forEach((button) => { | |
480 | button.addEventListener('click', (event) => { | |
481 | event.preventDefault(); | |
482 | const visibility = event.target.getAttribute('data-visibility'); | |
483 | ||
484 | const links = []; | |
485 | const linkCheckedCheckboxes = document.querySelectorAll('.link-checkbox:checked'); | |
486 | [...linkCheckedCheckboxes].forEach((checkbox) => { | |
487 | links.push({ | |
488 | id: checkbox.value, | |
489 | title: document.querySelector(`.linklist-item[data-id="${checkbox.value}"] .linklist-link`).innerHTML, | |
490 | }); | |
491 | }); | |
492 | ||
493 | const ids = links.map(item => item.id); | |
818b3193 | 494 | window.location = |
7b8a6f28 | 495 | `${basePath}/admin/shaare/visibility?token=${token.value}&newVisibility=${visibility}&id=${ids.join('+')}`; |
8d03f705 A |
496 | }); |
497 | }); | |
498 | } | |
499 | ||
fc574e64 A |
500 | /** |
501 | * Select all button | |
502 | */ | |
503 | const selectAllButtons = document.querySelectorAll('.select-all-button'); | |
504 | [...selectAllButtons].forEach((selectAllButton) => { | |
505 | selectAllButton.addEventListener('click', (e) => { | |
506 | e.preventDefault(); | |
507 | const checked = selectAllButton.classList.contains('filter-off'); | |
508 | [...selectAllButtons].forEach((selectAllButton2) => { | |
509 | selectAllButton2.classList.toggle('filter-off'); | |
510 | selectAllButton2.classList.toggle('filter-on'); | |
511 | }); | |
512 | [...linkCheckboxes].forEach((linkCheckbox) => { | |
513 | linkCheckbox.checked = checked; | |
514 | linkCheckbox.dispatchEvent(new Event('change')); | |
515 | }); | |
516 | }); | |
517 | }); | |
518 | ||
a33c5653 A |
519 | /** |
520 | * Tag list operations | |
521 | * | |
522 | * TODO: support error code in the backend for AJAX requests | |
523 | */ | |
524 | const tagList = document.querySelector('input[name="taglist"]'); | |
525 | let existingTags = tagList ? tagList.value.split(' ') : []; | |
526 | let awesomepletes = []; | |
527 | ||
528 | // Display/Hide rename form | |
529 | const renameTagButtons = document.querySelectorAll('.rename-tag'); | |
530 | [...renameTagButtons].forEach((rename) => { | |
531 | rename.addEventListener('click', (event) => { | |
532 | event.preventDefault(); | |
533 | const block = findParent(event.target, 'div', { class: 'tag-list-item' }); | |
534 | const form = block.querySelector('.rename-tag-form'); | |
535 | if (form.style.display === 'none' || form.style.display === '') { | |
536 | form.style.display = 'block'; | |
537 | } else { | |
538 | form.style.display = 'none'; | |
539 | } | |
540 | block.querySelector('input').focus(); | |
541 | }); | |
542 | }); | |
543 | ||
544 | // Rename a tag with an AJAX request | |
545 | const renameTagSubmits = document.querySelectorAll('.validate-rename-tag'); | |
546 | [...renameTagSubmits].forEach((rename) => { | |
547 | rename.addEventListener('click', (event) => { | |
548 | event.preventDefault(); | |
549 | const block = findParent(event.target, 'div', { class: 'tag-list-item' }); | |
550 | const input = block.querySelector('.rename-tag-input'); | |
551 | const totag = input.value.replace('/"/g', '\\"'); | |
552 | if (totag.trim() === '') { | |
553 | return; | |
554 | } | |
555 | const refreshedToken = document.getElementById('token').value; | |
556 | const fromtag = block.getAttribute('data-tag'); | |
557 | const xhr = new XMLHttpRequest(); | |
9c75f877 | 558 | xhr.open('POST', `${basePath}/admin/tags`); |
a33c5653 A |
559 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
560 | xhr.onload = () => { | |
561 | if (xhr.status !== 200) { | |
562 | alert(`An error occurred. Return code: ${xhr.status}`); | |
563 | location.reload(); | |
564 | } else { | |
565 | block.setAttribute('data-tag', totag); | |
566 | input.setAttribute('name', totag); | |
567 | input.setAttribute('value', totag); | |
568 | findParent(input, 'div', { class: 'rename-tag-form' }).style.display = 'none'; | |
569 | block.querySelector('a.tag-link').innerHTML = htmlEntities(totag); | |
818b3193 A |
570 | block |
571 | .querySelector('a.tag-link') | |
572 | .setAttribute('href', `${basePath}/?searchtags=${encodeURIComponent(totag)}`); | |
573 | block | |
574 | .querySelector('a.rename-tag') | |
9c75f877 | 575 | .setAttribute('href', `${basePath}/admin/tags?fromtag=${encodeURIComponent(totag)}`); |
a33c5653 A |
576 | |
577 | // Refresh awesomplete values | |
578 | existingTags = existingTags.map(tag => (tag === fromtag ? totag : tag)); | |
579 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); | |
580 | } | |
581 | }; | |
582 | xhr.send(`renametag=1&fromtag=${encodeURIComponent(fromtag)}&totag=${encodeURIComponent(totag)}&token=${refreshedToken}`); | |
818b3193 | 583 | refreshToken(basePath); |
a33c5653 A |
584 | }); |
585 | }); | |
586 | ||
587 | // Validate input with enter key | |
588 | const renameTagInputs = document.querySelectorAll('.rename-tag-input'); | |
589 | [...renameTagInputs].forEach((rename) => { | |
590 | rename.addEventListener('keypress', (event) => { | |
591 | if (event.keyCode === 13) { // enter | |
592 | findParent(event.target, 'div', { class: 'tag-list-item' }).querySelector('.validate-rename-tag').click(); | |
593 | } | |
594 | }); | |
595 | }); | |
596 | ||
597 | // Delete a tag with an AJAX query (alert popup confirmation) | |
598 | const deleteTagButtons = document.querySelectorAll('.delete-tag'); | |
599 | [...deleteTagButtons].forEach((rename) => { | |
600 | rename.style.display = 'inline'; | |
601 | rename.addEventListener('click', (event) => { | |
602 | event.preventDefault(); | |
603 | const block = findParent(event.target, 'div', { class: 'tag-list-item' }); | |
604 | const tag = block.getAttribute('data-tag'); | |
4fa9a3c5 | 605 | const refreshedToken = document.getElementById('token').value; |
a33c5653 A |
606 | |
607 | if (confirm(`Are you sure you want to delete the tag "${tag}"?`)) { | |
608 | const xhr = new XMLHttpRequest(); | |
9c75f877 | 609 | xhr.open('POST', `${basePath}/admin/tags`); |
a33c5653 A |
610 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
611 | xhr.onload = () => { | |
612 | block.remove(); | |
613 | }; | |
614 | xhr.send(encodeURI(`deletetag=1&fromtag=${tag}&token=${refreshedToken}`)); | |
818b3193 | 615 | refreshToken(basePath); |
a33c5653 A |
616 | |
617 | existingTags = existingTags.filter(tagItem => tagItem !== tag); | |
618 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); | |
619 | } | |
620 | }); | |
621 | }); | |
622 | ||
623 | const autocompleteFields = document.querySelectorAll('input[data-multiple]'); | |
624 | [...autocompleteFields].forEach((autocompleteField) => { | |
625 | awesomepletes.push(createAwesompleteInstance(autocompleteField)); | |
626 | }); | |
cd10bc23 A |
627 | |
628 | const exportForm = document.querySelector('#exportform'); | |
629 | if (exportForm != null) { | |
630 | exportForm.addEventListener('submit', (event) => { | |
631 | event.preventDefault(); | |
632 | ||
633 | refreshToken(basePath, () => { | |
634 | event.target.submit(); | |
635 | }); | |
636 | }); | |
637 | } | |
a33c5653 | 638 | })(); |