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