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