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