Déplacement du slot 'base' pour une utilisation plus commode par js.
[Plinn.git] / skins / ajax_scripts / javascript_events_api.js
index 712a44f..71baec7 100644 (file)
@@ -1,8 +1,8 @@
 // (c) Benoît PIN 2006-2009
 // http://plinn.org
 // Licence GPL
 // (c) Benoît PIN 2006-2009
 // http://plinn.org
 // Licence GPL
-// $Id: javascript_events_api.js 1535 2009-10-21 16:56:13Z pin $
-// $URL: http://svn.cri.ensmp.fr/svn/Plinn/branches/CMF-2.1/skins/ajax_scripts/javascript_events_api.js $
+// 
+// 
 // Meta functions for events management.
 
 var addListener; /* (ob, eventName, listenerFunction, group) add event listener eventName without "on" prefix.
 // Meta functions for events management.
 
 var addListener; /* (ob, eventName, listenerFunction, group) add event listener eventName without "on" prefix.
@@ -26,6 +26,7 @@ var getCopyOfNode; /* (node) returns a clone of the given node.
                                                * the node came from a foreign document (eg. XmlHttpRequest xml reponse)
                                                * to inject HMTL code inside tags where innerHtml is read only (IE)
                                        */
                                                * the node came from a foreign document (eg. XmlHttpRequest xml reponse)
                                                * to inject HMTL code inside tags where innerHtml is read only (IE)
                                        */
+var copyPrototype; // (descendant, parent) lightwheight javascript inheritance
 
 (function(){
 
 
 (function(){
 
@@ -61,11 +62,11 @@ function _build_addListener() {
        var common = function(ob, eventName, listenerFunction, group) {
                _browserSpecific(ob, eventName, listenerFunction);
                if (group) {
        var common = function(ob, eventName, listenerFunction, group) {
                _browserSpecific(ob, eventName, listenerFunction);
                if (group) {
-                       if(!__groupListeners[group])
-                               __groupListeners[group] = new Array();
+                       if(!__groupListeners[group]) {
+                               __groupListeners[group] = [];}
                        __groupListeners[group].push([ob, eventName, listenerFunction]);
                }
                        __groupListeners[group].push([ob, eventName, listenerFunction]);
                }
-       }
+       };
        return common;
 }
 
        return common;
 }
 
@@ -74,33 +75,33 @@ function _build_removeListener() {
                var _ie_removeListener = function(ob, eventName, listenerFunction) {
                        eventName = "on" + eventName;
                        ob.detachEvent(eventName, listenerFunction);
                var _ie_removeListener = function(ob, eventName, listenerFunction) {
                        eventName = "on" + eventName;
                        ob.detachEvent(eventName, listenerFunction);
-               }
+               };
                return _ie_removeListener;
        }
        else if (browser.isDOM2Event) {
                var _dom2_removeListener = function(ob, eventName, listenerFunction) {
                        ob.removeEventListener(eventName, listenerFunction, false); // only bubbling events :-(
                return _ie_removeListener;
        }
        else if (browser.isDOM2Event) {
                var _dom2_removeListener = function(ob, eventName, listenerFunction) {
                        ob.removeEventListener(eventName, listenerFunction, false); // only bubbling events :-(
-               }
+               };
                return _dom2_removeListener;
        }
 }
 
 removeGroupListeners = function(group) {
        var listeners = __groupListeners[group];
                return _dom2_removeListener;
        }
 }
 
 removeGroupListeners = function(group) {
        var listeners = __groupListeners[group];
-       var l;
-       for (var i=0 ; i<listeners.length ; i++){
+       var l, i;
+       for (i=0 ; i<listeners.length ; i++){
                l = listeners[i];
                l = listeners[i];
-               removeListener(l[0], l[1], l[2])
+               removeListener(l[0], l[1], l[2]);
        }
        __groupListeners[group] = null;
                
        }
        __groupListeners[group] = null;
                
-}
+};
 
 function  _build_raiseMouseEvent() {
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_raiseMouseEvent = function(ob, eventName) {
                        ob.fireEvent("on" + eventName);
 
 function  _build_raiseMouseEvent() {
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_raiseMouseEvent = function(ob, eventName) {
                        ob.fireEvent("on" + eventName);
-               }
+               };
                return _ie_raiseMouseEvent;
        }
        else if (browser.isDOM2Event) {
                return _ie_raiseMouseEvent;
        }
        else if (browser.isDOM2Event) {
@@ -108,7 +109,7 @@ function  _build_raiseMouseEvent() {
                        var event = document.createEvent("MouseEvents");
                        event.initEvent(eventName, true, true);
                        ob.dispatchEvent(event);
                        var event = document.createEvent("MouseEvents");
                        event.initEvent(eventName, true, true);
                        ob.dispatchEvent(event);
-               }
+               };
                return _dom2_raiseMouseEvent;
        }
 }
                return _dom2_raiseMouseEvent;
        }
 }
@@ -117,18 +118,18 @@ function _build_getTargetedObject(){
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_getTargetedObject = function() {
                        return window.event.srcElement;
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_getTargetedObject = function() {
                        return window.event.srcElement;
-               }
+               };
                return _ie_getTargetedObject;
        }
        else if (browser.isDOM2Event) {
                var _appleWebKit_getTargetedeObject = function(evt) {
                        var target = evt.target;
                        // is it really safe ?...
                return _ie_getTargetedObject;
        }
        else if (browser.isDOM2Event) {
                var _appleWebKit_getTargetedeObject = function(evt) {
                        var target = evt.target;
                        // is it really safe ?...
-                       return (target.nodeType == 3) ? target.parentNode : target;
-               }
+                       return (target.nodeType === 3) ? target.parentNode : target;
+               };
                var _dom2_getTargetedObject = function(evt) {
                var _dom2_getTargetedObject = function(evt) {
-                       return evt.target
-               }
+                       return evt.target;
+               };
                return (browser.isAppleWebKit) ? _appleWebKit_getTargetedeObject : _dom2_getTargetedObject;
        }
 }
                return (browser.isAppleWebKit) ? _appleWebKit_getTargetedeObject : _dom2_getTargetedObject;
        }
 }
@@ -137,13 +138,13 @@ function _build_getEventObject(){
        if (browser.isIE) {
                var _ie_getEventObject = function() {
                        return window.event;
        if (browser.isIE) {
                var _ie_getEventObject = function() {
                        return window.event;
-               }
+               };
                return _ie_getEventObject;
        }
        else if (browser.isDOM2Event) {
                var _dom2_getEventObject = function(evt) {
                        return evt;
                return _ie_getEventObject;
        }
        else if (browser.isDOM2Event) {
                var _dom2_getEventObject = function(evt) {
                        return evt;
-               }
+               };
                return _dom2_getEventObject;
        }
 }
                return _dom2_getEventObject;
        }
 }
@@ -153,13 +154,13 @@ function _build_disableDefault(){
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_disableDefault = function() {
                        window.event.returnValue = false;
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_disableDefault = function() {
                        window.event.returnValue = false;
-               }
+               };
                return _ie_disableDefault;
        }
        else if (browser.isDOM2Event) {
                var _dom2_disableDefault = function(evt) {
                        evt.preventDefault();
                return _ie_disableDefault;
        }
        else if (browser.isDOM2Event) {
                var _dom2_disableDefault = function(evt) {
                        evt.preventDefault();
-               }
+               };
                return _dom2_disableDefault;
        }
 }
                return _dom2_disableDefault;
        }
 }
@@ -168,19 +169,19 @@ function _build_disablePropagation() {
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_disablePropagation = function() {
                        window.event.cancelBubble = true;
        if (browser.isIE55 || browser.isIE6up) {
                var _ie_disablePropagation = function() {
                        window.event.cancelBubble = true;
-               }
+               };
                return _ie_disablePropagation;
        }
        else if (browser.isDOM2Event) {
                var _dom2_disablePropagation = function(evt) {
                        evt.stopPropagation();
                return _ie_disablePropagation;
        }
        else if (browser.isDOM2Event) {
                var _dom2_disablePropagation = function(evt) {
                        evt.stopPropagation();
-               }
+               };
                return _dom2_disablePropagation;
        }
 }
 
                return _dom2_disablePropagation;
        }
 }
 
-function _build_getWindowWidth() {
-       if (window.innerWidth != undefined){
+function _build_getWindowWidth() {
+       if (window.innerWidth !== undefined){
                return function(){
                        return window.innerWidth;
                        };
                return function(){
                        return window.innerWidth;
                        };
@@ -193,7 +194,7 @@ function _build_getWindowWidth() {
 }
 
 function _build_getWindowHeight() {
 }
 
 function _build_getWindowHeight() {
-       if (window.innerHeight != undefined) {
+       if (window.innerHeight !== undefined) {
                return function(){
                        return window.innerHeight;
                };
                return function(){
                        return window.innerHeight;
                };
@@ -214,7 +215,7 @@ function _build_clearSelection() {
        else {
                return function() {
                        window.getSelection().removeAllRanges();
        else {
                return function() {
                        window.getSelection().removeAllRanges();
-               }
+               };
        }
 }
 
        }
 }
 
@@ -233,22 +234,21 @@ getCopyOfNode = function(node) {
        
                        var e = document.createElement(node.nodeName);
 
        
                        var e = document.createElement(node.nodeName);
 
-                       var attribute;
-                       for(var i=0 ; i<attributes.length ; i++) {
+                       var attribute, i;
+                       for(i=0 ; i<attributes.length ; i++) {
                                attribute = attributes[i];
                                _setAttribute(e, attribute.name, attribute.value);
                        }
                        
                                attribute = attributes[i];
                                _setAttribute(e, attribute.name, attribute.value);
                        }
                        
-                       for(var i=0 ; i<childs.length ; i++)
-                               e.appendChild(getCopyOfNode(childs[i]));
+                       for(i=0 ; i<childs.length ; i++) {
+                               e.appendChild(getCopyOfNode(childs[i]));}
                        
                        return e;
                        
                        return e;
-                       break;
+
                case TEXT_NODE:
                        return document.createTextNode(node.nodeValue);
                case TEXT_NODE:
                        return document.createTextNode(node.nodeValue);
-                       break;
        }
        }
-}
+};
 
 if (browser.isIE) {
        _setAttribute = function(e, name, value) {
 
 if (browser.isIE) {
        _setAttribute = function(e, name, value) {
@@ -264,36 +264,53 @@ if (browser.isIE) {
                                loadCssText(e, value);
                                break;
                        default:
                                loadCssText(e, value);
                                break;
                        default:
-                               if (name.slice(0,2) == 'on') // event handler
-                                       e[name] = function(){eval(value);};
-                               else
-                                       e.setAttribute(name, value);
+                               if (name.slice(0,2) === 'on') { // event handler
+                                       // A browser normaly eval text code attached to a onXyz attribute. Not IE.
+                                       /*jslint evil: true */
+                                       e[name] = function(){eval(value);};}
+                               else {
+                                       e.setAttribute(name, value);}
                }
        };
        var reCompoundPropName = /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
                }
        };
        var reCompoundPropName = /^\s*([^\-]+)\-([a-z])([a-z]+)\s*$/;
-       function _capitalizeCssPropName(s, g1, g2, g3) { // gN args match above regexp groups 
-               if(g2)
-                       return g1 + g2.toUpperCase() + g3;
-               else
-                       return s;
-       }
+       var _capitalizeCssPropName = function (s, g1, g2, g3) { // gN args match above regexp groups 
+               if(g2) {
+                       return g1 + g2.toUpperCase() + g3;}
+               else {
+                       return s;}
+       };
 
 
-       function loadCssText(e, cssText) {
+       var loadCssText = function (e, cssText) {
                var pairs = cssText.split(';');
                var pairs = cssText.split(';');
-               var pair, name, value;
+               var pair, name, value, i;
                var style = e.style;
                var style = e.style;
-               for (var i= 0; i < pairs.length; i++) {
+               for (i= 0; i < pairs.length; i++) {
                        pair = pairs[i].split(':');
                        pair = pairs[i].split(':');
-                       if (pair.length != 2)
-                               continue;
-                       name = _capitalizeCssPropName(pair[0]);
-                       value = pair[1];
-                       style[name] = value;
+                       if (pair.length === 2) {
+                               name = _capitalizeCssPropName(pair[0]);
+                               value = pair[1];
+                               style[name] = value;
+                       }
                }
                }
-       }
+       };
 }
 else {
        _setAttribute = function(e, name, value) {e.setAttribute(name, value);};
 }
 
 }
 else {
        _setAttribute = function(e, name, value) {e.setAttribute(name, value);};
 }
 
-})();
+/* 
+* http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
+*/
+
+copyPrototype = function (descendant, parent) { 
+       var sConstructor = parent.toString(); 
+       var aMatch = sConstructor.match( /\s*function (.*)\(/ );
+       if ( aMatch !== null ) { descendant.prototype[aMatch[1]] = parent; }
+       var m;
+       for (m in parent.prototype) {
+               if (parent.prototype.hasOwnProperty(m)) {
+                       descendant.prototype[m] = parent.prototype[m]; }
+       }
+};
+
+}());