xref: /plugin/addnewpage/script.js (revision 2184b9df536ba26b3fce095eb7dbfd3499ee4eda)
1jQuery(function () {
2    jQuery(".addnewpage form").each(function () {
3        var $form = jQuery(this);
4        var $ns = $form.find("[name='np_cat']");
5        var $title = $form.find("input[name='title']");
6        var $id = $form.find("input[name='id']");
7        var $submit = $form.find(':submit');
8
9        // disable submit unless something is in input or input is disabled
10        if ($title.attr('type') === 'text') {
11            $submit.attr('disabled', 'disabled');
12            $title.on('input', function () {
13                if ($title.val().length > 0) {
14                    $submit.removeAttr('disabled');
15                } else {
16                    $submit.attr('disabled', 'disabled');
17                }
18            });
19        }
20
21        // Change the form's page-ID field on submit
22        $form.submit(function () {
23            const PLACEHOLDER = "@INPUT@";
24
25            // Build the new page ID
26            let page_id = $ns.val();
27            if (page_id.indexOf(PLACEHOLDER) !== -1) {
28                // Process the placeholder
29                page_id = page_id.replace(PLACEHOLDER, $title.val());
30            } else {
31                // There is no placeholder, just append the user's input
32                page_id += ":" + $title.val();
33            }
34
35            // Save the new page ID in the hidden form field
36            $id.val(page_id);
37
38            // Clean up the form vars, just to make the resultant URL a bit nicer
39            $ns.prop("disabled", true);
40            $title.prop("disabled", true);
41
42            return true;
43        });
44
45    });
46});
47