diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
commit | a0b13ebcacfd74b9f521110c7225bb404325bcd3 (patch) | |
tree | a07c5cc422645e31d7dfef81ce4e54f03f0945f6 /src/types/EventTarget.js | |
parent | 9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff) | |
download | svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip |
reordered modules, add es6 build
Diffstat (limited to 'src/types/EventTarget.js')
-rw-r--r-- | src/types/EventTarget.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/types/EventTarget.js b/src/types/EventTarget.js new file mode 100644 index 0000000..a32a1f1 --- /dev/null +++ b/src/types/EventTarget.js @@ -0,0 +1,90 @@ +import { dispatch, off, on } from '../modules/core/event.js' +import { registerMethods } from '../utils/methods.js' +import Base from './Base.js' + +export default class EventTarget extends Base { + constructor ({ events = {} } = {}) { + super() + this.events = events + } + + addEventListener () {} + + // Bind given event to listener + on (event, listener, binding, options) { + on(this, event, listener, binding, options) + return this + } + + // Unbind event from listener + off (event, listener) { + off(this, event, listener) + return this + } + + dispatch (event, data) { + return dispatch(this, event, data) + } + + dispatchEvent (event) { + const bag = this.getEventHolder().events + if (!bag) return true + + const events = bag[event.type] + + for (let i in events) { + for (let j in events[i]) { + events[i][j](event) + } + } + + return !event.defaultPrevented + } + + // Fire given event + fire (event, data) { + this.dispatch(event, data) + return this + } + + getEventHolder () { + return this + } + + getEventTarget () { + return this + } + + removeEventListener () {} +} + +// Add events to elements +const methods = [ 'click', + 'dblclick', + 'mousedown', + 'mouseup', + 'mouseover', + 'mouseout', + 'mousemove', + 'mouseenter', + 'mouseleave', + 'touchstart', + 'touchmove', + 'touchleave', + 'touchend', + 'touchcancel' ].reduce(function (last, event) { + // add event to Element + const fn = function (f) { + if (f === null) { + off(this, event) + } else { + on(this, event, f) + } + return this + } + + last[event] = fn + return last +}, {}) + +registerMethods('Element', methods) |