summaryrefslogtreecommitdiff
path: root/assets/js/components/list-item-selection.js
blob: 45c084e34c9986b2c74f2cfafc80ac680bb3c32d (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
document.addEventListener("DOMContentLoaded", function () {

    function selectAllItems(event) {
        event.preventDefault();

        var items = document.querySelectorAll("input[data-list-item=selectable]");
        for (var i = 0; i < items.length; i++) {
            items[i].checked = true;
        }

        showActionMenu();
    }

    function unselectAllItems(event) {
        event.preventDefault();

        var items = document.querySelectorAll("input[data-list-item=selectable]");
        for (var i = 0; i < items.length; i++) {
            items[i].checked = false;
        }

        hideActionMenu();
    }

    function onItemChange(event) {
        var selectedItems = document.querySelectorAll("input[data-list-item=selectable]:checked");

        if (selectedItems.length > 0) {
            showActionMenu();
        }
    }

    function showActionMenu() {
        var element = document.querySelector(".list-item-actions");
        if (element) {
            element.classList.remove("list-item-action-hidden");
        }
    }

    function hideActionMenu() {
        var element = document.querySelector(".list-item-actions");
        if (element && ! element.classList.contains("list-item-action-hidden")) {
            element.classList.add("list-item-action-hidden");
        }
    }

    function onActionClick(event) {
        event.preventDefault();
        var selectedItems = document.querySelectorAll("input[data-list-item=selectable]:checked");
        var taskIDs = [];

        for (var i = 0; i < selectedItems.length; i++) {
            taskIDs.push(selectedItems[i].value);
        }

        var link = event.target.href + "&task_ids=" + taskIDs.join(",");
        KB.modal.open(link, "medium", true);
    }

    var selectAllLink = document.querySelector("a[data-list-item-selection=all]");
    if (selectAllLink) {
        selectAllLink.addEventListener("click", selectAllItems);
    }

    var unselectLink = document.querySelector("a[data-list-item-selection=none]");
    if (unselectLink) {
        unselectLink.addEventListener("click", unselectAllItems);
    }

    var items = document.querySelectorAll("input[data-list-item=selectable]");
    for (var i = 0; i < items.length; i++) {
        items[i].addEventListener("change", onItemChange);
    }

    KB.on('dropdown.afterRender', function () {
        var actionLinks = document.querySelectorAll("a[data-list-item-action=modal]");

        for (var i = 0; i < actionLinks.length; i++) {
            actionLinks[i].addEventListener("click", onActionClick, false);
        }
    });
});