Fixed object extending and event triggering in `Core`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 24 Jun 2015 17:24:19 +0000 (19:24 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 24 Jun 2015 17:24:19 +0000 (19:24 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Core.js

index eab4c836d84028c700a59f0f01fcd8f1ecf94613..da79e3743829e8a4ebb75ce0982bd74a3a5681fa 100644 (file)
@@ -10,24 +10,20 @@ define([], function() {
        "use strict";
        
        var _clone = function(variable) {
-               if (typeof variable === 'object' && variable !== null) {
+               if (typeof variable === 'object' && (Array.isArray(variable) || Core.isPlainObject(variable))) {
                        return _cloneObject(variable);
                }
                
                return variable;
        };
        
-       var _cloneArray = function(oldArray) {
-               return oldArray.slice();
-       };
-       
        var _cloneObject = function(obj) {
                if (!obj) {
                        return null;
                }
                
                if (Array.isArray(obj)) {
-                       return _cloneArray(obj);
+                       return obj.slice();
                }
                
                var newObj = {};
@@ -181,16 +177,27 @@ define([], function() {
                        return parameters.join('&');
                },
                
-               triggerEvent: function(el, eventName) {
-                       var ev;
-                       if (document.createEvent) {
-                               ev = new Event(eventName);
-                               el.dispatchEvent(ev);
+               /**
+                * Triggers a custom or built-in event.
+                * 
+                * @param       {Element}       element         target element
+                * @param       {string}        eventName       event name
+                */
+               triggerEvent: function(element, eventName) {
+                       var event;
+                       
+                       try {
+                               event = new Event(eventName, {
+                                       bubbles: true,
+                                       cancelable: true
+                               });
                        }
-                       else {
-                               ev = document.createEventObject();
-                               el.fireEvent('on' + eventName, ev);
+                       catch (e) {
+                               event = document.createEvent('Event');
+                               event.initEvent(eventName, true, true);
                        }
+                       
+                       element.dispatchEvent(event);
                }
        };