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 if(result.responseJSON) { 23 if (result.responseJSON.stacktrace) { 24 console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace); 25 } 26 alert(result.responseJSON.error); 27 } else { 28 // some fatal error occured, get a text only version of the response 29 alert(jQuery(result.responseText).text()); 30 } 31 }); 32 } 33 34 /** 35 * @param {string} val 36 * @return {array} 37 */ 38 function split(val) { 39 return val.split(/,\s*/); 40 } 41 42 /** 43 * @param {string} term 44 * @returns {string} 45 */ 46 function extractLast(term) { 47 return split(term).pop(); 48 } 49 50 /** 51 * hints 52 */ 53 jQuery('.struct .hashint').tooltip(); 54 55 /** 56 * Attach datepicker to date types 57 */ 58 jQuery('input.struct_date').datepicker({ 59 dateFormat: 'yy-mm-dd' 60 }); 61 62 /** 63 * Attach image dialog to image types 64 */ 65 jQuery('button.struct_media').click(function () { 66 var input_id = jQuery(this).siblings('input').attr('id'); 67 window.open( 68 DOKU_BASE + 'lib/exe/mediamanager.php' + 69 '?ns=' + encodeURIComponent(JSINFO['namespace']) + 70 '&edid=' + encodeURIComponent(input_id) + 71 '&onselect=insertStructMedia', 72 'mediaselect', 73 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); // 74 }); 75 76 /** 77 * Custom onSelect handler for struct img button 78 */ 79 window.insertStructMedia = function (edid, mediaid, opts, align) { 80 jQuery('#' + edid).val(mediaid).change(); 81 }; 82 83 /** 84 * Autocomplete for single type 85 */ 86 jQuery('input.struct_autocomplete').autocomplete({ 87 ismulti: false, 88 source: function (request, cb) { 89 var name = this.element.attr('name'); 90 name = name.substring(19, name.length - 1); 91 name = name.replace('][', '.'); 92 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').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').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