/** * @license addEventListener polyfill 1.0 / Eirik Backer / MIT Licence * https://gist.github.com/2864711/946225eb3822c203e8d6218095d888aac5e1748e * * sounisi5011 version: * http://qiita.com/sounisi5011/items/a8fc80e075e4f767b79a#11 */ (function (window, document, listeners_prop_name) { if ((!window.addEventListener || !window.removeEventListener) && window.attachEvent && window.detachEvent) { /** * @param {*} value * @return {boolean} */ var is_callable = function (value) { return typeof value === 'function'; }; /** * @param {!Window|HTMLDocument|Node} self * @param {EventListener|function(!Event):(boolean|undefined)} listener * @return {!function(Event)|undefined} */ var listener_get = function (self, listener) { var listeners = listener[listeners_prop_name]; if (listeners) { var lis; var i = listeners.length; while (i--) { lis = listeners[i]; if (lis[0] === self) { return lis[1]; } } } }; /** * @param {!Window|HTMLDocument|Node} self * @param {EventListener|function(!Event):(boolean|undefined)} listener * @param {!function(Event)} callback * @return {!function(Event)} */ var listener_set = function (self, listener, callback) { var listeners = listener[listeners_prop_name] || (listener[listeners_prop_name] = []); return listener_get(self, listener) || (listeners[listeners.length] = [self, callback], callback); }; /** * @param {string} methodName */ var docHijack = function (methodName) { var old = document[methodName]; document[methodName] = function (v) { return addListen(old(v)); }; }; /** * @this {!Window|HTMLDocument|Node} * @param {string} type * @param {EventListener|function(!Event):(boolean|undefined)} listener * @param {boolean=} useCapture */ var addEvent = function (type, listener, useCapture) { if (is_callable(listener)) { var self = this; self.attachEvent( 'on' + type, listener_set(self, listener, function (e) { e = e || window.event; e.preventDefault = e.preventDefault || function () { e.returnValue = false }; e.stopPropagation = e.stopPropagation || function () { e.cancelBubble = true }; e.target = e.target || e.srcElement || document.documentElement; e.currentTarget = e.currentTarget || self; e.timeStamp = e.timeStamp || (new Date()).getTime(); listener.call(self, e); }) ); } }; /** * @this {!Window|HTMLDocument|Node} * @param {string} type * @param {EventListener|function(!Event):(boolean|undefined)} listener * @param {boolean=} useCapture */ var removeEvent = function (type, listener, useCapture) { if (is_callable(listener)) { var self = this; var lis = listener_get(self, listener); if (lis) { self.detachEvent('on' + type, lis); } } }; /** * @param {!Node|NodeList|Array} obj * @return {!Node|NodeList|Array} */ var addListen = function (obj) { var i = obj.length; if (i) { while (i--) { obj[i].addEventListener = addEvent; obj[i].removeEventListener = removeEvent; } } else { obj.addEventListener = addEvent; obj.removeEventListener = removeEvent; } return obj; }; addListen([document, window]); if ('Element' in window) { /** * IE8 */ var element = window.Element; element.prototype.addEventListener = addEvent; element.prototype.removeEventListener = removeEvent; } else { /** * IE < 8 */ //Make sure we also init at domReady document.attachEvent('onreadystatechange', function () { addListen(document.all) }); docHijack('getElementsByTagName'); docHijack('getElementById'); docHijack('createElement'); addListen(document.all); } } })(window, document, 'x-ms-event-listeners'); if (!Function.prototype.bind) { Function.prototype.bind = function (oThis) { if (typeof this !== "function") { throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function () {}, fBound = function () { return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))); }; fNOP.prototype = this.prototype; fBound.prototype = new fNOP(); return fBound; }; } (function ($) { /** * polyfill for Array */ var ArrayPrototype = Array.prototype; Array.isArray = $.isArray; ArrayPrototype.map = function (callback, thisArg) { var that = this; return $.map(that, function (value, index) { return callback.call(thisArg, value, index, that); }); }; ArrayPrototype.forEach = function (callback, thisArg) { this.map(callback, thisArg); }; ArrayPrototype.indexOf = function (searchElement, fromIndex) { var that = this; fromIndex = fromIndex * 1 || 0; var result = $(that.slice(fromIndex)).index(searchElement); return result > -1 ? result + fromIndex : result; }; ArrayPrototype.filter = function (callback, thisArg) { var that = this; return $.grep(that, function (value, index) { return callback.call(thisArg, value, index, that); }); }; ArrayPrototype.every = function (callback, thisArg) { var that = this; return that.filter(callback, thisArg).length == that.length; }; ArrayPrototype.some = function (callback, thisArg) { return this.filter(callback, thisArg).length > 0; }; /** * polyfill for String */ String.prototype.trim = function () { return $.trim(this); }; /** * polyfill for Date */ Date.now = function () { return +new Date(); }; /** * polyfill for Object */ Object.keys = function (o) { if (o !== Object(o)) { throw new TypeError("Object.keys called on a non-object"); } var k = [], p; for (p in o) { if (Object.prototype.hasOwnProperty.call(o, p)) { k.push(p); } } return k; }; })(jQuery);