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