source: trunk/plugins/LocalFilesEditor/editarea/edit_area_functions.js @ 5160

Last change on this file since 5160 was 5160, checked in by patdenice, 14 years ago

Editor
Update editarea to 0.8.2.
Remove CSS tab.
Fix jQuery path.

File size: 38.6 KB
Line 
1        //replace tabulation by the good number of white spaces
2        EditArea.prototype.replace_tab= function(text){
3                return text.replace(/((\n?)([^\t\n]*)\t)/gi, editArea.smartTab);                // slower than simple replace...       
4        };
5       
6        // call by the replace_tab function
7        EditArea.prototype.smartTab= function(){
8                val="                   ";
9                return EditArea.prototype.smartTab.arguments[2] + EditArea.prototype.smartTab.arguments[3] + val.substr(0, editArea.tab_nb_char - (EditArea.prototype.smartTab.arguments[3].length)%editArea.tab_nb_char);
10        };
11       
12        EditArea.prototype.show_waiting_screen= function(){
13                width   = this.editor_area.offsetWidth;
14                height  = this.editor_area.offsetHeight;
15                if( !(this.isIE && this.isIE<6) )
16                {
17                        width   -= 2;
18                        height  -= 2;
19                }
20                this.processing_screen.style.display= "block";
21                this.processing_screen.style.width      = width+"px";
22                this.processing_screen.style.height     = height+"px";
23                this.waiting_screen_displayed           = true;
24        };
25       
26        EditArea.prototype.hide_waiting_screen= function(){
27                this.processing_screen.style.display="none";
28                this.waiting_screen_displayed= false;
29        };
30       
31        EditArea.prototype.add_style= function(styles){
32                if(styles.length>0){
33                        newcss = document.createElement("style");
34                        newcss.type="text/css";
35                        newcss.media="all";
36                        if(newcss.styleSheet){ // IE
37                                newcss.styleSheet.cssText = styles;
38                        } else { // W3C
39                                newcss.appendChild(document.createTextNode(styles));
40                        }
41                        document.getElementsByTagName("head")[0].appendChild(newcss);
42                }
43        };
44       
45        EditArea.prototype.set_font= function(family, size){
46                var t=this, a=this.textarea, s=this.settings, elem_font, i, elem;
47                // list all elements concerned by font changes
48                var elems= ["textarea", "content_highlight", "cursor_pos", "end_bracket", "selection_field", "selection_field_text", "line_number"];
49               
50                if(family && family!="")
51                        s["font_family"]= family;
52                if(size && size>0)
53                        s["font_size"]  = size;
54                if( t.isOpera && t.isOpera < 9.6 )      // opera<9.6 can't manage non monospace font
55                        s['font_family']="monospace";
56                       
57                // update the select tag
58                if( elem_font = _$("area_font_size") )
59                {       
60                        for( i = 0; i < elem_font.length; i++ )
61                        {
62                                if( elem_font.options[i].value && elem_font.options[i].value == s["font_size"] )
63                                        elem_font.options[i].selected=true;
64                        }
65                }
66               
67                /*
68                 * somethimes firefox has rendering mistake with non-monospace font for text width in textarea vs in div for changing font size (eg: verdana change between 11pt to 12pt)
69                 * => looks like a browser internal random bug as text width can change while content_highlight is updated
70                 * we'll check if the font-size produce the same text width inside textarea and div and if not, we'll increment the font-size
71                 *
72                 * This is an ugly fix
73                 */ 
74                if( t.isFirefox )
75                {
76                        var nbTry = 3;
77                        do {
78                                var div1 = document.createElement( 'div' ), text1 = document.createElement( 'textarea' );
79                                var styles = {
80                                        width:          '40px',
81                                        overflow:       'scroll',
82                                        zIndex:         50,
83                                        visibility:     'hidden',
84                                        fontFamily:     s["font_family"],
85                                        fontSize:       s["font_size"]+"pt",
86                                        lineHeight:     t.lineHeight+"px",
87                                        padding:        '0',
88                                        margin:         '0',
89                                        border:         'none',
90                                        whiteSpace:     'nowrap'
91                                };
92                                var diff, changed = false;
93                                for( i in styles )
94                                {
95                                        div1.style[ i ]         = styles[i];
96                                        text1.style[ i ]        = styles[i];
97                                }
98                                // no wrap for this text
99                                text1.wrap = 'off';
100                                text1.setAttribute('wrap', 'off');
101                                t.container.appendChild( div1 );
102                                t.container.appendChild( text1 );
103                                // try to make FF to bug
104                                div1.innerHTML          = text1.value   = 'azertyuiopqsdfghjklm';
105                                div1.innerHTML          = text1.value   = text1.value+'wxcvbn^p*ù$!:;,,';
106                                diff    =  text1.scrollWidth - div1.scrollWidth;
107                               
108                                // firefox return here a diff of 1 px between equals scrollWidth (can't explain)
109                                if( Math.abs( diff ) >= 2 )
110                                {
111                                        s["font_size"]++;
112                                        changed = true;
113                                }
114                                t.container.removeChild( div1 );
115                                t.container.removeChild( text1 );
116                                nbTry--;
117                        }while( changed && nbTry > 0 );
118                }
119               
120               
121                // calc line height
122                elem                                    = t.test_font_size;
123                elem.style.fontFamily   = ""+s["font_family"];
124                elem.style.fontSize             = s["font_size"]+"pt";                         
125                elem.innerHTML                  = "0";         
126                t.lineHeight                    = elem.offsetHeight;
127
128                // update font for all concerned elements
129                for( i=0; i<elems.length; i++)
130                {
131                        elem    = _$(elems[i]); 
132                        elem.style.fontFamily   = s["font_family"];
133                        elem.style.fontSize             = s["font_size"]+"pt";
134                        elem.style.lineHeight   = t.lineHeight+"px";
135                }
136                // define a css for <pre> tags
137                t.add_style("pre{font-family:"+s["font_family"]+"}");
138               
139                // old opera and IE>=8 doesn't update font changes to the textarea
140                if( ( t.isOpera && t.isOpera < 9.6 ) || t.isIE >= 8 )
141                {
142                        var parNod = a.parentNode, nxtSib = a.nextSibling, start= a.selectionStart, end= a.selectionEnd;
143                        parNod.removeChild(a);
144                        parNod.insertBefore(a, nxtSib);
145                        t.area_select(start, end-start);
146                }
147               
148                // force update of selection field
149                this.focus();
150                this.update_size();
151                this.check_line_selection();
152        };
153       
154        EditArea.prototype.change_font_size= function(){
155                var size=_$("area_font_size").value;
156                if(size>0)
157                        this.set_font("", size);                       
158        };
159       
160       
161        EditArea.prototype.open_inline_popup= function(popup_id){
162                this.close_all_inline_popup();
163                var popup= _$(popup_id);               
164                var editor= _$("editor");
165               
166                // search matching icon
167                for(var i=0; i<this.inlinePopup.length; i++){
168                        if(this.inlinePopup[i]["popup_id"]==popup_id){
169                                var icon= _$(this.inlinePopup[i]["icon_id"]);
170                                if(icon){
171                                        this.switchClassSticky(icon, 'editAreaButtonSelected', true);                   
172                                        break;
173                                }
174                        }
175                }
176                // check size
177                popup.style.height="auto";
178                popup.style.overflow= "visible";
179                       
180                if(document.body.offsetHeight< popup.offsetHeight){
181                        popup.style.height= (document.body.offsetHeight-10)+"px";
182                        popup.style.overflow= "auto";
183                }
184               
185                if(!popup.positionned){
186                        var new_left= editor.offsetWidth /2 - popup.offsetWidth /2;
187                        var new_top= editor.offsetHeight /2 - popup.offsetHeight /2;
188                        //var new_top= area.offsetHeight /2 - popup.offsetHeight /2;
189                        //var new_left= area.offsetWidth /2 - popup.offsetWidth /2;
190                        //alert("new_top: ("+new_top+") = calculeOffsetTop(area) ("+calculeOffsetTop(area)+") + area.offsetHeight /2("+ area.offsetHeight /2+") - popup.offsetHeight /2("+popup.offsetHeight /2+") - scrollTop: "+document.body.scrollTop);
191                        popup.style.left= new_left+"px";
192                        popup.style.top= new_top+"px";
193                        popup.positionned=true;
194                }
195                popup.style.visibility="visible";
196               
197                //popup.style.display="block";
198        };
199
200        EditArea.prototype.close_inline_popup= function(popup_id){
201                var popup= _$(popup_id);               
202                // search matching icon
203                for(var i=0; i<this.inlinePopup.length; i++){
204                        if(this.inlinePopup[i]["popup_id"]==popup_id){
205                                var icon= _$(this.inlinePopup[i]["icon_id"]);
206                                if(icon){
207                                        this.switchClassSticky(icon, 'editAreaButtonNormal', false);                   
208                                        break;
209                                }
210                        }
211                }
212               
213                popup.style.visibility="hidden";       
214        };
215       
216        EditArea.prototype.close_all_inline_popup= function(e){
217                for(var i=0; i<this.inlinePopup.length; i++){
218                        this.close_inline_popup(this.inlinePopup[i]["popup_id"]);               
219                }
220                this.textarea.focus();
221        };
222       
223        EditArea.prototype.show_help= function(){
224               
225                this.open_inline_popup("edit_area_help");
226               
227        };
228                       
229        EditArea.prototype.new_document= function(){
230                this.textarea.value="";
231                this.area_select(0,0);
232        };
233       
234        EditArea.prototype.get_all_toolbar_height= function(){
235                var area= _$("editor");
236                var results= parent.getChildren(area, "div", "class", "area_toolbar", "all", "0");      // search only direct children
237                //results= results.concat(getChildren(area, "table", "class", "area_toolbar", "all", "0"));
238                var height=0;
239                for(var i=0; i<results.length; i++){                   
240                        height+= results[i].offsetHeight;
241                }
242                //alert("toolbar height: "+height);
243                return height;
244        };
245       
246        EditArea.prototype.go_to_line= function(line){ 
247                if(!line)
248                {       
249                        var icon= _$("go_to_line");
250                        if(icon != null){
251                                this.restoreClass(icon);
252                                this.switchClassSticky(icon, 'editAreaButtonSelected', true);
253                        }
254                       
255                        line= prompt(this.get_translation("go_to_line_prompt"));
256                        if(icon != null)
257                                this.switchClassSticky(icon, 'editAreaButtonNormal', false);
258                }
259                if(line && line!=null && line.search(/^[0-9]+$/)!=-1){
260                        var start=0;
261                        var lines= this.textarea.value.split("\n");
262                        if(line > lines.length)
263                                start= this.textarea.value.length;
264                        else{
265                                for(var i=0; i<Math.min(line-1, lines.length); i++)
266                                        start+= lines[i].length + 1;
267                        }
268                        this.area_select(start, 0);
269                }
270               
271               
272        };
273       
274       
275        EditArea.prototype.change_smooth_selection_mode= function(setTo){
276                //alert("setTo: "+setTo);
277                if(this.do_highlight)
278                        return;
279                       
280                if(setTo != null){
281                        if(setTo === false)
282                                this.smooth_selection=true;
283                        else
284                                this.smooth_selection=false;
285                }
286                var icon= _$("change_smooth_selection");
287                this.textarea.focus();
288                if(this.smooth_selection===true){
289                        //setAttribute(icon, "class", getAttribute(icon, "class").replace(/ selected/g, "") );
290                        /*setAttribute(icon, "oldClassName", "editAreaButtonNormal" );
291                        setAttribute(icon, "className", "editAreaButtonNormal" );*/
292                        //this.restoreClass(icon);
293                        //this.restoreAndSwitchClass(icon,'editAreaButtonNormal');
294                        this.switchClassSticky(icon, 'editAreaButtonNormal', false);
295                       
296                        this.smooth_selection=false;
297                        this.selection_field.style.display= "none";
298                        _$("cursor_pos").style.display= "none";
299                        _$("end_bracket").style.display= "none";
300                }else{
301                        //setAttribute(icon, "class", getAttribute(icon, "class") + " selected");
302                        //this.switchClass(icon,'editAreaButtonSelected');
303                        this.switchClassSticky(icon, 'editAreaButtonSelected', false);
304                        this.smooth_selection=true;
305                        this.selection_field.style.display= "block";
306                        _$("cursor_pos").style.display= "block";
307                        _$("end_bracket").style.display= "block";
308                }       
309        };
310       
311        // the auto scroll of the textarea has some lacks when it have to show cursor in the visible area when the textarea size change
312        // show specifiy whereas it is the "top" or "bottom" of the selection that is showned
313        EditArea.prototype.scroll_to_view= function(show){
314                var zone, lineElem;
315                if(!this.smooth_selection)
316                        return;
317                zone= _$("result");
318               
319                // manage height scroll
320                var cursor_pos_top= _$("cursor_pos").cursor_top;
321                if(show=="bottom")
322                {
323                        //cursor_pos_top+=  (this.last_selection["line_nb"]-1)* this.lineHeight;
324                        cursor_pos_top+= this.getLinePosTop( this.last_selection['line_start'] + this.last_selection['line_nb'] - 1 );
325                }
326                       
327                var max_height_visible= zone.clientHeight + zone.scrollTop;
328                var miss_top    = cursor_pos_top + this.lineHeight - max_height_visible;
329                if(miss_top>0){
330                        //alert(miss_top);
331                        zone.scrollTop=  zone.scrollTop + miss_top;
332                }else if( zone.scrollTop > cursor_pos_top){
333                        // when erase all the content -> does'nt scroll back to the top
334                        //alert("else: "+cursor_pos_top);
335                        zone.scrollTop= cursor_pos_top; 
336                }
337               
338                // manage left scroll
339                //var cursor_pos_left= parseInt(_$("cursor_pos").style.left.replace("px",""));
340                var cursor_pos_left= _$("cursor_pos").cursor_left;
341                var max_width_visible= zone.clientWidth + zone.scrollLeft;
342                var miss_left= cursor_pos_left + 10 - max_width_visible;
343                if(miss_left>0){                       
344                        zone.scrollLeft= zone.scrollLeft + miss_left + 50;
345                }else if( zone.scrollLeft > cursor_pos_left){
346                        zone.scrollLeft= cursor_pos_left ;
347                }else if( zone.scrollLeft == 45){
348                        // show the line numbers if textarea align to it's left
349                        zone.scrollLeft=0;
350                }
351        };
352       
353        EditArea.prototype.check_undo= function(only_once){
354                if(!editAreas[this.id])
355                        return false;
356                if(this.textareaFocused && editAreas[this.id]["displayed"]==true){
357                        var text=this.textarea.value;
358                        if(this.previous.length<=1)
359                                this.switchClassSticky(_$("undo"), 'editAreaButtonDisabled', true);
360               
361                        if(!this.previous[this.previous.length-1] || this.previous[this.previous.length-1]["text"] != text){
362                                this.previous.push({"text": text, "selStart": this.textarea.selectionStart, "selEnd": this.textarea.selectionEnd});
363                                if(this.previous.length > this.settings["max_undo"]+1)
364                                        this.previous.shift();
365                               
366                        }
367                        if(this.previous.length >= 2)
368                                this.switchClassSticky(_$("undo"), 'editAreaButtonNormal', false);             
369                }
370
371                if(!only_once)
372                        setTimeout("editArea.check_undo()", 3000);
373        };
374       
375        EditArea.prototype.undo= function(){
376                //alert("undo"+this.previous.length);
377                if(this.previous.length > 0)
378                {
379                        this.getIESelection();
380                //      var pos_cursor=this.textarea.selectionStart;
381                        this.next.push( { "text": this.textarea.value, "selStart": this.textarea.selectionStart, "selEnd": this.textarea.selectionEnd } );
382                        var prev= this.previous.pop();
383                        if( prev["text"] == this.textarea.value && this.previous.length > 0 )
384                                prev    =this.previous.pop();                                           
385                        this.textarea.value     = prev["text"];
386                        this.last_undo          = prev["text"];
387                        this.area_select(prev["selStart"], prev["selEnd"]-prev["selStart"]);
388                        this.switchClassSticky(_$("redo"), 'editAreaButtonNormal', false);
389                        this.resync_highlight(true);
390                        //alert("undo"+this.previous.length);
391                        this.check_file_changes();
392                }
393        };
394       
395        EditArea.prototype.redo= function(){
396                if(this.next.length > 0)
397                {
398                        /*this.getIESelection();*/
399                        //var pos_cursor=this.textarea.selectionStart;
400                        var next= this.next.pop();
401                        this.previous.push(next);
402                        this.textarea.value= next["text"];
403                        this.last_undo= next["text"];
404                        this.area_select(next["selStart"], next["selEnd"]-next["selStart"]);
405                        this.switchClassSticky(_$("undo"), 'editAreaButtonNormal', false);
406                        this.resync_highlight(true);
407                        this.check_file_changes();
408                }
409                if(     this.next.length == 0)
410                        this.switchClassSticky(_$("redo"), 'editAreaButtonDisabled', true);
411        };
412       
413        EditArea.prototype.check_redo= function(){
414                if(editArea.next.length == 0 || editArea.textarea.value!=editArea.last_undo){
415                        editArea.next= [];      // undo the ability to use "redo" button
416                        editArea.switchClassSticky(_$("redo"), 'editAreaButtonDisabled', true);
417                }
418                else
419                {
420                        this.switchClassSticky(_$("redo"), 'editAreaButtonNormal', false);
421                }
422        };
423       
424       
425        // functions that manage icons roll over, disabled, etc...
426        EditArea.prototype.switchClass = function(element, class_name, lock_state) {
427                var lockChanged = false;
428       
429                if (typeof(lock_state) != "undefined" && element != null) {
430                        element.classLock = lock_state;
431                        lockChanged = true;
432                }
433       
434                if (element != null && (lockChanged || !element.classLock)) {
435                        element.oldClassName = element.className;
436                        element.className = class_name;
437                }
438        };
439       
440        EditArea.prototype.restoreAndSwitchClass = function(element, class_name) {
441                if (element != null && !element.classLock) {
442                        this.restoreClass(element);
443                        this.switchClass(element, class_name);
444                }
445        };
446       
447        EditArea.prototype.restoreClass = function(element) {
448                if (element != null && element.oldClassName && !element.classLock) {
449                        element.className = element.oldClassName;
450                        element.oldClassName = null;
451                }
452        };
453       
454        EditArea.prototype.setClassLock = function(element, lock_state) {
455                if (element != null)
456                        element.classLock = lock_state;
457        };
458       
459        EditArea.prototype.switchClassSticky = function(element, class_name, lock_state) {
460                var lockChanged = false;
461                if (typeof(lock_state) != "undefined" && element != null) {
462                        element.classLock = lock_state;
463                        lockChanged = true;
464                }
465       
466                if (element != null && (lockChanged || !element.classLock)) {
467                        element.className = class_name;
468                        element.oldClassName = class_name;
469                }
470        };
471       
472        //make the "page up" and "page down" buttons works correctly
473        EditArea.prototype.scroll_page= function(params){
474                var dir= params["dir"], shift_pressed= params["shift"];
475                var lines= this.textarea.value.split("\n");             
476                var new_pos=0, length=0, char_left=0, line_nb=0, curLine=0;
477                var toScrollAmount      = _$("result").clientHeight -30;
478                var nbLineToScroll      = 0, diff= 0;
479               
480                if(dir=="up"){
481                        nbLineToScroll  = Math.ceil( toScrollAmount / this.lineHeight );
482                       
483                        // fix number of line to scroll
484                        for( i = this.last_selection["line_start"]; i - diff > this.last_selection["line_start"] - nbLineToScroll ; i-- )
485                        {
486                                if( elem = _$('line_'+ i) )
487                                {
488                                        diff +=  Math.floor( ( elem.offsetHeight - 1 ) / this.lineHeight );
489                                }
490                        }
491                        nbLineToScroll  -= diff;
492                       
493                        if(this.last_selection["selec_direction"]=="up"){
494                                for(line_nb=0; line_nb< Math.min(this.last_selection["line_start"]-nbLineToScroll, lines.length); line_nb++){
495                                        new_pos+= lines[line_nb].length + 1;
496                                }
497                                char_left=Math.min(lines[Math.min(lines.length-1, line_nb)].length, this.last_selection["curr_pos"]-1);
498                                if(shift_pressed)
499                                        length=this.last_selection["selectionEnd"]-new_pos-char_left;   
500                                this.area_select(new_pos+char_left, length);
501                                view="top";
502                        }else{                 
503                                view="bottom";
504                                for(line_nb=0; line_nb< Math.min(this.last_selection["line_start"]+this.last_selection["line_nb"]-1-nbLineToScroll, lines.length); line_nb++){
505                                        new_pos+= lines[line_nb].length + 1;
506                                }
507                                char_left=Math.min(lines[Math.min(lines.length-1, line_nb)].length, this.last_selection["curr_pos"]-1);
508                                if(shift_pressed){
509                                        //length=this.last_selection["selectionEnd"]-new_pos-char_left;
510                                        start= Math.min(this.last_selection["selectionStart"], new_pos+char_left);
511                                        length= Math.max(new_pos+char_left, this.last_selection["selectionStart"] )- start ;
512                                        if(new_pos+char_left < this.last_selection["selectionStart"])
513                                                view="top";
514                                }else
515                                        start=new_pos+char_left;
516                                this.area_select(start, length);
517                               
518                        }
519                }
520                else
521                {
522                        var nbLineToScroll= Math.floor( toScrollAmount / this.lineHeight );
523                        // fix number of line to scroll
524                        for( i = this.last_selection["line_start"]; i + diff < this.last_selection["line_start"] + nbLineToScroll ; i++ )
525                        {
526                                if( elem = _$('line_'+ i) )
527                                {
528                                        diff +=  Math.floor( ( elem.offsetHeight - 1 ) / this.lineHeight );
529                                }
530                        }
531                        nbLineToScroll  -= diff;
532                               
533                        if(this.last_selection["selec_direction"]=="down"){
534                                view="bottom";
535                                for(line_nb=0; line_nb< Math.min(this.last_selection["line_start"]+this.last_selection["line_nb"]-2+nbLineToScroll, lines.length); line_nb++){
536                                        if(line_nb==this.last_selection["line_start"]-1)
537                                                char_left= this.last_selection["selectionStart"] -new_pos;
538                                        new_pos+= lines[line_nb].length + 1;
539                                                                       
540                                }
541                                if(shift_pressed){
542                                        length=Math.abs(this.last_selection["selectionStart"]-new_pos); 
543                                        length+=Math.min(lines[Math.min(lines.length-1, line_nb)].length, this.last_selection["curr_pos"]);
544                                        //length+=Math.min(lines[Math.min(lines.length-1, line_nb)].length, char_left);
545                                        this.area_select(Math.min(this.last_selection["selectionStart"], new_pos), length);
546                                }else{
547                                        this.area_select(new_pos+char_left, 0);
548                                }
549                               
550                        }else{
551                                view="top";
552                                for(line_nb=0; line_nb< Math.min(this.last_selection["line_start"]+nbLineToScroll-1, lines.length, lines.length); line_nb++){
553                                        if(line_nb==this.last_selection["line_start"]-1)
554                                                char_left= this.last_selection["selectionStart"] -new_pos;
555                                        new_pos+= lines[line_nb].length + 1;                                                                   
556                                }
557                                if(shift_pressed){
558                                        length=Math.abs(this.last_selection["selectionEnd"]-new_pos-char_left); 
559                                        length+=Math.min(lines[Math.min(lines.length-1, line_nb)].length, this.last_selection["curr_pos"])- char_left-1;
560                                        //length+=Math.min(lines[Math.min(lines.length-1, line_nb)].length, char_left);
561                                        this.area_select(Math.min(this.last_selection["selectionEnd"], new_pos+char_left), length);
562                                        if(new_pos+char_left > this.last_selection["selectionEnd"])
563                                                view="bottom";
564                                }else{
565                                        this.area_select(new_pos+char_left, 0);
566                                }
567                               
568                        }
569                }
570                //console.log( new_pos, char_left, length, nbLineToScroll, toScrollAmount, _$("result").clientHeigh );
571                this.check_line_selection();
572                this.scroll_to_view(view);
573        };
574       
575        EditArea.prototype.start_resize= function(e){
576                parent.editAreaLoader.resize["id"]              = editArea.id;         
577                parent.editAreaLoader.resize["start_x"] = (e)? e.pageX : event.x + document.body.scrollLeft;           
578                parent.editAreaLoader.resize["start_y"] = (e)? e.pageY : event.y + document.body.scrollTop;
579                if(editArea.isIE)
580                {
581                        editArea.textarea.focus();
582                        editArea.getIESelection();
583                }
584                parent.editAreaLoader.resize["selectionStart"]  = editArea.textarea.selectionStart;
585                parent.editAreaLoader.resize["selectionEnd"]    = editArea.textarea.selectionEnd;
586                parent.editAreaLoader.start_resize_area();
587        };
588       
589        EditArea.prototype.toggle_full_screen= function(to){
590                var t=this, p=parent, a=t.textarea, html, frame, selStart, selEnd, old, icon;
591                if(typeof(to)=="undefined")
592                        to= !t.fullscreen['isFull'];
593                old                     = t.fullscreen['isFull'];
594                t.fullscreen['isFull']= to;
595                icon            = _$("fullscreen");
596                selStart        = t.textarea.selectionStart;
597                selEnd          = t.textarea.selectionEnd;
598                html            = p.document.getElementsByTagName("html")[0];
599                frame           = p.document.getElementById("frame_"+t.id);
600               
601                if(to && to!=old)
602                {       // toogle on fullscreen         
603                       
604                        t.fullscreen['old_overflow']    = p.get_css_property(html, "overflow");
605                        t.fullscreen['old_height']              = p.get_css_property(html, "height");
606                        t.fullscreen['old_width']               = p.get_css_property(html, "width");
607                        t.fullscreen['old_scrollTop']   = html.scrollTop;
608                        t.fullscreen['old_scrollLeft']  = html.scrollLeft;
609                        t.fullscreen['old_zIndex']              = p.get_css_property(frame, "z-index");
610                        if(t.isOpera){
611                                html.style.height       = "100%";
612                                html.style.width        = "100%";       
613                        }
614                        html.style.overflow     = "hidden";
615                        html.scrollTop          = 0;
616                        html.scrollLeft         = 0;
617                       
618                        frame.style.position    = "absolute";
619                        frame.style.width               = html.clientWidth+"px";
620                        frame.style.height              = html.clientHeight+"px";
621                        frame.style.display             = "block";
622                        frame.style.zIndex              = "999999";
623                        frame.style.top                 = "0px";
624                        frame.style.left                = "0px";
625                       
626                        // if the iframe was in a div with position absolute, the top and left are the one of the div,
627                        // so I fix it by seeing at witch position the iframe start and correcting it
628                        frame.style.top                 = "-"+p.calculeOffsetTop(frame)+"px";
629                        frame.style.left                = "-"+p.calculeOffsetLeft(frame)+"px";
630                       
631                //      parent.editAreaLoader.execCommand(t.id, "update_size();");
632                //      var body=parent.document.getElementsByTagName("body")[0];
633                //      body.appendChild(frame);
634                       
635                        t.switchClassSticky(icon, 'editAreaButtonSelected', false);
636                        t.fullscreen['allow_resize']= t.resize_allowed;
637                        t.allow_resize(false);
638       
639                        //t.area_select(selStart, selEnd-selStart);
640                       
641               
642                        // opera can't manage to do a direct size update
643                        if(t.isFirefox){
644                                p.editAreaLoader.execCommand(t.id, "update_size();");
645                                t.area_select(selStart, selEnd-selStart);
646                                t.scroll_to_view();
647                                t.focus();
648                        }else{
649                                setTimeout("parent.editAreaLoader.execCommand('"+ t.id +"', 'update_size();');editArea.focus();", 10);
650                        }       
651                       
652       
653                }
654                else if(to!=old)
655                {       // toogle off fullscreen
656                        frame.style.position="static";
657                        frame.style.zIndex= t.fullscreen['old_zIndex'];
658               
659                        if(t.isOpera)
660                        {
661                                html.style.height       = "auto"; 
662                                html.style.width        = "auto";
663                                html.style.overflow     = "auto";
664                        }
665                        else if(t.isIE && p!=top)
666                        {       // IE doesn't manage html overflow in frames like in normal page...
667                                html.style.overflow     = "auto";
668                        }
669                        else
670                        {
671                                html.style.overflow     = t.fullscreen['old_overflow'];
672                        }
673                        html.scrollTop  = t.fullscreen['old_scrollTop'];
674                        html.scrollLeft = t.fullscreen['old_scrollLeft'];
675               
676                        p.editAreaLoader.hide(t.id);
677                        p.editAreaLoader.show(t.id);
678                       
679                        t.switchClassSticky(icon, 'editAreaButtonNormal', false);
680                        if(t.fullscreen['allow_resize'])
681                                t.allow_resize(t.fullscreen['allow_resize']);
682                        if(t.isFirefox){
683                                t.area_select(selStart, selEnd-selStart);
684                                setTimeout("editArea.scroll_to_view();", 10);
685                        }                       
686                       
687                        //p.editAreaLoader.remove_event(p.window, "resize", editArea.update_size);
688                }
689               
690        };
691       
692        EditArea.prototype.allow_resize= function(allow){
693                var resize= _$("resize_area");
694                if(allow){
695                       
696                        resize.style.visibility="visible";
697                        parent.editAreaLoader.add_event(resize, "mouseup", editArea.start_resize);
698                }else{
699                        resize.style.visibility="hidden";
700                        parent.editAreaLoader.remove_event(resize, "mouseup", editArea.start_resize);
701                }
702                this.resize_allowed= allow;
703        };
704       
705       
706        EditArea.prototype.change_syntax= function(new_syntax, is_waiting){
707        //      alert("cahnge to "+new_syntax);
708                // the syntax is the same
709                if(new_syntax==this.settings['syntax'])
710                        return true;
711               
712                // check that the syntax is one allowed
713                var founded= false;
714                for(var i=0; i<this.syntax_list.length; i++)
715                {
716                        if(this.syntax_list[i]==new_syntax)
717                                founded= true;
718                }
719               
720                if(founded==true)
721                {
722                        // the reg syntax file is not loaded
723                        if(!parent.editAreaLoader.load_syntax[new_syntax])
724                        {
725                                // load the syntax file and wait for file loading
726                                if(!is_waiting)
727                                        parent.editAreaLoader.load_script(parent.editAreaLoader.baseURL + "reg_syntax/" + new_syntax + ".js");
728                                setTimeout("editArea.change_syntax('"+ new_syntax +"', true);", 100);
729                                this.show_waiting_screen();
730                        }
731                        else
732                        {
733                                if(!this.allready_used_syntax[new_syntax])
734                                {       // the syntax has still not been used
735                                        // rebuild syntax definition for new languages
736                                        parent.editAreaLoader.init_syntax_regexp();
737                                        // add style to the new list
738                                        this.add_style(parent.editAreaLoader.syntax[new_syntax]["styles"]);
739                                        this.allready_used_syntax[new_syntax]=true;
740                                }
741                                // be sure that the select option is correctly updated
742                                var sel= _$("syntax_selection");
743                                if(sel && sel.value!=new_syntax)
744                                {
745                                        for(var i=0; i<sel.length; i++){
746                                                if(sel.options[i].value && sel.options[i].value == new_syntax)
747                                                        sel.options[i].selected=true;
748                                        }
749                                }
750                               
751                        /*      if(this.settings['syntax'].length==0)
752                                {
753                                        this.switchClassSticky(_$("highlight"), 'editAreaButtonNormal', false);
754                                        this.switchClassSticky(_$("reset_highlight"), 'editAreaButtonNormal', false);
755                                        this.change_highlight(true);
756                                }
757                                */
758                                this.settings['syntax']= new_syntax;
759                                this.resync_highlight(true);
760                                this.hide_waiting_screen();
761                                return true;
762                        }
763                }
764                return false;
765        };
766       
767       
768        // check if the file has changed
769        EditArea.prototype.set_editable= function(is_editable){
770                if(is_editable)
771                {
772                        document.body.className= "";
773                        this.textarea.readOnly= false;
774                        this.is_editable= true;
775                }
776                else
777                {
778                        document.body.className= "non_editable";
779                        this.textarea.readOnly= true;
780                        this.is_editable= false;
781                }
782               
783                if(editAreas[this.id]["displayed"]==true)
784                        this.update_size();
785        };
786       
787        /***** Wrap mode *****/
788       
789        // toggling function for set_wrap_mode
790        EditArea.prototype.toggle_word_wrap= function(){
791                this.set_word_wrap( !this.settings['word_wrap'] );
792        };
793       
794       
795        // open a new tab for the given file
796        EditArea.prototype.set_word_wrap= function(to){
797                var t=this, a= t.textarea;
798                if( t.isOpera && t.isOpera < 9.8 )
799                {
800                        this.settings['word_wrap']= false;
801                        t.switchClassSticky( _$("word_wrap"), 'editAreaButtonDisabled', true );
802                        return false;
803                }
804               
805                if( to )
806                {
807                        wrap_mode = 'soft';
808                        this.container.className+= ' word_wrap';
809                        this.container.style.width="";
810                        this.content_highlight.style.width="";
811                        a.style.width="100%";
812                        if( t.isIE && t.isIE < 7 )      // IE 6 count 50 px too much
813                        {
814                                a.style.width   = ( a.offsetWidth-5 )+"px";
815                        }
816                       
817                        t.switchClassSticky( _$("word_wrap"), 'editAreaButtonSelected', false );
818                }
819                else
820                {
821                        wrap_mode = 'off';
822                        this.container.className        = this.container.className.replace(/word_wrap/g, '');
823                        t.switchClassSticky( _$("word_wrap"), 'editAreaButtonNormal', true );
824                }
825                this.textarea.previous_scrollWidth = '';
826                this.textarea.previous_scrollHeight = '';
827               
828                a.wrap= wrap_mode;
829                a.setAttribute('wrap', wrap_mode);
830                // only IE can change wrap mode on the fly without element reloading
831                if(!this.isIE)
832                {
833                        var start=a.selectionStart, end= a.selectionEnd;
834                        var parNod = a.parentNode, nxtSib = a.nextSibling;
835                        parNod.removeChild(a);
836                        parNod.insertBefore(a, nxtSib);
837                        this.area_select(start, end-start);
838                }
839                // reset some optimisation
840                this.settings['word_wrap']      = to;
841                this.focus();
842                this.update_size();
843                this.check_line_selection();
844        };     
845        /***** tabbed files managing functions *****/
846       
847        // open a new tab for the given file
848        EditArea.prototype.open_file= function(settings){
849               
850                if(settings['id']!="undefined")
851                {
852                        var id= settings['id'];
853                        // create a new file object with defautl values
854                        var new_file= {};
855                        new_file['id']                  = id;
856                        new_file['title']               = id;
857                        new_file['text']                = "";
858                        new_file['last_selection']      = "";           
859                        new_file['last_text_to_highlight']      = "";
860                        new_file['last_hightlighted_text']      = "";
861                        new_file['previous']    = [];
862                        new_file['next']                = [];
863                        new_file['last_undo']   = "";
864                        new_file['smooth_selection']    = this.settings['smooth_selection'];
865                        new_file['do_highlight']= this.settings['start_highlight'];
866                        new_file['syntax']              = this.settings['syntax'];
867                        new_file['scroll_top']  = 0;
868                        new_file['scroll_left'] = 0;
869                        new_file['selection_start']= 0;
870                        new_file['selection_end']= 0;
871                        new_file['edited']              = false;
872                        new_file['font_size']   = this.settings["font_size"];
873                        new_file['font_family'] = this.settings["font_family"];
874                        new_file['word_wrap']   = this.settings["word_wrap"];
875                        new_file['toolbar']             = {'links':{}, 'selects': {}};
876                        new_file['compare_edited_text']= new_file['text'];
877                       
878                       
879                        this.files[id]= new_file;
880                        this.update_file(id, settings);
881                        this.files[id]['compare_edited_text']= this.files[id]['text'];
882                       
883                       
884                        var html_id= 'tab_file_'+encodeURIComponent(id);
885                        this.filesIdAssoc[html_id]= id;
886                        this.files[id]['html_id']= html_id;
887               
888                        if(!_$(this.files[id]['html_id']) && id!="")
889                        {
890                                // be sure the tab browsing area is displayed
891                                this.tab_browsing_area.style.display= "block";
892                                var elem= document.createElement('li');
893                                elem.id= this.files[id]['html_id'];
894                                var close= "<img src=\""+ parent.editAreaLoader.baseURL +"images/close.gif\" title=\""+ this.get_translation('close_tab', 'word') +"\" onclick=\"editArea.execCommand('close_file', editArea.filesIdAssoc['"+ html_id +"']);return false;\" class=\"hidden\" onmouseover=\"this.className=''\" onmouseout=\"this.className='hidden'\" />";
895                                elem.innerHTML= "<a onclick=\"javascript:editArea.execCommand('switch_to_file', editArea.filesIdAssoc['"+ html_id +"']);\" selec=\"none\"><b><span><strong class=\"edited\">*</strong>"+ this.files[id]['title'] + close +"</span></b></a>";
896                                _$('tab_browsing_list').appendChild(elem);
897                                var elem= document.createElement('text');
898                                this.update_size();
899                        }
900                       
901                        // open file callback (for plugin)
902                        if(id!="")
903                                this.execCommand('file_open', this.files[id]);
904                       
905                        this.switch_to_file(id, true);
906                        return true;
907                }
908                else
909                        return false;
910        };
911       
912        // close the given file
913        EditArea.prototype.close_file= function(id){
914                if(this.files[id])
915                {
916                        this.save_file(id);
917                       
918                        // close file callback
919                        if(this.execCommand('file_close', this.files[id])!==false)
920                        {
921                                // remove the tab in the toolbar
922                                var li= _$(this.files[id]['html_id']);
923                                li.parentNode.removeChild(li);
924                                // select a new file
925                                if(id== this.curr_file)
926                                {
927                                        var next_file= "";
928                                        var is_next= false;
929                                        for(var i in this.files)
930                                        {
931                                                if( is_next )
932                                                {
933                                                        next_file       = i;
934                                                        break;
935                                                }
936                                                else if( i == id )
937                                                        is_next         = true;
938                                                else
939                                                        next_file       = i;
940                                        }
941                                        // display the next file
942                                        this.switch_to_file(next_file);
943                                }
944                                // clear datas
945                                delete (this.files[id]);
946                                this.update_size();
947                        }       
948                }
949        };
950       
951        // backup current file datas
952        EditArea.prototype.save_file= function(id){
953                var t= this, save, a_links, a_selects, save_butt, img, i;
954                if(t.files[id])
955                {
956                        var save= t.files[id];
957                        save['last_selection']                  = t.last_selection;             
958                        save['last_text_to_highlight']  = t.last_text_to_highlight;
959                        save['last_hightlighted_text']  = t.last_hightlighted_text;
960                        save['previous']                                = t.previous;
961                        save['next']                                    = t.next;
962                        save['last_undo']                               = t.last_undo;
963                        save['smooth_selection']                = t.smooth_selection;
964                        save['do_highlight']                    = t.do_highlight;
965                        save['syntax']                                  = t.settings['syntax'];
966                        save['text']                                    = t.textarea.value;
967                        save['scroll_top']                              = t.result.scrollTop;
968                        save['scroll_left']                             = t.result.scrollLeft;
969                        save['selection_start']                 = t.last_selection["selectionStart"];
970                        save['selection_end']                   = t.last_selection["selectionEnd"];
971                        save['font_size']                               = t.settings["font_size"];
972                        save['font_family']                             = t.settings["font_family"];
973                        save['word_wrap']                               = t.settings["word_wrap"];
974                        save['toolbar']                                 = {'links':{}, 'selects': {}};
975                       
976                        // save toolbar buttons state for fileSpecific buttons
977                        a_links= _$("toolbar_1").getElementsByTagName("a");
978                        for( i=0; i<a_links.length; i++ )
979                        {
980                                if( a_links[i].getAttribute('fileSpecific') == 'yes' )
981                                {
982                                        save_butt       = {};
983                                        img                     = a_links[i].getElementsByTagName('img')[0];
984                                        save_butt['classLock']          = img.classLock;
985                                        save_butt['className']          = img.className;
986                                        save_butt['oldClassName']       = img.oldClassName;
987                                       
988                                        save['toolbar']['links'][a_links[i].id]= save_butt;
989                                }
990                        }
991                       
992                        // save toolbar select state for fileSpecific buttons
993                        a_selects= _$("toolbar_1").getElementsByTagName("select");
994                        for( i=0; i<a_selects.length; i++)
995                        {
996                                if(a_selects[i].getAttribute('fileSpecific')=='yes')
997                                {
998                                        save['toolbar']['selects'][a_selects[i].id]= a_selects[i].value;
999                                }
1000                        }
1001                               
1002                        t.files[id]= save;
1003                       
1004                        return save;
1005                }
1006               
1007                return false;
1008        };
1009       
1010        // update file_datas
1011        EditArea.prototype.update_file= function(id, new_values){
1012                for(var i in new_values)
1013                {
1014                        this.files[id][i]= new_values[i];
1015                }
1016        };
1017       
1018        // display file datas
1019        EditArea.prototype.display_file= function(id){
1020                var t = this, a= t.textarea, new_file, a_lis, a_selects, a_links, a_options, i, j;
1021               
1022                // we're showing the empty file
1023                if(id=='')
1024                {
1025                        a.readOnly= true;
1026                        t.tab_browsing_area.style.display= "none";
1027                        _$("no_file_selected").style.display= "block";
1028                        t.result.className= "empty";
1029                        // clear current datas
1030                        if(!t.files[''])
1031                        {
1032                                t.open_file({id: ''});
1033                        }
1034                }
1035                // we try to show a non existent file, so we left
1036                else if( typeof( t.files[id] ) == 'undefined' )
1037                {
1038                        return false;
1039                }
1040                // display a normal file
1041                else
1042                {
1043                        t.result.className= "";
1044                        a.readOnly= !t.is_editable;
1045                        _$("no_file_selected").style.display= "none";
1046                        t.tab_browsing_area.style.display= "block";
1047                }
1048               
1049                // ensure to have last state for undo/redo actions
1050                t.check_redo(true);
1051                t.check_undo(true);
1052                t.curr_file= id;
1053               
1054                // replace selected tab file
1055                a_lis= t.tab_browsing_area.getElementsByTagName('li');
1056                for( i=0; i<a_lis.length; i++)
1057                {
1058                        if(a_lis[i].id == t.files[id]['html_id'])
1059                                a_lis[i].className='selected';
1060                        else
1061                                a_lis[i].className='';
1062                }
1063               
1064                // replace next files datas
1065                new_file= t.files[id];
1066       
1067                // restore text content
1068                a.value= new_file['text'];
1069               
1070                // restore font-size
1071                t.set_font(new_file['font_family'], new_file['font_size']);
1072               
1073                // restore selection and scroll
1074                t.area_select(new_file['selection_start'], new_file['selection_end'] - new_file['selection_start']);
1075                t.manage_size(true);
1076                t.result.scrollTop= new_file['scroll_top'];
1077                t.result.scrollLeft= new_file['scroll_left'];
1078               
1079                // restore undo, redo
1080                t.previous=     new_file['previous'];
1081                t.next= new_file['next'];
1082                t.last_undo=    new_file['last_undo'];
1083                t.check_redo(true);
1084                t.check_undo(true);
1085               
1086                // restore highlight
1087                t.execCommand("change_highlight", new_file['do_highlight']);
1088                t.execCommand("change_syntax", new_file['syntax']);
1089               
1090                // smooth mode
1091                t.execCommand("change_smooth_selection_mode", new_file['smooth_selection']);
1092               
1093                // word_wrap
1094                t.execCommand("set_word_wrap", new_file['word_wrap']);
1095                       
1096                // restore links state in toolbar
1097                a_links= new_file['toolbar']['links'];
1098                for( i in a_links)
1099                {
1100                        if( img =  _$(i).getElementsByTagName('img')[0] )
1101                        {
1102                                img.classLock   = a_links[i]['classLock'];
1103                                img.className   = a_links[i]['className'];
1104                                img.oldClassName= a_links[i]['oldClassName'];
1105                        }
1106                }
1107               
1108                // restore select state in toolbar
1109                a_selects = new_file['toolbar']['selects'];
1110                for( i in a_selects)
1111                {
1112                        a_options       = _$(i).options;
1113                        for( j=0; j<a_options.length; j++)
1114                        {
1115                                if( a_options[j].value == a_selects[i] )
1116                                        _$(i).options[j].selected=true;
1117                        }
1118                }
1119       
1120        };
1121
1122        // change tab for displaying a new one
1123        EditArea.prototype.switch_to_file= function(file_to_show, force_refresh){
1124                if(file_to_show!=this.curr_file || force_refresh)
1125                {
1126                        this.save_file(this.curr_file);
1127                        if(this.curr_file!='')
1128                                this.execCommand('file_switch_off', this.files[this.curr_file]);
1129                        this.display_file(file_to_show);
1130                        if(file_to_show!='')
1131                                this.execCommand('file_switch_on', this.files[file_to_show]);
1132                }
1133        };
1134
1135        // get all infos for the given file
1136        EditArea.prototype.get_file= function(id){
1137                if(id==this.curr_file)
1138                        this.save_file(id);
1139                return this.files[id];
1140        };
1141       
1142        // get all available files infos
1143        EditArea.prototype.get_all_files= function(){
1144                tmp_files= this.files;
1145                this.save_file(this.curr_file);
1146                if(tmp_files[''])
1147                        delete(this.files['']);
1148                return tmp_files;
1149        };
1150       
1151       
1152        // check if the file has changed
1153        EditArea.prototype.check_file_changes= function(){
1154       
1155                var id= this.curr_file;
1156                if(this.files[id] && this.files[id]['compare_edited_text']!=undefined)
1157                {
1158                        if(this.files[id]['compare_edited_text'].length==this.textarea.value.length && this.files[id]['compare_edited_text']==this.textarea.value)
1159                        {
1160                                if(this.files[id]['edited']!= false)
1161                                        this.set_file_edited_mode(id, false);
1162                        }
1163                        else
1164                        {
1165                                if(this.files[id]['edited']!= true)
1166                                        this.set_file_edited_mode(id, true);
1167                        }
1168                }
1169        };
1170       
1171        // set if the file is edited or not
1172        EditArea.prototype.set_file_edited_mode= function(id, to){
1173                // change CSS for edited tab
1174                if(this.files[id] && _$(this.files[id]['html_id']))
1175                {
1176                        var link= _$(this.files[id]['html_id']).getElementsByTagName('a')[0];
1177                        if(to==true)
1178                        {
1179                                link.className= 'edited';
1180                        }
1181                        else
1182                        {
1183                                link.className= '';
1184                                if(id==this.curr_file)
1185                                        text= this.textarea.value;
1186                                else
1187                                        text= this.files[id]['text'];
1188                                this.files[id]['compare_edited_text']= text;
1189                        }
1190                               
1191                        this.files[id]['edited']= to;
1192                }
1193        };
1194
1195        EditArea.prototype.set_show_line_colors = function(new_value){
1196                this.show_line_colors = new_value;
1197               
1198                if( new_value )
1199                        this.selection_field.className  += ' show_colors';
1200                else
1201                        this.selection_field.className  = this.selection_field.className.replace( / show_colors/g, '' );
1202        };
Note: See TracBrowser for help on using the repository browser.