xref: /plugin/tagging/script.js (revision 2c170fd58edbebd150f1a869e70991b117a11a0f)
1jQuery(function () {
2
3    /**
4     * Add JavaScript confirmation to the User Delete button
5     */
6    jQuery('#tagging__del').click(function(){
7        return confirm(LANG.del_confirm);
8    });
9
10    var $form = jQuery('#tagging__edit').hide();
11    if (!$form.length) return;
12
13    var $btn = jQuery('form.btn_tagging_edit');
14
15    $btn.submit(function (e) {
16        $btn.hide();
17        $form.show();
18        var $input = $form.find('input[type="text"]');
19        var len = $input.val().length;
20        $input.focus();
21        try {
22            $input[0].setSelectionRange(len, len);
23        } catch (ex) {
24            // ignore stupid IE
25        }
26
27        e.preventDefault();
28        e.stopPropagation();
29        return false;
30    });
31
32    jQuery('#tagging__edit_save').click(function (e) {
33        jQuery('div.plugin_tagging_edit ul.tagging_cloud').load(
34            DOKU_BASE + 'lib/exe/ajax.php',
35            $form.serialize()
36        );
37        $btn.show();
38        $form.hide();
39
40        e.preventDefault();
41        e.stopPropagation();
42        return false;
43    });
44
45    jQuery('#tagging__edit_cancel').click(function (e) {
46        $btn.show();
47        $form.hide();
48
49        e.preventDefault();
50        e.stopPropagation();
51        return false;
52    });
53
54
55    /**
56     * below follows auto completion as described on  http://jqueryui.com/autocomplete/#multiple-remote
57     */
58
59    function split(val) {
60        return val.split(/,\s*/);
61    }
62
63    function extractLast(term) {
64        return split(term).pop();
65    }
66
67    $form.find('input[type="text"]')
68    // don't navigate away from the field on tab when selecting an item
69        .bind("keydown", function (event) {
70            if (event.keyCode === jQuery.ui.keyCode.TAB &&
71                jQuery(this).data("ui-autocomplete").menu.active) {
72                event.preventDefault();
73            }
74        })
75        .autocomplete({
76            source: function (request, response) {
77                jQuery.getJSON(DOKU_BASE + 'lib/exe/ajax.php?call=plugin_tagging_autocomplete', {
78                    term: extractLast(request.term),
79                }, response);
80            },
81            search: function () {
82                // custom minLength
83                var term = extractLast(this.value);
84                if (term.length < 2) {
85                    return false;
86                }
87                return true;
88            },
89            focus: function () {
90                // prevent value inserted on focus
91                return false;
92            },
93            select: function (event, ui) {
94                var terms = split(this.value);
95                // remove the current input
96                terms.pop();
97                // add the selected item
98                terms.push(ui.item.value);
99                // add placeholder to get the comma-and-space at the end
100                terms.push("");
101                this.value = terms.join(", ");
102                return false;
103            },
104        });
105});
106