xref: /plugin/tagging/script.js (revision 9097c6c0a153a4dc41ede1cb201e84a1dc8a0c76)
1/* DOKUWIKI:include script/editable.js */
2/* DOKUWIKI:include script/admin.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');
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('textarea');
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:   {
51                        'call':   'plugin_tagging_admin_change',
52                        'id':     JSINFO.id,
53                        'sectok': JSINFO.sectok
54                    },
55                    success:
56                              function (response) {
57                                  jQuery('div.plugin_tagging_edit ul.tagging_cloud').html(response.html_cloud);
58                                  $form.find('textarea').val(response.tags_edit_value);
59                                  add_editable();
60                              }
61                });
62        };
63
64    add_editable();
65
66    jQuery('#tagging__edit_save').click(function (e) {
67        jQuery('div.plugin_tagging_edit ul.tagging_cloud').load(
68            DOKU_BASE + 'lib/exe/ajax.php',
69            $form.serialize(),
70            add_editable
71        );
72        $btns.show();
73        $form.hide();
74
75        e.preventDefault();
76        e.stopPropagation();
77        return false;
78    });
79
80    jQuery('#tagging__edit_cancel').click(function (e) {
81        $btns.show();
82        $form.hide();
83
84        e.preventDefault();
85        e.stopPropagation();
86        return false;
87    });
88
89    jQuery('.btn_tagging_edit button, #tagging__edit_save, #tagging__edit_cancel').button();
90
91    /**
92     * below follows auto completion as described on  http://jqueryui.com/autocomplete/#multiple-remote
93     */
94
95    function split(val) {
96        return val.split(/,\s*/);
97    }
98
99    function extractLast(term) {
100        return split(term).pop();
101    }
102
103    $form.find('textarea')
104    // don't navigate away from the field on tab when selecting an item
105        .bind('keydown', function (event) {
106            if (event.keyCode === jQuery.ui.keyCode.TAB &&
107                jQuery(this).data('ui-autocomplete').menu.active) {
108                event.preventDefault();
109            }
110        })
111        .autocomplete({
112            source: function (request, response) {
113                jQuery.getJSON(DOKU_BASE + 'lib/exe/ajax.php?call=plugin_tagging_autocomplete', {
114                    term: extractLast(request.term)
115                }, response);
116            },
117            search: function () {
118                // custom minLength
119                var term = extractLast(this.value);
120                if (term.length < 2) {
121                    return false;
122                }
123                return true;
124            },
125            focus:  function () {
126                // prevent value inserted on focus
127                return false;
128            },
129            select: function (event, ui) {
130                var terms = split(this.value);
131                // remove the current input
132                terms.pop();
133                // add the selected item
134                terms.push(ui.item.value);
135                // add placeholder to get the comma-and-space at the end
136                terms.push('');
137                this.value = terms.join(', ');
138                return false;
139            }
140        });
141
142    /**
143     * Add tag search parameter to all links in the advanced search tools
144     *
145     * This duplicates the solution from the watchcycle plugin, and should also be replaced
146     * with a DokuWiki event, which does not exist yet, but should handle extending search tools.
147     */
148    const $advancedOptions = jQuery('.search-results-form .advancedOptions');
149    if (!$advancedOptions.length) {
150        return;
151    }
152
153    /**
154     * Extracts the value of a given parameter from the URL querystring
155     *
156     * taken via watchcycle from https://stackoverflow.com/a/31412050/3293343
157     * @param param
158     * @returns {*}
159     */
160    function getQueryParam(param) {
161        location.search.substr(1)
162            .split("&")
163            .some(function(item) { // returns first occurence and stops
164                return item.split("=")[0] === param && (param = item.split("=")[1])
165            });
166        return param
167    }
168
169    if (getQueryParam('taggings') === 'and') {
170        $advancedOptions.find('a').each(function (index, element) {
171            const $link = jQuery(element);
172            // do not override parameters in our own links
173            if ($link.attr('href').indexOf('taggings') === -1) {
174                $link.attr('href', $link.attr('href') + '&taggings=and');
175            }
176        });
177    }
178});
179