xref: /plugin/recommend/script.js (revision 75494ca1620e263b1ca1a80c8e4863ca3326371d)
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        recommend.$dialog.html(data);
69        recommend.$dialog.find('button[type=reset]').click(recommend.cancel);
70        recommend.$dialog.find('button[type=submit]').click(recommend.send);
71    },
72
73    /**
74     * Cancel the recommend form
75     *
76     * @param {Event} e
77     */
78    cancel: function (e) {
79        e.preventDefault();
80        e.stopPropagation();
81        recommend.$dialog.dialog('destroy');
82    },
83
84    /**
85     * Serialize the form and send it
86     *
87     * @param {Event} e
88     */
89    send: function (e) {
90        e.preventDefault();
91        e.stopPropagation();
92
93        let data = recommend.$dialog.find('form').serialize();
94        data = data + '&call=recommend';
95
96        recommend.$dialog.html('...');
97        jQuery.post(
98            DOKU_BASE + 'lib/exe/ajax.php',
99            data,
100            recommend.handleResult,
101            'html'
102        );
103    }
104};
105jQuery(recommend.init);
106