xref: /plugin/struct/script/EntryEditor.js (revision 37d0530f92e9df6b2c4a35e31f369f5a79f6cf0b)
1/**
2 * Attaches all the special handlers to the entry edit form
3 *
4 * @param {jQuery} $form The form where all the handlers are attached
5 */
6var EntryEditor = function($form) {
7
8    /** counter for copied multi templates */
9    var copycount = 0;
10
11    /**
12     * hints
13     */
14    $form.find('.struct .hashint').tooltip();
15
16    /**
17     * Attach datepicker to date types
18     */
19    $form.find('input.struct_date').datepicker({
20        dateFormat: 'yy-mm-dd'
21    });
22
23    /**
24     * Attach datepicker to datetype types, keeps time part
25     */
26    $form.find('input.struct_datetime').datepicker({
27        dateFormat: 'yy-mm-dd',
28        onSelect: function (date, inst) {
29            var $input = jQuery(this);
30            var both = inst.lastVal.split(' ', 2);
31            if (both.length == 2) {
32                date += ' ' + both[1];
33            } else {
34                date += ' 00:00:00';
35            }
36            $input.val(date);
37        }
38    });
39
40    /**
41     * Attach image dialog to image types
42     */
43    $form.find('button.struct_media').click(function () {
44        var input_id = jQuery(this).siblings('input').attr('id');
45        window.open(
46            DOKU_BASE + 'lib/exe/mediamanager.php' +
47            '?ns=' + encodeURIComponent(JSINFO['namespace']) +
48            '&edid=' + encodeURIComponent(input_id) +
49            '&onselect=insertStructMedia',
50            'mediaselect',
51            'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); //
52    });
53
54    /**
55     * Autocomplete for single type
56     */
57    $form.find('input.struct_autocomplete').autocomplete({
58        ismulti: false,
59        source: function (request, cb) {
60            var name = jQuery(this.element[0]).closest('label').data('column');
61            var term = request.term;
62            if (this.options.ismulti) {
63                term = extractLast(term);
64            }
65            struct_ajax(name, cb, {search: term});
66        }
67    });
68
69    /**
70     * Autocomplete for multi type
71     */
72    $form.find('.multiwrap input.struct_autocomplete').autocomplete('option', {
73        ismulti: true,
74        focus: function () {
75            // prevent value inserted on focus
76            return false;
77        },
78        select: function (event, ui) {
79            var terms = split(this.value);
80            // remove the current input
81            terms.pop();
82            // add the selected item
83            terms.push(ui.item.value);
84            // add placeholder to get the comma-and-space at the end
85            terms.push("");
86            this.value = terms.join(", ");
87            return false;
88        }
89    });
90
91    /**
92     * Duplicate the elements in .newtemplate whenever any input in it changes
93     */
94    $form.find('.struct .newtemplate').each(function () {
95        var $tplwrapper = jQuery(this);
96        var $tpl = $tplwrapper.children().clone(true, true);
97
98        $tplwrapper.on('change', 'input,textarea,select', function () {
99            if (jQuery(this).val() == '') return;
100
101            // prepare a new template and make sure all the IDs in it are unique
102            var $copy = $tpl.clone(true, true);
103            copycount++;
104            $copy.find('*[id]').each(function () {
105                this.id = this.id + '_' + copycount;
106            });
107
108            // append the template
109            $tplwrapper.append($copy);
110        });
111    });
112
113    /**
114     * Toggle fieldsets in edit form and remeber in cookie
115     */
116    $form.find('.struct fieldset legend').each(function () {
117        var $legend = jQuery(this);
118        var $fset = $legend.parent();
119
120        // reinit saved state from cookie
121        if (DokuCookie.getValue($fset.data('schema'))) {
122            $fset.toggleClass('closed');
123        }
124
125        // attach click handler
126
127        $legend.click(function () {
128            $fset.toggleClass('closed');
129            // remember setting in preference cookie
130            if ($fset.hasClass('closed')) {
131                DokuCookie.setValue($fset.data('schema'), 1);
132            } else {
133                DokuCookie.setValue($fset.data('schema'), '');
134            }
135        });
136    });
137
138};
139