2016-01-14 22:07:00 +00:00
|
|
|
/*
|
|
|
|
* Implements EventTarget
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
|
|
|
|
*/
|
2018-05-02 01:54:43 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
window.textsecure = window.textsecure || {};
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
function EventTarget() {}
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
EventTarget.prototype = {
|
|
|
|
constructor: EventTarget,
|
|
|
|
dispatchEvent: function(ev) {
|
|
|
|
if (!(ev instanceof Event)) {
|
|
|
|
throw new Error('Expects an event');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
|
|
|
var listeners = this.listeners[ev.type];
|
|
|
|
var results = [];
|
|
|
|
if (typeof listeners === 'object') {
|
|
|
|
for (var i = 0, max = listeners.length; i < max; i += 1) {
|
|
|
|
var listener = listeners[i];
|
|
|
|
if (typeof listener === 'function') {
|
|
|
|
results.push(listener.call(null, ev));
|
2016-01-14 22:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-02 01:54:43 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
addEventListener: function(eventName, callback) {
|
|
|
|
if (typeof eventName !== 'string') {
|
|
|
|
throw new Error('First argument expects a string');
|
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('Second argument expects a function');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
|
|
|
var listeners = this.listeners[eventName];
|
|
|
|
if (typeof listeners !== 'object') {
|
|
|
|
listeners = [];
|
|
|
|
}
|
|
|
|
listeners.push(callback);
|
|
|
|
this.listeners[eventName] = listeners;
|
|
|
|
},
|
|
|
|
removeEventListener: function(eventName, callback) {
|
|
|
|
if (typeof eventName !== 'string') {
|
|
|
|
throw new Error('First argument expects a string');
|
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('Second argument expects a function');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
|
|
|
var listeners = this.listeners[eventName];
|
|
|
|
if (typeof listeners === 'object') {
|
|
|
|
for (var i = 0; i < listeners.length; ++i) {
|
|
|
|
if (listeners[i] === callback) {
|
|
|
|
listeners.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.listeners[eventName] = listeners;
|
|
|
|
},
|
|
|
|
extend: function(obj) {
|
|
|
|
for (var prop in obj) {
|
|
|
|
this[prop] = obj[prop];
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
};
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
textsecure.EventTarget = EventTarget;
|
|
|
|
})();
|