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)
30 var copyPrototype
; // (descendant, parent) lightwheight javascript inheritance
31 if (!history
.pushState
) {
32 history
.pushState = function(){};
37 function buildMetaFunctions() {
38 addListener
= _build_addListener();
39 removeListener
= _build_removeListener();
40 raiseMouseEvent
= _build_raiseMouseEvent();
42 getTargetedObject
= _build_getTargetedObject();
43 getEventObject
= _build_getEventObject();
44 disableDefault
= _build_disableDefault();
45 disablePropagation
= _build_disablePropagation();
46 getWindowWidth
= _build_getWindowWidth();
47 getWindowHeight
= _build_getWindowHeight();
48 clearSelection
= _build_clearSelection();
51 var __groupListeners
= {};
53 function _build_addListener() {
55 if (!browser
.isDOM2Event
) {
56 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
57 eventName
= "on" + eventName
;
58 ob
.attachEvent(eventName
, listenerFunction
);
62 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
63 ob
.addEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
66 var common = function(ob
, eventName
, listenerFunction
, group
) {
67 _browserSpecific(ob
, eventName
, listenerFunction
);
69 if(!__groupListeners
[group
]) {
70 __groupListeners
[group
] = [];}
71 __groupListeners
[group
].push([ob
, eventName
, listenerFunction
]);
77 function _build_removeListener() {
78 if (!browser
.isDOM2Event
) {
79 var _ie_removeListener = function(ob
, eventName
, listenerFunction
) {
80 eventName
= "on" + eventName
;
81 ob
.detachEvent(eventName
, listenerFunction
);
83 return _ie_removeListener
;
86 var _dom2_removeListener = function(ob
, eventName
, listenerFunction
) {
87 ob
.removeEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
89 return _dom2_removeListener
;
93 removeGroupListeners = function(group
) {
94 var listeners
= __groupListeners
[group
];
96 for (i
=0 ; i
<listeners
.length
; i
++){
98 removeListener(l
[0], l
[1], l
[2]);
100 __groupListeners
[group
] = null;
104 function _build_raiseMouseEvent() {
105 if (!browser
.isDOM2Event
) {
106 var _ie_raiseMouseEvent = function(ob
, eventName
) {
107 ob
.fireEvent("on" + eventName
);
109 return _ie_raiseMouseEvent
;
112 var _dom2_raiseMouseEvent = function(ob
, eventName
) {
113 var event
= document
.createEvent("MouseEvents");
114 event
.initEvent(eventName
, true, true);
115 ob
.dispatchEvent(event
);
117 return _dom2_raiseMouseEvent
;
121 function _build_getTargetedObject(){
122 if (!browser
.isDOM2Event
) {
123 var _ie_getTargetedObject = function() {
124 return window
.event
.srcElement
;
126 return _ie_getTargetedObject
;
129 var _appleWebKit_getTargetedeObject = function(evt
) {
130 var target
= evt
.target
;
131 // is it really safe ?...
132 return (target
.nodeType
=== 3) ? target
.parentNode
: target
;
134 var _dom2_getTargetedObject = function(evt
) {
137 return (browser
.isAppleWebKit
) ? _appleWebKit_getTargetedeObject
: _dom2_getTargetedObject
;
141 function _build_getEventObject(){
142 if (!browser
.isDOM2Event
) {
143 var _ie_getEventObject = function() {
146 return _ie_getEventObject
;
149 var _dom2_getEventObject = function(evt
) {
152 return _dom2_getEventObject
;
157 function _build_disableDefault(){
158 if (!browser
.isDOM2Event
) {
159 var _ie_disableDefault = function() {
160 window
.event
.returnValue
= false;
162 return _ie_disableDefault
;
165 var _dom2_disableDefault = function(evt
) {
166 evt
.preventDefault();
168 return _dom2_disableDefault
;
172 function _build_disablePropagation() {
173 if (!browser
.isDOM2Event
) {
174 var _ie_disablePropagation = function() {
175 window
.event
.cancelBubble
= true;
177 return _ie_disablePropagation
;
180 var _dom2_disablePropagation = function(evt
) {
181 evt
.stopPropagation();
183 return _dom2_disablePropagation
;
187 function _build_getWindowWidth() {
188 if (window
.innerWidth
!== undefined){
190 return window
.innerWidth
;
195 return document
.documentElement
.clientWidth
;
200 function _build_getWindowHeight() {
201 if (window
.innerHeight
!== undefined) {
203 return window
.innerHeight
;
208 return document
.documentElement
.clientHeight
;
213 function _build_clearSelection() {
214 if (document
.selection
) {
216 document
.selection
.clear();
221 window
.getSelection().removeAllRanges();
226 buildMetaFunctions();
228 addListener(window
, 'load', function(evt
) {
230 if (!document
.body
.classList
) {
231 var nop = function(){};
232 var fakeDOMTokenList
= {'length':0, 'item':nop
, 'contains':nop
, 'add':nop
, 'remove':nop
, 'toggle':nop
}
233 Element
.prototype.classList
= fakeDOMTokenList
;
239 var ELEMENT_NODE
= 1;
242 getCopyOfNode = function(node
) {
244 switch(node
.nodeType
) {
246 var attributes
= node
.attributes
;
247 var childs
= node
.childNodes
;
249 var e
= document
.createElement(node
.nodeName
);
252 for(i
=0 ; i
<attributes
.length
; i
++) {
253 attribute
= attributes
[i
];
254 _setAttribute(e
, attribute
.name
, attribute
.value
);
257 for(i
=0 ; i
<childs
.length
; i
++) {
258 e
.appendChild(getCopyOfNode(childs
[i
]));}
263 return document
.createTextNode(node
.nodeValue
);
268 _setAttribute = function(e
, name
, value
) {
269 // workarround IE lack of dom implementation.
270 switch(name
.toLowerCase()) {
278 loadCssText(e
, value
);
281 if (name
.slice(0,2) === 'on') { // event handler
282 // A browser normaly eval text code attached to a onXyz attribute. Not IE.
283 /*jslint evil: true */
284 e
[name
] = function(){eval(value
);};}
286 e
.setAttribute(name
, value
);}
289 var reCompoundPropName
= /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
290 var _capitalizeCssPropName = function (s
, g1
, g2
, g3
) { // gN args match above regexp groups
292 return g1
+ g2
.toUpperCase() + g3
;}
297 var loadCssText = function (e
, cssText
) {
298 var pairs
= cssText
.split(';');
299 var pair
, name
, value
, i
;
301 for (i
= 0; i
< pairs
.length
; i
++) {
302 pair
= pairs
[i
].split(':');
303 if (pair
.length
=== 2) {
304 name
= _capitalizeCssPropName(pair
[0]);
312 _setAttribute = function(e
, name
, value
) {e
.setAttribute(name
, value
);};
316 * http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
319 copyPrototype = function (descendant
, parent
) {
320 var sConstructor
= parent
.toString();
321 var aMatch
= sConstructor
.match( /\s*function (.*)\(/ );
322 if ( aMatch
!== null ) { descendant
.prototype[aMatch
[1]] = parent
; }
324 for (m
in parent
.prototype) {
325 if (parent
.prototype.hasOwnProperty(m
)) {
326 descendant
.prototype[m
] = parent
.prototype[m
]; }