1/**
2 * Reuses parts of lib/scripts/tree.js
3 */
4
5jQuery(function () {
6
7    // namespace links
8    const toggleSelector = 'a.idx_dir';
9
10    /**
11     * Translates selected pages into syntax and inserts it into editor
12     *
13     * @param e
14     * @returns {boolean}
15     */
16    const insert = function (e) {
17        if (!window.opener) return false;
18
19        e.preventDefault();
20        e.stopPropagation();
21
22        const $items = $tree.find("input[type=checkbox]:checked").toArray();
23
24        const syntaxArray = $items.map(function (item) {
25            const $item = jQuery(item)[0];
26            let id = $item.value;
27            const nsRegex = new RegExp(':');
28            if (nsRegex.test(id) === false) {
29                id = ':' + id;
30            }
31            return "\n  * [[" + id + "]]";
32        });
33
34        const syntax = syntaxArray.join("");
35
36        opener.insertAtCarret('wiki__text', syntax, '');
37        window.close();
38        opener.focus();
39
40        return false;
41    }
42
43    // button for inserting syntax into the editor
44    const $submitButton = jQuery('#plugin_linkscollection__insert');
45    $submitButton.on('click', insert);
46
47    /**
48     * Handles toggling of namespace links
49     *
50     * @param e
51     */
52    const toggle = function (e) {
53
54        const $nsLink = jQuery(this);
55        const $listitem = $nsLink.closest('li');
56        let $sublist = $listitem.find('ul').first();
57        const opening = $listitem.hasClass('closed');
58
59        e.preventDefault();
60
61        // fetch sublist of currently clicked namespace
62        const load_data = function (show_sublist, $nsLink) {
63            jQuery.post(
64                DOKU_BASE + 'lib/exe/ajax.php',
65                $nsLink[0].search.substr(1) + '&call=index',
66                show_sublist,
67                'html'
68            );
69        }
70
71        const show_sublist = function (data) {
72            $sublist.hide();
73            if (typeof data !== 'undefined') {
74
75                const $tree = jQuery(data);
76                addCheckboxes($tree);
77                $sublist.prepend($tree);
78            }
79            if ($listitem.hasClass('open')) {
80                // Only show if user didn't close the list since starting
81                // to load the content
82                $sublist.dw_show();
83            }
84        };
85
86        if ($sublist.is(':visible')) {
87            $listitem.removeClass('open').addClass('closed');
88        } else {
89            $listitem.removeClass('closed').addClass('open');
90        }
91
92        // if already open, close by hiding the sublist
93        if (!opening) {
94            $sublist.dw_hide();
95            return;
96        }
97
98        // just show if already loaded
99        if ($sublist.length > 0) {
100            show_sublist();
101            return;
102        }
103
104        //prepare the new ul
105        $sublist = jQuery('<ul class="idx" role="group"/>');
106        $listitem.append($sublist);
107
108        load_data(function (data) {
109            show_sublist(data);
110        }, $nsLink);
111    }
112
113    /**
114     * Add checkboxes to page links, skipping namespace links
115     * @param $tree
116     */
117    const addCheckboxes = function ($tree) {
118        $tree.find('a').not(toggleSelector).each(function () {
119
120            const $link = jQuery(this);
121            $link.on('click', function (e) {
122                e.preventDefault();
123            });
124
125            const id = $link.prop('title');
126
127            let $label = jQuery('<label for="'+ id + '"/>');
128            let $checkbox = jQuery('<input type="checkbox" name="collection[]" value="' + id + '"  id="' + id + '" />');
129            $label.prepend($checkbox);
130            $label.insertBefore($link);
131        });
132    };
133
134    /**
135     * Main functionality of the script: attach events to page tree
136     */
137    const $tree = jQuery('#plugin_linkscollection__tree');
138
139    const processTree = function ($tree) {
140        $tree.find('a').click(function (e) {
141            e.preventDefault();
142        });
143
144        addCheckboxes($tree);
145        $tree.on('click', toggleSelector, {data: $tree}, toggle);
146    }
147
148    if ($tree) {
149        processTree($tree);
150    }
151});
152