xref: /plugin/recommend/script.js (revision 945a672e77330a21e244c20981fe0c4baec1a002)
1const recommend = {
2    $dialog: null,
3
4    /**
5     * Attach click handler to our link
6     */
7    init: function () {
8        jQuery('a.plugin_recommend').click(recommend.initform);
9        jQuery('li.recommend a').click(recommend.initform);
10    },
11
12    /**
13     * Initializes the form dialog on click
14     *
15     * @param {Event} e
16     */
17    initform: function (e) {
18        e.stopPropagation();
19        e.preventDefault();
20
21        let url = new URL(e.currentTarget.href);
22        // searchParams only works, when no URL rewriting takes place
23        // from Dokuwiki - else there is no parameter id and this
24        // returns null
25        let id = url.searchParams.get('id');
26        if ( id === null ) {
27            // Convert url to string an get the last part without
28            // any parameters from actions and the like
29            url = String(url);
30            id = url.split('/').pop().split('?')[0];
31        }
32
33        recommend.$dialog = jQuery('<div></div>');
34        recommend.$dialog.dialog(
35            {
36                modal: true,
37                title: LANG.plugins.recommend.formname + ' ' + id,
38                minWidth: 680,
39                height: "auto",
40                close: function () {
41                    recommend.$dialog.dialog('destroy')
42                }
43            }
44        );
45
46        jQuery.get(
47            DOKU_BASE + 'lib/exe/ajax.php',
48            {
49                'call': 'recommend',
50                'id': id
51            },
52            recommend.handleResult,
53            'html'
54        );
55    },
56
57    /**
58     * Display the result and attach handlers
59     *
60     * @param {string} data The HTML
61     */
62    handleResult: function (data) {
63        recommend.$dialog.html(data);
64        recommend.$dialog.find('button[type=reset]').click(recommend.cancel);
65        recommend.$dialog.find('button[type=submit]').click(recommend.send);
66    },
67
68    /**
69     * Cancel the recommend form
70     *
71     * @param {Event} e
72     */
73    cancel: function (e) {
74        e.preventDefault();
75        e.stopPropagation();
76        recommend.$dialog.dialog('destroy');
77    },
78
79    /**
80     * Serialize the form and send it
81     *
82     * @param {Event} e
83     */
84    send: function (e) {
85        e.preventDefault();
86        e.stopPropagation();
87
88        let data = recommend.$dialog.find('form').serialize();
89        data = data + '&call=recommend';
90
91        recommend.$dialog.html('...');
92        jQuery.post(
93            DOKU_BASE + 'lib/exe/ajax.php',
94            data,
95            recommend.handleResult,
96            'html'
97        );
98    }
99};
100jQuery(recommend.init);
101