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        const { rev } = JSINFO.plugin_structsection;
22        const rid = 0;
23
24        if (!pid) return;
25        if (!field) return;
26
27        // prepare the edit overlay
28        const $div = jQuery('<div class="struct_inlineditor"><form></form><div class="err"></div></div>');
29        const $form = $div.find('form');
30        const $errors = $div.find('div.err').hide();
31        const $save = jQuery('<button type="submit">Save</button>');
32        const $cancel = jQuery('<button>Cancel</button>');
33        $form.append(jQuery('<input type="hidden" name="pid">').val(pid));
34        $form.append(jQuery('<input type="hidden" name="field">').val(field));
35        $form.append(jQuery('<input type="hidden" name="rid">').val(rid));
36        $form.append(jQuery('<input type="hidden" name="rev">').val(rev));
37        $form.append('<input type="hidden" name="call" value="plugin_struct_inline_save">');
38        $form.append(jQuery('<div class="ctl">').append($save).append($cancel));
39
40        /**
41         * load the editor
42         */
43        jQuery.post(
44            `${DOKU_BASE}lib/exe/ajax.php`,
45            {
46                call: 'plugin_struct_inline_editor',
47                pid,
48                field,
49                rid,
50                rev,
51            },
52            (data) => {
53                if (!data) return; // we're done
54
55                $form.prepend(data);
56
57                // show
58                $self.closest('.dokuwiki').append($div);
59                $div.position({
60                    my: 'left top',
61                    at: 'left top',
62                    of: $self,
63                });
64
65                // attach entry handlers to the inline form
66                // EntryEditor($form);
67
68                // focus first input
69                $form.find('input, textarea').first().focus();
70            },
71        );
72
73        /**
74         * Save the data, then close the form
75         */
76        $form.submit((submitEvent) => {
77            submitEvent.preventDefault();
78            jQuery.post(
79                `${DOKU_BASE}lib/exe/ajax.php`,
80                $form.serialize(),
81            )
82                .done((data) => {
83                    const saved = JSON.parse(data);
84                    $self.html(saved.value);
85                    JSINFO.plugin_structsection.rev = saved.rev;
86                    // save succeeded display new value and close editor
87                    $div.remove();
88                })
89                .fail((data) => {
90                    // something went wrong, display error
91                    $errors.text(data.responseText).show();
92                });
93        });
94
95        /**
96         * Close the editor without saving
97         */
98        $cancel.click((clickEvent) => {
99            // unlock page
100            jQuery.post(
101                `${DOKU_BASE}lib/exe/ajax.php`,
102                {
103                    call: 'plugin_struct_inline_cancel',
104                    pid,
105                },
106            );
107
108            clickEvent.preventDefault();
109            $div.remove();
110        });
111    });
112});
113