xref: /plugin/struct/script/InlineEditor.js (revision 63e019d0e7d287267656881798da4ca77bc2da4b)
1/**
2 * Inline Editor
3 */
4var InlineEditor = function ($table) {
5
6
7    $table.find('td').dblclick(function (e) {
8        e.preventDefault();
9
10        var $self = jQuery(this);
11        var pid = $self.parent().data('pid');
12        var field = $self.parents('table').find('tr th').eq($self.index()).data('field');
13
14        if (!pid) return;
15        if (!field) return;
16
17        // prepare the edit overlay
18        var $div = jQuery('<div class="struct_inlineditor"><form></form><div class="err"></div></div>');
19        var $form = $div.find('form');
20        var $errors = $div.find('div.err').hide();
21        var $save = jQuery('<button type="submit">Save</button>');
22        var $cancel = jQuery('<button>Cancel</button>');
23        $form.append(jQuery('<input type="hidden" name="pid">').val(pid));
24        $form.append(jQuery('<input type="hidden" name="field">').val(field));
25        $form.append('<input type="hidden" name="call" value="plugin_struct_inline_save">');
26        $form.append(jQuery('<div class="ctl">').append($save).append($cancel));
27
28        /**
29         * load the editor
30         */
31        jQuery.post(
32            DOKU_BASE + 'lib/exe/ajax.php',
33            {
34                call: 'plugin_struct_inline_editor',
35                pid: pid,
36                field: field
37            },
38            function (data) {
39                if (!data) return; // we're done
40
41                $form.prepend(data);
42
43                // show
44                jQuery('.dokuwiki').append($div);
45                $div.position({
46                    my: 'left top',
47                    at: 'left top',
48                    of: $self
49                });
50
51                // attach entry handlers to the inline form
52                EntryEditor($form);
53
54                // focus first input
55                $form.find('input, textarea').first().focus();
56            }
57        );
58
59        /**
60         * Save the data, then close the form
61         */
62        $form.submit(function (e) {
63            e.preventDefault();
64            jQuery.post(
65                DOKU_BASE + 'lib/exe/ajax.php',
66                $form.serialize()
67            )
68                .done(function (data) {
69                    // save succeeded display new vlaue and close editor
70                    $self.html(data);
71                    $div.remove();
72                })
73                .fail(function (data) {
74                    // something went wrong, display error
75                    $errors.text(data.responseText).show();
76                })
77            ;
78
79
80        });
81
82        /**
83         * Close the editor without saving
84         */
85        $cancel.click(function (e) {
86            // unlock page
87            jQuery.post(
88                DOKU_BASE + 'lib/exe/ajax.php',
89                {
90                    call: 'plugin_struct_inline_cancel',
91                    pid: pid
92                }
93            );
94
95            e.preventDefault();
96            $div.remove();
97        });
98    });
99
100};
101