1/** 2 * Functions for text editing (toolbar stuff) 3 * 4 * @todo most of the stuff in here should be revamped and then moved to toolbar.js 5 * @author Andreas Gohr <andi@splitbrain.org> 6 */ 7 8/** 9 * Creates a toolbar button through the DOM 10 * 11 * Style the buttons through the toolbutton class 12 * 13 * @author Andreas Gohr <andi@splitbrain.org> 14 */ 15function createToolButton(icon,label,key,id,classname){ 16 var btn = document.createElement('button'); 17 var ico = document.createElement('img'); 18 19 // preapare the basic button stuff 20 btn.className = 'toolbutton'; 21 if(classname){ 22 btn.className += ' '+classname; 23 } 24 btn.title = label; 25 if(key){ 26 btn.title += ' ['+key.toUpperCase()+']'; 27 btn.accessKey = key; 28 } 29 30 // set IDs if given 31 if(id){ 32 btn.id = id; 33 ico.id = id+'_ico'; 34 } 35 36 // create the icon and add it to the button 37 if(icon.substr(0,1) == '/'){ 38 ico.src = icon; 39 }else{ 40 ico.src = DOKU_BASE+'lib/images/toolbar/'+icon; 41 } 42 btn.appendChild(ico); 43 44 return btn; 45} 46 47/** 48 * Creates a picker window for inserting text 49 * 50 * The given list can be an associative array with text,icon pairs 51 * or a simple list of text. Style the picker window through the picker 52 * class or the picker buttons with the pickerbutton class. Picker 53 * windows are appended to the body and created invisible. 54 * 55 * @param string id the ID to assign to the picker 56 * @param array props the properties for the picker 57 * @param string edid the ID of the textarea 58 * @rteurn DOMobject the created picker 59 * @author Andreas Gohr <andi@splitbrain.org> 60 */ 61function createPicker(id,props,edid){ 62 var icobase = props['icobase']; 63 var list = props['list']; 64 65 // create the wrapping div 66 var picker = document.createElement('div'); 67 picker.className = 'picker'; 68 if(props['class']){ 69 picker.className += ' '+props['class']; 70 } 71 picker.id = id; 72 picker.style.position = 'absolute'; 73 picker.style.marginLeft = '-10000px'; // no display:none, to keep access keys working 74 picker.style.marginTop = '-10000px'; 75 76 for(var key in list){ 77 if (!list.hasOwnProperty(key)) continue; 78 79 if(isNaN(key)){ 80 // associative array -> treat as image/value pairs 81 var btn = document.createElement('button'); 82 btn.className = 'pickerbutton'; 83 var ico = document.createElement('img'); 84 if(list[key].substr(0,1) == '/'){ 85 ico.src = list[key]; 86 }else{ 87 ico.src = DOKU_BASE+'lib/images/'+icobase+'/'+list[key]; 88 } 89 btn.title = key; 90 btn.appendChild(ico); 91 addEvent(btn,'click',bind(pickerInsert,key,edid)); 92 picker.appendChild(btn); 93 }else if(isString(list[key])){ 94 // a list of text -> treat as text picker 95 var btn = document.createElement('button'); 96 btn.className = 'pickerbutton'; 97 var txt = document.createTextNode(list[key]); 98 btn.title = list[key]; 99 btn.appendChild(txt); 100 addEvent(btn,'click',bind(pickerInsert,list[key],edid)); 101 picker.appendChild(btn); 102 }else{ 103 // a list of lists -> treat it as subtoolbar 104 initToolbar(picker,edid,list); 105 break; // all buttons handled already 106 } 107 108 } 109 var body = document.getElementsByTagName('body')[0]; 110 body.appendChild(picker); 111 return picker; 112} 113 114/** 115 * Called by picker buttons to insert Text and close the picker again 116 * 117 * @author Andreas Gohr <andi@splitbrain.org> 118 */ 119function pickerInsert(text,edid){ 120 insertAtCarret(edid,text); 121 pickerClose(); 122} 123 124/** 125 * Add button action for signature button 126 * 127 * @param DOMElement btn Button element to add the action to 128 * @param array props Associative array of button properties 129 * @param string edid ID of the editor textarea 130 * @return boolean If button should be appended 131 * @author Gabriel Birke <birke@d-scribe.de> 132 */ 133function addBtnActionSignature(btn, props, edid) { 134 if(typeof(SIG) != 'undefined' && SIG != ''){ 135 addEvent(btn,'click',bind(insertAtCarret,edid,SIG)); 136 return true; 137 } 138 return false; 139} 140 141/** 142 * Make intended formattings easier to handle 143 * 144 * Listens to all key inputs and handle indentions 145 * of lists and code blocks 146 * 147 * Currently handles space, backspce and enter presses 148 * 149 * @author Andreas Gohr <andi@splitbrain.org> 150 * @fixme handle tabs 151 */ 152function keyHandler(e){ 153 if(e.keyCode != 13 && 154 e.keyCode != 8 && 155 e.keyCode != 32) return; 156 var field = e.target; 157 var selection = getSelection(field); 158 if(selection.getLength()) return; //there was text selected, keep standard behavior 159 var search = "\n"+field.value.substr(0,selection.start); 160 var linestart = Math.max(search.lastIndexOf("\n"), 161 search.lastIndexOf("\r")); //IE workaround 162 search = search.substr(linestart); 163 164 165 if(e.keyCode == 13){ // Enter 166 // keep current indention for lists and code 167 var match = search.match(/(\n +([\*-] ?)?)/); 168 if(match){ 169 var scroll = field.scrollHeight; 170 var match2 = search.match(/^\n +[\*-]\s*$/); 171 // Cancel list if the last item is empty (i. e. two times enter) 172 if (match2 && field.value.substr(selection.start).match(/^($|\n)/)) { 173 field.value = field.value.substr(0, linestart) + "\n" + 174 field.value.substr(selection.start); 175 selection.start = linestart + 1; 176 selection.end = linestart + 1; 177 setSelection(selection); 178 } else { 179 insertAtCarret(field.id,match[1]); 180 } 181 field.scrollTop += (field.scrollHeight - scroll); 182 e.preventDefault(); // prevent enter key 183 return false; 184 } 185 }else if(e.keyCode == 8){ // Backspace 186 // unindent lists 187 var match = search.match(/(\n +)([*-] ?)$/); 188 if(match){ 189 var spaces = match[1].length-1; 190 191 if(spaces > 3){ // unindent one level 192 field.value = field.value.substr(0,linestart)+ 193 field.value.substr(linestart+2); 194 selection.start = selection.start - 2; 195 selection.end = selection.start; 196 }else{ // delete list point 197 field.value = field.value.substr(0,linestart)+ 198 field.value.substr(selection.start); 199 selection.start = linestart; 200 selection.end = linestart; 201 } 202 setSelection(selection); 203 e.preventDefault(); // prevent backspace 204 return false; 205 } 206 }else if(e.keyCode == 32){ // Space 207 // intend list item 208 var match = search.match(/(\n +)([*-] )$/); 209 if(match){ 210 field.value = field.value.substr(0,linestart)+' '+ 211 field.value.substr(linestart); 212 selection.start = selection.start + 2; 213 selection.end = selection.start; 214 setSelection(selection); 215 e.preventDefault(); // prevent space 216 return false; 217 } 218 } 219} 220 221//FIXME consolidate somewhere else 222addInitEvent(function(){ 223 var field = $('wiki__text'); 224 if(!field) return; 225 addEvent(field,'keydown',keyHandler); 226}); 227 228/** 229 * Determine the current section level while editing 230 * 231 * @author Andreas Gohr <gohr@cosmocode.de> 232 */ 233function currentHeadlineLevel(textboxId){ 234 var field = $(textboxId); 235 var selection = getSelection(field); 236 var search = "\n"+field.value.substr(0,selection.start); 237 var lasthl = search.lastIndexOf("\n=="); 238 if(lasthl == -1 && field.form.prefix){ 239 // we need to look in prefix context 240 search = field.form.prefix.value; 241 lasthl = search.lastIndexOf("\n=="); 242 } 243 search = search.substr(lasthl+1,6); 244 245 if(search == '======') return 1; 246 if(search.substr(0,5) == '=====') return 2; 247 if(search.substr(0,4) == '====') return 3; 248 if(search.substr(0,3) == '===') return 4; 249 if(search.substr(0,2) == '==') return 5; 250 251 return 0; 252} 253 254 255/** 256 * global var used for not saved yet warning 257 */ 258var textChanged = false; 259 260/** 261 * Delete the draft before leaving the page 262 */ 263function deleteDraft() { 264 if (is_opera) return; 265 266 // remove a possibly saved draft using ajax 267 var dwform = $('dw__editform'); 268 if(dwform){ 269 var params = 'call=draftdel'; 270 params += '&id='+encodeURIComponent(dwform.elements.id.value); 271 272 var sackobj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); 273 // this needs to be synchronous and GET to not be aborted upon page unload 274 sackobj.asynchronous = false; 275 sackobj.method = 'GET'; 276 sackobj.AjaxFailedAlert = ''; 277 sackobj.encodeURIString = false; 278 sackobj.runAJAX(params); 279 } 280} 281 282/** 283 * Activate "not saved" dialog, add draft deletion to page unload, 284 * add handlers to monitor changes 285 * 286 * Sets focus to the editbox as well 287 */ 288addInitEvent(function (){ 289 var editform = $('dw__editform'); 290 if (!editform) return; 291 292 var edit_text = $('wiki__text'); 293 if(edit_text) { 294 if(edit_text.readOnly) return; 295 296 // set focus 297 edit_text.focus(); 298 } 299 300 var checkfunc = function(){ 301 textChanged = true; //global var 302 summaryCheck(); 303 }; 304 addEvent(editform, 'change', checkfunc); 305 addEvent(editform, 'keydown', checkfunc); 306 307 window.onbeforeunload = function(){ 308 if(textChanged) { 309 return LANG.notsavedyet; 310 } 311 }; 312 window.onunload = deleteDraft; 313 314 // reset change memory var on submit 315 addEvent($('edbtn__save'), 'click', function(){ textChanged = false; }); 316 addEvent($('edbtn__preview'), 'click', function(){ textChanged = false; }); 317 318 var summary = $('edit__summary'); 319 addEvent(summary, 'change', summaryCheck); 320 addEvent(summary, 'keyup', summaryCheck); 321 if (textChanged) summaryCheck(); 322}); 323 324/** 325 * Checks if a summary was entered - if not the style is changed 326 * 327 * @author Andreas Gohr <andi@splitbrain.org> 328 */ 329function summaryCheck(){ 330 var sum = document.getElementById('edit__summary'); 331 if(sum.value === ''){ 332 sum.className='missing'; 333 }else{ 334 sum.className='edit'; 335 } 336} 337 338 339/** 340 * Class managing the timer to display a warning on a expiring lock 341 */ 342function locktimer_class(){ 343 this.sack = null; 344 this.timeout = 0; 345 this.timerID = null; 346 this.lasttime = null; 347 this.msg = ''; 348 this.pageid = ''; 349}; 350var locktimer = new locktimer_class(); 351 locktimer.init = function(timeout,msg,draft){ 352 // init values 353 locktimer.timeout = timeout*1000; 354 locktimer.msg = msg; 355 locktimer.draft = draft; 356 locktimer.lasttime = new Date(); 357 358 if(!$('dw__editform')) return; 359 locktimer.pageid = $('dw__editform').elements.id.value; 360 if(!locktimer.pageid) return; 361 362 // init ajax component 363 locktimer.sack = new sack(DOKU_BASE + 'lib/exe/ajax.php'); 364 locktimer.sack.AjaxFailedAlert = ''; 365 locktimer.sack.encodeURIString = false; 366 locktimer.sack.onCompletion = locktimer.refreshed; 367 368 // register refresh event 369 addEvent($('dw__editform'),'keypress',function(){locktimer.refresh();}); 370 // start timer 371 locktimer.reset(); 372 }; 373 374 /** 375 * (Re)start the warning timer 376 */ 377 locktimer.reset = function(){ 378 locktimer.clear(); 379 locktimer.timerID = window.setTimeout("locktimer.warning()", locktimer.timeout); 380 }; 381 382 /** 383 * Display the warning about the expiring lock 384 */ 385 locktimer.warning = function(){ 386 locktimer.clear(); 387 alert(locktimer.msg); 388 }; 389 390 /** 391 * Remove the current warning timer 392 */ 393 locktimer.clear = function(){ 394 if(locktimer.timerID !== null){ 395 window.clearTimeout(locktimer.timerID); 396 locktimer.timerID = null; 397 } 398 }; 399 400 /** 401 * Refresh the lock via AJAX 402 * 403 * Called on keypresses in the edit area 404 */ 405 locktimer.refresh = function(){ 406 var now = new Date(); 407 // refresh every minute only 408 if(now.getTime() - locktimer.lasttime.getTime() > 30*1000){ //FIXME decide on time 409 var params = 'call=lock&id='+encodeURIComponent(locktimer.pageid); 410 var dwform = $('dw__editform'); 411 if(locktimer.draft && dwform.elements.wikitext){ 412 params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value); 413 params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value); 414 params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value); 415 params += '&date='+encodeURIComponent(dwform.elements.date.value); 416 } 417 locktimer.sack.runAJAX(params); 418 locktimer.lasttime = now; 419 } 420 }; 421 422 423 /** 424 * Callback. Resets the warning timer 425 */ 426 locktimer.refreshed = function(){ 427 var data = this.response; 428 var error = data.charAt(0); 429 data = data.substring(1); 430 431 $('draft__status').innerHTML=data; 432 if(error != '1') return; // locking failed 433 locktimer.reset(); 434 }; 435// end of locktimer class functions 436 437