source: trunk/plugins/LocalFilesEditor/editarea/elements_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: 9.8 KB
Line 
1/****
2 * This page contains some general usefull functions for javascript
3 *
4 ****/ 
5       
6       
7        // need to redefine this functiondue to IE problem
8        function getAttribute( elm, aName ) {
9                var aValue,taName,i;
10                try{
11                        aValue = elm.getAttribute( aName );
12                }catch(exept){}
13               
14                if( ! aValue ){
15                        for( i = 0; i < elm.attributes.length; i ++ ) {
16                                taName = elm.attributes[i] .name.toLowerCase();
17                                if( taName == aName ) {
18                                        aValue = elm.attributes[i] .value;
19                                        return aValue;
20                                }
21                        }
22                }
23                return aValue;
24        };
25       
26        // need to redefine this function due to IE problem
27        function setAttribute( elm, attr, val ) {
28                if(attr=="class"){
29                        elm.setAttribute("className", val);
30                        elm.setAttribute("class", val);
31                }else{
32                        elm.setAttribute(attr, val);
33                }
34        };
35       
36        /* return a child element
37                elem: element we are searching in
38                elem_type: type of the eleemnt we are searching (DIV, A, etc...)
39                elem_attribute: attribute of the searched element that must match
40                elem_attribute_match: value that elem_attribute must match
41                option: "all" if must return an array of all children, otherwise return the first match element
42                depth: depth of search (-1 or no set => unlimited)
43        */
44        function getChildren(elem, elem_type, elem_attribute, elem_attribute_match, option, depth)
45        {           
46                if(!option)
47                        var option="single";
48                if(!depth)
49                        var depth=-1;
50                if(elem){
51                        var children= elem.childNodes;
52                        var result=null;
53                        var results= [];
54                        for (var x=0;x<children.length;x++) {
55                                strTagName = new String(children[x].tagName);
56                                children_class="?";
57                                if(strTagName!= "undefined"){
58                                        child_attribute= getAttribute(children[x],elem_attribute);
59                                        if((strTagName.toLowerCase()==elem_type.toLowerCase() || elem_type=="") && (elem_attribute=="" || child_attribute==elem_attribute_match)){
60                                                if(option=="all"){
61                                                        results.push(children[x]);
62                                                }else{
63                                                        return children[x];
64                                                }
65                                        }
66                                        if(depth!=0){
67                                                result=getChildren(children[x], elem_type, elem_attribute, elem_attribute_match, option, depth-1);
68                                                if(option=="all"){
69                                                        if(result.length>0){
70                                                                results= results.concat(result);
71                                                        }
72                                                }else if(result!=null){                                                                         
73                                                        return result;
74                                                }
75                                        }
76                                }
77                        }
78                        if(option=="all")
79                           return results;
80                }
81                return null;
82        };       
83       
84        function isChildOf(elem, parent){
85                if(elem){
86                        if(elem==parent)
87                                return true;
88                        while(elem.parentNode != 'undefined'){
89                                return isChildOf(elem.parentNode, parent);
90                        }
91                }
92                return false;
93        };
94       
95        function getMouseX(e){
96
97                if(e!=null && typeof(e.pageX)!="undefined"){
98                        return e.pageX;
99                }else{
100                        return (e!=null?e.x:event.x)+ document.documentElement.scrollLeft;
101                }
102        };
103       
104        function getMouseY(e){
105                if(e!=null && typeof(e.pageY)!="undefined"){
106                        return e.pageY;
107                }else{
108                        return (e!=null?e.y:event.y)+ document.documentElement.scrollTop;
109                }
110        };
111       
112        function calculeOffsetLeft(r){
113                return calculeOffset(r,"offsetLeft")
114        };
115       
116        function calculeOffsetTop(r){
117                return calculeOffset(r,"offsetTop")
118        };
119       
120        function calculeOffset(element,attr){
121                var offset=0;
122                while(element){
123                        offset+=element[attr];
124                        element=element.offsetParent
125                }
126                return offset;
127        };
128       
129        /** return the computed style
130         *      @param: elem: the reference to the element
131         *      @param: prop: the name of the css property       
132         */
133        function get_css_property(elem, prop)
134        {
135                if(document.defaultView)
136                {
137                        return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
138                }
139                else if(elem.currentStyle)
140                {
141                        var prop = prop.replace(/-\D/gi, function(sMatch)
142                        {
143                                return sMatch.charAt(sMatch.length - 1).toUpperCase();
144                        });
145                        return elem.currentStyle[prop];
146                }
147                else return null;
148        }
149       
150/****
151 * Moving an element
152 ***/ 
153       
154        var _mCE;       // currently moving element
155       
156        /* allow to move an element in a window
157                e: the event
158                id: the id of the element
159                frame: the frame of the element
160                ex of use:
161                        in html:        <img id='move_area_search_replace' onmousedown='return parent.start_move_element(event,"area_search_replace", parent.frames["this_frame_id"]);' .../> 
162                or
163                        in javascript: document.getElementById("my_div").onmousedown= start_move_element
164        */
165        function start_move_element(e, id, frame){
166                var elem_id=(e.target || e.srcElement).id;
167                if(id)
168                        elem_id=id;             
169                if(!frame)
170                        frame=window;
171                if(frame.event)
172                        e=frame.event;
173                       
174                _mCE= frame.document.getElementById(elem_id);
175                _mCE.frame=frame;
176                frame.document.onmousemove= move_element;
177                frame.document.onmouseup= end_move_element;
178                /*_mCE.onmousemove= move_element;
179                _mCE.onmouseup= end_move_element;*/
180               
181                //alert(_mCE.frame.document.body.offsetHeight);
182               
183                mouse_x= getMouseX(e);
184                mouse_y= getMouseY(e);
185                //window.status=frame+ " elem: "+elem_id+" elem: "+ _mCE + " mouse_x: "+mouse_x;
186                _mCE.start_pos_x = mouse_x - (_mCE.style.left.replace("px","") || calculeOffsetLeft(_mCE));
187                _mCE.start_pos_y = mouse_y - (_mCE.style.top.replace("px","") || calculeOffsetTop(_mCE));
188                return false;
189        };
190       
191        function end_move_element(e){
192                _mCE.frame.document.onmousemove= "";
193                _mCE.frame.document.onmouseup= "";             
194                _mCE=null;
195        };
196       
197        function move_element(e){
198                var newTop,newLeft,maxLeft;
199
200                if( _mCE.frame && _mCE.frame.event )
201                        e=_mCE.frame.event;
202                newTop  = getMouseY(e) - _mCE.start_pos_y;
203                newLeft = getMouseX(e) - _mCE.start_pos_x;
204               
205                maxLeft = _mCE.frame.document.body.offsetWidth- _mCE.offsetWidth;
206                max_top = _mCE.frame.document.body.offsetHeight- _mCE.offsetHeight;
207                newTop  = Math.min(Math.max(0, newTop), max_top);
208                newLeft = Math.min(Math.max(0, newLeft), maxLeft);
209               
210                _mCE.style.top  = newTop+"px";
211                _mCE.style.left = newLeft+"px";         
212                return false;
213        };
214       
215/***
216 * Managing a textarea (this part need the navigator infos from editAreaLoader
217 ***/ 
218       
219        var nav= editAreaLoader.nav;
220       
221        // allow to get infos on the selection: array(start, end)
222        function getSelectionRange(textarea){
223                return {"start": textarea.selectionStart, "end": textarea.selectionEnd};
224        };
225       
226        // allow to set the selection
227        function setSelectionRange(t, start, end){
228                t.focus();
229               
230                start   = Math.max(0, Math.min(t.value.length, start));
231                end             = Math.max(start, Math.min(t.value.length, end));
232       
233                if( nav.isOpera && nav.isOpera < 9.6 ){ // Opera bug when moving selection start and selection end
234                        t.selectionEnd = 1;     
235                        t.selectionStart = 0;                   
236                        t.selectionEnd = 1;     
237                        t.selectionStart = 0;           
238                }
239                t.selectionStart        = start;
240                t.selectionEnd          = end;         
241                //textarea.setSelectionRange(start, end);
242               
243                if(nav.isIE)
244                        set_IE_selection(t);
245        };
246
247       
248        // set IE position in Firefox mode (textarea.selectionStart and textarea.selectionEnd). should work as a repeated task
249        function get_IE_selection(t){
250                var d=document,div,range,stored_range,elem,scrollTop,relative_top,line_start,line_nb,range_start,range_end,tab;
251                if(t && t.focused)
252                {       
253                        if(!t.ea_line_height)
254                        {       // calculate the lineHeight
255                                div= d.createElement("div");
256                                div.style.fontFamily= get_css_property(t, "font-family");
257                                div.style.fontSize= get_css_property(t, "font-size");
258                                div.style.visibility= "hidden";                 
259                                div.innerHTML="0";
260                                d.body.appendChild(div);
261                                t.ea_line_height= div.offsetHeight;
262                                d.body.removeChild(div);
263                        }
264                        //t.focus();
265                        range = d.selection.createRange();
266                        try
267                        {
268                                stored_range = range.duplicate();
269                                stored_range.moveToElementText( t );
270                                stored_range.setEndPoint( 'EndToEnd', range );
271                                if(stored_range.parentElement() == t){
272                                        // the range don't take care of empty lines in the end of the selection
273                                        elem            = t;
274                                        scrollTop       = 0;
275                                        while(elem.parentNode){
276                                                scrollTop+= elem.scrollTop;
277                                                elem    = elem.parentNode;
278                                        }
279                               
280                                //      var scrollTop= t.scrollTop + document.body.scrollTop;
281                                       
282                                //      var relative_top= range.offsetTop - calculeOffsetTop(t) + scrollTop;
283                                        relative_top= range.offsetTop - calculeOffsetTop(t)+ scrollTop;
284                                //      alert("rangeoffset: "+ range.offsetTop +"\ncalcoffsetTop: "+ calculeOffsetTop(t) +"\nrelativeTop: "+ relative_top);
285                                        line_start      = Math.round((relative_top / t.ea_line_height) +1);
286                                       
287                                        line_nb         = Math.round(range.boundingHeight / t.ea_line_height);
288                                       
289                                        range_start     = stored_range.text.length - range.text.length;
290                                        tab     = t.value.substr(0, range_start).split("\n");                   
291                                        range_start     += (line_start - tab.length)*2;         // add missing empty lines to the selection
292                                        t.selectionStart = range_start;
293                                       
294                                        range_end       = t.selectionStart + range.text.length;
295                                        tab     = t.value.substr(0, range_start + range.text.length).split("\n");                       
296                                        range_end       += (line_start + line_nb - 1 - tab.length)*2;
297                                        t.selectionEnd = range_end;
298                                }
299                        }
300                        catch(e){}
301                }
302                if( t && t.id )
303                {
304                        setTimeout("get_IE_selection(document.getElementById('"+ t.id +"'));", 50);
305                }
306        };
307       
308        function IE_textarea_focus(){
309                event.srcElement.focused= true;
310        }
311       
312        function IE_textarea_blur(){
313                event.srcElement.focused= false;
314        }
315       
316        // select the text for IE (take into account the \r difference)
317        function set_IE_selection( t ){
318                var nbLineStart,nbLineStart,nbLineEnd,range;
319                if(!window.closed){ 
320                        nbLineStart=t.value.substr(0, t.selectionStart).split("\n").length - 1;
321                        nbLineEnd=t.value.substr(0, t.selectionEnd).split("\n").length - 1;
322                        try
323                        {
324                                range = document.selection.createRange();
325                                range.moveToElementText( t );
326                                range.setEndPoint( 'EndToStart', range );
327                                range.moveStart('character', t.selectionStart - nbLineStart);
328                                range.moveEnd('character', t.selectionEnd - nbLineEnd - (t.selectionStart - nbLineStart)  );
329                                range.select();
330                        }
331                        catch(e){}
332                }
333        };
334       
335       
336        editAreaLoader.waiting_loading["elements_functions.js"]= "loaded";
Note: See TracBrowser for help on using the repository browser.