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            // Convert url to string an get the last part without
33            // any parameters from actions and the like
34            url = String(url);
35            id = url.split('/').pop().split('?')[0];
36        }
37
38        recommend.$dialog = jQuery('<div></div>');
39        recommend.$dialog.dialog(
40            {
41                modal: true,
42                title: LANG.plugins.recommend.formname + ' ' + id,
43                minWidth: 680,
44                height: "auto",
45                close: function () {
46                    recommend.$dialog.dialog('destroy')
47                }
48            }
49        );
50
51        jQuery.get(
52            DOKU_BASE + 'lib/exe/ajax.php',
53            {
54                'call': 'recommend',
55                'id': id
56            },
57            recommend.handleResult,
58            'html'
59        );
60    },
61
62    /**
63     * Display the result and attach handlers
64     *
65     * @param {string} data The HTML
66     */
67    handleResult: function (data) {
68
69        function commasplit( val ) {
70            return val.split( /,\s*/ );
71        }
72
73        recommend.$dialog.html(data);
74        recommend.$dialog.find('button[type=reset]').click(recommend.cancel);
75        recommend.$dialog.find('button[type=submit]').click(recommend.send);
76        recommend.$dialog.find('input[name=r_email]').autocomplete({
77            source: function (request, cb) {
78                let term = request.term;
79                term = commasplit(term).pop();
80
81                const payload = {};
82                payload['call'] = 'plugin_recommend_ac';
83                payload['search'] = term;
84
85                jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', payload, cb, 'json')
86                    .fail(function (result) {
87                        if (result.responseJSON) {
88                            if (result.responseJSON.stacktrace) {
89                                console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace);
90                            }
91                            alert(result.responseJSON.error);
92                        } else {
93                            // some fatal error occurred, get a text only version of the response
94                            alert(jQuery(result.responseText).text());
95                        }
96                    });
97            },
98            focus: function() {
99                // prevent value inserted on focus
100                return false;
101            },
102            select: function( event, ui ) {
103                let terms = commasplit( this.value );
104                // remove the current input
105                terms.pop();
106                // add the selected item
107                terms.push( ui.item.value );
108                // add placeholder to get the comma-and-space at the end
109                terms.push( "" );
110                this.value = terms.join( ", " );
111                return false;
112            }
113        });
114    },
115
116    /**
117     * Cancel the recommend form
118     *
119     * @param {Event} e
120     */
121    cancel: function (e) {
122        e.preventDefault();
123        e.stopPropagation();
124        recommend.$dialog.dialog('destroy');
125    },
126
127    /**
128     * Serialize the form and send it
129     *
130     * @param {Event} e
131     */
132    send: function (e) {
133        e.preventDefault();
134        e.stopPropagation();
135
136        let data = recommend.$dialog.find('form').serialize();
137        data = data + '&call=recommend';
138
139        recommend.$dialog.html('...');
140        jQuery.post(
141            DOKU_BASE + 'lib/exe/ajax.php',
142            data,
143            recommend.handleResult,
144            'html'
145        );
146    }
147};
148jQuery(recommend.init);
149