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 = jQuery(this.element[0]).closest('label').data('column'); 90 var term = request.term; 91 if (this.options.ismulti) { 92 term = extractLast(term); 93 } 94 struct_ajax(name, cb, {search: term}); 95 } 96 }); 97 98 /** 99 * Autocomplete for multi type 100 */ 101 jQuery('.multiwrap input.struct_autocomplete').autocomplete('option', { 102 ismulti: true, 103 focus: function () { 104 // prevent value inserted on focus 105 return false; 106 }, 107 select: function (event, ui) { 108 var terms = split(this.value); 109 // remove the current input 110 terms.pop(); 111 // add the selected item 112 terms.push(ui.item.value); 113 // add placeholder to get the comma-and-space at the end 114 terms.push(""); 115 this.value = terms.join(", "); 116 return false; 117 } 118 }); 119 120 /** 121 * Handle tabs in the Schema Editor 122 */ 123 jQuery('#plugin__struct_json, #plugin__struct_delete').hide(); 124 jQuery('#plugin__struct_tabs').find('a').click(function (e) { 125 e.preventDefault(); 126 e.stopPropagation(); 127 var $me = jQuery(this); 128 if($me.parent().hasClass('active')) return; // nothing to do 129 130 $me.parent().parent().find('li').removeClass('active'); 131 $me.parent().addClass('active'); 132 jQuery('#plugin__struct_json, #plugin__struct_editor, #plugin__struct_delete').hide(); 133 jQuery($me.attr('href')).show(); 134 }); 135 136 137 /** 138 * Toggle the disabled class in the schema editor 139 */ 140 jQuery('#plugin__struct_editor').find('td.isenabled input').change(function () { 141 var $checkbox = jQuery(this); 142 $checkbox.parents('tr').toggleClass('disabled', !$checkbox.prop('checked')); 143 }); 144 145 var $dokuform = jQuery('#dw__editform'); 146 147 /** 148 * Duplicate the elements in .newtemplate whenever any input in it changes 149 */ 150 $dokuform.find('.struct .newtemplate').each(function () { 151 var $tplwrapper = jQuery(this); 152 var $tpl = $tplwrapper.children().clone(true, true); 153 154 $tplwrapper.on('change', 'input,textarea,select', function () { 155 if (jQuery(this).val() == '') return; 156 157 // prepare a new template and make sure all the IDs in it are unique 158 var $copy = $tpl.clone(true, true); 159 copycount++; 160 $copy.find('*[id]').each(function () { 161 this.id = this.id + '_' + copycount; 162 }); 163 164 // append the template 165 $tplwrapper.append($copy); 166 }); 167 }); 168 169 /** 170 * Toggle fieldsets in edit form and remeber in cookie 171 */ 172 $dokuform.find('.struct fieldset legend').each(function () { 173 var $legend = jQuery(this); 174 var $fset = $legend.parent(); 175 176 // reinit saved state from cookie 177 if (DokuCookie.getValue($fset.data('schema'))) { 178 $fset.toggleClass('closed'); 179 } 180 181 // attach click handler 182 183 $legend.click(function () { 184 $fset.toggleClass('closed'); 185 // remember setting in preference cookie 186 if ($fset.hasClass('closed')) { 187 DokuCookie.setValue($fset.data('schema'), 1); 188 } else { 189 DokuCookie.setValue($fset.data('schema'), ''); 190 } 191 }); 192 }); 193 194}); 195