UNPKG

derby

Version:

MVC framework making it easy to write realtime, collaborative applications that run in both Node.js and browsers.

147 lines (146 loc) 5.61 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DestroyListener = exports.DomListener = exports.Dom = void 0; var Dom = /** @class */ (function () { function Dom(controller) { this.controller = controller; this._listeners = null; } Dom.prototype._initListeners = function () { var _this = this; this.controller.on('destroy', function () { var listeners = _this._listeners; if (!listeners) return; for (var i = listeners.length; i--;) { listeners[i].remove(); } _this._listeners = null; }); return this._listeners = []; }; Dom.prototype._listenerIndex = function (domListener) { var listeners = this._listeners; if (!listeners) return -1; for (var i = listeners.length; i--;) { if (listeners[i].equals(domListener)) return i; } return -1; }; Dom.prototype.addListener = function (type, target, listener, useCapture) { if (typeof target === 'function') { useCapture = !!listener; listener = target; target = document; } var domListener = (type === 'destroy') ? new DestroyListener(target, listener) : new DomListener(type, target, listener, useCapture); if (-1 === this._listenerIndex(domListener)) { var listeners = this._listeners || this._initListeners(); listeners.push(domListener); } domListener.add(); }; Dom.prototype.on = function (type, target, listener, useCapture) { if (typeof target === 'function') { listener = target; target = document; } this.addListener(type, target, listener, useCapture); }; Dom.prototype.once = function (type, target, listener, useCapture) { var _this = this; if (typeof target === 'function') { useCapture = !!(listener); listener = target; target = document; } var wrappedListener = (function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } _this.removeListener(type, target, wrappedListener, useCapture); return listener.apply(_this, args); }); this.addListener(type, target, wrappedListener, useCapture); }; Dom.prototype.removeListener = function (type, target, listener, useCapture) { if (typeof target === 'function') { useCapture = !!(listener); listener = target; target = document; } var domListener = new DomListener(type, target, listener, useCapture); domListener.remove(); var i = this._listenerIndex(domListener); if (i > -1) this._listeners.splice(i, 1); }; return Dom; }()); exports.Dom = Dom; var DomListener = /** @class */ (function () { function DomListener(type, target, listener, useCapture) { this.type = type; this.target = target; this.listener = listener; this.useCapture = !!useCapture; } DomListener.prototype.equals = function (domListener) { return this.listener === domListener.listener && this.target === domListener.target && this.type === domListener.type && this.useCapture === domListener.useCapture; }; DomListener.prototype.add = function () { this.target.addEventListener(this.type, this.listener, this.useCapture); }; DomListener.prototype.remove = function () { this.target.removeEventListener(this.type, this.listener, this.useCapture); }; return DomListener; }()); exports.DomListener = DomListener; var DestroyListener = /** @class */ (function (_super) { __extends(DestroyListener, _super); function DestroyListener(target, listener) { var _this = _super.call(this, 'destroy', target, listener) || this; DomListener.call(_this, 'destroy', target, listener); return _this; } DestroyListener.prototype.add = function () { var listeners = this.target.$destroyListeners || (this.target.$destroyListeners = []); if (listeners.indexOf(this.listener) === -1) { listeners.push(this.listener); } }; DestroyListener.prototype.remove = function () { var listeners = this.target.$destroyListeners; if (!listeners) return; var index = listeners.indexOf(this.listener); if (index !== -1) { listeners.splice(index, 1); } }; return DestroyListener; }(DomListener)); exports.DestroyListener = DestroyListener;