]>
Commit | Line | Data |
---|---|---|
b9b41d25 A |
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 | ||
dea0ba28 A |
29 | /** |
30 | * Change the position counter of a row. | |
31 | * | |
32 | * @param elem Element Node to change. | |
33 | * @param toPos int New position. | |
34 | */ | |
35 | function changePos(elem, toPos) | |
36 | { | |
37 | var elemName = elem.getAttribute('data-line') | |
38 | ||
39 | elem.setAttribute('data-order', toPos); | |
40 | var hiddenInput = document.querySelector('[name="order_'+ elemName +'"]'); | |
41 | hiddenInput.setAttribute('value', toPos); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Move a row up or down. | |
46 | * | |
47 | * @param pos Element Node to move. | |
48 | * @param move int Move: +1 (down) or -1 (up) | |
49 | */ | |
50 | function changeOrder(pos, move) | |
51 | { | |
52 | var newpos = parseInt(pos) + move; | |
147f4df8 A |
53 | var lines = document.querySelectorAll('[data-order="'+ pos +'"]'); |
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 | ||
61 | for (var i = 0 ; i < lines.length ; i++) { | |
62 | var parent = changelines[0].parentNode; | |
63 | changePos(lines[i], newpos); | |
64 | changePos(changelines[i], parseInt(pos)); | |
65 | var changeItem = move < 0 ? changelines[0] : changelines[changelines.length - 1].nextSibling; | |
66 | parent.insertBefore(lines[i], changeItem); | |
67 | } | |
dea0ba28 | 68 | |
dea0ba28 A |
69 | } |
70 | ||
71 | /** | |
72 | * Move a row up in the table. | |
73 | * | |
74 | * @param pos int row counter. | |
75 | * | |
76 | * @returns false | |
77 | */ | |
78 | function orderUp(pos) | |
79 | { | |
80 | if (pos == 0) { | |
81 | return false; | |
82 | } | |
83 | changeOrder(pos, -1); | |
84 | return false; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Move a row down in the table. | |
89 | * | |
90 | * @param pos int row counter. | |
91 | * | |
92 | * @returns false | |
93 | */ | |
94 | function orderDown(pos) | |
95 | { | |
96 | var lastpos = document.querySelector('[data-order]:last-child').getAttribute('data-order'); | |
97 | if (pos == lastpos) { | |
98 | return false; | |
99 | } | |
100 | ||
101 | changeOrder(pos, +1); | |
102 | return false; | |
103 | } |