xref: /plugin/tagging/script.js (revision 3bbb8b9b617360c97791d7a1586a472334bf2d2a)
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');
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('textarea');
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:   {
50                        '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('textarea').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('textarea')
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