1// used to identify pickers 2var pickercounter=0; 3 4/** 5 * Create a toolbar 6 * 7 * @param string tbid ID of the element where to insert the toolbar 8 * @param string edid ID of the editor textarea 9 * @param array tb Associative array defining the buttons 10 * @param bool allowblock Allow buttons creating multiline content 11 * @author Andreas Gohr <andi@splitbrain.org> 12 */ 13function initToolbar(tbid,edid,tb, allowblock){ 14 var $ = jQuery; 15 if (typeof tbid == 'string') { 16 var toolbar = $('#' + tbid); 17 } else { 18 var toolbar = $(tbid); 19 } 20 21 if(toolbar.length == 0) return; 22 23 var edit = $('#' + edid); 24 if(edit.length == 0) return; 25 26 if(edit.attr('readOnly')) return; 27 28 if (typeof allowblock === 'undefined') { 29 allowblock = true; 30 } 31 32 //empty the toolbar area: 33 toolbar.html(''); 34 35 var cnt = tb.length; 36 37 for(var i=0; i<cnt; i++){ 38 if (!allowblock && tb[i].block === true) { 39 continue; 40 } 41 var actionFunc; 42 43 // create new button (jQuery object) 44 var btn = $(createToolButton(tb[i]['icon'], 45 tb[i]['title'], 46 tb[i]['key'], 47 tb[i]['id'], 48 tb[i]['class'])); 49 50 // type is a tb function -> assign it as onclick 51 actionFunc = 'tb_'+tb[i]['type']; 52 if( $.isFunction(window[actionFunc]) ){ 53 btn.bind('click', bind(window[actionFunc],btn,tb[i],edid) ); 54 toolbar.append(btn); 55 continue; 56 } 57 58 // type is a init function -> execute it 59 actionFunc = 'addBtnAction'+tb[i]['type'].charAt(0).toUpperCase()+tb[i]['type'].substring(1); 60 if( $.isFunction(window[actionFunc]) ){ 61 if(window[actionFunc](btn, tb[i], edid)){ 62 toolbar.append(btn); 63 } 64 continue; 65 } 66 67 alert('unknown toolbar type: '+tb[i]['type']+' '+actionFunc); 68 } // end for 69 70} 71 72/** 73 * Button action for format buttons 74 * 75 * @param DOMElement btn Button element to add the action to 76 * @param array props Associative array of button properties 77 * @param string edid ID of the editor textarea 78 * @author Gabriel Birke <birke@d-scribe.de> 79 * @author Andreas Gohr <andi@splitbrain.org> 80 */ 81function tb_format(btn, props, edid) { 82 var sample = props['title']; 83 if(props['sample']){ 84 sample = props['sample']; 85 } 86 insertTags(edid, 87 fixtxt(props['open']), 88 fixtxt(props['close']), 89 fixtxt(sample)); 90 pickerClose(); 91 return false; 92} 93 94/** 95 * Button action for format buttons 96 * 97 * This works exactly as tb_format() except that, if multiple lines 98 * are selected, each line will be formatted seperately 99 * 100 * @param DOMElement btn Button element to add the action to 101 * @param array props Associative array of button properties 102 * @param string edid ID of the editor textarea 103 * @author Gabriel Birke <birke@d-scribe.de> 104 * @author Andreas Gohr <andi@splitbrain.org> 105 */ 106function tb_formatln(btn, props, edid) { 107 var sample = props['title']; 108 if(props['sample']){ 109 sample = props['sample']; 110 } 111 sample = fixtxt(sample); 112 113 props['open'] = fixtxt(props['open']); 114 props['close'] = fixtxt(props['close']); 115 116 // is something selected? 117 var opts; 118 var selection = getSelection($(edid)); 119 if(selection.getLength()){ 120 sample = selection.getText(); 121 opts = {nosel: true}; 122 }else{ 123 opts = { 124 startofs: props['open'].length, 125 endofs: props['close'].length 126 }; 127 } 128 129 sample = sample.split("\n").join(props['close']+"\n"+props['open']); 130 sample = props['open']+sample+props['close']; 131 132 pasteText(selection,sample,opts); 133 134 pickerClose(); 135 return false; 136} 137 138/** 139 * Button action for insert buttons 140 * 141 * @param DOMElement btn Button element to add the action to 142 * @param array props Associative array of button properties 143 * @param string edid ID of the editor textarea 144 * @author Gabriel Birke <birke@d-scribe.de> 145 * @author Andreas Gohr <andi@splitbrain.org> 146 */ 147function tb_insert(btn, props, edid) { 148 insertAtCarret(edid,fixtxt(props['insert'])); 149 pickerClose(); 150 return false; 151} 152 153/** 154 * Button action for the media popup 155 * 156 * @param DOMElement btn Button element to add the action to 157 * @param array props Associative array of button properties 158 * @param string edid ID of the editor textarea 159 * @author Andreas Gohr <andi@splitbrain.org> 160 */ 161function tb_mediapopup(btn, props, edid) { 162 window.open( 163 DOKU_BASE+props['url']+encodeURIComponent(NS)+'&edid='+encodeURIComponent(edid), 164 props['name'], 165 props['options']); 166 return false; 167} 168 169/** 170 * Button action for automatic headlines 171 * 172 * Insert a new headline based on the current section level 173 * 174 * @param DOMElement btn Button element to add the action to 175 * @param array props Associative array of button properties 176 * @param string edid ID of the editor textarea 177 * @author Andreas Gohr <andi@splitbrain.org> 178 */ 179function tb_autohead(btn, props, edid){ 180 var lvl = currentHeadlineLevel(edid); 181 182 // determine new level 183 lvl += props['mod']; 184 if(lvl < 1) lvl = 1; 185 if(lvl > 5) lvl = 5; 186 187 var tags = '='; 188 for(var i=0; i<=5-lvl; i++) tags += '='; 189 insertTags(edid, tags+' ', ' '+tags+"\n", props['text']); 190 pickerClose(); 191 return false; 192} 193 194 195/** 196 * Add button action for picker buttons and create picker element 197 * 198 * @param DOMElement btn Button element to add the action to 199 * @param array props Associative array of button properties 200 * @param string edid ID of the editor textarea 201 * @return boolean If button should be appended 202 * @author Gabriel Birke <birke@d-scribe.de> 203 */ 204function addBtnActionPicker($btn, props, edid) { 205 var pickerid = 'picker'+(pickercounter++); 206 createPicker(pickerid, props, edid); 207 208 $btn.click( 209 function() { 210 pickerToggle(pickerid,$btn); 211 return false; 212 } 213 ); 214 215 return true; 216} 217 218/** 219 * Add button action for the link wizard button 220 * 221 * @param DOMElement btn Button element to add the action to 222 * @param array props Associative array of button properties 223 * @param string edid ID of the editor textarea 224 * @return boolean If button should be appended 225 * @author Andreas Gohr <gohr@cosmocode.de> 226 */ 227function addBtnActionLinkwiz(btn, props, edid) { 228 dw_linkwiz.init(jQuery('#'+edid)); 229 jQuery(btn).click(function(){ 230 dw_linkwiz.toggle(); 231 return false; 232 }); 233 return true; 234} 235 236 237/** 238 * Show/Hide a previosly created picker window 239 * 240 * @author Andreas Gohr <andi@splitbrain.org> 241 */ 242function pickerToggle(pickerid,$btn){ 243 var $picker = jQuery('#' + pickerid); 244 if ($picker.hasClass('hiddenpicker')) { 245 var pos = $btn.offset(); 246 $picker.hide().removeClass('hiddenpicker') 247 .dw_show() 248 .offset({left: pos.left+3, top: pos.top+$btn[0].offsetHeight+3}) 249 } else { 250 $picker.dw_hide(function () { 251 jQuery(this).addClass('hiddenpicker').show(); 252 }); 253 } 254} 255 256/** 257 * Close all open pickers 258 * 259 * @author Andreas Gohr <andi@splitbrain.org> 260 */ 261function pickerClose(){ 262 jQuery('.picker').addClass('hiddenpicker'); 263} 264 265 266/** 267 * Replaces \n with linebreaks 268 */ 269function fixtxt(str){ 270 return str.replace(/\\n/g,"\n"); 271} 272 273