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