event-target-shim
Advanced tools
Comparing version 1.0.5 to 1.0.6
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.eventTargetShim = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.defineCustomEventTarget = defineCustomEventTarget; | ||
/** | ||
* Creates a unique key. | ||
* | ||
* @param {string} name - A name to create. | ||
* @returns {symbol|string} | ||
* @private | ||
*/ | ||
var createUniqueKey = exports.createUniqueKey = (typeof Symbol !== "undefined" ? | ||
Symbol : | ||
function createUniqueKey(name) { | ||
return "[[" + name + "_" + Math.random().toFixed(8).slice(2) + "]]"; | ||
}); | ||
var _commons = require("./commons"); | ||
/** | ||
* The key of listeners. | ||
* | ||
* @type {symbol|string} | ||
* @private | ||
*/ | ||
exports.LISTENERS = createUniqueKey("listeners"); | ||
/** | ||
* @typedef object ListenerNode | ||
* @property {function} listener - A listener function. | ||
* @property {number} kind - The kind of the listener. | ||
* @property {ListenerNode|null} next - The next node. | ||
* If this node is the last, this is `null`. | ||
*/ | ||
/** | ||
* Creates a node of singly linked list for a list of listeners. | ||
* | ||
* @param {function} listener - A listener function. | ||
* @param {number} kind - The kind of the listener. | ||
* @returns {ListenerNode} The created listener node. | ||
*/ | ||
exports.newNode = function newNode(listener, kind) { | ||
return {listener: listener, kind: kind, next: null}; | ||
}; | ||
},{}],2:[function(require,module,exports){ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict"; | ||
//----------------------------------------------------------------------------- | ||
// Requirements | ||
//----------------------------------------------------------------------------- | ||
var Commons = require("./commons"); | ||
var LISTENERS = Commons.LISTENERS; | ||
var newNode = Commons.newNode; | ||
//----------------------------------------------------------------------------- | ||
// Constants | ||
//----------------------------------------------------------------------------- | ||
/** | ||
* A value of kind for listeners which are registered as an attribute. | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
var ATTRIBUTE = 3; | ||
//----------------------------------------------------------------------------- | ||
// Helpers | ||
//----------------------------------------------------------------------------- | ||
/** | ||
* Gets a specified attribute listener from a given EventTarget object. | ||
* | ||
* @param {EventTarget} eventTarget - An EventTarget object to get. | ||
* @param {string} type - An event type to get. | ||
* @returns {function|null} The found attribute listener. | ||
*/ | ||
function getAttributeListener(eventTarget, type) { | ||
var node = eventTarget[_commons.LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === _commons.ATTRIBUTE) { | ||
return node.listener; | ||
var node = eventTarget[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === ATTRIBUTE) { | ||
return node.listener; | ||
} | ||
node = node.next; | ||
} | ||
node = node.next; | ||
} | ||
return null; | ||
return null; | ||
} | ||
/** | ||
* Sets a specified attribute listener to a given EventTarget object. | ||
* | ||
* @param {EventTarget} eventTarget - An EventTarget object to set. | ||
* @param {string} type - An event type to set. | ||
* @param {function|null} listener - A listener to be set. | ||
* @returns {void} | ||
*/ | ||
function setAttributeListener(eventTarget, type, listener) { | ||
if (listener != null && typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
var prev = null; | ||
var node = eventTarget[_commons.LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === _commons.ATTRIBUTE) { | ||
// Remove old value. | ||
if (prev == null) { | ||
eventTarget[_commons.LISTENERS][type] = node.next; | ||
} else { | ||
prev.next = node.next; | ||
} | ||
} else { | ||
prev = node; | ||
if (listener != null && typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
node = node.next; | ||
} | ||
var prev = null; | ||
var node = eventTarget[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.kind === ATTRIBUTE) { | ||
// Remove old value. | ||
if (prev == null) { | ||
eventTarget[LISTENERS][type] = node.next; | ||
} | ||
else { | ||
prev.next = node.next; | ||
} | ||
} | ||
else { | ||
prev = node; | ||
} | ||
// Add new value. | ||
if (listener != null) { | ||
if (prev == null) { | ||
eventTarget[_commons.LISTENERS][type] = (0, _commons.newNode)(listener, _commons.ATTRIBUTE); | ||
} else { | ||
prev.next = (0, _commons.newNode)(listener, _commons.ATTRIBUTE); | ||
node = node.next; | ||
} | ||
} | ||
// Add new value. | ||
if (listener != null) { | ||
if (prev == null) { | ||
eventTarget[LISTENERS][type] = newNode(listener, ATTRIBUTE); | ||
} | ||
else { | ||
prev.next = newNode(listener, ATTRIBUTE); | ||
} | ||
} | ||
} | ||
function defineCustomEventTarget(EventTargetBase, types) { | ||
function EventTarget() { | ||
EventTargetBase.call(this); | ||
} | ||
//----------------------------------------------------------------------------- | ||
// Public Interface | ||
//----------------------------------------------------------------------------- | ||
var descripter = { | ||
constructor: { | ||
value: EventTarget, | ||
configurable: true, | ||
writable: true | ||
/** | ||
* Defines an `EventTarget` implementation which has `onfoobar` attributes. | ||
* | ||
* @param {EventTarget} EventTargetBase - A base implementation of EventTarget. | ||
* @param {string[]} types - A list of event types which are defined as attribute listeners. | ||
* @returns {EventTarget} The defined `EventTarget` implementation which has attribute listeners. | ||
*/ | ||
exports.defineCustomEventTarget = function(EventTargetBase, types) { | ||
function EventTarget() { | ||
EventTargetBase.call(this); | ||
} | ||
}; | ||
types.forEach(function (type) { | ||
descripter["on" + type] = { | ||
get: function get() { | ||
return getAttributeListener(this, type); | ||
}, | ||
set: function set(listener) { | ||
setAttributeListener(this, type, listener); | ||
}, | ||
configurable: true, | ||
enumerable: true | ||
var descripter = { | ||
constructor: { | ||
value: EventTarget, | ||
configurable: true, | ||
writable: true | ||
} | ||
}; | ||
}); | ||
EventTarget.prototype = Object.create(EventTargetBase.prototype, descripter); | ||
types.forEach(function(type) { | ||
descripter["on" + type] = { | ||
get: function() { return getAttributeListener(this, type); }, | ||
set: function(listener) { setAttributeListener(this, type, listener); }, | ||
configurable: true, | ||
enumerable: true | ||
}; | ||
}); | ||
return EventTarget; | ||
} | ||
},{"./commons":3}],2:[function(require,module,exports){ | ||
EventTarget.prototype = Object.create(EventTargetBase.prototype, descripter); | ||
return EventTarget; | ||
}; | ||
},{"./commons":1}],3:[function(require,module,exports){ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createEventWrapper = createEventWrapper; | ||
//----------------------------------------------------------------------------- | ||
// Requirements | ||
//----------------------------------------------------------------------------- | ||
var _commons = require("./commons"); | ||
var Commons = require("./commons"); | ||
var CustomEventTarget = require("./custom-event-target"); | ||
var EventWrapper = require("./event-wrapper"); | ||
var LISTENERS = Commons.LISTENERS; | ||
var newNode = Commons.newNode; | ||
var defineCustomEventTarget = CustomEventTarget.defineCustomEventTarget; | ||
var createEventWrapper = EventWrapper.createEventWrapper; | ||
var STOP_IMMEDIATE_PROPAGATION_FLAG = EventWrapper.STOP_IMMEDIATE_PROPAGATION_FLAG; | ||
var STOP_IMMEDIATE_PROPAGATION_FLAG = (0, _commons.symbol)("stop_immediate_propagation_flag"); | ||
//----------------------------------------------------------------------------- | ||
// Constants | ||
//----------------------------------------------------------------------------- | ||
exports.STOP_IMMEDIATE_PROPAGATION_FLAG = STOP_IMMEDIATE_PROPAGATION_FLAG; | ||
var CANCELED_FLAG = (0, _commons.symbol)("canceled_flag"); | ||
var ORIGINAL_EVENT = (0, _commons.symbol)("original_event"); | ||
/** | ||
* A flag which shows there is the native `EventTarget` interface object. | ||
* | ||
* @type {boolean} | ||
* @private | ||
*/ | ||
var HAS_EVENTTARGET_INTERFACE = ( | ||
typeof window !== "undefined" && | ||
typeof window.EventTarget !== "undefined" | ||
); | ||
var wrapperPrototypeDefinition = { | ||
stopPropagation: { | ||
value: function stopPropagation() { | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.stopPropagation === "function") { | ||
e.stopPropagation(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}, | ||
/** | ||
* A value of kind for listeners which are registered in the capturing phase. | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
var CAPTURE = 1; | ||
stopImmediatePropagation: { | ||
value: function stopImmediatePropagation() { | ||
this[STOP_IMMEDIATE_PROPAGATION_FLAG] = true; | ||
/** | ||
* A value of kind for listeners which are registered in the bubbling phase. | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
var BUBBLE = 2; | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.stopImmediatePropagation === "function") { | ||
e.stopImmediatePropagation(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}, | ||
//----------------------------------------------------------------------------- | ||
// Public Interface | ||
//----------------------------------------------------------------------------- | ||
preventDefault: { | ||
value: function preventDefault() { | ||
if (this.cancelable === true) { | ||
this[CANCELED_FLAG] = true; | ||
} | ||
/** | ||
* An implementation for `EventTarget` interface. | ||
* | ||
* @constructor | ||
* @public | ||
*/ | ||
var EventTarget = module.exports = function EventTarget() { | ||
if (this instanceof EventTarget) { | ||
// this[LISTENERS] is a Map. | ||
// Its key is event type. | ||
// Its value is ListenerNode object or null. | ||
// | ||
// interface ListenerNode { | ||
// var listener: Function | ||
// var kind: CAPTURE|BUBBLE|ATTRIBUTE | ||
// var next: ListenerNode|null | ||
// } | ||
Object.defineProperty(this, LISTENERS, {value: Object.create(null)}); | ||
} | ||
else if (arguments.length > 0) { | ||
var types = Array(arguments.length); | ||
for (var i = 0; i < arguments.length; ++i) { | ||
types[i] = arguments[i]; | ||
} | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.preventDefault === "function") { | ||
e.preventDefault(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}, | ||
defaultPrevented: { | ||
get: function get() { | ||
return this[CANCELED_FLAG]; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
} | ||
// To use to extend with attribute listener properties. | ||
// e.g. | ||
// class MyCustomObject extends EventTarget("message", "error") { | ||
// //... | ||
// } | ||
return defineCustomEventTarget(EventTarget, types); | ||
} | ||
else { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
function createEventWrapper(event, eventTarget) { | ||
var timeStamp = typeof event.timeStamp === "number" ? event.timeStamp : Date.now(); | ||
EventTarget.prototype = Object.create( | ||
(HAS_EVENTTARGET_INTERFACE ? window.EventTarget : Object).prototype, | ||
{ | ||
constructor: { | ||
value: EventTarget, | ||
writable: true, | ||
configurable: true | ||
}, | ||
var props = { | ||
type: { value: event.type, enumerable: true }, | ||
target: { value: eventTarget, enumerable: true }, | ||
currentTarget: { value: eventTarget, enumerable: true }, | ||
eventPhase: { value: 2, enumerable: true }, | ||
bubbles: { value: Boolean(event.bubbles), enumerable: true }, | ||
cancelable: { value: Boolean(event.cancelable), enumerable: true }, | ||
timeStamp: { value: timeStamp, enumerable: true }, | ||
isTrusted: { value: false, enumerable: true } | ||
}; | ||
if (typeof event.detail !== "undefined") { | ||
props.detail = { value: event.detail, enumerable: true }; | ||
} | ||
addEventListener: { | ||
value: function addEventListener(type, listener, capture) { | ||
if (listener == null) { | ||
return false; | ||
} | ||
if (typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
var retv = Object.create(Object.create(event, wrapperPrototypeDefinition), props); | ||
Object.defineProperty(retv, STOP_IMMEDIATE_PROPAGATION_FLAG, { value: false, writable: true }); | ||
Object.defineProperty(retv, CANCELED_FLAG, { value: false, writable: true }); | ||
Object.defineProperty(retv, ORIGINAL_EVENT, { value: event }); | ||
var kind = (capture ? CAPTURE : BUBBLE); | ||
var node = this[LISTENERS][type]; | ||
if (node == null) { | ||
this[LISTENERS][type] = newNode(listener, kind); | ||
return true; | ||
} | ||
return retv; | ||
} | ||
},{"./commons":3}],3:[function(require,module,exports){ | ||
"use strict"; | ||
var prev = null; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
// Should ignore a duplicated listener. | ||
return false; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.newNode = newNode; | ||
var symbol = typeof Symbol !== "undefined" ? Symbol : function Symbol(name) { | ||
return "[[" + name + "_" + Math.random().toFixed(8).slice(2) + "]]"; | ||
}; | ||
prev.next = newNode(listener, kind); | ||
return true; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
exports.symbol = symbol; | ||
var LISTENERS = symbol("listeners"); | ||
exports.LISTENERS = LISTENERS; | ||
var CAPTURE = 1; | ||
exports.CAPTURE = CAPTURE; | ||
var BUBBLE = 2; | ||
exports.BUBBLE = BUBBLE; | ||
var ATTRIBUTE = 3; | ||
removeEventListener: { | ||
value: function removeEventListener(type, listener, capture) { | ||
if (listener == null) { | ||
return false; | ||
} | ||
exports.ATTRIBUTE = ATTRIBUTE; | ||
// Create a LinkedList structure for EventListener. | ||
var kind = (capture ? CAPTURE : BUBBLE); | ||
var prev = null; | ||
var node = this[LISTENERS][type]; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
if (prev == null) { | ||
this[LISTENERS][type] = node.next; | ||
} | ||
else { | ||
prev.next = node.next; | ||
} | ||
return true; | ||
} | ||
function newNode(listener, kind) { | ||
return { listener: listener, kind: kind, next: null }; | ||
} | ||
},{}],4:[function(require,module,exports){ | ||
"use strict"; | ||
prev = node; | ||
node = node.next; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = EventTarget; | ||
return false; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
var _commons = require("./commons"); | ||
dispatchEvent: { | ||
value: function dispatchEvent(event) { | ||
// If listeners aren't registered, terminate. | ||
var node = this[LISTENERS][event.type]; | ||
if (node == null) { | ||
return true; | ||
} | ||
var _CustomEventTarget = require("./CustomEventTarget"); | ||
// Since we cannot rewrite several properties, so wrap object. | ||
var wrapped = createEventWrapper(event, this); | ||
var _EventWrapper = require("./EventWrapper"); | ||
// This doesn't process capturing phase and bubbling phase. | ||
// This isn't participating in a tree. | ||
while (node != null) { | ||
node.listener.call(this, wrapped); | ||
if (wrapped[STOP_IMMEDIATE_PROPAGATION_FLAG]) { | ||
break; | ||
} | ||
node = node.next; | ||
} | ||
var HAS_EVENTTARGET_INTERFACE = typeof window !== "undefined" && typeof window.EventTarget !== "undefined"; | ||
return !wrapped.defaultPrevented; | ||
}, | ||
configurable: true, | ||
writable: true | ||
} | ||
} | ||
); | ||
function EventTarget() { | ||
for (var _len = arguments.length, types = Array(_len), _key = 0; _key < _len; _key++) { | ||
types[_key] = arguments[_key]; | ||
} | ||
},{"./commons":1,"./custom-event-target":2,"./event-wrapper":4}],4:[function(require,module,exports){ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
if (this instanceof EventTarget) { | ||
// this[LISTENERS] is a Map. | ||
// Its key is event type. | ||
// Its value is ListenerNode object or null. | ||
// | ||
// interface ListenerNode { | ||
// let listener: Function | ||
// let kind: CAPTURE|BUBBLE|ATTRIBUTE | ||
// let next: ListenerNode|null | ||
// } | ||
Object.defineProperty(this, _commons.LISTENERS, { value: Object.create(null) }); | ||
} else if (types.length > 0) { | ||
// To use to extend with attribute listener properties. | ||
// e.g. | ||
// class MyCustomObject extends EventTarget("message", "error") { | ||
// //... | ||
// } | ||
return (0, _CustomEventTarget.defineCustomEventTarget)(EventTarget, types); | ||
} else { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
"use strict"; | ||
EventTarget.prototype = Object.create((HAS_EVENTTARGET_INTERFACE ? window.EventTarget : Object).prototype, { | ||
constructor: { | ||
value: EventTarget, | ||
writable: true, | ||
configurable: true | ||
}, | ||
//----------------------------------------------------------------------------- | ||
// Requirements | ||
//----------------------------------------------------------------------------- | ||
addEventListener: { | ||
value: function addEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
var createUniqueKey = require("./commons").createUniqueKey; | ||
if (listener == null) { | ||
return false; | ||
} | ||
if (typeof listener !== "function") { | ||
throw new TypeError("listener should be a function."); | ||
} | ||
//----------------------------------------------------------------------------- | ||
// Constsnts | ||
//----------------------------------------------------------------------------- | ||
var kind = capture ? _commons.CAPTURE : _commons.BUBBLE; | ||
var node = this[_commons.LISTENERS][type]; | ||
if (node == null) { | ||
this[_commons.LISTENERS][type] = (0, _commons.newNode)(listener, kind); | ||
return true; | ||
} | ||
/** | ||
* The key of the flag which is turned on by `stopImmediatePropagation` method. | ||
* | ||
* @type {symbol|string} | ||
* @private | ||
*/ | ||
var STOP_IMMEDIATE_PROPAGATION_FLAG = | ||
createUniqueKey("stop_immediate_propagation_flag"); | ||
var prev = null; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
// Should ignore a duplicated listener. | ||
return false; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
/** | ||
* The key of the flag which is turned on by `preventDefault` method. | ||
* | ||
* @type {symbol|string} | ||
* @private | ||
*/ | ||
var CANCELED_FLAG = createUniqueKey("canceled_flag"); | ||
prev.next = (0, _commons.newNode)(listener, kind); | ||
return true; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
/** | ||
* The key of the original event object. | ||
* | ||
* @type {symbol|string} | ||
* @private | ||
*/ | ||
var ORIGINAL_EVENT = createUniqueKey("original_event"); | ||
removeEventListener: { | ||
value: function removeEventListener(type, listener) { | ||
var capture = arguments[2] === undefined ? false : arguments[2]; | ||
/** | ||
* Method definitions for the event wrapper. | ||
* | ||
* @type {object} | ||
* @private | ||
*/ | ||
var wrapperPrototypeDefinition = Object.freeze({ | ||
stopPropagation: Object.freeze({ | ||
value: function stopPropagation() { | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.stopPropagation === "function") { | ||
e.stopPropagation(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}), | ||
if (listener == null) { | ||
return false; | ||
} | ||
stopImmediatePropagation: Object.freeze({ | ||
value: function stopImmediatePropagation() { | ||
this[STOP_IMMEDIATE_PROPAGATION_FLAG] = true; | ||
var kind = capture ? _commons.CAPTURE : _commons.BUBBLE; | ||
var prev = null; | ||
var node = this[_commons.LISTENERS][type]; | ||
while (node != null) { | ||
if (node.listener === listener && node.kind === kind) { | ||
if (prev == null) { | ||
this[_commons.LISTENERS][type] = node.next; | ||
} else { | ||
prev.next = node.next; | ||
} | ||
return true; | ||
} | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.stopImmediatePropagation === "function") { | ||
e.stopImmediatePropagation(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}), | ||
prev = node; | ||
node = node.next; | ||
} | ||
preventDefault: Object.freeze({ | ||
value: function preventDefault() { | ||
if (this.cancelable === true) { | ||
this[CANCELED_FLAG] = true; | ||
} | ||
return false; | ||
}, | ||
configurable: true, | ||
writable: true | ||
}, | ||
var e = this[ORIGINAL_EVENT]; | ||
if (typeof e.preventDefault === "function") { | ||
e.preventDefault(); | ||
} | ||
}, | ||
writable: true, | ||
configurable: true | ||
}), | ||
dispatchEvent: { | ||
value: function dispatchEvent(event) { | ||
// If listeners aren't registered, terminate. | ||
var node = this[_commons.LISTENERS][event.type]; | ||
if (node == null) { | ||
return true; | ||
} | ||
defaultPrevented: Object.freeze({ | ||
get: function defaultPrevented() { return this[CANCELED_FLAG]; }, | ||
enumerable: true, | ||
configurable: true | ||
}) | ||
}); | ||
// Since we cannot rewrite several properties, so wrap object. | ||
event = (0, _EventWrapper.createEventWrapper)(event, this); | ||
//----------------------------------------------------------------------------- | ||
// Public Interface | ||
//----------------------------------------------------------------------------- | ||
// This doesn't process capturing phase and bubbling phase. | ||
// This isn't participating in a tree. | ||
while (node != null) { | ||
node.listener.call(this, event); | ||
if (event[_EventWrapper.STOP_IMMEDIATE_PROPAGATION_FLAG]) { | ||
break; | ||
} | ||
node = node.next; | ||
} | ||
exports.STOP_IMMEDIATE_PROPAGATION_FLAG = STOP_IMMEDIATE_PROPAGATION_FLAG; | ||
return !event.defaultPrevented; | ||
}, | ||
configurable: true, | ||
writable: true | ||
} | ||
}); | ||
module.exports = exports["default"]; | ||
},{"./CustomEventTarget":1,"./EventWrapper":2,"./commons":3}]},{},[4])(4) | ||
/** | ||
* Creates an event wrapper. | ||
* | ||
* We cannot modify several properties of `Event` object, so we need to create the wrapper. | ||
* Plus, this wrapper supports non `Event` objects. | ||
* | ||
* @param {Event|{type: string}} event - An original event to create the wrapper. | ||
* @param {EventTarget} eventTarget - The event target of the event. | ||
* @returns {Event} The created wrapper. This object is implemented `Event` interface. | ||
* @private | ||
*/ | ||
exports.createEventWrapper = function createEventWrapper(event, eventTarget) { | ||
var timeStamp = ( | ||
typeof event.timeStamp === "number" ? event.timeStamp : Date.now() | ||
); | ||
var propertyDefinition = { | ||
type: {value: event.type, enumerable: true}, | ||
target: {value: eventTarget, enumerable: true}, | ||
currentTarget: {value: eventTarget, enumerable: true}, | ||
eventPhase: {value: 2, enumerable: true}, | ||
bubbles: {value: Boolean(event.bubbles), enumerable: true}, | ||
cancelable: {value: Boolean(event.cancelable), enumerable: true}, | ||
timeStamp: {value: timeStamp, enumerable: true}, | ||
isTrusted: {value: false, enumerable: true} | ||
}; | ||
propertyDefinition[STOP_IMMEDIATE_PROPAGATION_FLAG] = {value: false, writable: true}; | ||
propertyDefinition[CANCELED_FLAG] = {value: false, writable: true}; | ||
propertyDefinition[ORIGINAL_EVENT] = {value: event}; | ||
// For CustomEvent. | ||
if (typeof event.detail !== "undefined") { | ||
propertyDefinition.detail = {value: event.detail, enumerable: true}; | ||
} | ||
return Object.create( | ||
Object.create(event, wrapperPrototypeDefinition), | ||
propertyDefinition | ||
); | ||
}; | ||
},{"./commons":1}]},{},[3])(3) | ||
}); |
@@ -1,1 +0,1 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.eventTargetShim=e()}}(function(){return function e(t,n,r){function o(u,i){if(!n[u]){if(!t[u]){var l="function"==typeof require&&require;if(!i&&l)return l(u,!0);if(a)return a(u,!0);var f=new Error("Cannot find module '"+u+"'");throw f.code="MODULE_NOT_FOUND",f}var c=n[u]={exports:{}};t[u][0].call(c.exports,function(e){var n=t[u][1][e];return o(n?n:e)},c,c.exports,e,t,n,r)}return n[u].exports}for(var a="function"==typeof require&&require,u=0;u<r.length;u++)o(r[u]);return o}({1:[function(e,t,n){"use strict";function r(e,t){for(var n=e[u.LISTENERS][t];null!=n;){if(n.kind===u.ATTRIBUTE)return n.listener;n=n.next}return null}function o(e,t,n){if(null!=n&&"function"!=typeof n)throw new TypeError("listener should be a function.");for(var r=null,o=e[u.LISTENERS][t];null!=o;)o.kind===u.ATTRIBUTE?null==r?e[u.LISTENERS][t]=o.next:r.next=o.next:r=o,o=o.next;null!=n&&(null==r?e[u.LISTENERS][t]=u.newNode(n,u.ATTRIBUTE):r.next=u.newNode(n,u.ATTRIBUTE))}function a(e,t){function n(){e.call(this)}var a={constructor:{value:n,configurable:!0,writable:!0}};return t.forEach(function(e){a["on"+e]={get:function(){return r(this,e)},set:function(t){o(this,e,t)},configurable:!0,enumerable:!0}}),n.prototype=Object.create(e.prototype,a),n}Object.defineProperty(n,"__esModule",{value:!0}),n.defineCustomEventTarget=a;var u=e("./commons")},{"./commons":3}],2:[function(e,t,n){"use strict";function r(e,t){var n="number"==typeof e.timeStamp?e.timeStamp:Date.now(),r={type:{value:e.type,enumerable:!0},target:{value:t,enumerable:!0},currentTarget:{value:t,enumerable:!0},eventPhase:{value:2,enumerable:!0},bubbles:{value:Boolean(e.bubbles),enumerable:!0},cancelable:{value:Boolean(e.cancelable),enumerable:!0},timeStamp:{value:n,enumerable:!0},isTrusted:{value:!1,enumerable:!0}};"undefined"!=typeof e.detail&&(r.detail={value:e.detail,enumerable:!0});var o=Object.create(Object.create(e,l),r);return Object.defineProperty(o,a,{value:!1,writable:!0}),Object.defineProperty(o,u,{value:!1,writable:!0}),Object.defineProperty(o,i,{value:e}),o}Object.defineProperty(n,"__esModule",{value:!0}),n.createEventWrapper=r;var o=e("./commons"),a=o.symbol("stop_immediate_propagation_flag");n.STOP_IMMEDIATE_PROPAGATION_FLAG=a;var u=o.symbol("canceled_flag"),i=o.symbol("original_event"),l={stopPropagation:{value:function(){var e=this[i];"function"==typeof e.stopPropagation&&e.stopPropagation()},writable:!0,configurable:!0},stopImmediatePropagation:{value:function(){this[a]=!0;var e=this[i];"function"==typeof e.stopImmediatePropagation&&e.stopImmediatePropagation()},writable:!0,configurable:!0},preventDefault:{value:function(){this.cancelable===!0&&(this[u]=!0);var e=this[i];"function"==typeof e.preventDefault&&e.preventDefault()},writable:!0,configurable:!0},defaultPrevented:{get:function(){return this[u]},enumerable:!0,configurable:!0}}},{"./commons":3}],3:[function(e,t,n){"use strict";function r(e,t){return{listener:e,kind:t,next:null}}Object.defineProperty(n,"__esModule",{value:!0}),n.newNode=r;var o="undefined"!=typeof Symbol?Symbol:function(e){return"[["+e+"_"+Math.random().toFixed(8).slice(2)+"]]"};n.symbol=o;var a=o("listeners");n.LISTENERS=a;var u=1;n.CAPTURE=u;var i=2;n.BUBBLE=i;var l=3;n.ATTRIBUTE=l},{}],4:[function(e,t,n){"use strict";function r(){for(var e=arguments.length,t=Array(e),n=0;e>n;n++)t[n]=arguments[n];if(!(this instanceof r)){if(t.length>0)return a.defineCustomEventTarget(r,t);throw new TypeError("Cannot call a class as a function")}Object.defineProperty(this,o.LISTENERS,{value:Object.create(null)})}Object.defineProperty(n,"__esModule",{value:!0}),n["default"]=r;var o=e("./commons"),a=e("./CustomEventTarget"),u=e("./EventWrapper"),i="undefined"!=typeof window&&"undefined"!=typeof window.EventTarget;r.prototype=Object.create((i?window.EventTarget:Object).prototype,{constructor:{value:r,writable:!0,configurable:!0},addEventListener:{value:function(e,t){var n=void 0===arguments[2]?!1:arguments[2];if(null==t)return!1;if("function"!=typeof t)throw new TypeError("listener should be a function.");var r=n?o.CAPTURE:o.BUBBLE,a=this[o.LISTENERS][e];if(null==a)return this[o.LISTENERS][e]=o.newNode(t,r),!0;for(var u=null;null!=a;){if(a.listener===t&&a.kind===r)return!1;u=a,a=a.next}return u.next=o.newNode(t,r),!0},configurable:!0,writable:!0},removeEventListener:{value:function(e,t){var n=void 0===arguments[2]?!1:arguments[2];if(null==t)return!1;for(var r=n?o.CAPTURE:o.BUBBLE,a=null,u=this[o.LISTENERS][e];null!=u;){if(u.listener===t&&u.kind===r)return null==a?this[o.LISTENERS][e]=u.next:a.next=u.next,!0;a=u,u=u.next}return!1},configurable:!0,writable:!0},dispatchEvent:{value:function(e){var t=this[o.LISTENERS][e.type];if(null==t)return!0;for(e=u.createEventWrapper(e,this);null!=t&&(t.listener.call(this,e),!e[u.STOP_IMMEDIATE_PROPAGATION_FLAG]);)t=t.next;return!e.defaultPrevented},configurable:!0,writable:!0}}),t.exports=n["default"]},{"./CustomEventTarget":1,"./EventWrapper":2,"./commons":3}]},{},[4])(4)}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.eventTargetShim=e()}}(function(){return function e(t,n,r){function o(a,i){if(!n[a]){if(!t[a]){var l="function"==typeof require&&require;if(!i&&l)return l(a,!0);if(u)return u(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=n[a]={exports:{}};t[a][0].call(c.exports,function(e){var n=t[a][1][e];return o(n?n:e)},c,c.exports,e,t,n,r)}return n[a].exports}for(var u="function"==typeof require&&require,a=0;a<r.length;a++)o(r[a]);return o}({1:[function(e,t,n){"use strict";var r=n.createUniqueKey="undefined"!=typeof Symbol?Symbol:function(e){return"[["+e+"_"+Math.random().toFixed(8).slice(2)+"]]"};n.LISTENERS=r("listeners"),n.newNode=function(e,t){return{listener:e,kind:t,next:null}}},{}],2:[function(e,t,n){"use strict";function r(e,t){for(var n=e[a][t];null!=n;){if(n.kind===l)return n.listener;n=n.next}return null}function o(e,t,n){if(null!=n&&"function"!=typeof n)throw new TypeError("listener should be a function.");for(var r=null,o=e[a][t];null!=o;)o.kind===l?null==r?e[a][t]=o.next:r.next=o.next:r=o,o=o.next;null!=n&&(null==r?e[a][t]=i(n,l):r.next=i(n,l))}var u=e("./commons"),a=u.LISTENERS,i=u.newNode,l=3;n.defineCustomEventTarget=function(e,t){function n(){e.call(this)}var u={constructor:{value:n,configurable:!0,writable:!0}};return t.forEach(function(e){u["on"+e]={get:function(){return r(this,e)},set:function(t){o(this,e,t)},configurable:!0,enumerable:!0}}),n.prototype=Object.create(e.prototype,u),n}},{"./commons":1}],3:[function(e,t,n){"use strict";var r=e("./commons"),o=e("./custom-event-target"),u=e("./event-wrapper"),a=r.LISTENERS,i=r.newNode,l=o.defineCustomEventTarget,f=u.createEventWrapper,c=u.STOP_IMMEDIATE_PROPAGATION_FLAG,s="undefined"!=typeof window&&"undefined"!=typeof window.EventTarget,p=1,v=2,d=t.exports=function b(){if(!(this instanceof b)){if(arguments.length>0){for(var e=Array(arguments.length),t=0;t<arguments.length;++t)e[t]=arguments[t];return l(b,e)}throw new TypeError("Cannot call a class as a function")}Object.defineProperty(this,a,{value:Object.create(null)})};d.prototype=Object.create((s?window.EventTarget:Object).prototype,{constructor:{value:d,writable:!0,configurable:!0},addEventListener:{value:function(e,t,n){if(null==t)return!1;if("function"!=typeof t)throw new TypeError("listener should be a function.");var r=n?p:v,o=this[a][e];if(null==o)return this[a][e]=i(t,r),!0;for(var u=null;null!=o;){if(o.listener===t&&o.kind===r)return!1;u=o,o=o.next}return u.next=i(t,r),!0},configurable:!0,writable:!0},removeEventListener:{value:function(e,t,n){if(null==t)return!1;for(var r=n?p:v,o=null,u=this[a][e];null!=u;){if(u.listener===t&&u.kind===r)return null==o?this[a][e]=u.next:o.next=u.next,!0;o=u,u=u.next}return!1},configurable:!0,writable:!0},dispatchEvent:{value:function(e){var t=this[a][e.type];if(null==t)return!0;for(var n=f(e,this);null!=t&&(t.listener.call(this,n),!n[c]);)t=t.next;return!n.defaultPrevented},configurable:!0,writable:!0}})},{"./commons":1,"./custom-event-target":2,"./event-wrapper":4}],4:[function(e,t,n){"use strict";var r=e("./commons").createUniqueKey,o=r("stop_immediate_propagation_flag"),u=r("canceled_flag"),a=r("original_event"),i=Object.freeze({stopPropagation:Object.freeze({value:function(){var e=this[a];"function"==typeof e.stopPropagation&&e.stopPropagation()},writable:!0,configurable:!0}),stopImmediatePropagation:Object.freeze({value:function(){this[o]=!0;var e=this[a];"function"==typeof e.stopImmediatePropagation&&e.stopImmediatePropagation()},writable:!0,configurable:!0}),preventDefault:Object.freeze({value:function(){this.cancelable===!0&&(this[u]=!0);var e=this[a];"function"==typeof e.preventDefault&&e.preventDefault()},writable:!0,configurable:!0}),defaultPrevented:Object.freeze({get:function(){return this[u]},enumerable:!0,configurable:!0})});n.STOP_IMMEDIATE_PROPAGATION_FLAG=o,n.createEventWrapper=function(e,t){var n="number"==typeof e.timeStamp?e.timeStamp:Date.now(),r={type:{value:e.type,enumerable:!0},target:{value:t,enumerable:!0},currentTarget:{value:t,enumerable:!0},eventPhase:{value:2,enumerable:!0},bubbles:{value:Boolean(e.bubbles),enumerable:!0},cancelable:{value:Boolean(e.cancelable),enumerable:!0},timeStamp:{value:n,enumerable:!0},isTrusted:{value:!1,enumerable:!0}};return r[o]={value:!1,writable:!0},r[u]={value:!1,writable:!0},r[a]={value:e},"undefined"!=typeof e.detail&&(r.detail={value:e.detail,enumerable:!0}),Object.create(Object.create(e,i),r)}},{"./commons":1}]},{},[3])(3)}); |
@@ -0,25 +1,47 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.newNode = newNode; | ||
var symbol = typeof Symbol !== "undefined" ? Symbol : function Symbol(name) { | ||
return "[[" + name + "_" + Math.random().toFixed(8).slice(2) + "]]"; | ||
}; | ||
/** | ||
* Creates a unique key. | ||
* | ||
* @param {string} name - A name to create. | ||
* @returns {symbol|string} | ||
* @private | ||
*/ | ||
var createUniqueKey = exports.createUniqueKey = (typeof Symbol !== "undefined" ? | ||
Symbol : | ||
function createUniqueKey(name) { | ||
return "[[" + name + "_" + Math.random().toFixed(8).slice(2) + "]]"; | ||
}); | ||
exports.symbol = symbol; | ||
var LISTENERS = symbol("listeners"); | ||
exports.LISTENERS = LISTENERS; | ||
var CAPTURE = 1; | ||
exports.CAPTURE = CAPTURE; | ||
var BUBBLE = 2; | ||
exports.BUBBLE = BUBBLE; | ||
var ATTRIBUTE = 3; | ||
/** | ||
* The key of listeners. | ||
* | ||
* @type {symbol|string} | ||
* @private | ||
*/ | ||
exports.LISTENERS = createUniqueKey("listeners"); | ||
exports.ATTRIBUTE = ATTRIBUTE; | ||
// Create a LinkedList structure for EventListener. | ||
/** | ||
* @typedef object ListenerNode | ||
* @property {function} listener - A listener function. | ||
* @property {number} kind - The kind of the listener. | ||
* @property {ListenerNode|null} next - The next node. | ||
* If this node is the last, this is `null`. | ||
*/ | ||
function newNode(listener, kind) { | ||
return { listener: listener, kind: kind, next: null }; | ||
} | ||
/** | ||
* Creates a node of singly linked list for a list of listeners. | ||
* | ||
* @param {function} listener - A listener function. | ||
* @param {number} kind - The kind of the listener. | ||
* @returns {ListenerNode} The created listener node. | ||
*/ | ||
exports.newNode = function newNode(listener, kind) { | ||
return {listener: listener, kind: kind, next: null}; | ||
}; |
{ | ||
"name": "event-target-shim", | ||
"version": "1.0.5", | ||
"description": "A polyfill for W3C EventTarget Constructor.", | ||
"main": "lib/EventTarget.js", | ||
"version": "1.0.6", | ||
"description": "An implementation of W3C EventTarget interface.", | ||
"main": "lib/event-target.js", | ||
"files": [ | ||
@@ -11,28 +11,37 @@ "dist", | ||
"scripts": { | ||
"clean": "rimraf lib dist", | ||
"lint": "eslint src", | ||
"build": "npm-run-all clean lint build:*", | ||
"build:lib": "babel src --out-dir lib", | ||
"build:dist": "mkdirp dist && browserify lib/EventTarget.js --standalone event-target-shim > dist/event-target-shim.js", | ||
"preversion": "npm run build", | ||
"postversion": "git push && git push --tags", | ||
"clean": "rimraf coverage dist", | ||
"lint": "eslint lib test", | ||
"build": "npm-run-all clean lint test build:*", | ||
"build:dist": "mkdirp dist && browserify lib/event-target.js --standalone event-target-shim > dist/event-target-shim.js", | ||
"build:dist-min": "uglifyjs dist/event-target-shim.js --compress --mangle > dist/event-target-shim.min.js", | ||
"test": "npm run lint && karma start karma.conf.js --single-run", | ||
"testing": "karma start karma.conf.js --auto-watch --reporters growl,progress" | ||
"test": "npm-run-all clean lint && karma start karma.conf.js --single-run", | ||
"watch": "karma start karma.conf.js --watch", | ||
"travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- test/**/*.js --require scripts/power-assert", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.4.7", | ||
"babel-plugin-espower": "^1.0.0", | ||
"babelify": "^6.1.2", | ||
"browserify": "^10.2.1", | ||
"eslint": "^0.21.2", | ||
"karma": "^0.12.33", | ||
"browserify": "^12.0.1", | ||
"browserify-istanbul": "^0.2.1", | ||
"coveralls": "^2.11.4", | ||
"eslint": "^1.10.2", | ||
"eslint-config-mysticatea": "^1.9.0", | ||
"eslint-plugin-mysticatea": "^1.0.3", | ||
"eslint-plugin-node": "^0.2.0", | ||
"espower-loader": "^1.0.0", | ||
"espowerify": "^1.0.0", | ||
"istanbul": "^0.4.1", | ||
"karma": "^0.13.15", | ||
"karma-browserify": "^4.2.1", | ||
"karma-chrome-launcher": "^0.1.12", | ||
"karma-chrome-launcher": "^0.2.1", | ||
"karma-coverage": "^0.5.3", | ||
"karma-firefox-launcher": "^0.1.6", | ||
"karma-growl-reporter": "^0.1.1", | ||
"karma-ie-launcher": "^0.1.5", | ||
"karma-mocha": "^0.1.10", | ||
"karma-ie-launcher": "^0.2.0", | ||
"karma-mocha": "^0.2.1", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.2.5", | ||
"npm-run-all": "^1.2.5", | ||
"power-assert": "^0.11.0", | ||
"power-assert": "^1.2.0", | ||
"rimraf": "^2.3.4", | ||
@@ -51,4 +60,3 @@ "spy": "^0.1.3", | ||
"events", | ||
"shim", | ||
"polyfill" | ||
"shim" | ||
], | ||
@@ -55,0 +63,0 @@ "author": "Toru Nagashima", |
135
README.md
# event-target-shim | ||
[![npm version](https://badge.fury.io/js/event-target-shim.svg)](http://badge.fury.io/js/event-target-shim) | ||
[![Build Status](https://travis-ci.org/mysticatea/event-target-shim.svg)](https://travis-ci.org/mysticatea/event-target-shim) | ||
[![Coverage Status](https://coveralls.io/repos/mysticatea/event-target-shim/badge.svg?branch=master&service=github)](https://coveralls.io/github/mysticatea/event-target-shim?branch=master) | ||
[![Dependency Status](https://david-dm.org/mysticatea/event-target-shim.svg)](https://david-dm.org/mysticatea/event-target-shim) | ||
[![devDependency Status](https://david-dm.org/mysticatea/event-target-shim/dev-status.svg)](https://david-dm.org/mysticatea/event-target-shim#info=devDependencies)<br> | ||
[![npm version](https://img.shields.io/npm/v/event-target-shim.svg)](https://www.npmjs.com/package/event-target-shim) | ||
[![Downloads/month](https://img.shields.io/npm/dm/event-target-shim.svg)](https://www.npmjs.com/package/event-target-shim) | ||
A polyfill for W3C EventTarget, plus few extensions. | ||
An implementation of [W3C EventTarget interface](http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-EventTarget), plus few extensions. | ||
See Also: https://dom.spec.whatwg.org/#interface-eventtarget | ||
- This provides `EventTarget` constructor that can inherit for your custom object. | ||
- This provides an utility that defines properties of attribute listeners (e.g. `obj.onclick`). | ||
## Installation | ||
## Overview | ||
``` | ||
npm install event-target-shim | ||
``` | ||
- This module provides `EventTarget` constructor that is possible to inherit for | ||
your custom object. | ||
- This module provides an utility in order to define properties for attribute | ||
listeners (e.g. `obj.onclick`). | ||
## Usage | ||
If `window.EventTarget` exists, `EventTarget` is inherit from | ||
`window.EventTarget`. | ||
In short, `obj instanceof window.EventTarget === true`. | ||
### Basic | ||
```ts | ||
declare class EventTarget { | ||
constructor(); | ||
addEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void; | ||
removeEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void; | ||
dispatchEvent(event: Event): void; | ||
```js | ||
// import. | ||
const EventTarget = require("event-target-shim"); | ||
// define a custom type. | ||
class Foo extends EventTarget { | ||
} | ||
// Define EventTarget type with attribute listeners. | ||
declare function EventTarget(...types: string[]): typeof EventTarget; | ||
``` | ||
// add event listeners. | ||
let foo = new Foo(); | ||
foo.addEventListener("foo", event => { | ||
console.log(event.hello); | ||
}); | ||
foo.addEventListener("foo", event => { | ||
if (event.hello !== "hello") { | ||
// event implements Event interface. | ||
event.preventDefault(); | ||
} | ||
}); | ||
// dispatch an event. | ||
let event = document.createEvent("CustomEvent"); | ||
event.initCustomEvent("foo", /*bubbles*/ false, /*cancelable*/ false, /*detail*/ null); | ||
event.hello = "hello"; | ||
foo.dispatchEvent(event); | ||
## Installation | ||
// dispatch an event simply (non standard). | ||
foo.dispatchEvent({type: "foo", hello: "hello"}); | ||
// dispatch a cancelable event. | ||
if (!foo.dispatchEvent({type: "foo", cancelable: true, hello: "hey"})) { | ||
console.log("defaultPrevented"); | ||
} | ||
``` | ||
npm install event-target-shim | ||
``` | ||
### The Extension for Attribute Listeners | ||
## Usage | ||
```js | ||
import EventTarget from "event-target-shim"; | ||
// import. | ||
const EventTarget = require("event-target-shim"); | ||
class YourCoolType extends EventTarget { | ||
// ... | ||
// define a custom type. | ||
class Foo extends EventTarget("message", "error") { | ||
} | ||
// This prototype has getters/setters of `onmessage` and `onerror`. | ||
class YourAwesomeType extends EventTarget("message", "error") { | ||
// ... | ||
// add event listeners. | ||
let foo = new Foo(); | ||
foo.onmessage = event => { | ||
console.log(event.data); | ||
}; | ||
foo.onerror = event => { | ||
console.log(event.message); | ||
}; | ||
foo.addEventListener("message", event => { | ||
console.log(event.data); | ||
}); | ||
// dispatch a event simply (non standard). | ||
foo.dispatchEvent({type: "message", data: "hello"}); | ||
foo.dispatchEvent({type: "error", message: "an error"}); | ||
``` | ||
## API | ||
```ts | ||
declare class EventTarget { | ||
constructor(); | ||
addEventListener(type: string, listener?: (event: Event) => void, capture: boolean = false): void; | ||
removeEventListener(type: string, listener?: (event: Event) => void, capture: boolean = false): void; | ||
dispatchEvent(event: Event | {type: string, babbles?: boolean, cancelable?: boolean}): void; | ||
} | ||
// Define EventTarget type with attribute listeners. | ||
declare function EventTarget(...types: string[]): EventTarget; | ||
``` | ||
I prefer use together with [Browserify](http://browserify.org). | ||
If `window.EventTarget` exists, `EventTarget` is inherit from `window.EventTarget`. | ||
So, | ||
But we can use together with [RequireJS](http://requirejs.org/), instead. | ||
In this case, please download a file from dist directory of repo. | ||
```js | ||
define("MagicalBox", ["event-target-shim"], function (EventTarget) { | ||
function MagicalBox() { | ||
EventTarget.call(this); | ||
} | ||
const EventTarget = require("event-target-shim"); | ||
MagicalBox.prototype = Object.create(EventTarget.prototype, { | ||
constructor: { | ||
value: MagicalBox, | ||
configurable: true, | ||
writable: true | ||
}, | ||
class Foo extends EventTarget { | ||
} | ||
// ... | ||
}); | ||
return MagicalBox; | ||
}); | ||
let foo = new Foo(); | ||
if (foo instanceof window.EventTarget) { | ||
console.log("yay!"); | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43685
923
115
25
1