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