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