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
) {
258 if (!document
.body
.classList
) {
259 var nop = function(){};
260 var fakeDOMTokenList
= {'length':0, 'item':nop
, 'contains':nop
, 'add':nop
, 'remove':nop
, 'toggle':nop
};
261 Element
.prototype.classList
= fakeDOMTokenList
;
267 var ELEMENT_NODE
= 1;
270 getCopyOfNode = function(node
) {
272 switch(node
.nodeType
) {
274 var attributes
= node
.attributes
;
275 var childs
= node
.childNodes
;
277 var e
= document
.createElement(node
.nodeName
);
280 for(i
=0 ; i
<attributes
.length
; i
++) {
281 attribute
= attributes
[i
];
282 _setAttribute(e
, attribute
.name
, attribute
.value
);
285 for(i
=0 ; i
<childs
.length
; i
++) {
286 e
.appendChild(getCopyOfNode(childs
[i
]));}
291 return document
.createTextNode(node
.nodeValue
);
295 if (browser
.isIE7max
) {
296 _setAttribute = function(e
, name
, value
) {
297 // workarround IE lack of dom implementation.
298 switch(name
.toLowerCase()) {
306 loadCssText(e
, value
);
309 if (name
.slice(0,2) === 'on') { // event handler
310 // A browser normaly eval text code attached to a onXyz attribute. Not IE.
311 /*jslint evil: true */
312 e
[name
] = function(){eval(value
);};}
314 e
.setAttribute(name
, value
);}
317 var reCompoundPropName
= /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
318 var _capitalizeCssPropName = function (s
) {
319 var g
= reCompoundPropName
.exec(s
);
321 return g
[1] + g
[2].toUpperCase() + g
[3];}
326 var loadCssText = function (e
, cssText
) {
327 var pairs
= cssText
.split(';');
328 var pair
, name
, value
, i
;
330 for (i
= 0; i
< pairs
.length
; i
++) {
331 pair
= pairs
[i
].split(':');
332 if (pair
.length
=== 2) {
333 name
= _capitalizeCssPropName(pair
[0]);
341 _setAttribute = function(e
, name
, value
) {e
.setAttribute(name
, value
);};
345 * http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
348 copyPrototype = function (descendant
, parent
) {
349 var sConstructor
= parent
.toString();
350 var aMatch
= sConstructor
.match( /\s*function (.*)\(/ );
351 if ( aMatch
!== null ) { descendant
.prototype[aMatch
[1]] = parent
; }
353 for (m
in parent
.prototype) {
354 if (parent
.prototype.hasOwnProperty(m
)) {
355 descendant
.prototype[m
] = parent
.prototype[m
]; }