xref: /plugin/tagging/script.js (revision e49005ffae3338048442585f37818bab6936cda6)
1/* DOKUWIKI:include script/editable.js */
2
3jQuery(function () {
4
5    /**
6     * Add JavaScript confirmation to the User Delete button
7     */
8    jQuery('#tagging__del').click(function(){
9        return confirm(LANG.del_confirm);
10    });
11
12    var $form = jQuery('#tagging__edit').hide();
13    if (!$form.length) return;
14
15    var $btn = jQuery('form.btn_tagging_edit');
16    var $btns = jQuery('#tagging__edit_buttons_group');
17
18    $btn.submit(function (e) {
19        $btns.hide();
20        $form.show();
21        var $input = $form.find('input[type="text"]');
22        var len = $input.val().length;
23        $input.focus();
24        try {
25            $input[0].setSelectionRange(len, len);
26        } catch (ex) {
27            // ignore stupid IE
28        }
29
30        e.preventDefault();
31        e.stopPropagation();
32        return false;
33    });
34
35    var $admin_toggle_btn = jQuery('#tagging__edit_toggle_admin')
36                                .checkboxradio()
37                                .click(function(){
38                                    jQuery('div.plugin_tagging_edit ul.tagging_cloud a').editable('toggleDisabled');
39                                }),
40        add_editable = function() {
41            //no editable button - we are not the admin
42            if ($admin_toggle_btn.length === 0) return;
43
44            jQuery('div.plugin_tagging_edit ul.tagging_cloud a')
45                .editable({
46                    disabled  : !$admin_toggle_btn[0].checked,
47                    label     : LANG.plugins.tagging.admin_change_tag,
48                    url       : DOKU_BASE + 'lib/exe/ajax.php?call=plugin_tagging_admin_change',
49                    params    : { 'call'   : 'plugin_tagging_admin_change',
50                                  'id'     : JSINFO.id,
51                                  'sectok' : JSINFO.sectok
52                                },
53                    success   :
54                        function(response) {
55                            jQuery('div.plugin_tagging_edit ul.tagging_cloud').html(response.html_cloud);
56                            $form.find('input[type="text"]').val(response.tags_edit_value);
57                            add_editable();
58                        }
59                });
60        };
61
62    add_editable();
63
64    jQuery('#tagging__edit_save').click(function (e) {
65        jQuery('div.plugin_tagging_edit ul.tagging_cloud').load(
66            DOKU_BASE + 'lib/exe/ajax.php',
67            $form.serialize(),
68            add_editable
69        );
70        $btns.show();
71        $form.hide();
72
73        e.preventDefault();
74        e.stopPropagation();
75        return false;
76    });
77
78    jQuery('#tagging__edit_cancel').click(function (e) {
79        $btns.show();
80        $form.hide();
81
82        e.preventDefault();
83        e.stopPropagation();
84        return false;
85    });
86
87    jQuery('.btn_tagging_edit button, #tagging__edit_save, #tagging__edit_cancel').button();
88
89    /**
90     * below follows auto completion as described on  http://jqueryui.com/autocomplete/#multiple-remote
91     */
92
93    function split(val) {
94        return val.split(/,\s*/);
95    }
96
97    function extractLast(term) {
98        return split(term).pop();
99    }
100
101    $form.find('input[type="text"]')
102    // don't navigate away from the field on tab when selecting an item
103        .bind("keydown", function (event) {
104            if (event.keyCode === jQuery.ui.keyCode.TAB &&
105                jQuery(this).data("ui-autocomplete").menu.active) {
106                event.preventDefault();
107            }
108        })
109        .autocomplete({
110            source: function (request, response) {
111                jQuery.getJSON(DOKU_BASE + 'lib/exe/ajax.php?call=plugin_tagging_autocomplete', {
112                    term: extractLast(request.term),
113                }, response);
114            },
115            search: function () {
116                // custom minLength
117                var term = extractLast(this.value);
118                if (term.length < 2) {
119                    return false;
120                }
121                return true;
122            },
123            focus: function () {
124                // prevent value inserted on focus
125                return false;
126            },
127            select: function (event, ui) {
128                var terms = split(this.value);
129                // remove the current input
130                terms.pop();
131                // add the selected item
132                terms.push(ui.item.value);
133                // add placeholder to get the comma-and-space at the end
134                terms.push("");
135                this.value = terms.join(", ");
136                return false;
137            },
138        });
139});
140