diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /assets/default/js/base.js | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'assets/default/js/base.js')
-rw-r--r-- | assets/default/js/base.js | 73 |
1 files changed, 52 insertions, 21 deletions
diff --git a/assets/default/js/base.js b/assets/default/js/base.js index d5c29c69..be986ae0 100644 --- a/assets/default/js/base.js +++ b/assets/default/js/base.js | |||
@@ -10,7 +10,7 @@ import Awesomplete from 'awesomplete'; | |||
10 | * @returns Found element or null. | 10 | * @returns Found element or null. |
11 | */ | 11 | */ |
12 | function findParent(element, tagName, attributes) { | 12 | function findParent(element, tagName, attributes) { |
13 | const parentMatch = key => attributes[key] !== '' && element.getAttribute(key).indexOf(attributes[key]) !== -1; | 13 | const parentMatch = (key) => attributes[key] !== '' && element.getAttribute(key).indexOf(attributes[key]) !== -1; |
14 | while (element) { | 14 | while (element) { |
15 | if (element.tagName.toLowerCase() === tagName) { | 15 | if (element.tagName.toLowerCase() === tagName) { |
16 | if (Object.keys(attributes).find(parentMatch)) { | 16 | if (Object.keys(attributes).find(parentMatch)) { |
@@ -25,12 +25,18 @@ function findParent(element, tagName, attributes) { | |||
25 | /** | 25 | /** |
26 | * Ajax request to refresh the CSRF token. | 26 | * Ajax request to refresh the CSRF token. |
27 | */ | 27 | */ |
28 | function refreshToken() { | 28 | function refreshToken(basePath, callback) { |
29 | const xhr = new XMLHttpRequest(); | 29 | const xhr = new XMLHttpRequest(); |
30 | xhr.open('GET', '?do=token'); | 30 | xhr.open('GET', `${basePath}/admin/token`); |
31 | xhr.onload = () => { | 31 | xhr.onload = () => { |
32 | const token = document.getElementById('token'); | 32 | const elements = document.querySelectorAll('input[name="token"]'); |
33 | token.setAttribute('value', xhr.responseText); | 33 | [...elements].forEach((element) => { |
34 | element.setAttribute('value', xhr.responseText); | ||
35 | }); | ||
36 | |||
37 | if (callback) { | ||
38 | callback(xhr.response); | ||
39 | } | ||
34 | }; | 40 | }; |
35 | xhr.send(); | 41 | xhr.send(); |
36 | } | 42 | } |
@@ -95,7 +101,7 @@ function updateAwesompleteList(selector, tags, instances) { | |||
95 | * @see http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript | 101 | * @see http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript |
96 | */ | 102 | */ |
97 | function htmlEntities(str) { | 103 | function htmlEntities(str) { |
98 | return str.replace(/[\u00A0-\u9999<>&]/gim, i => `&#${i.charCodeAt(0)};`); | 104 | return str.replace(/[\u00A0-\u9999<>&]/gim, (i) => `&#${i.charCodeAt(0)};`); |
99 | } | 105 | } |
100 | 106 | ||
101 | /** | 107 | /** |
@@ -188,8 +194,8 @@ function removeClass(element, classname) { | |||
188 | function init(description) { | 194 | function init(description) { |
189 | function resize() { | 195 | function resize() { |
190 | /* Fix jumpy resizing: https://stackoverflow.com/a/18262927/1484919 */ | 196 | /* Fix jumpy resizing: https://stackoverflow.com/a/18262927/1484919 */ |
191 | const scrollTop = window.pageYOffset || | 197 | const scrollTop = window.pageYOffset |
192 | (document.documentElement || document.body.parentNode || document.body).scrollTop; | 198 | || (document.documentElement || document.body.parentNode || document.body).scrollTop; |
193 | 199 | ||
194 | description.style.height = 'auto'; | 200 | description.style.height = 'auto'; |
195 | description.style.height = `${description.scrollHeight + 10}px`; | 201 | description.style.height = `${description.scrollHeight + 10}px`; |
@@ -215,6 +221,8 @@ function init(description) { | |||
215 | } | 221 | } |
216 | 222 | ||
217 | (() => { | 223 | (() => { |
224 | const basePath = document.querySelector('input[name="js_base_path"]').value; | ||
225 | |||
218 | /** | 226 | /** |
219 | * Handle responsive menu. | 227 | * Handle responsive menu. |
220 | * Source: http://purecss.io/layouts/tucked-menu-vertical/ | 228 | * Source: http://purecss.io/layouts/tucked-menu-vertical/ |
@@ -461,7 +469,7 @@ function init(description) { | |||
461 | }); | 469 | }); |
462 | 470 | ||
463 | if (window.confirm(message)) { | 471 | if (window.confirm(message)) { |
464 | window.location = `?delete_link&lf_linkdate=${ids.join('+')}&token=${token.value}`; | 472 | window.location = `${basePath}/admin/shaare/delete?id=${ids.join('+')}&token=${token.value}`; |
465 | } | 473 | } |
466 | }); | 474 | }); |
467 | } | 475 | } |
@@ -482,8 +490,10 @@ function init(description) { | |||
482 | }); | 490 | }); |
483 | }); | 491 | }); |
484 | 492 | ||
485 | const ids = links.map(item => item.id); | 493 | const ids = links.map((item) => item.id); |
486 | window.location = `?change_visibility&token=${token.value}&newVisibility=${visibility}&ids=${ids.join('+')}`; | 494 | window.location = ( |
495 | `${basePath}/admin/shaare/visibility?token=${token.value}&newVisibility=${visibility}&id=${ids.join('+')}` | ||
496 | ); | ||
487 | }); | 497 | }); |
488 | }); | 498 | }); |
489 | } | 499 | } |
@@ -545,8 +555,9 @@ function init(description) { | |||
545 | } | 555 | } |
546 | const refreshedToken = document.getElementById('token').value; | 556 | const refreshedToken = document.getElementById('token').value; |
547 | const fromtag = block.getAttribute('data-tag'); | 557 | const fromtag = block.getAttribute('data-tag'); |
558 | const fromtagUrl = block.getAttribute('data-tag-url'); | ||
548 | const xhr = new XMLHttpRequest(); | 559 | const xhr = new XMLHttpRequest(); |
549 | xhr.open('POST', '?do=changetag'); | 560 | xhr.open('POST', `${basePath}/admin/tags`); |
550 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | 561 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
551 | xhr.onload = () => { | 562 | xhr.onload = () => { |
552 | if (xhr.status !== 200) { | 563 | if (xhr.status !== 200) { |
@@ -554,20 +565,28 @@ function init(description) { | |||
554 | location.reload(); | 565 | location.reload(); |
555 | } else { | 566 | } else { |
556 | block.setAttribute('data-tag', totag); | 567 | block.setAttribute('data-tag', totag); |
568 | block.setAttribute('data-tag-url', encodeURIComponent(totag)); | ||
557 | input.setAttribute('name', totag); | 569 | input.setAttribute('name', totag); |
558 | input.setAttribute('value', totag); | 570 | input.setAttribute('value', totag); |
559 | findParent(input, 'div', { class: 'rename-tag-form' }).style.display = 'none'; | 571 | findParent(input, 'div', { class: 'rename-tag-form' }).style.display = 'none'; |
560 | block.querySelector('a.tag-link').innerHTML = htmlEntities(totag); | 572 | block.querySelector('a.tag-link').innerHTML = htmlEntities(totag); |
561 | block.querySelector('a.tag-link').setAttribute('href', `?searchtags=${encodeURIComponent(totag)}`); | 573 | block |
562 | block.querySelector('a.rename-tag').setAttribute('href', `?do=changetag&fromtag=${encodeURIComponent(totag)}`); | 574 | .querySelector('a.tag-link') |
575 | .setAttribute('href', `${basePath}/?searchtags=${encodeURIComponent(totag)}`); | ||
576 | block | ||
577 | .querySelector('a.count') | ||
578 | .setAttribute('href', `${basePath}/add-tag/${encodeURIComponent(totag)}`); | ||
579 | block | ||
580 | .querySelector('a.rename-tag') | ||
581 | .setAttribute('href', `${basePath}/admin/tags?fromtag=${encodeURIComponent(totag)}`); | ||
563 | 582 | ||
564 | // Refresh awesomplete values | 583 | // Refresh awesomplete values |
565 | existingTags = existingTags.map(tag => (tag === fromtag ? totag : tag)); | 584 | existingTags = existingTags.map((tag) => (tag === fromtag ? totag : tag)); |
566 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); | 585 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); |
567 | } | 586 | } |
568 | }; | 587 | }; |
569 | xhr.send(`renametag=1&fromtag=${encodeURIComponent(fromtag)}&totag=${encodeURIComponent(totag)}&token=${refreshedToken}`); | 588 | xhr.send(`renametag=1&fromtag=${fromtagUrl}&totag=${encodeURIComponent(totag)}&token=${refreshedToken}`); |
570 | refreshToken(); | 589 | refreshToken(basePath); |
571 | }); | 590 | }); |
572 | }); | 591 | }); |
573 | 592 | ||
@@ -589,19 +608,20 @@ function init(description) { | |||
589 | event.preventDefault(); | 608 | event.preventDefault(); |
590 | const block = findParent(event.target, 'div', { class: 'tag-list-item' }); | 609 | const block = findParent(event.target, 'div', { class: 'tag-list-item' }); |
591 | const tag = block.getAttribute('data-tag'); | 610 | const tag = block.getAttribute('data-tag'); |
611 | const tagUrl = block.getAttribute('data-tag-url'); | ||
592 | const refreshedToken = document.getElementById('token').value; | 612 | const refreshedToken = document.getElementById('token').value; |
593 | 613 | ||
594 | if (confirm(`Are you sure you want to delete the tag "${tag}"?`)) { | 614 | if (confirm(`Are you sure you want to delete the tag "${tag}"?`)) { |
595 | const xhr = new XMLHttpRequest(); | 615 | const xhr = new XMLHttpRequest(); |
596 | xhr.open('POST', '?do=changetag'); | 616 | xhr.open('POST', `${basePath}/admin/tags`); |
597 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | 617 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
598 | xhr.onload = () => { | 618 | xhr.onload = () => { |
599 | block.remove(); | 619 | block.remove(); |
600 | }; | 620 | }; |
601 | xhr.send(encodeURI(`deletetag=1&fromtag=${tag}&token=${refreshedToken}`)); | 621 | xhr.send(`deletetag=1&fromtag=${tagUrl}&token=${refreshedToken}`); |
602 | refreshToken(); | 622 | refreshToken(basePath); |
603 | 623 | ||
604 | existingTags = existingTags.filter(tagItem => tagItem !== tag); | 624 | existingTags = existingTags.filter((tagItem) => tagItem !== tag); |
605 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); | 625 | awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes); |
606 | } | 626 | } |
607 | }); | 627 | }); |
@@ -611,4 +631,15 @@ function init(description) { | |||
611 | [...autocompleteFields].forEach((autocompleteField) => { | 631 | [...autocompleteFields].forEach((autocompleteField) => { |
612 | awesomepletes.push(createAwesompleteInstance(autocompleteField)); | 632 | awesomepletes.push(createAwesompleteInstance(autocompleteField)); |
613 | }); | 633 | }); |
634 | |||
635 | const exportForm = document.querySelector('#exportform'); | ||
636 | if (exportForm != null) { | ||
637 | exportForm.addEventListener('submit', (event) => { | ||
638 | event.preventDefault(); | ||
639 | |||
640 | refreshToken(basePath, () => { | ||
641 | event.target.submit(); | ||
642 | }); | ||
643 | }); | ||
644 | } | ||
614 | })(); | 645 | })(); |