1jQuery(function () { 2 'use strict'; 3 4 /** counter for copied multi templates */ 5 var copycount = 0; 6 7 /** 8 * Simplyfies AJAX requests for types 9 * 10 * @param {string} column A configured column in the form schema.name 11 * @param {function} fn Callback on success 12 * @param {object} data Additional data to pass 13 */ 14 function struct_ajax(column, fn, data) { 15 if (!data) data = {}; 16 17 data['call'] = 'plugin_struct'; 18 data['column'] = column; 19 20 21 console.log(data); 22 23 jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, fn, 'json') 24 .fail(function (result) { 25 alert(result.error); 26 }); 27 } 28 29 /** 30 * Attach datepicker to date types 31 */ 32 jQuery('input.struct_date').datepicker({ 33 dateFormat: 'yy-mm-dd' 34 }); 35 36 /** 37 * Attach image dialog to image types 38 */ 39 jQuery('button.struct_img').click(function () { 40 var input_id = jQuery(this).siblings('input').attr('id'); 41 window.open( 42 DOKU_BASE + 'lib/exe/mediamanager.php' + 43 '?ns=' + encodeURIComponent(JSINFO['namespace']) + 44 '&edid=' + encodeURIComponent(input_id) + 45 '&onselect=insertStructImage', 46 'mediaselect', 47 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); // 48 }); 49 50 /** 51 * Custom onSelect handler for struct img button 52 */ 53 window.insertStructImage = function (edid, mediaid, opts, align) { 54 jQuery('#' + edid).val(mediaid).change(); 55 }; 56 57 /** 58 * Autocomplete for user type 59 */ 60 jQuery('input.struct_user').autocomplete({ 61 source: function (request, cb) { 62 var name = this.element.attr('name'); 63 name = name.substring(19, name.length - 1); 64 name = name.replace('][', '.'); 65 struct_ajax(name, cb, {search: request.term}); 66 } 67 }); 68 69 /** 70 * Toggle the disabled class in the schema editor 71 */ 72 jQuery('#plugin__struct').find('td.isenabled input').change(function () { 73 var $checkbox = jQuery(this); 74 $checkbox.parents('tr').toggleClass('disabled', !$checkbox.prop('checked')); 75 }); 76 77 var $dokuform = jQuery('#dw__editform'); 78 79 /** 80 * Duplicate the elements in .newtemplate whenever any input in it changes 81 */ 82 $dokuform.find('.struct .newtemplate').each(function () { 83 var $tplwrapper = jQuery(this); 84 var $tpl = $tplwrapper.children().clone(true, true); 85 86 $tplwrapper.on('change', 'input,textarea,select', function () { 87 if (jQuery(this).val() == '') return; 88 89 // prepare a new template and make sure all the IDs in it are unique 90 var $copy = $tpl.clone(true, true); 91 copycount++; 92 $copy.find('*[id]').each(function () { 93 this.id = this.id + '_' + copycount; 94 }); 95 96 // append the template 97 $tplwrapper.append($copy); 98 }); 99 }); 100 101 /** 102 * Toggle fieldsets in edit form and remeber in cookie 103 */ 104 $dokuform.find('.struct fieldset legend').each(function () { 105 var $legend = jQuery(this); 106 var $fset = $legend.parent(); 107 108 // reinit saved state from cookie 109 if (DokuCookie.getValue($fset.data('schema'))) { 110 $fset.toggleClass('closed'); 111 } 112 113 // attach click handler 114 115 $legend.click(function () { 116 $fset.toggleClass('closed'); 117 // remember setting in preference cookie 118 if ($fset.hasClass('closed')) { 119 DokuCookie.setValue($fset.data('schema'), 1); 120 } else { 121 DokuCookie.setValue($fset.data('schema'), ''); 122 } 123 }); 124 }); 125 126}); 127