This commit is contained in:
Dennis Eichhorn 2017-03-31 14:21:26 +02:00 committed by GitHub
parent 5d3333da1e
commit 8f7b866be9

View File

@ -5,7 +5,7 @@
* *
* @author OMS Development Team <dev@oms.com> * @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn * @copyright Dennis Eichhorn
* @license OMS License 1.0 * @license OMS License 1.0
* @version 1.0.0 * @since 1.0.0 * @version 1.0.0 * @since 1.0.0
*/ */
@ -23,6 +23,7 @@
*/ */
jsOMS.Event.EventManager = function () jsOMS.Event.EventManager = function ()
{ {
this.logger = jsOMS.Log.Logger.getInstance();
this.groups = {}; this.groups = {};
this.callbacks = {}; this.callbacks = {};
}; };
@ -49,6 +50,27 @@
this.groups[group][id] = false; this.groups[group][id] = false;
}; };
/**
* Resets the group status
*
* @param {string|int} group Group id
*
* @return {void}
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.Event.EventManager.prototype.reset = function (group)
{
for (let id in this.groups[group]) {
if (this.groups[group].hasOwnProperty(id)) {
this.groups[group][id] = false;
}
}
};
/** /**
* Does group have outstanding events * Does group have outstanding events
* *
@ -68,10 +90,8 @@
} }
for (let id in this.groups[group]) { for (let id in this.groups[group]) {
if (this.groups[group].hasOwnProperty(id)) { if (this.groups[group].hasOwnProperty(id) && this.groups[group][id]) {
return true; return true;
} else {
this.app.logger.warning('Invalid property.');
} }
} }
@ -93,12 +113,12 @@
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
jsOMS.Event.EventManager.prototype.trigger = function (group, id, reset) jsOMS.Event.EventManager.prototype.trigger = function (group, id)
{ {
id = typeof id !== 'undefined' ? id : 0; id = typeof id !== 'undefined' ? id : 0;
if (typeof this.groups[group] !== 'undefined') { if (typeof this.groups[group] !== 'undefined') {
delete this.groups[group][id]; this.groups[group][id] = true;
} }
if (!this.hasOutstanding(group)) { if (!this.hasOutstanding(group)) {
@ -106,6 +126,8 @@
if (this.callbacks[group].remove) { if (this.callbacks[group].remove) {
this.detach(group); this.detach(group);
} else if(this.callbacks[group].reset) {
this.reset(group);
} }
} }
}; };
@ -136,6 +158,7 @@
* @param {string|int} group Group id * @param {string|int} group Group id
* @param {function} callback Callback * @param {function} callback Callback
* @param {boolean} remove Should be removed after execution * @param {boolean} remove Should be removed after execution
* @param {boolean} remove Reset after triggering
* *
* @return {void} * @return {void}
* *
@ -144,15 +167,16 @@
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
jsOMS.Event.EventManager.prototype.attach = function (group, callback, remove) jsOMS.Event.EventManager.prototype.attach = function (group, callback, remove, reset)
{ {
if(this.callbacks.hasOwnProperty(group)) { if(this.callbacks.hasOwnProperty(group)) {
return false; return false;
} }
remove = typeof remove === 'undefined' ? false : remove; remove = typeof remove === 'undefined' ? false : remove;
reset = typeof reset === 'undefined' ? false : reset;
this.callbacks[group] = {remove: remove, func: callback}; this.callbacks[group] = {remove: remove, reset: reset, func: callback};
return true; return true;
}; };