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