Implement action manager

This commit is contained in:
Dennis Eichhorn 2016-08-03 22:20:42 +02:00
parent 6ad6a4f928
commit 4d1e08ca81
2 changed files with 146 additions and 31 deletions

133
UI/ActionManager.js Normal file
View File

@ -0,0 +1,133 @@
/**
* Form manager class.
*
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0 * @since 1.0.0
*/
(function (jsOMS)
{
"use strict";
/** @namespace jsOMS.UI */
jsOMS.Autoloader.defineNamespace('jsOMS.UI');
/**
* Constructor
*
* @param {Object} app Application
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.ActionManager = function (app)
{
this.app = app;
this.actions = {};
};
/**
* Bind button.
*
* @param {string} [id] Button id (optional)
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.ActionManager.prototype.bind = function (id)
{
let uiElements = typeof e === 'undefined' ? document.querySelectorAll('input, select, textarea, button') : [e],
length = uiElements.length;
for (let i = 0; i < length; i++) {
if (uiElements[i] !== null && uiElements[i].hasAttribute('data-action')) {
this.bindElement(uiElements[i]);
}
}
};
/**
* Bind button.
*
* @param {Element} e Button element
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.ActionManager.prototype.bindElement = function (e)
{
let listeners = JSON.parse(e.getAttribute('data-action')),
listenerLength = listeners.length,
actionLength = 0,
self = this;
// For everey action an event is registered
for (let i = 0; i < listenerLength; i++) {
actionLength = listeners[i].action.length;
for (let j = 1; j < actionLength; j++) {
// todo: right now one event type can only exist once... needs fixing!!!
this.app.eventManager.addGroup(listeners[i].action[j - 1].type, e.id + listeners[i].action[j - 1].type);
this.app.eventManager.setDone(e.id + listeners[i].action[j - 1].type, function ()
{
// todo: how to pass result from previous action to next action?!
self.runAction(e, listeners[i].action[j]);
});
}
// Register event for first action
e.addEventListener(listeners[i].listener, function ()
{
self.runAction(this, listeners[i].action[0]);
});
}
};
/**
* Run event action.
*
* @param {Element} e Button
* @param {Object} action Action
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.ActionManager.prototype.runAction = function (e, action)
{
let self = this;
console.log(action.type);
console.log(this.actions);
this.actions[action.type](action, function ()
{
self.app.eventManager.trigger(e.id, e.id + action.type, false);
});
};
/**
* Add action callback.
*
* @param {string} name Action identifier
* @param {function} callback Action callback
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.ActionManager.prototype.add = function (name, callback)
{
this.actions[name] = callback;
};
}(window.jsOMS = window.jsOMS || {}));

View File

@ -24,12 +24,12 @@
*/
jsOMS.UI.UIManager = function (app)
{
this.app = app;
this.formManager = new jsOMS.UI.FormManager(this.app);
this.tabManager = new jsOMS.UI.TabManager(this.app.responseManager);
this.tableManager = new jsOMS.UI.TableManager(this.app.responseManager);
this.button = new jsOMS.UI.Button(this.app);
this.input = new jsOMS.UI.Input();
this.app = app;
this.formManager = new jsOMS.UI.FormManager(this.app);
this.tabManager = new jsOMS.UI.TabManager(this.app.responseManager);
this.tableManager = new jsOMS.UI.TableManager(this.app.responseManager);
this.actionManager = new jsOMS.UI.ActionManager(this.app);
};
/**
@ -48,7 +48,7 @@
this.formManager.bind();
this.tabManager.bind();
this.tableManager.bind();
this.bindAction();
this.actionManager.bind();
} else {
let tag = document.getElementById(id);
@ -60,30 +60,7 @@
this.tableManager.bind(id);
break;
default:
this.bindAction(tag);
}
}
};
jsOMS.UI.UIManager.prototype.bindAction = function(e)
{
let uiElements = typeof e === 'undefined' ? jsOMS.getAll('input, select, textarea, button', document) : [e],
length = uiElements.length;
for(let i = 0; i < length; i++) {
switch(uiElements.tagName) {
case 'input':
this.input.bind(uiElements[i]);
break;
case 'select':
this.select.bind(uiElements[i]);
break;
case 'button':
this.button.bind(uiElements[i]);
break;
case 'textarea':
this.textarea.bind(uiElements[i]);
break;
this.actionManager.bind(tag);
}
}
};
@ -108,6 +85,11 @@
return this.button;
};
jsOMS.UI.UIManager.prototype.getActionManager = function ()
{
return this.actionManager;
};
/**
* Get tab manager.
*