2016-12-06 01:38:53 +00:00
|
|
|
var EventBinding = require('./EventBinding');
|
|
|
|
|
2016-12-05 15:29:35 +00:00
|
|
|
var EventDispatcher = function ()
|
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
this.bindings = {};
|
2016-12-05 15:29:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EventDispatcher.prototype.constructor = EventDispatcher;
|
|
|
|
|
|
|
|
EventDispatcher.prototype = {
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
getBinding: function (type)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
if (this.bindings.hasOwnProperty(type))
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
return this.bindings[type];
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
createBinding: function (type)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
if (!this.getBinding(type))
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
this.bindings[type] = new EventBinding(this, type);
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
return this.bindings[type];
|
2016-12-05 15:29:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
on: function (type, listener, priority)
|
|
|
|
{
|
|
|
|
if (priority === undefined) { priority = 0; }
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.createBinding(type);
|
|
|
|
|
|
|
|
if (binding)
|
|
|
|
{
|
|
|
|
binding.add(type, listener, priority, false);
|
|
|
|
}
|
2016-12-05 15:29:35 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
once: function (type, listener, priority)
|
|
|
|
{
|
|
|
|
if (priority === undefined) { priority = 0; }
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.createBinding(type);
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (binding)
|
2016-12-05 17:19:12 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding.add(type, listener, priority, true);
|
2016-12-05 17:19:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
return this;
|
2016-12-05 17:19:12 +00:00
|
|
|
},
|
|
|
|
|
2016-12-05 15:29:35 +00:00
|
|
|
has: function (type, listener)
|
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.getBinding(type);
|
|
|
|
|
|
|
|
if (binding)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
return binding.has(listener);
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
2016-12-06 01:38:53 +00:00
|
|
|
else
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
return false;
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes an event listener.
|
|
|
|
// If there is no matching listener registered with the EventDispatcher, a call to this method has no effect.
|
|
|
|
off: function (type, listener)
|
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.getBinding(type);
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (binding)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding.remove(listener);
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatch: function (event)
|
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding;
|
2016-12-05 17:19:12 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (Array.isArray(event))
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
for (var i = 0; i < event.length; i++)
|
2016-12-05 17:19:12 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding = this.getBinding(event[i].type);
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (binding)
|
|
|
|
{
|
|
|
|
return binding.dispatch(event[i]);
|
|
|
|
}
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding = this.getBinding(event.type);
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (binding)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
return binding.dispatch(event);
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes all listeners, but retains the event type entries
|
2016-12-06 01:38:53 +00:00
|
|
|
removeAll: function (type)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.getBinding(type);
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
if (binding)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding.removeAll();
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
delete: function (type)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
var binding = this.getBinding(type);
|
|
|
|
|
|
|
|
if (binding)
|
2016-12-05 15:29:35 +00:00
|
|
|
{
|
2016-12-06 01:38:53 +00:00
|
|
|
binding.destroy();
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
delete this.bindings[type];
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
return this;
|
|
|
|
},
|
2016-12-05 15:29:35 +00:00
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
deleteAll: function ()
|
|
|
|
{
|
|
|
|
for (var binding in this.bindings)
|
|
|
|
{
|
|
|
|
binding.destroy();
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:38:53 +00:00
|
|
|
this.bindings = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
// What would it do any differently to deleteAll?
|
2016-12-05 15:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = EventDispatcher;
|