xref: /plugin/structsection/script/main.js (revision 591eaa94ffdb89ec3e21a085097de1f8e497030a)
1jQuery(() => {
2    /**
3     * This removes unintended elements created by other plugins and broken for structsection
4     *
5     * Currently it removes the edit-button shown by edittable, which cannot work inside this plugin
6     *
7     * @returns {void}
8     */
9    function cleanOtherPlugins() {
10        jQuery('#plugin__structsection_output').find('.editbutton_table').remove();
11    }
12    cleanOtherPlugins();
13
14    jQuery('#plugin__structsection_output').on('submit', 'form.btn_secedit', function handleEdit(e) {
15        e.preventDefault();
16        e.stopPropagation();
17
18        const $self = jQuery(this).parent().prev().find('div.level2'); // fixme: too fragile!
19        const pid = JSINFO.id;
20        const field = $self.data('struct');
21
22        if (!pid) return;
23        if (!field) return;
24
25        // prepare the edit overlay
26        const $div = jQuery('<div class="struct_inlineditor"><form></form><div class="err"></div></div>');
27        const $form = $div.find('form');
28        const $errors = $div.find('div.err').hide();
29        const $save = jQuery('<button type="submit">Save</button>');
30        const $cancel = jQuery('<button>Cancel</button>');
31        $form.append(jQuery('<input type="hidden" name="pid">').val(pid));
32        $form.append(jQuery('<input type="hidden" name="field">').val(field));
33        $form.append('<input type="hidden" name="call" value="plugin_struct_inline_save">');
34        $form.append(jQuery('<div class="ctl">').append($save).append($cancel));
35
36        /**
37         * load the editor
38         */
39        jQuery.post(
40            `${DOKU_BASE}lib/exe/ajax.php`,
41            {
42                call: 'plugin_struct_inline_editor',
43                pid,
44                field,
45            },
46            (data) => {
47                if (!data) return; // we're done
48
49                $form.prepend(data);
50
51                // show
52                $self.closest('.dokuwiki').append($div);
53                $div.position({
54                    my: 'left top',
55                    at: 'left top',
56                    of: $self,
57                });
58
59                // attach entry handlers to the inline form
60                // EntryEditor($form);
61
62                // focus first input
63                $form.find('input, textarea').first().focus();
64            },
65        );
66
67        /**
68         * Save the data, then close the form
69         */
70        $form.submit((submitEvent) => {
71            submitEvent.preventDefault();
72            jQuery.post(
73                `${DOKU_BASE}lib/exe/ajax.php`,
74                $form.serialize(),
75            )
76                .done((data) => {
77                    // save succeeded display new value and close editor
78                    $self.html(data);
79                    $div.remove();
80                })
81                .fail((data) => {
82                    // something went wrong, display error
83                    $errors.text(data.responseText).show();
84                });
85        });
86
87        /**
88         * Close the editor without saving
89         */
90        $cancel.click((clickEvent) => {
91            // unlock page
92            jQuery.post(
93                `${DOKU_BASE}lib/exe/ajax.php`,
94                {
95                    call: 'plugin_struct_inline_cancel',
96                    pid,
97                },
98            );
99
100            clickEvent.preventDefault();
101            $div.remove();
102        });
103    });
104});
105