1 // (c) BenoƮt PIN 2006-2009
6 // Meta functions for events management.
8 var addListener
; /* (ob, eventName, listenerFunction, group) add event listener eventName without "on" prefix.
9 * optionally, listeners can be grouped to make removing convenient.
11 var removeListener
; // (ob, eventName, listenerFunction, group) remove event listener.
12 var removeGroupListeners
; // (group) remove all listeners in group.
13 var raiseMouseEvent
; // (ob, eventName) raise mouse event (without "on" prefix) on object.
15 var getTargetedObject
; // (event) retrieves the object that fired the event. Event parameter is optional.
16 var getEventObject
; // (event) return the event object. Event parameter is optional.
17 var disableDefault
; // (event) disable default event's action. Event parameter is optional.
18 var disablePropagation
; // (event) disable event propagation or bubbling.
21 var getWindowWidth
; // returns browser's window width
22 var getWindowHeight
; // returns browser's window height
23 var clearSelection
; // clear current selection (useful on drag and drop)
24 var getCopyOfNode
; /* (node) returns a clone of the given node.
26 * the node came from a foreign document (eg. XmlHttpRequest xml reponse)
27 * to inject HMTL code inside tags where innerHtml is read only (IE)
29 var copyPrototype
; // (descendant, parent) lightwheight javascript inheritance
33 function buildMetaFunctions() {
34 addListener
= _build_addListener();
35 removeListener
= _build_removeListener();
36 raiseMouseEvent
= _build_raiseMouseEvent();
38 getTargetedObject
= _build_getTargetedObject();
39 getEventObject
= _build_getEventObject();
40 disableDefault
= _build_disableDefault();
41 disablePropagation
= _build_disablePropagation();
42 getWindowWidth
= _build_getWindowWidth();
43 getWindowHeight
= _build_getWindowHeight();
44 clearSelection
= _build_clearSelection();
47 __groupListeners
= {};
49 function _build_addListener() {
51 if (browser
.isIE55
|| browser
.isIE6up
) {
52 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
53 eventName
= "on" + eventName
;
54 ob
.attachEvent(eventName
, listenerFunction
);
57 else if (browser
.isDOM2Event
) {
58 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
59 ob
.addEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
62 var common = function(ob
, eventName
, listenerFunction
, group
) {
63 _browserSpecific(ob
, eventName
, listenerFunction
);
65 if(!__groupListeners
[group
]) {
66 __groupListeners
[group
] = [];}
67 __groupListeners
[group
].push([ob
, eventName
, listenerFunction
]);
73 function _build_removeListener() {
74 if (browser
.isIE55
|| browser
.isIE6up
) {
75 var _ie_removeListener = function(ob
, eventName
, listenerFunction
) {
76 eventName
= "on" + eventName
;
77 ob
.detachEvent(eventName
, listenerFunction
);
79 return _ie_removeListener
;
81 else if (browser
.isDOM2Event
) {
82 var _dom2_removeListener = function(ob
, eventName
, listenerFunction
) {
83 ob
.removeEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
85 return _dom2_removeListener
;
89 removeGroupListeners = function(group
) {
90 var listeners
= __groupListeners
[group
];
92 for (i
=0 ; i
<listeners
.length
; i
++){
94 removeListener(l
[0], l
[1], l
[2]);
96 __groupListeners
[group
] = null;
100 function _build_raiseMouseEvent() {
101 if (browser
.isIE55
|| browser
.isIE6up
) {
102 var _ie_raiseMouseEvent = function(ob
, eventName
) {
103 ob
.fireEvent("on" + eventName
);
105 return _ie_raiseMouseEvent
;
107 else if (browser
.isDOM2Event
) {
108 var _dom2_raiseMouseEvent = function(ob
, eventName
) {
109 var event
= document
.createEvent("MouseEvents");
110 event
.initEvent(eventName
, true, true);
111 ob
.dispatchEvent(event
);
113 return _dom2_raiseMouseEvent
;
117 function _build_getTargetedObject(){
118 if (browser
.isIE55
|| browser
.isIE6up
) {
119 var _ie_getTargetedObject = function() {
120 return window
.event
.srcElement
;
122 return _ie_getTargetedObject
;
124 else if (browser
.isDOM2Event
) {
125 var _appleWebKit_getTargetedeObject = function(evt
) {
126 var target
= evt
.target
;
127 // is it really safe ?...
128 return (target
.nodeType
=== 3) ? target
.parentNode
: target
;
130 var _dom2_getTargetedObject = function(evt
) {
133 return (browser
.isAppleWebKit
) ? _appleWebKit_getTargetedeObject
: _dom2_getTargetedObject
;
137 function _build_getEventObject(){
139 var _ie_getEventObject = function() {
142 return _ie_getEventObject
;
144 else if (browser
.isDOM2Event
) {
145 var _dom2_getEventObject = function(evt
) {
148 return _dom2_getEventObject
;
153 function _build_disableDefault(){
154 if (browser
.isIE55
|| browser
.isIE6up
) {
155 var _ie_disableDefault = function() {
156 window
.event
.returnValue
= false;
158 return _ie_disableDefault
;
160 else if (browser
.isDOM2Event
) {
161 var _dom2_disableDefault = function(evt
) {
162 evt
.preventDefault();
164 return _dom2_disableDefault
;
168 function _build_disablePropagation() {
169 if (browser
.isIE55
|| browser
.isIE6up
) {
170 var _ie_disablePropagation = function() {
171 window
.event
.cancelBubble
= true;
173 return _ie_disablePropagation
;
175 else if (browser
.isDOM2Event
) {
176 var _dom2_disablePropagation = function(evt
) {
177 evt
.stopPropagation();
179 return _dom2_disablePropagation
;
183 function _build_getWindowWidth() {
184 if (window
.innerWidth
!== undefined){
186 return window
.innerWidth
;
191 return document
.documentElement
.clientWidth
;
196 function _build_getWindowHeight() {
197 if (window
.innerHeight
!== undefined) {
199 return window
.innerHeight
;
204 return document
.documentElement
.clientHeight
;
209 function _build_clearSelection() {
210 if (document
.selection
) {
212 document
.selection
.clear();
217 window
.getSelection().removeAllRanges();
223 buildMetaFunctions();
225 var ELEMENT_NODE
= 1;
228 getCopyOfNode = function(node
) {
230 switch(node
.nodeType
) {
232 var attributes
= node
.attributes
;
233 var childs
= node
.childNodes
;
235 var e
= document
.createElement(node
.nodeName
);
238 for(i
=0 ; i
<attributes
.length
; i
++) {
239 attribute
= attributes
[i
];
240 _setAttribute(e
, attribute
.name
, attribute
.value
);
243 for(i
=0 ; i
<childs
.length
; i
++) {
244 e
.appendChild(getCopyOfNode(childs
[i
]));}
249 return document
.createTextNode(node
.nodeValue
);
254 _setAttribute = function(e
, name
, value
) {
255 // workarround IE lack of dom implementation.
256 switch(name
.toLowerCase()) {
264 loadCssText(e
, value
);
267 if (name
.slice(0,2) === 'on') { // event handler
268 // A browser normaly eval text code attached to a onXyz attribute. Not IE.
269 /*jslint evil: true */
270 e
[name
] = function(){eval(value
);};}
272 e
.setAttribute(name
, value
);}
275 var reCompoundPropName
= /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
276 var _capitalizeCssPropName = function (s
, g1
, g2
, g3
) { // gN args match above regexp groups
278 return g1
+ g2
.toUpperCase() + g3
;}
283 var loadCssText = function (e
, cssText
) {
284 var pairs
= cssText
.split(';');
285 var pair
, name
, value
, i
;
287 for (i
= 0; i
< pairs
.length
; i
++) {
288 pair
= pairs
[i
].split(':');
289 if (pair
.length
=== 2) {
290 name
= _capitalizeCssPropName(pair
[0]);
298 _setAttribute = function(e
, name
, value
) {e
.setAttribute(name
, value
);};
302 * http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
305 var copyPrototype = function (descendant
, parent
) {
306 var sConstructor
= parent
.toString();
307 var aMatch
= sConstructor
.match( /\s*function (.*)\(/ );
308 if ( aMatch
!== null ) { descendant
.prototype[aMatch
[1]] = parent
; }
310 for (m
in parent
.prototype) {
311 if (parent
.prototype.hasOwnProperty(m
)) {
312 descendant
.prototype[m
] = parent
.prototype[m
]; }