diff options
Diffstat (limited to 'assets/default/js/plugins-admin.js')
-rw-r--r-- | assets/default/js/plugins-admin.js | 114 |
1 files changed, 46 insertions, 68 deletions
diff --git a/assets/default/js/plugins-admin.js b/assets/default/js/plugins-admin.js index 4b55e0f3..46df4a3c 100644 --- a/assets/default/js/plugins-admin.js +++ b/assets/default/js/plugins-admin.js | |||
@@ -1,44 +1,14 @@ | |||
1 | /** @licstart The following is the entire license notice for the | ||
2 | * JavaScript code in this page. | ||
3 | * | ||
4 | * Copyright: (c) 2011-2015 Sébastien SAUVAGE <sebsauvage@sebsauvage.net> | ||
5 | * (c) 2011-2017 The Shaarli Community, see AUTHORS | ||
6 | * | ||
7 | * This software is provided 'as-is', without any express or implied warranty. | ||
8 | * In no event will the authors be held liable for any damages arising from | ||
9 | * the use of this software. | ||
10 | * | ||
11 | * Permission is granted to anyone to use this software for any purpose, | ||
12 | * including commercial applications, and to alter it and redistribute it | ||
13 | * freely, subject to the following restrictions: | ||
14 | * | ||
15 | * 1. The origin of this software must not be misrepresented; you must not | ||
16 | * claim that you wrote the original software. If you use this software | ||
17 | * in a product, an acknowledgment in the product documentation would | ||
18 | * be appreciated but is not required. | ||
19 | * | ||
20 | * 2. Altered source versions must be plainly marked as such, and must | ||
21 | * not be misrepresented as being the original software. | ||
22 | * | ||
23 | * 3. This notice may not be removed or altered from any source distribution. | ||
24 | * | ||
25 | * @licend The above is the entire license notice | ||
26 | * for the JavaScript code in this page. | ||
27 | */ | ||
28 | |||
29 | /** | 1 | /** |
30 | * Change the position counter of a row. | 2 | * Change the position counter of a row. |
31 | * | 3 | * |
32 | * @param elem Element Node to change. | 4 | * @param elem Element Node to change. |
33 | * @param toPos int New position. | 5 | * @param toPos int New position. |
34 | */ | 6 | */ |
35 | function changePos(elem, toPos) | 7 | function changePos(elem, toPos) { |
36 | { | 8 | const elemName = elem.getAttribute('data-line'); |
37 | var elemName = elem.getAttribute('data-line') | 9 | elem.setAttribute('data-order', toPos); |
38 | 10 | const hiddenInput = document.querySelector(`[name="order_${elemName}"]`); | |
39 | elem.setAttribute('data-order', toPos); | 11 | hiddenInput.setAttribute('value', toPos); |
40 | var hiddenInput = document.querySelector('[name="order_'+ elemName +'"]'); | ||
41 | hiddenInput.setAttribute('value', toPos); | ||
42 | } | 12 | } |
43 | 13 | ||
44 | /** | 14 | /** |
@@ -47,25 +17,23 @@ function changePos(elem, toPos) | |||
47 | * @param pos Element Node to move. | 17 | * @param pos Element Node to move. |
48 | * @param move int Move: +1 (down) or -1 (up) | 18 | * @param move int Move: +1 (down) or -1 (up) |
49 | */ | 19 | */ |
50 | function changeOrder(pos, move) | 20 | function changeOrder(pos, move) { |
51 | { | 21 | const newpos = parseInt(pos, 10) + move; |
52 | var newpos = parseInt(pos) + move; | 22 | let lines = document.querySelectorAll(`[data-order="${pos}"]`); |
53 | var lines = document.querySelectorAll('[data-order="'+ pos +'"]'); | 23 | const changelines = document.querySelectorAll(`[data-order="${newpos}"]`); |
54 | var changelines = document.querySelectorAll('[data-order="'+ newpos +'"]'); | ||
55 | |||
56 | // If we go down reverse lines to preserve the rows order | ||
57 | if (move > 0) { | ||
58 | lines = [].slice.call(lines).reverse(); | ||
59 | } | ||
60 | 24 | ||
61 | for (var i = 0 ; i < lines.length ; i++) { | 25 | // If we go down reverse lines to preserve the rows order |
62 | var parent = changelines[0].parentNode; | 26 | if (move > 0) { |
63 | changePos(lines[i], newpos); | 27 | lines = [].slice.call(lines).reverse(); |
64 | changePos(changelines[i], parseInt(pos)); | 28 | } |
65 | var changeItem = move < 0 ? changelines[0] : changelines[changelines.length - 1].nextSibling; | ||
66 | parent.insertBefore(lines[i], changeItem); | ||
67 | } | ||
68 | 29 | ||
30 | for (let i = 0; i < lines.length; i += 1) { | ||
31 | const parent = changelines[0].parentNode; | ||
32 | changePos(lines[i], newpos); | ||
33 | changePos(changelines[i], parseInt(pos, 10)); | ||
34 | const changeItem = move < 0 ? changelines[0] : changelines[changelines.length - 1].nextSibling; | ||
35 | parent.insertBefore(lines[i], changeItem); | ||
36 | } | ||
69 | } | 37 | } |
70 | 38 | ||
71 | /** | 39 | /** |
@@ -73,15 +41,12 @@ function changeOrder(pos, move) | |||
73 | * | 41 | * |
74 | * @param pos int row counter. | 42 | * @param pos int row counter. |
75 | * | 43 | * |
76 | * @returns false | 44 | * @return false |
77 | */ | 45 | */ |
78 | function orderUp(pos) | 46 | function orderUp(pos) { |
79 | { | 47 | if (pos !== 0) { |
80 | if (pos == 0) { | ||
81 | return false; | ||
82 | } | ||
83 | changeOrder(pos, -1); | 48 | changeOrder(pos, -1); |
84 | return false; | 49 | } |
85 | } | 50 | } |
86 | 51 | ||
87 | /** | 52 | /** |
@@ -91,13 +56,26 @@ function orderUp(pos) | |||
91 | * | 56 | * |
92 | * @returns false | 57 | * @returns false |
93 | */ | 58 | */ |
94 | function orderDown(pos) | 59 | function orderDown(pos) { |
95 | { | 60 | const lastpos = parseInt(document.querySelector('[data-order]:last-child').getAttribute('data-order'), 10); |
96 | var lastpos = document.querySelector('[data-order]:last-child').getAttribute('data-order'); | 61 | if (pos !== lastpos) { |
97 | if (pos == lastpos) { | 62 | changeOrder(pos, 1); |
98 | return false; | 63 | } |
99 | } | ||
100 | |||
101 | changeOrder(pos, +1); | ||
102 | return false; | ||
103 | } | 64 | } |
65 | |||
66 | (() => { | ||
67 | /** | ||
68 | * Plugin admin order | ||
69 | */ | ||
70 | const orderPA = document.querySelectorAll('.order'); | ||
71 | [...orderPA].forEach((link) => { | ||
72 | link.addEventListener('click', (event) => { | ||
73 | event.preventDefault(); | ||
74 | if (event.target.classList.contains('order-up')) { | ||
75 | orderUp(parseInt(event.target.parentNode.parentNode.getAttribute('data-order'), 10)); | ||
76 | } else if (event.target.classList.contains('order-down')) { | ||
77 | orderDown(parseInt(event.target.parentNode.parentNode.getAttribute('data-order'), 10)); | ||
78 | } | ||
79 | }); | ||
80 | }); | ||
81 | })(); | ||