xref: /plugin/recommend/script.js (revision 2b1d0a413589ad2c5e5688d08a43a115363f9f3b)
1/**
2 * jQuery rewrite is almost completely taken from infomail plugin
3 * @author Andreas Gohr
4 */
5
6const recommend = {
7    $dialog: null,
8
9    /**
10     * Attach click handler to our link
11     */
12    init: function () {
13        jQuery('a.plugin_recommend').click(recommend.initform);
14        jQuery('li.recommend a').click(recommend.initform);
15    },
16
17    /**
18     * Initializes the form dialog on click
19     *
20     * @param {Event} e
21     */
22    initform: function (e) {
23        e.stopPropagation();
24        e.preventDefault();
25
26        let url = new URL(e.currentTarget.href);
27        // searchParams only works, when no URL rewriting takes place
28        // from Dokuwiki - else there is no parameter id and this
29        // returns null
30        let id = url.searchParams.get('id');
31        if ( id === null ) {
32            //get full path component to pass full path for later namespace convert
33            id = url.pathname;
34        }
35
36        recommend.$dialog = jQuery('<div></div>');
37        recommend.$dialog.dialog(
38            {
39                modal: true,
40                title: LANG.plugins.recommend.formname + ' ' + id,
41                minWidth: 680,
42                height: "auto",
43                close: function () {
44                    recommend.$dialog.dialog('destroy')
45                }
46            }
47        );
48
49        jQuery.get(
50            DOKU_BASE + 'lib/exe/ajax.php',
51            {
52                'call': 'recommend',
53                'id': id
54            },
55            recommend.handleResult,
56            'html'
57        );
58    },
59
60    /**
61     * Display the result and attach handlers
62     *
63     * @param {string} data The HTML
64     */
65    handleResult: function (data) {
66
67        function commasplit( val ) {
68            return val.split( /,\s*/ );
69        }
70
71        recommend.$dialog.html(data);
72        recommend.$dialog.find('button[type=reset]').click(recommend.cancel);
73        recommend.$dialog.find('button[type=submit]').click(recommend.send);
74        recommend.$dialog.find('input[name=r_email]').autocomplete({
75            source: function (request, cb) {
76                let term = request.term;
77                term = commasplit(term).pop();
78
79                const payload = {};
80                payload['call'] = 'plugin_recommend_ac';
81                payload['search'] = term;
82
83                jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', payload, cb, 'json')
84                    .fail(function (result) {
85                        if (result.responseJSON) {
86                            if (result.responseJSON.stacktrace) {
87                                console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace);
88                            }
89                            alert(result.responseJSON.error);
90                        } else {
91                            // some fatal error occurred, get a text only version of the response
92                            alert(jQuery(result.responseText).text());
93                        }
94                    });
95            },
96            focus: function() {
97                // prevent value inserted on focus
98                return false;
99            },
100            select: function( event, ui ) {
101                let terms = commasplit( this.value );
102                // remove the current input
103                terms.pop();
104                // add the selected item
105                terms.push( ui.item.value );
106                // add placeholder to get the comma-and-space at the end
107                terms.push( "" );
108                this.value = terms.join( ", " );
109                return false;
110            }
111        });
112    },
113
114    /**
115     * Cancel the recommend form
116     *
117     * @param {Event} e
118     */
119    cancel: function (e) {
120        e.preventDefault();
121        e.stopPropagation();
122        recommend.$dialog.dialog('destroy');
123    },
124
125    /**
126     * Serialize the form and send it
127     *
128     * @param {Event} e
129     */
130    send: function (e) {
131        e.preventDefault();
132        e.stopPropagation();
133
134        let data = recommend.$dialog.find('form').serialize();
135        data = data + '&call=recommend';
136
137        recommend.$dialog.html('...');
138        jQuery.post(
139            DOKU_BASE + 'lib/exe/ajax.php',
140            data,
141            recommend.handleResult,
142            'html'
143        );
144    }
145};
146jQuery(recommend.init);
147