|
|
@@ -12,7 +12,7 @@ after class:
|
|
|
EmitterMixin.mixInto(TheClass)
|
|
|
*/
|
|
|
|
|
|
-import * as $ from 'jquery'
|
|
|
+import { applyAll } from '../util'
|
|
|
import Mixin from './Mixin'
|
|
|
|
|
|
export interface EmitterInterface {
|
|
|
@@ -26,75 +26,74 @@ export interface EmitterInterface {
|
|
|
|
|
|
export default class EmitterMixin extends Mixin implements EmitterInterface {
|
|
|
|
|
|
- // jQuery-ification via $(this) allows a non-DOM object to have
|
|
|
- // the same event handling capabilities (including namespaces).
|
|
|
+ _handlers: any
|
|
|
+ _oneHandlers: any
|
|
|
|
|
|
-
|
|
|
- on(types, handler) {
|
|
|
- $(this).on(types, this._prepareIntercept(handler))
|
|
|
+ on(type, handler) {
|
|
|
+ addToHash(
|
|
|
+ this._handlers || (this._handlers = {}),
|
|
|
+ type,
|
|
|
+ handler
|
|
|
+ )
|
|
|
return this // for chaining
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- one(types, handler) {
|
|
|
- $(this).one(types, this._prepareIntercept(handler))
|
|
|
+ // todo: add comments
|
|
|
+ one(type, handler) {
|
|
|
+ addToHash(
|
|
|
+ this._oneHandlers || (this._oneHandlers = {}),
|
|
|
+ type,
|
|
|
+ handler
|
|
|
+ )
|
|
|
return this // for chaining
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- _prepareIntercept(handler) {
|
|
|
- // handlers are always called with an "event" object as their first param.
|
|
|
- // sneak the `this` context and arguments into the extra parameter object
|
|
|
- // and forward them on to the original handler.
|
|
|
- let intercept = function(ev, extra) {
|
|
|
- return handler.apply(
|
|
|
- extra.context || this,
|
|
|
- extra.args || []
|
|
|
- )
|
|
|
+ off(type, handler?) {
|
|
|
+ if (this._handlers) {
|
|
|
+ removeFromHash(this._handlers, type, handler)
|
|
|
}
|
|
|
-
|
|
|
- // mimick jQuery's internal "proxy" system (risky, I know)
|
|
|
- // causing all functions with the same .guid to appear to be the same.
|
|
|
- // https://github.com/jquery/jquery/blob/2.2.4/src/core.js#L448
|
|
|
- // this is needed for calling .off with the original non-intercept handler.
|
|
|
- if (!handler.guid) {
|
|
|
- handler.guid = ($ as any).guid++
|
|
|
+ if (this._oneHandlers) {
|
|
|
+ removeFromHash(this._oneHandlers, type, handler)
|
|
|
}
|
|
|
- (intercept as any).guid = handler.guid
|
|
|
-
|
|
|
- return intercept
|
|
|
+ return this // for chaining
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- off(types, handler) {
|
|
|
- $(this).off(types, handler)
|
|
|
-
|
|
|
+ trigger(type, ...args) {
|
|
|
+ this.triggerWith(type, this, args)
|
|
|
return this // for chaining
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- trigger(types, ...args) {
|
|
|
- // pass in "extra" info to the intercept
|
|
|
- $(this).triggerHandler(types, { args: args })
|
|
|
-
|
|
|
+ triggerWith(type, context, args) {
|
|
|
+ if (this._handlers) {
|
|
|
+ applyAll(this._handlers[type], context, args)
|
|
|
+ }
|
|
|
+ if (this._oneHandlers) {
|
|
|
+ applyAll(this._oneHandlers[type], context, args)
|
|
|
+ delete this._oneHandlers[type]
|
|
|
+ }
|
|
|
return this // for chaining
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- triggerWith(types, context, args) {
|
|
|
-
|
|
|
- // `triggerHandler` is less reliant on the DOM compared to `trigger`.
|
|
|
- // pass in "extra" info to the intercept.
|
|
|
- $(this).triggerHandler(types, { context: context, args: args })
|
|
|
-
|
|
|
- return this // for chaining
|
|
|
+ hasHandlers(type) {
|
|
|
+ return (this._handlers && this._handlers[type] && this._handlers[type].length) ||
|
|
|
+ (this._oneHandlers && this._oneHandlers[type] && this._oneHandlers[type].length)
|
|
|
}
|
|
|
|
|
|
+}
|
|
|
|
|
|
- hasHandlers(type) {
|
|
|
- let hash = ($ as any)._data(this, 'events') // http://blog.jquery.com/2012/08/09/jquery-1-8-released/
|
|
|
+function addToHash(hash, type, handler) {
|
|
|
+ (hash[type] || (hash[type] = []))
|
|
|
+ .push(handler)
|
|
|
+}
|
|
|
|
|
|
- return hash && hash[type] && hash[type].length > 0
|
|
|
+function removeFromHash(hash, type, handler?) {
|
|
|
+ if (handler) {
|
|
|
+ if (hash[type]) {
|
|
|
+ hash[type] = hash[type].filter(function(func) {
|
|
|
+ return func !== handler;
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ delete hash[type];
|
|
|
}
|
|
|
-
|
|
|
}
|