2
* This page contains some general usefull functions for javascript
7
// need to redefine this functiondue to IE problem
8
function getAttribute( elm, aname ) {
10
var avalue = elm.getAttribute( aname );
15
for ( var i = 0; i < elm.attributes.length; i ++ ) {
16
var taName = elm.attributes [i] .name.toLowerCase();
17
if ( taName == aname ) {
18
avalue = elm.attributes [i] .value;
26
// need to redefine this function due to IE problem
27
function setAttribute( elm, attr, val ) {
29
elm.setAttribute("className", val);
30
elm.setAttribute("class", val);
32
elm.setAttribute(attr, val);
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)
44
function getChildren(elem, elem_type, elem_attribute, elem_attribute_match, option, depth)
51
var children= elem.childNodes;
53
var results= new Array();
54
for (var x=0;x<children.length;x++) {
55
strTagName = new String(children[x].tagName);
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)){
61
results.push(children[x]);
67
result=getChildren(children[x], elem_type, elem_attribute, elem_attribute_match, option, depth-1);
70
results= results.concat(result);
72
}else if(result!=null){
84
function isChildOf(elem, parent){
88
while(elem.parentNode != 'undefined'){
89
return isChildOf(elem.parentNode, parent);
95
function getMouseX(e){
97
return event.x + document.body.scrollLeft;
100
if(e!=null && typeof(e.pageX)!="undefined"){
103
return (e!=null?e.x:event.x)+ document.documentElement.scrollLeft;
105
//return (e!=null) ? e.pageX : event.x + document.documentElement.scrollLeft;
108
function getMouseY(e){
110
return event.y + document.body.scrollTop;
113
if(e!=null && typeof(e.pageY)!="undefined"){
116
return (e!=null?e.y:event.y)+ document.documentElement.scrollTop;
118
//return (e!=null) ? e.pageY : event.y + document.documentElement.scrollTop;
121
function calculeOffsetLeft(r){
122
return calculeOffset(r,"offsetLeft")
125
function calculeOffsetTop(r){
126
return calculeOffset(r,"offsetTop")
129
function calculeOffset(element,attr){
132
offset+=element[attr];
133
element=element.offsetParent
138
/** return the computed style
139
* @param: elem: the reference to the element
140
* @param: prop: the name of the css property
142
function get_css_property(elem, prop)
144
if(document.defaultView)
146
return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
148
else if(elem.currentStyle)
150
var prop = prop.replace(/-\D/gi, function(sMatch)
152
return sMatch.charAt(sMatch.length - 1).toUpperCase();
154
return elem.currentStyle[prop];
163
var move_current_element;
164
/* allow to move an element in a window
166
id: the id of the element
167
frame: the frame of the element
169
in html: <img id='move_area_search_replace' onmousedown='return parent.start_move_element(event,"area_search_replace", parent.frames["this_frame_id"]);' .../>
171
in javascript: document.getElementById("my_div").onmousedown= start_move_element
173
function start_move_element(e, id, frame){
174
var elem_id=(e.target || e.srcElement).id;
182
move_current_element= frame.document.getElementById(elem_id);
183
move_current_element.frame=frame;
184
frame.document.onmousemove= move_element;
185
frame.document.onmouseup= end_move_element;
186
/*move_current_element.onmousemove= move_element;
187
move_current_element.onmouseup= end_move_element;*/
189
//alert(move_current_element.frame.document.body.offsetHeight);
191
mouse_x= getMouseX(e);
192
mouse_y= getMouseY(e);
193
//window.status=frame+ " elem: "+elem_id+" elem: "+ move_current_element + " mouse_x: "+mouse_x;
194
move_current_element.start_pos_x = mouse_x - (move_current_element.style.left.replace("px","") || calculeOffsetLeft(move_current_element));
195
move_current_element.start_pos_y = mouse_y - (move_current_element.style.top.replace("px","") || calculeOffsetTop(move_current_element));
199
function end_move_element(e){
200
move_current_element.frame.document.onmousemove= "";
201
move_current_element.frame.document.onmouseup= "";
202
move_current_element=null;
205
function move_element(e){
206
/*window.status="move"+frame;
207
window.status="move2"+frame.event;*/
208
if(move_current_element.frame && move_current_element.frame.event)
209
e=move_current_element.frame.event;
210
var mouse_x=getMouseX(e);
211
var mouse_y=getMouseY(e);
212
var new_top= mouse_y - move_current_element.start_pos_y;
213
var new_left= mouse_x - move_current_element.start_pos_x;
215
var max_left= move_current_element.frame.document.body.offsetWidth- move_current_element.offsetWidth;
216
max_top= move_current_element.frame.document.body.offsetHeight- move_current_element.offsetHeight;
217
new_top= Math.min(Math.max(0, new_top), max_top);
218
new_left= Math.min(Math.max(0, new_left), max_left);
220
move_current_element.style.top= new_top+"px";
221
move_current_element.style.left= new_left+"px";
226
* Managing a textarea (this part need the navigator infos from editAreaLoader
229
var nav= editAreaLoader.nav;
231
// allow to get infos on the selection: array(start, end)
232
function getSelectionRange(textarea){
234
// get_IE_selection(textarea);
235
return {"start": textarea.selectionStart, "end": textarea.selectionEnd};
238
// allow to set the selection
239
function setSelectionRange(textarea, start, end){
242
start= Math.max(0, Math.min(textarea.value.length, start));
243
end= Math.max(start, Math.min(textarea.value.length, end));
245
if(nav['isOpera']){ // Opera bug when moving selection start and selection end
246
textarea.selectionEnd = 1;
247
textarea.selectionStart = 0;
248
textarea.selectionEnd = 1;
249
textarea.selectionStart = 0;
251
textarea.selectionStart = start;
252
textarea.selectionEnd = end;
253
//textarea.setSelectionRange(start, end);
256
set_IE_selection(textarea);
260
// set IE position in Firefox mode (textarea.selectionStart and textarea.selectionEnd). should work as a repeated task
261
function get_IE_selection(textarea){
263
if(textarea && textarea.focused)
265
if(!textarea.ea_line_height)
266
{ // calculate the lineHeight
267
var div= document.createElement("div");
268
div.style.fontFamily= get_css_property(textarea, "font-family");
269
div.style.fontSize= get_css_property(textarea, "font-size");
270
div.style.visibility= "hidden";
272
document.body.appendChild(div);
273
textarea.ea_line_height= div.offsetHeight;
274
document.body.removeChild(div);
277
var range = document.selection.createRange();
278
var stored_range = range.duplicate();
279
stored_range.moveToElementText( textarea );
280
stored_range.setEndPoint( 'EndToEnd', range );
281
if(stored_range.parentElement()==textarea){
282
// the range don't take care of empty lines in the end of the selection
285
while(elem.parentNode){
286
scrollTop+= elem.scrollTop;
287
elem= elem.parentNode;
290
// var scrollTop= textarea.scrollTop + document.body.scrollTop;
292
// var relative_top= range.offsetTop - calculeOffsetTop(textarea) + scrollTop;
293
var relative_top= range.offsetTop - calculeOffsetTop(textarea)+ scrollTop;
294
// alert("rangeoffset: "+ range.offsetTop +"\ncalcoffsetTop: "+ calculeOffsetTop(textarea) +"\nrelativeTop: "+ relative_top);
295
var line_start = Math.round((relative_top / textarea.ea_line_height) +1);
297
var line_nb= Math.round(range.boundingHeight / textarea.ea_line_height);
299
// alert("store_range: "+ stored_range.text.length+"\nrange: "+range.text.length+"\nrange_text: "+ range.text);
300
var range_start= stored_range.text.length - range.text.length;
301
var tab= textarea.value.substr(0, range_start).split("\n");
302
range_start+= (line_start - tab.length)*2; // add missing empty lines to the selection
303
textarea.selectionStart = range_start;
305
var range_end= textarea.selectionStart + range.text.length;
306
tab= textarea.value.substr(0, range_start + range.text.length).split("\n");
307
range_end+= (line_start + line_nb - 1 - tab.length)*2;
308
textarea.selectionEnd = range_end;
311
setTimeout("get_IE_selection(document.getElementById('"+ textarea.id +"'));", 50);
314
function IE_textarea_focus(){
315
event.srcElement.focused= true;
318
function IE_textarea_blur(){
319
event.srcElement.focused= false;
322
// select the text for IE (take into account the \r difference)
323
function set_IE_selection(textarea){
324
var nbLineStart=textarea.value.substr(0, textarea.selectionStart).split("\n").length - 1;
325
var nbLineEnd=textarea.value.substr(0, textarea.selectionEnd).split("\n").length - 1;
326
var range = document.selection.createRange();
327
range.moveToElementText( textarea );
328
range.setEndPoint( 'EndToStart', range );
329
range.moveStart('character', textarea.selectionStart - nbLineStart);
330
range.moveEnd('character', textarea.selectionEnd - nbLineEnd - (textarea.selectionStart - nbLineStart) );
335
editAreaLoader.waiting_loading["elements_functions.js"]= "loaded";