xref: /plugin/struct/script.js (revision d7b296500cab514c167c2a2c46ca79305636d116)
1jQuery(function () {
2    'use strict';
3    /* globals JSINFO */
4
5    /** counter for copied multi templates */
6    var copycount = 0;
7
8    /**
9     * Simplyfies AJAX requests for types
10     *
11     * @param {string} column A configured column in the form schema.name
12     * @param {function} fn Callback on success
13     * @param {object} data Additional data to pass
14     */
15    function struct_ajax(column, fn, data) {
16        if (!data) data = {};
17
18        data['call'] = 'plugin_struct';
19        data['column'] = column;
20        data['id'] = JSINFO.id;
21        data['ns'] = JSINFO.namespace;
22
23        jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, fn, 'json')
24            .fail(function (result) {
25                if(result.responseJSON) {
26                    if (result.responseJSON.stacktrace) {
27                        console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace);
28                    }
29                    alert(result.responseJSON.error);
30                } else {
31                    // some fatal error occured, get a text only version of the response
32                    alert(jQuery(result.responseText).text());
33                }
34            });
35    }
36
37    /**
38     * @param {string} val
39     * @return {Array}
40     */
41    function split(val) {
42        return val.split(/,\s*/);
43    }
44
45    /**
46     * @param {string} term
47     * @returns {string}
48     */
49    function extractLast(term) {
50        return split(term).pop();
51    }
52
53    /**
54     * hints
55     */
56    jQuery('.struct .hashint').tooltip();
57
58    /**
59     * Attach datepicker to date types
60     */
61    jQuery('input.struct_date').datepicker({
62        dateFormat: 'yy-mm-dd'
63    });
64
65    /**
66     * Attach image dialog to image types
67     */
68    jQuery('button.struct_media').click(function () {
69        var input_id = jQuery(this).siblings('input').attr('id');
70        window.open(
71            DOKU_BASE + 'lib/exe/mediamanager.php' +
72            '?ns=' + encodeURIComponent(JSINFO['namespace']) +
73            '&edid=' + encodeURIComponent(input_id) +
74            '&onselect=insertStructMedia',
75            'mediaselect',
76            'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); //
77    });
78
79    /**
80     * Custom onSelect handler for struct img button
81     */
82    window.insertStructMedia = function (edid, mediaid, opts, align) {
83        jQuery('#' + edid).val(mediaid).change();
84    };
85
86    /**
87     * Autocomplete for single type
88     */
89    jQuery('input.struct_autocomplete').autocomplete({
90        ismulti: false,
91        source: function (request, cb) {
92            var name = jQuery(this.element[0]).closest('label').data('column');
93            var term = request.term;
94            if (this.options.ismulti) {
95                term = extractLast(term);
96            }
97            struct_ajax(name, cb, {search: term});
98        }
99    });
100
101    /**
102     * Autocomplete for multi type
103     */
104    jQuery('.multiwrap input.struct_autocomplete').autocomplete('option', {
105        ismulti: true,
106        focus: function () {
107            // prevent value inserted on focus
108            return false;
109        },
110        select: function (event, ui) {
111            var terms = split(this.value);
112            // remove the current input
113            terms.pop();
114            // add the selected item
115            terms.push(ui.item.value);
116            // add placeholder to get the comma-and-space at the end
117            terms.push("");
118            this.value = terms.join(", ");
119            return false;
120        }
121    });
122
123    /**
124     * Handle tabs in the Schema Editor
125     */
126    jQuery('#plugin__struct_json, #plugin__struct_delete').hide();
127    jQuery('#plugin__struct_tabs').find('a').click(function (e) {
128        e.preventDefault();
129        e.stopPropagation();
130        var $me = jQuery(this);
131        if($me.parent().hasClass('active')) return; // nothing to do
132
133        $me.parent().parent().find('li').removeClass('active');
134        $me.parent().addClass('active');
135        jQuery('#plugin__struct_json, #plugin__struct_editor, #plugin__struct_delete').hide();
136        jQuery($me.attr('href')).show();
137    });
138
139
140    /**
141     * Toggle the disabled class in the schema editor
142     */
143    jQuery('#plugin__struct_editor').find('td.isenabled input').change(function () {
144        var $checkbox = jQuery(this);
145        $checkbox.parents('tr').toggleClass('disabled', !$checkbox.prop('checked'));
146    });
147
148    var $dokuform = jQuery('#dw__editform');
149
150    /**
151     * Duplicate the elements in .newtemplate whenever any input in it changes
152     */
153    $dokuform.find('.struct .newtemplate').each(function () {
154        var $tplwrapper = jQuery(this);
155        var $tpl = $tplwrapper.children().clone(true, true);
156
157        $tplwrapper.on('change', 'input,textarea,select', function () {
158            if (jQuery(this).val() == '') return;
159
160            // prepare a new template and make sure all the IDs in it are unique
161            var $copy = $tpl.clone(true, true);
162            copycount++;
163            $copy.find('*[id]').each(function () {
164                this.id = this.id + '_' + copycount;
165            });
166
167            // append the template
168            $tplwrapper.append($copy);
169        });
170    });
171
172    /**
173     * Toggle fieldsets in edit form and remeber in cookie
174     */
175    $dokuform.find('.struct fieldset legend').each(function () {
176        var $legend = jQuery(this);
177        var $fset = $legend.parent();
178
179        // reinit saved state from cookie
180        if (DokuCookie.getValue($fset.data('schema'))) {
181            $fset.toggleClass('closed');
182        }
183
184        // attach click handler
185
186        $legend.click(function () {
187            $fset.toggleClass('closed');
188            // remember setting in preference cookie
189            if ($fset.hasClass('closed')) {
190                DokuCookie.setValue($fset.data('schema'), 1);
191            } else {
192                DokuCookie.setValue($fset.data('schema'), '');
193            }
194        });
195    });
196
197});
198