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 jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, fn, 'json') 21 .fail(function (result) { 22 alert(result.responseJSON.error); 23 }); 24 } 25 26 /** 27 * @param {string} val 28 * @return {array} 29 */ 30 function split(val) { 31 return val.split(/,\s*/); 32 } 33 34 /** 35 * @param {string} term 36 * @returns {string} 37 */ 38 function extractLast(term) { 39 return split(term).pop(); 40 } 41 42 /** 43 * Attach datepicker to date types 44 */ 45 jQuery('input.struct_date').datepicker({ 46 dateFormat: 'yy-mm-dd' 47 }); 48 49 /** 50 * Attach image dialog to image types 51 */ 52 jQuery('button.struct_img').click(function () { 53 var input_id = jQuery(this).siblings('input').attr('id'); 54 window.open( 55 DOKU_BASE + 'lib/exe/mediamanager.php' + 56 '?ns=' + encodeURIComponent(JSINFO['namespace']) + 57 '&edid=' + encodeURIComponent(input_id) + 58 '&onselect=insertStructImage', 59 'mediaselect', 60 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); // 61 }); 62 63 /** 64 * Custom onSelect handler for struct img button 65 */ 66 window.insertStructImage = function (edid, mediaid, opts, align) { 67 jQuery('#' + edid).val(mediaid).change(); 68 }; 69 70 /** 71 * Autocomplete for single type 72 */ 73 jQuery('input.struct_user, input.struct_page').autocomplete({ 74 ismulti: false, 75 source: function (request, cb) { 76 var name = this.element.attr('name'); 77 name = name.substring(19, name.length - 1); 78 name = name.replace('][', '.'); 79 80 var term = request.term; 81 if (this.options.ismulti) { 82 term = extractLast(term); 83 } 84 struct_ajax(name, cb, {search: term}); 85 } 86 }); 87 88 /** 89 * Autocomplete for multi type 90 */ 91 jQuery('.multiwrap input.struct_user, .multiwrap input.struct_page').autocomplete('option', { 92 ismulti: true, 93 focus: function () { 94 // prevent value inserted on focus 95 return false; 96 }, 97 select: function (event, ui) { 98 var terms = split(this.value); 99 // remove the current input 100 terms.pop(); 101 // add the selected item 102 terms.push(ui.item.value); 103 // add placeholder to get the comma-and-space at the end 104 terms.push(""); 105 this.value = terms.join(", "); 106 return false; 107 } 108 }); 109 110 /** 111 * Toggle the disabled class in the schema editor 112 */ 113 jQuery('#plugin__struct').find('td.isenabled input').change(function () { 114 var $checkbox = jQuery(this); 115 $checkbox.parents('tr').toggleClass('disabled', !$checkbox.prop('checked')); 116 }); 117 118 var $dokuform = jQuery('#dw__editform'); 119 120 /** 121 * Duplicate the elements in .newtemplate whenever any input in it changes 122 */ 123 $dokuform.find('.struct .newtemplate').each(function () { 124 var $tplwrapper = jQuery(this); 125 var $tpl = $tplwrapper.children().clone(true, true); 126 127 $tplwrapper.on('change', 'input,textarea,select', function () { 128 if (jQuery(this).val() == '') return; 129 130 // prepare a new template and make sure all the IDs in it are unique 131 var $copy = $tpl.clone(true, true); 132 copycount++; 133 $copy.find('*[id]').each(function () { 134 this.id = this.id + '_' + copycount; 135 }); 136 137 // append the template 138 $tplwrapper.append($copy); 139 }); 140 }); 141 142 /** 143 * Toggle fieldsets in edit form and remeber in cookie 144 */ 145 $dokuform.find('.struct fieldset legend').each(function () { 146 var $legend = jQuery(this); 147 var $fset = $legend.parent(); 148 149 // reinit saved state from cookie 150 if (DokuCookie.getValue($fset.data('schema'))) { 151 $fset.toggleClass('closed'); 152 } 153 154 // attach click handler 155 156 $legend.click(function () { 157 $fset.toggleClass('closed'); 158 // remember setting in preference cookie 159 if ($fset.hasClass('closed')) { 160 DokuCookie.setValue($fset.data('schema'), 1); 161 } else { 162 DokuCookie.setValue($fset.data('schema'), ''); 163 } 164 }); 165 }); 166 167}); 168