]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - assets/default/js/plugins-admin.js
Reviewed Apache configuration
[github/shaarli/Shaarli.git] / assets / default / js / plugins-admin.js
... / ...
CommitLineData
1/**
2 * Change the position counter of a row.
3 *
4 * @param elem Element Node to change.
5 * @param toPos int New position.
6 */
7function changePos(elem, toPos) {
8 const elemName = elem.getAttribute('data-line');
9 elem.setAttribute('data-order', toPos);
10 const hiddenInput = document.querySelector(`[name="order_${elemName}"]`);
11 hiddenInput.setAttribute('value', toPos);
12}
13
14/**
15 * Move a row up or down.
16 *
17 * @param pos Element Node to move.
18 * @param move int Move: +1 (down) or -1 (up)
19 */
20function changeOrder(pos, move) {
21 const newpos = parseInt(pos, 10) + move;
22 let lines = document.querySelectorAll(`[data-order="${pos}"]`);
23 const changelines = document.querySelectorAll(`[data-order="${newpos}"]`);
24
25 // If we go down reverse lines to preserve the rows order
26 if (move > 0) {
27 lines = [].slice.call(lines).reverse();
28 }
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 }
37}
38
39/**
40 * Move a row up in the table.
41 *
42 * @param pos int row counter.
43 *
44 * @return false
45 */
46function orderUp(pos) {
47 if (pos !== 0) {
48 changeOrder(pos, -1);
49 }
50}
51
52/**
53 * Move a row down in the table.
54 *
55 * @param pos int row counter.
56 *
57 * @returns false
58 */
59function orderDown(pos) {
60 const lastpos = parseInt(document.querySelector('[data-order]:last-child').getAttribute('data-order'), 10);
61 if (pos !== lastpos) {
62 changeOrder(pos, 1);
63 }
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})();