1/**
2 * ACL Manager AJAX enhancements
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6var dw_acl = {
7    /**
8     * Initialize the object and attach the event handlers
9     */
10    init: function () {
11        var $tree;
12
13        //FIXME only one underscore!!
14        if (jQuery('#acl_manager').length === 0) {
15            return;
16        }
17
18        jQuery('#acl__user select').on('change', dw_acl.userselhandler);
19        jQuery('#acl__user button').on('click', dw_acl.loadinfo);
20
21        $tree = jQuery('#acl__tree');
22        $tree.dw_tree({toggle_selector: 'img',
23                       load_data: function (show_sublist, $clicky) {
24                           // get the enclosed link and the edit form
25                           var $frm = jQuery('#acl__detail form');
26
27                           jQuery.post(
28                               DOKU_BASE + 'lib/exe/ajax.php',
29                               jQuery.extend(dw_acl.parseatt($clicky.parent().find('a')[0].search),
30                                             {call: 'plugin_acl',
31                                              ajax: 'tree',
32                                              current_ns: $frm.find('input[name=ns]').val(),
33                                              current_id: $frm.find('input[name=id]').val()}),
34                               show_sublist,
35                               'html'
36                           );
37                       },
38
39                       toggle_display: function ($clicky, opening) {
40                           $clicky.attr('src',
41                                        DOKU_BASE + 'lib/images/' +
42                                        (opening ? 'minus' : 'plus') + '.gif');
43                       }});
44        $tree.delegate('a', 'click', dw_acl.treehandler);
45    },
46
47    /**
48     * Handle user dropdown
49     *
50     * Hides or shows the user/group entry box depending on what was selected in the
51     * dropdown element
52     */
53    userselhandler: function () {
54        // make entry field visible/invisible
55        jQuery('#acl__user input').toggle(this.value === '__g__' ||
56                                          this.value === '__u__');
57        dw_acl.loadinfo();
58    },
59
60    /**
61     * Load the current permission info and edit form
62     */
63    loadinfo: function () {
64        jQuery('#acl__info')
65            .attr('role', 'alert')
66            .html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="..." />')
67            .load(
68                DOKU_BASE + 'lib/exe/ajax.php',
69                jQuery('#acl__detail form').serialize() + '&call=plugin_acl&ajax=info'
70            );
71        return false;
72    },
73
74    /**
75     * parse URL attributes into a associative array
76     *
77     * @todo put into global script lib?
78     */
79    parseatt: function (str) {
80        if (str[0] === '?') {
81            str = str.substr(1);
82        }
83        var attributes = {};
84        var all = str.split('&');
85        for (var i = 0; i < all.length; i++) {
86            var att = all[i].split('=');
87            attributes[att[0]] = decodeURIComponent(att[1]);
88        }
89        return attributes;
90    },
91
92    /**
93     * Handles clicks to the tree nodes
94     */
95    treehandler: function () {
96        var $link, $frm;
97
98        $link = jQuery(this);
99
100            // remove highlighting
101            jQuery('#acl__tree a.cur').removeClass('cur');
102
103            // add new highlighting
104        $link.addClass('cur');
105
106            // set new page to detail form
107        $frm = jQuery('#acl__detail form');
108        if ($link.hasClass('wikilink1')) {
109            $frm.find('input[name=ns]').val('');
110            $frm.find('input[name=id]').val(dw_acl.parseatt($link[0].search).id);
111        } else if ($link.hasClass('idx_dir')) {
112            $frm.find('input[name=ns]').val(dw_acl.parseatt($link[0].search).ns);
113            $frm.find('input[name=id]').val('');
114            }
115        dw_acl.loadinfo();
116
117        return false;
118    }
119};
120
121jQuery(dw_acl.init);
122