1 // (c) Benoît PIN 2006-2014
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 getWindowScrollX
= _build_getWindowScrollX();
49 getWindowScrollY
= _build_getWindowScrollY();
50 clearSelection
= _build_clearSelection();
53 var __groupListeners
= {};
55 function _build_addListener() {
57 if (!browser
.isDOM2Event
) {
58 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
59 eventName
= "on" + eventName
;
60 ob
.attachEvent(eventName
, listenerFunction
);
64 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
65 ob
.addEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
68 var common = function(ob
, eventName
, listenerFunction
, group
) {
69 _browserSpecific(ob
, eventName
, listenerFunction
);
71 if(!__groupListeners
[group
]) {
72 __groupListeners
[group
] = [];}
73 __groupListeners
[group
].push([ob
, eventName
, listenerFunction
]);
79 function _build_removeListener() {
80 if (!browser
.isDOM2Event
) {
81 var _ie_removeListener = function(ob
, eventName
, listenerFunction
) {
82 eventName
= "on" + eventName
;
83 ob
.detachEvent(eventName
, listenerFunction
);
85 return _ie_removeListener
;
88 var _dom2_removeListener = function(ob
, eventName
, listenerFunction
) {
89 ob
.removeEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
91 return _dom2_removeListener
;
95 removeGroupListeners = function(group
) {
96 var listeners
= __groupListeners
[group
];
98 for (i
=0 ; i
<listeners
.length
; i
++){
100 removeListener(l
[0], l
[1], l
[2]);
102 __groupListeners
[group
] = null;
106 function _build_raiseMouseEvent() {
107 if (!browser
.isDOM2Event
) {
108 var _ie_raiseMouseEvent = function(ob
, eventName
) {
109 ob
.fireEvent("on" + eventName
);
111 return _ie_raiseMouseEvent
;
114 var _dom2_raiseMouseEvent = function(ob
, eventName
) {
115 var event
= document
.createEvent("MouseEvents");
116 event
.initEvent(eventName
, true, true);
117 ob
.dispatchEvent(event
);
119 return _dom2_raiseMouseEvent
;
123 function _build_getTargetedObject(){
124 if (!browser
.isDOM2Event
) {
125 var _ie_getTargetedObject = function() {
126 return window
.event
.srcElement
;
128 return _ie_getTargetedObject
;
131 var _appleWebKit_getTargetedeObject = function(evt
) {
132 var target
= evt
.target
;
133 // is it really safe ?...
134 return (target
.nodeType
=== 3) ? target
.parentNode
: target
;
136 var _dom2_getTargetedObject = function(evt
) {
139 return (browser
.isAppleWebKit
) ? _appleWebKit_getTargetedeObject
: _dom2_getTargetedObject
;
143 function _build_getEventObject(){
144 if (!browser
.isDOM2Event
) {
145 var _ie_getEventObject = function() {
148 return _ie_getEventObject
;
151 var _dom2_getEventObject = function(evt
) {
154 return _dom2_getEventObject
;
159 function _build_disableDefault(){
160 if (!browser
.isDOM2Event
) {
161 var _ie_disableDefault = function() {
162 window
.event
.returnValue
= false;
164 return _ie_disableDefault
;
167 var _dom2_disableDefault = function(evt
) {
168 evt
.preventDefault();
170 return _dom2_disableDefault
;
174 function _build_disablePropagation() {
175 if (!browser
.isDOM2Event
) {
176 var _ie_disablePropagation = function() {
177 window
.event
.cancelBubble
= true;
179 return _ie_disablePropagation
;
182 var _dom2_disablePropagation = function(evt
) {
183 evt
.stopPropagation();
185 return _dom2_disablePropagation
;
189 function _build_getWindowWidth() {
190 if (window
.innerWidth
!== undefined){
192 return window
.innerWidth
;
197 return document
.documentElement
.clientWidth
;
202 function _build_getWindowHeight() {
203 if (window
.innerHeight
!== undefined) {
205 return window
.innerHeight
;
210 return document
.documentElement
.clientHeight
;
215 function _build_getWindowScrollX() {
216 if (window
.scrollX
!== undefined) {
218 return window
.scrollX
;
223 return document
.documentElement
.scrollLeft
;
228 function _build_getWindowScrollY() {
229 if (window
.scrollY
!== undefined) {
231 return window
.scrollY
;
236 return document
.documentElement
.scrollTop
;
241 function _build_clearSelection() {
242 if (document
.selection
) {
244 document
.selection
.clear();
249 window
.getSelection().removeAllRanges();
254 buildMetaFunctions();
256 addListener(window
, 'load', function(evt
) {
259 if (!document
.body
.classList
) {
260 var nop = function(){};
261 var fakeDOMTokenList
= {'length':0, 'item':nop
, 'contains':nop
, 'add':nop
, 'remove':nop
, 'toggle':nop
};
262 Element
.prototype.classList
= fakeDOMTokenList
;
270 var ELEMENT_NODE
= 1;
273 getCopyOfNode = function(node
) {
275 switch(node
.nodeType
) {
277 var attributes
= node
.attributes
;
278 var childs
= node
.childNodes
;
280 var e
= document
.createElement(node
.nodeName
);
283 for(i
=0 ; i
<attributes
.length
; i
++) {
284 attribute
= attributes
[i
];
285 _setAttribute(e
, attribute
.name
, attribute
.value
);
288 for(i
=0 ; i
<childs
.length
; i
++) {
289 e
.appendChild(getCopyOfNode(childs
[i
]));}
294 return document
.createTextNode(node
.nodeValue
);
298 if (browser
.isIE7max
) {
299 _setAttribute = function(e
, name
, value
) {
300 // workarround IE lack of dom implementation.
301 switch(name
.toLowerCase()) {
309 loadCssText(e
, value
);
312 if (name
.slice(0,2) === 'on') { // event handler
313 // A browser normaly eval text code attached to a onXyz attribute. Not IE.
314 /*jslint evil: true */
315 e
[name
] = function(){eval(value
);};}
317 e
.setAttribute(name
, value
);}
320 var reCompoundPropName
= /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
321 var _capitalizeCssPropName = function (s
) {
322 var g
= reCompoundPropName
.exec(s
);
324 return g
[1] + g
[2].toUpperCase() + g
[3];}
329 var loadCssText = function (e
, cssText
) {
330 var pairs
= cssText
.split(';');
331 var pair
, name
, value
, i
;
333 for (i
= 0; i
< pairs
.length
; i
++) {
334 pair
= pairs
[i
].split(':');
335 if (pair
.length
=== 2) {
336 name
= _capitalizeCssPropName(pair
[0]);
344 _setAttribute = function(e
, name
, value
) {e
.setAttribute(name
, value
);};
348 * http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
351 copyPrototype = function (descendant
, parent
) {
352 var sConstructor
= parent
.toString();
353 var aMatch
= sConstructor
.match( /\s*function (.*)\(/ );
354 if ( aMatch
!== null ) { descendant
.prototype[aMatch
[1]] = parent
; }
356 for (m
in parent
.prototype) {
357 if (parent
.prototype.hasOwnProperty(m
)) {
358 descendant
.prototype[m
] = parent
.prototype[m
]; }