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 match2 = search.match(/^\n +[\*-]\s*$/); 170 // Cancel list if the last item is empty (i. e. two times enter) 171 if (match2 && field.value.substr(selection.start).match(/^($|\n)/)) { 172 field.value = field.value.substr(0, linestart) + "\n"; 173 } else { 174 insertAtCarret(field.id,match[1]); 175 } 176 var scroll = field.scrollHeight; 177 field.scrollTop += (field.scrollHeight - scroll); 178 e.preventDefault(); // prevent enter key 179 return false; 180 } 181 }else if(e.keyCode == 8){ // Backspace 182 // unindent lists 183 var match = search.match(/(\n +)([*-] ?)$/); 184 if(match){ 185 var spaces = match[1].length-1; 186 187 if(spaces > 3){ // unindent one level 188 field.value = field.value.substr(0,linestart)+ 189 field.value.substr(linestart+2); 190 selection.start = selection.start - 2; 191 selection.end = selection.start; 192 }else{ // delete list point 193 field.value = field.value.substr(0,linestart)+ 194 field.value.substr(selection.start); 195 selection.start = linestart; 196 selection.end = linestart; 197 } 198 setSelection(selection); 199 e.preventDefault(); // prevent backspace 200 return false; 201 } 202 }else if(e.keyCode == 32){ // Space 203 // intend list item 204 var match = search.match(/(\n +)([*-] )$/); 205 if(match){ 206 field.value = field.value.substr(0,linestart)+' '+ 207 field.value.substr(linestart); 208 selection.start = selection.start + 2; 209 selection.end = selection.start; 210 setSelection(selection); 211 e.preventDefault(); // prevent space 212 return false; 213 } 214 } 215} 216 217//FIXME consolidate somewhere else 218addInitEvent(function(){ 219 var field = $('wiki__text'); 220 if(!field) return; 221 addEvent(field,'keydown',keyHandler); 222}); 223 224/** 225 * Determine the current section level while editing 226 * 227 * @author Andreas Gohr <gohr@cosmocode.de> 228 */ 229function currentHeadlineLevel(textboxId){ 230 var field = $(textboxId); 231 var selection = getSelection(field); 232 var search = "\n"+field.value.substr(0,selection.start); 233 var lasthl = search.lastIndexOf("\n=="); 234 if(lasthl == -1 && field.form.prefix){ 235 // we need to look in prefix context 236 search = field.form.prefix.value; 237 lasthl = search.lastIndexOf("\n=="); 238 } 239 search = search.substr(lasthl+1,6); 240 241 if(search == '======') return 1; 242 if(search.substr(0,5) == '=====') return 2; 243 if(search.substr(0,4) == '====') return 3; 244 if(search.substr(0,3) == '===') return 4; 245 if(search.substr(0,2) == '==') return 5; 246 247 return 0; 248} 249 250 251/** 252 * global var used for not saved yet warning 253 */ 254var textChanged = false; 255 256/** 257 * Check for changes before leaving the page 258 */ 259function changeCheck(msg){ 260 if(textChanged){ 261 var ok = confirm(msg); 262 if(ok){ 263 // remove a possibly saved draft using ajax 264 var dwform = $('dw__editform'); 265 if(dwform){ 266 var params = 'call=draftdel'; 267 params += '&id='+encodeURIComponent(dwform.elements.id.value); 268 269 var sackobj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); 270 sackobj.AjaxFailedAlert = ''; 271 sackobj.encodeURIString = false; 272 sackobj.runAJAX(params); 273 // we send this request blind without waiting for 274 // and handling the returned data 275 } 276 } 277 return ok; 278 }else{ 279 return true; 280 } 281} 282 283/** 284 * Add changeCheck to all Links and Forms (except those with a 285 * JSnocheck class), add handlers to monitor changes 286 * 287 * Sets focus to the editbox as well 288 * 289 * @fixme this is old and crappy code. needs to be redone 290 */ 291function initChangeCheck(msg){ 292 var edit_text = document.getElementById('wiki__text'); 293 if(!edit_text) return; 294 if(edit_text.readOnly) return; 295 if(!$('dw__editform')) return; 296 297 // add change check for links 298 var links = document.getElementsByTagName('a'); 299 for(var i=0; i < links.length; i++){ 300 if(links[i].className.indexOf('JSnocheck') == -1){ 301 links[i].onclick = function(){ 302 var rc = changeCheck(msg); 303 if(window.event) window.event.returnValue = rc; 304 return rc; 305 }; 306 } 307 } 308 // add change check for forms 309 var forms = document.forms; 310 for(i=0; i < forms.length; i++){ 311 if(forms[i].className.indexOf('JSnocheck') == -1){ 312 forms[i].onsubmit = function(){ 313 var rc = changeCheck(msg); 314 if(window.event) window.event.returnValue = rc; 315 return rc; 316 }; 317 } 318 } 319 320 // reset change memory var on submit 321 var btn_save = document.getElementById('edbtn__save'); 322 btn_save.onclick = function(){ textChanged = false; }; 323 var btn_prev = document.getElementById('edbtn__preview'); 324 btn_prev.onclick = function(){ textChanged = false; }; 325 326 // add change memory setter 327 edit_text.onchange = function(){ 328 textChanged = true; //global var 329 summaryCheck(); 330 }; 331 var summary = document.getElementById('edit__summary'); 332 addEvent(summary, 'change', summaryCheck); 333 addEvent(summary, 'keyup', summaryCheck); 334 if (textChanged) summaryCheck(); 335 336 // set focus 337 edit_text.focus(); 338} 339 340/** 341 * Checks if a summary was entered - if not the style is changed 342 * 343 * @author Andreas Gohr <andi@splitbrain.org> 344 */ 345function summaryCheck(){ 346 var sum = document.getElementById('edit__summary'); 347 if(sum.value === ''){ 348 sum.className='missing'; 349 }else{ 350 sum.className='edit'; 351 } 352} 353 354 355/** 356 * Class managing the timer to display a warning on a expiring lock 357 */ 358function locktimer_class(){ 359 this.sack = null; 360 this.timeout = 0; 361 this.timerID = null; 362 this.lasttime = null; 363 this.msg = ''; 364 this.pageid = ''; 365}; 366var locktimer = new locktimer_class(); 367 locktimer.init = function(timeout,msg,draft){ 368 // init values 369 locktimer.timeout = timeout*1000; 370 locktimer.msg = msg; 371 locktimer.draft = draft; 372 locktimer.lasttime = new Date(); 373 374 if(!$('dw__editform')) return; 375 locktimer.pageid = $('dw__editform').elements.id.value; 376 if(!locktimer.pageid) return; 377 378 // init ajax component 379 locktimer.sack = new sack(DOKU_BASE + 'lib/exe/ajax.php'); 380 locktimer.sack.AjaxFailedAlert = ''; 381 locktimer.sack.encodeURIString = false; 382 locktimer.sack.onCompletion = locktimer.refreshed; 383 384 // register refresh event 385 addEvent($('dw__editform').elements.wikitext,'keypress',function(){locktimer.refresh();}); 386 387 // start timer 388 locktimer.reset(); 389 }; 390 391 /** 392 * (Re)start the warning timer 393 */ 394 locktimer.reset = function(){ 395 locktimer.clear(); 396 locktimer.timerID = window.setTimeout("locktimer.warning()", locktimer.timeout); 397 }; 398 399 /** 400 * Display the warning about the expiring lock 401 */ 402 locktimer.warning = function(){ 403 locktimer.clear(); 404 alert(locktimer.msg); 405 }; 406 407 /** 408 * Remove the current warning timer 409 */ 410 locktimer.clear = function(){ 411 if(locktimer.timerID !== null){ 412 window.clearTimeout(locktimer.timerID); 413 locktimer.timerID = null; 414 } 415 }; 416 417 /** 418 * Refresh the lock via AJAX 419 * 420 * Called on keypresses in the edit area 421 */ 422 locktimer.refresh = function(){ 423 var now = new Date(); 424 // refresh every minute only 425 if(now.getTime() - locktimer.lasttime.getTime() > 30*1000){ //FIXME decide on time 426 var params = 'call=lock&id='+encodeURIComponent(locktimer.pageid); 427 if(locktimer.draft){ 428 var dwform = $('dw__editform'); 429 params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value); 430 params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value); 431 params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value); 432 params += '&date='+encodeURIComponent(dwform.elements.date.value); 433 } 434 locktimer.sack.runAJAX(params); 435 locktimer.lasttime = now; 436 } 437 }; 438 439 440 /** 441 * Callback. Resets the warning timer 442 */ 443 locktimer.refreshed = function(){ 444 var data = this.response; 445 var error = data.charAt(0); 446 data = data.substring(1); 447 448 $('draft__status').innerHTML=data; 449 if(error != '1') return; // locking failed 450 locktimer.reset(); 451 }; 452// end of locktimer class functions 453 454