aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/plugin_admin.js
blob: 134ffb337607d84c3bc7bfa0e77db7512fd27a27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * Change the position counter of a row.
 *
 * @param elem  Element Node to change.
 * @param toPos int     New position.
 */
function changePos(elem, toPos)
{
    var elemName = elem.getAttribute('data-line')

    elem.setAttribute('data-order', toPos);
    var hiddenInput = document.querySelector('[name="order_'+ elemName +'"]');
    hiddenInput.setAttribute('value', toPos);
}

/**
 * Move a row up or down.
 *
 * @param pos  Element Node to move.
 * @param move int     Move: +1 (down) or -1 (up)
 */
function changeOrder(pos, move)
{
    var newpos = parseInt(pos) + move;
    var line = document.querySelector('[data-order="'+ pos +'"]');
    var changeline = document.querySelector('[data-order="'+ newpos +'"]');
    var parent = changeline.parentNode;

    changePos(line, newpos);
    changePos(changeline, parseInt(pos));
    var changeItem = move < 0 ? changeline : changeline.nextSibling;
    parent.insertBefore(line, changeItem);
}

/**
 * Move a row up in the table.
 *
 * @param pos int row counter.
 *
 * @returns false
 */
function orderUp(pos)
{
    if (pos == 0) {
        return false;
    }
    changeOrder(pos, -1);
    return false;
}

/**
 * Move a row down in the table.
 *
 * @param pos int row counter.
 *
 * @returns false
 */
function orderDown(pos)
{
    var lastpos = document.querySelector('[data-order]:last-child').getAttribute('data-order');
    if (pos == lastpos) {
        return false;
    }

    changeOrder(pos, +1);
    return false;
}