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
30 if (!history
.pushState
) {
31 history
.pushState = function(){};
36 function buildMetaFunctions() {
37 addListener
= _build_addListener();
38 removeListener
= _build_removeListener();
39 raiseMouseEvent
= _build_raiseMouseEvent();
41 getTargetedObject
= _build_getTargetedObject();
42 getEventObject
= _build_getEventObject();
43 disableDefault
= _build_disableDefault();
44 disablePropagation
= _build_disablePropagation();
45 getWindowWidth
= _build_getWindowWidth();
46 getWindowHeight
= _build_getWindowHeight();
47 clearSelection
= _build_clearSelection();
50 var __groupListeners
= {};
52 function _build_addListener() {
54 if (!browser
.isDOM2Event
) {
55 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
56 eventName
= "on" + eventName
;
57 ob
.attachEvent(eventName
, listenerFunction
);
61 _browserSpecific = function(ob
, eventName
, listenerFunction
) {
62 ob
.addEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
65 var common = function(ob
, eventName
, listenerFunction
, group
) {
66 _browserSpecific(ob
, eventName
, listenerFunction
);
68 if(!__groupListeners
[group
]) {
69 __groupListeners
[group
] = [];}
70 __groupListeners
[group
].push([ob
, eventName
, listenerFunction
]);
76 function _build_removeListener() {
77 if (!browser
.isDOM2Event
) {
78 var _ie_removeListener = function(ob
, eventName
, listenerFunction
) {
79 eventName
= "on" + eventName
;
80 ob
.detachEvent(eventName
, listenerFunction
);
82 return _ie_removeListener
;
85 var _dom2_removeListener = function(ob
, eventName
, listenerFunction
) {
86 ob
.removeEventListener(eventName
, listenerFunction
, false); // only bubbling events :-(
88 return _dom2_removeListener
;
92 removeGroupListeners = function(group
) {
93 var listeners
= __groupListeners
[group
];
95 for (i
=0 ; i
<listeners
.length
; i
++){
97 removeListener(l
[0], l
[1], l
[2]);
99 __groupListeners
[group
] = null;
103 function _build_raiseMouseEvent() {
104 if (!browser
.isDOM2Event
) {
105 var _ie_raiseMouseEvent = function(ob
, eventName
) {
106 ob
.fireEvent("on" + eventName
);
108 return _ie_raiseMouseEvent
;
111 var _dom2_raiseMouseEvent = function(ob
, eventName
) {
112 var event
= document
.createEvent("MouseEvents");
113 event
.initEvent(eventName
, true, true);
114 ob
.dispatchEvent(event
);
116 return _dom2_raiseMouseEvent
;
120 function _build_getTargetedObject(){
121 if (!browser
.isDOM2Event
) {
122 var _ie_getTargetedObject = function() {
123 return window
.event
.srcElement
;
125 return _ie_getTargetedObject
;
128 var _appleWebKit_getTargetedeObject = function(evt
) {
129 var target
= evt
.target
;
130 // is it really safe ?...
131 return (target
.nodeType
=== 3) ? target
.parentNode
: target
;
133 var _dom2_getTargetedObject = function(evt
) {
136 return (browser
.isAppleWebKit
) ? _appleWebKit_getTargetedeObject
: _dom2_getTargetedObject
;
140 function _build_getEventObject(){
141 if (!browser
.isDOM2Event
) {
142 var _ie_getEventObject = function() {
145 return _ie_getEventObject
;
148 var _dom2_getEventObject = function(evt
) {
151 return _dom2_getEventObject
;
156 function _build_disableDefault(){
157 if (!browser
.isDOM2Event
) {
158 var _ie_disableDefault = function() {
159 window
.event
.returnValue
= false;
161 return _ie_disableDefault
;
164 var _dom2_disableDefault = function(evt
) {
165 evt
.preventDefault();
167 return _dom2_disableDefault
;
171 function _build_disablePropagation() {
172 if (!browser
.isDOM2Event
) {
173 var _ie_disablePropagation = function() {
174 window
.event
.cancelBubble
= true;
176 return _ie_disablePropagation
;
179 var _dom2_disablePropagation = function(evt
) {
180 evt
.stopPropagation();
182 return _dom2_disablePropagation
;
186 function _build_getWindowWidth() {
187 if (window
.innerWidth
!== undefined){
189 return window
.innerWidth
;
194 return document
.documentElement
.clientWidth
;
199 function _build_getWindowHeight() {
200 if (window
.innerHeight
!== undefined) {
202 return window
.innerHeight
;
207 return document
.documentElement
.clientHeight
;
212 function _build_clearSelection() {
213 if (document
.selection
) {
215 document
.selection
.clear();
220 window
.getSelection().removeAllRanges();
226 buildMetaFunctions();
228 var ELEMENT_NODE
= 1;
231 getCopyOfNode = function(node
) {
233 switch(node
.nodeType
) {
235 var attributes
= node
.attributes
;
236 var childs
= node
.childNodes
;
238 var e
= document
.createElement(node
.nodeName
);
241 for(i
=0 ; i
<attributes
.length
; i
++) {
242 attribute
= attributes
[i
];
243 _setAttribute(e
, attribute
.name
, attribute
.value
);
246 for(i
=0 ; i
<childs
.length
; i
++) {
247 e
.appendChild(getCopyOfNode(childs
[i
]));}
252 return document
.createTextNode(node
.nodeValue
);
257 _setAttribute = function(e
, name
, value
) {
258 // workarround IE lack of dom implementation.
259 switch(name
.toLowerCase()) {
267 loadCssText(e
, value
);
270 if (name
.slice(0,2) === 'on') { // event handler
271 // A browser normaly eval text code attached to a onXyz attribute. Not IE.
272 /*jslint evil: true */
273 e
[name
] = function(){eval(value
);};}
275 e
.setAttribute(name
, value
);}
278 var reCompoundPropName
= /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
279 var _capitalizeCssPropName = function (s
, g1
, g2
, g3
) { // gN args match above regexp groups
281 return g1
+ g2
.toUpperCase() + g3
;}
286 var loadCssText = function (e
, cssText
) {
287 var pairs
= cssText
.split(';');
288 var pair
, name
, value
, i
;
290 for (i
= 0; i
< pairs
.length
; i
++) {
291 pair
= pairs
[i
].split(':');
292 if (pair
.length
=== 2) {
293 name
= _capitalizeCssPropName(pair
[0]);
301 _setAttribute = function(e
, name
, value
) {e
.setAttribute(name
, value
);};
305 * http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
308 copyPrototype = function (descendant
, parent
) {
309 var sConstructor
= parent
.toString();
310 var aMatch
= sConstructor
.match( /\s*function (.*)\(/ );
311 if ( aMatch
!== null ) { descendant
.prototype[aMatch
[1]] = parent
; }
313 for (m
in parent
.prototype) {
314 if (parent
.prototype.hasOwnProperty(m
)) {
315 descendant
.prototype[m
] = parent
.prototype[m
]; }