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