]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - inc/plugin_admin.js
Fix blocking namespace issue
[github/shaarli/Shaarli.git] / inc / plugin_admin.js
1 /**
2 * Change the position counter of a row.
3 *
4 * @param elem Element Node to change.
5 * @param toPos int New position.
6 */
7 function changePos(elem, toPos)
8 {
9 var elemName = elem.getAttribute('data-line')
10
11 elem.setAttribute('data-order', toPos);
12 var hiddenInput = document.querySelector('[name="order_'+ elemName +'"]');
13 hiddenInput.setAttribute('value', toPos);
14 }
15
16 /**
17 * Move a row up or down.
18 *
19 * @param pos Element Node to move.
20 * @param move int Move: +1 (down) or -1 (up)
21 */
22 function changeOrder(pos, move)
23 {
24 var newpos = parseInt(pos) + move;
25 var lines = document.querySelectorAll('[data-order="'+ pos +'"]');
26 var changelines = document.querySelectorAll('[data-order="'+ newpos +'"]');
27
28 // If we go down reverse lines to preserve the rows order
29 if (move > 0) {
30 lines = [].slice.call(lines).reverse();
31 }
32
33 for (var i = 0 ; i < lines.length ; i++) {
34 var parent = changelines[0].parentNode;
35 changePos(lines[i], newpos);
36 changePos(changelines[i], parseInt(pos));
37 var changeItem = move < 0 ? changelines[0] : changelines[changelines.length - 1].nextSibling;
38 parent.insertBefore(lines[i], changeItem);
39 }
40
41 }
42
43 /**
44 * Move a row up in the table.
45 *
46 * @param pos int row counter.
47 *
48 * @returns false
49 */
50 function orderUp(pos)
51 {
52 if (pos == 0) {
53 return false;
54 }
55 changeOrder(pos, -1);
56 return false;
57 }
58
59 /**
60 * Move a row down in the table.
61 *
62 * @param pos int row counter.
63 *
64 * @returns false
65 */
66 function orderDown(pos)
67 {
68 var lastpos = document.querySelector('[data-order]:last-child').getAttribute('data-order');
69 if (pos == lastpos) {
70 return false;
71 }
72
73 changeOrder(pos, +1);
74 return false;
75 }