Blame view

docs/js/editarea_0_8_2/edit_area/elements_functions.js 9.78 KB
c2589de4   Luigi Serra   added demo page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  /****

   * This page contains some general usefull functions for javascript

   *

   ****/  

  	

  	

  	// need to redefine this functiondue to IE problem

  	function getAttribute( elm, aName ) {

  		var aValue,taName,i;

  		try{

  			aValue = elm.getAttribute( aName );

  		}catch(exept){}

  		

  		if( ! aValue ){

  			for( i = 0; i < elm.attributes.length; i ++ ) {

  				taName = elm.attributes[i] .name.toLowerCase();

  				if( taName == aName ) {

  					aValue = elm.attributes[i] .value;

  					return aValue;

  				}

  			}

  		}

  		return aValue;

  	};

  	

  	// need to redefine this function due to IE problem

  	function setAttribute( elm, attr, val ) {

  		if(attr=="class"){

  			elm.setAttribute("className", val);

  			elm.setAttribute("class", val);

  		}else{

  			elm.setAttribute(attr, val);

  		}

  	};

  	

  	/* return a child element

  		elem: element we are searching in

  		elem_type: type of the eleemnt we are searching (DIV, A, etc...)

  		elem_attribute: attribute of the searched element that must match

  		elem_attribute_match: value that elem_attribute must match

  		option: "all" if must return an array of all children, otherwise return the first match element

  		depth: depth of search (-1 or no set => unlimited)

  	*/

  	function getChildren(elem, elem_type, elem_attribute, elem_attribute_match, option, depth)

  	{           

  		if(!option)

  			var option="single";

  		if(!depth)

  			var depth=-1;

  		if(elem){

  			var children= elem.childNodes;

  			var result=null;

  			var results= [];

  			for (var x=0;x<children.length;x++) {

  				strTagName = new String(children[x].tagName);

  				children_class="?";

  				if(strTagName!= "undefined"){

  					child_attribute= getAttribute(children[x],elem_attribute);

  					if((strTagName.toLowerCase()==elem_type.toLowerCase() || elem_type=="") && (elem_attribute=="" || child_attribute==elem_attribute_match)){

  						if(option=="all"){

  							results.push(children[x]);

  						}else{

  							return children[x];

  						}

  					}

  					if(depth!=0){

  						result=getChildren(children[x], elem_type, elem_attribute, elem_attribute_match, option, depth-1);

  						if(option=="all"){

  							if(result.length>0){

  								results= results.concat(result);

  							}

  						}else if(result!=null){                                                                          

  							return result;

  						}

  					}

  				}

  			}

  			if(option=="all")

  			   return results;

  		}

  		return null;

  	};       

  	

  	function isChildOf(elem, parent){

  		if(elem){

  			if(elem==parent)

  				return true;

  			while(elem.parentNode != 'undefined'){

  				return isChildOf(elem.parentNode, parent);

  			}

  		}

  		return false;

  	};

  	

  	function getMouseX(e){

  

  		if(e!=null && typeof(e.pageX)!="undefined"){

  			return e.pageX;

  		}else{

  			return (e!=null?e.x:event.x)+ document.documentElement.scrollLeft;

  		}

  	};

  	

  	function getMouseY(e){

  		if(e!=null && typeof(e.pageY)!="undefined"){

  			return e.pageY;

  		}else{

  			return (e!=null?e.y:event.y)+ document.documentElement.scrollTop;

  		}

  	};

  	

  	function calculeOffsetLeft(r){

  		return calculeOffset(r,"offsetLeft")

  	};

  	

  	function calculeOffsetTop(r){

  		return calculeOffset(r,"offsetTop")

  	};

  	

  	function calculeOffset(element,attr){

  		var offset=0;

  		while(element){

  			offset+=element[attr];

  			element=element.offsetParent

  		}

  		return offset;

  	};

  	

  	/** return the computed style

  	 *	@param: elem: the reference to the element

  	 *	@param: prop: the name of the css property	 

  	 */

  	function get_css_property(elem, prop)

  	{

  		if(document.defaultView)

  		{

  			return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);

  		}

  		else if(elem.currentStyle)

  		{

  			var prop = prop.replace(/-\D/gi, function(sMatch)

  			{

  				return sMatch.charAt(sMatch.length - 1).toUpperCase();

  			});

  			return elem.currentStyle[prop];

  		}

  		else return null;

  	}

  	

  /****

   * Moving an element 

   ***/  

  	

  	var _mCE;	// currently moving element

  	

  	/* allow to move an element in a window

  		e: the event

  		id: the id of the element

  		frame: the frame of the element 

  		ex of use:

  			in html:	<img id='move_area_search_replace' onmousedown='return parent.start_move_element(event,"area_search_replace", parent.frames["this_frame_id"]);' .../>  

  		or

  			in javascript: document.getElementById("my_div").onmousedown= start_move_element

  	*/

  	function start_move_element(e, id, frame){

  		var elem_id=(e.target || e.srcElement).id;

  		if(id)

  			elem_id=id;		

  		if(!frame)

  			frame=window;

  		if(frame.event)

  			e=frame.event;

  			

  		_mCE= frame.document.getElementById(elem_id);

  		_mCE.frame=frame;

  		frame.document.onmousemove= move_element;

  		frame.document.onmouseup= end_move_element;

  		/*_mCE.onmousemove= move_element;

  		_mCE.onmouseup= end_move_element;*/

  		

  		//alert(_mCE.frame.document.body.offsetHeight);

  		

  		mouse_x= getMouseX(e);

  		mouse_y= getMouseY(e);

  		//window.status=frame+ " elem: "+elem_id+" elem: "+ _mCE + " mouse_x: "+mouse_x;

  		_mCE.start_pos_x = mouse_x - (_mCE.style.left.replace("px","") || calculeOffsetLeft(_mCE));

  		_mCE.start_pos_y = mouse_y - (_mCE.style.top.replace("px","") || calculeOffsetTop(_mCE));

  		return false;

  	};

  	

  	function end_move_element(e){

  		_mCE.frame.document.onmousemove= "";

  		_mCE.frame.document.onmouseup= "";		

  		_mCE=null;

  	};

  	

  	function move_element(e){

  		var newTop,newLeft,maxLeft;

  

  		if( _mCE.frame && _mCE.frame.event )

  			e=_mCE.frame.event;

  		newTop	= getMouseY(e) - _mCE.start_pos_y;

  		newLeft	= getMouseX(e) - _mCE.start_pos_x;

  		

  		maxLeft	= _mCE.frame.document.body.offsetWidth- _mCE.offsetWidth;

  		max_top	= _mCE.frame.document.body.offsetHeight- _mCE.offsetHeight;

  		newTop	= Math.min(Math.max(0, newTop), max_top);

  		newLeft	= Math.min(Math.max(0, newLeft), maxLeft);

  		

  		_mCE.style.top	= newTop+"px";

  		_mCE.style.left	= newLeft+"px";		

  		return false;

  	};

  	

  /***

   * Managing a textarea (this part need the navigator infos from editAreaLoader

   ***/ 

  	

  	var nav= editAreaLoader.nav;

  	

  	// allow to get infos on the selection: array(start, end)

  	function getSelectionRange(textarea){

  		return {"start": textarea.selectionStart, "end": textarea.selectionEnd};

  	};

  	

  	// allow to set the selection

  	function setSelectionRange(t, start, end){

  		t.focus();

  		

  		start	= Math.max(0, Math.min(t.value.length, start));

  		end		= Math.max(start, Math.min(t.value.length, end));

  	

  		if( nav.isOpera && nav.isOpera < 9.6 ){	// Opera bug when moving selection start and selection end

  			t.selectionEnd = 1;	

  			t.selectionStart = 0;			

  			t.selectionEnd = 1;	

  			t.selectionStart = 0;		

  		}

  		t.selectionStart	= start;

  		t.selectionEnd		= end;		

  		//textarea.setSelectionRange(start, end);

  		

  		if(nav.isIE)

  			set_IE_selection(t);

  	};

  

  	

  	// set IE position in Firefox mode (textarea.selectionStart and textarea.selectionEnd). should work as a repeated task

  	function get_IE_selection(t){

  		var d=document,div,range,stored_range,elem,scrollTop,relative_top,line_start,line_nb,range_start,range_end,tab;

  		if(t && t.focused)

  		{	

  			if(!t.ea_line_height)

  			{	// calculate the lineHeight

  				div= d.createElement("div");

  				div.style.fontFamily= get_css_property(t, "font-family");

  				div.style.fontSize= get_css_property(t, "font-size");

  				div.style.visibility= "hidden";			

  				div.innerHTML="0";

  				d.body.appendChild(div);

  				t.ea_line_height= div.offsetHeight;

  				d.body.removeChild(div);

  			}

  			//t.focus();

  			range = d.selection.createRange();

  			try

  			{

  				stored_range = range.duplicate();

  				stored_range.moveToElementText( t );

  				stored_range.setEndPoint( 'EndToEnd', range );

  				if(stored_range.parentElement() == t){

  					// the range don't take care of empty lines in the end of the selection

  					elem		= t;

  					scrollTop	= 0;

  					while(elem.parentNode){

  						scrollTop+= elem.scrollTop;

  						elem	= elem.parentNode;

  					}

  				

  				//	var scrollTop= t.scrollTop + document.body.scrollTop;

  					

  				//	var relative_top= range.offsetTop - calculeOffsetTop(t) + scrollTop;

  					relative_top= range.offsetTop - calculeOffsetTop(t)+ scrollTop;

  				//	alert("rangeoffset: "+ range.offsetTop +"\ncalcoffsetTop: "+ calculeOffsetTop(t) +"\nrelativeTop: "+ relative_top);

  					line_start	= Math.round((relative_top / t.ea_line_height) +1);

  					

  					line_nb		= Math.round(range.boundingHeight / t.ea_line_height);

  					

  					range_start	= stored_range.text.length - range.text.length;

  					tab	= t.value.substr(0, range_start).split("\n");			

  					range_start	+= (line_start - tab.length)*2;		// add missing empty lines to the selection

  					t.selectionStart = range_start;

  					

  					range_end	= t.selectionStart + range.text.length;

  					tab	= t.value.substr(0, range_start + range.text.length).split("\n");			

  					range_end	+= (line_start + line_nb - 1 - tab.length)*2;

  					t.selectionEnd = range_end;

  				}

  			}

  			catch(e){}

  		}

  		if( t && t.id )

  		{

  			setTimeout("get_IE_selection(document.getElementById('"+ t.id +"'));", 50);

  		}

  	};

  	

  	function IE_textarea_focus(){

  		event.srcElement.focused= true;

  	}

  	

  	function IE_textarea_blur(){

  		event.srcElement.focused= false;

  	}

  	

  	// select the text for IE (take into account the \r difference)

  	function set_IE_selection( t ){

  		var nbLineStart,nbLineStart,nbLineEnd,range;

  		if(!window.closed){ 

  			nbLineStart=t.value.substr(0, t.selectionStart).split("\n").length - 1;

  			nbLineEnd=t.value.substr(0, t.selectionEnd).split("\n").length - 1;

  			try

  			{

  				range = document.selection.createRange();

  				range.moveToElementText( t );

  				range.setEndPoint( 'EndToStart', range );

  				range.moveStart('character', t.selectionStart - nbLineStart);

  				range.moveEnd('character', t.selectionEnd - nbLineEnd - (t.selectionStart - nbLineStart)  );

  				range.select();

  			}

  			catch(e){}

  		}

  	};

  	

  	

  	editAreaLoader.waiting_loading["elements_functions.js"]= "loaded";