Button action concept implemented

This commit is contained in:
Dennis Eichhorn 2016-05-04 22:37:09 +02:00
parent 703281900f
commit 9167fc8873
4 changed files with 131 additions and 73 deletions

View File

@ -1,6 +1,69 @@
(function (jsOMS, undefined) {
/**
* Request manager class.
*
* Used for pooling requests.
*
* @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, undefined)
{
jsOMS.Autoloader.defineNamespace('jsOMS.Event');
jsOMS.Event.EventManager = function () {
/**
* @constructor
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.Event.EventManager = function ()
{
this.groups = {};
this.callbacks = {};
};
jsOMS.Event.EventManager.prototype.addGroup = function(id, group)
{
if(typeof this.groups[group] == 'undefined') {
this.groups[group] = {};
}
this.groups[group][id] = false;
};
jsOMS.Event.EventManager.prototype.hasOutstanding = function(group)
{
if(typeof this.groups[group] === 'undefined') {
return false;
}
for (let id in this.groups[group]) {
if (!this.groups[group][id]) {
return true;
}
}
return false;
};
jsOMS.Event.EventManager.prototype.triggerDone = function(id, group)
{
if(typeof this.groups[group] !== 'undefined') {
this.groups[group][id] = true;
}
if(!this.hasOutstanding(group)) {
this.callbacks[group]();
delete this.callbacks[group];
delete this.groups[group];
}
};
jsOMS.Event.EventManager.prototype.setDone = function(group, callback)
{
this.callbacks[group] = callback;
};
}(window.jsOMS = window.jsOMS || {}));

View File

@ -11,82 +11,63 @@
{
jsOMS.Autoloader.defineNamespace('jsOMS.UI');
jsOMS.UI.Button = function()
jsOMS.UI.Button = function (app)
{
this.buttons = {};
this.app = app;
this.actions = {};
};
jsOMS.UI.Button.prototype.bind = function(id)
jsOMS.UI.Button.prototype.bind = function (id)
{
if (typeof id !== 'undefined') {
this.bindButton(id)
} else {
let buttons = document.getElementsByTagName('form'),
length = buttons.length;
};
jsOMS.UI.Button.prototype.bindButton = function(id)
{
let e = document.getElementById(id),
actions = e.getAttribute('data-action'),
actionLength = e.length;
for(let i = 0; i < actionLength; i++) {
this.bindListener(e, actions[i]);
for (var i = 0; i < length; i++) {
if (typeof buttons[i].getAttribute('data-action') !== 'undefined') {
this.bind(buttons[i].id);
}
}
}
};
jsOMS.UI.Button.prototype.bindListener = function(e, listener)
jsOMS.UI.Button.prototype.bindButton = function (id)
{
let e = document.getElementById(id),
actions = JSON.parse(e.getAttribute('data-action')),
actionLength = actions.length,
self = this;
// todo: carefull this means a type has to be unique in a button. no multiple actions with same type!!! CHANGE!
for (let i = 1; i < actionLength; i++) {
this.app.eventManager.addGroup(actions[i - 1]['type'], id + actions[i - 1]['type']);
this.app.eventManager.setDone(id + actions[i - 1]['type'], function ()
{
// todo: how to pass result from previous action to next action?!
self.runAction(document.getElementById(id), actions[i]);
});
}
e.addEventListener('click', function (event)
{
self.runAction(document.getElementById(id), actions[0]);
});
};
jsOMS.UI.Button.prototype.add = function (name, callback)
{
this.actions[name] = callback;
};
jsOMS.UI.Button.prototype.runAction = function (e, action)
{
let self = this;
switch(listener['type']) {
case 'click':
e.addEventListener('click', function(event) {
self.runAction(this, listener['action']);
});
break;
}
};
jsOMS.UI.Button.prototype.runAction = function(e, action)
{
let result = null,
actionLength = action.length;
for(let i = 0; i < actionLength; i++) {
result = this.actionRunner(action[i], result);
}
};
jsOMS.UI.Button.prototype.actionRunner = function(action, data)
{
switch(action['type']) {
case 'load':
return this.runLoad(action);
case 'fill':
return this.runFill(action, data);
case 'show':
return this.runShow(action);
case 'hide':
return this.runHide(action);
}
};
jsOMS.UI.Button.prototype.runLoad = function(action)
{
let request = new jsOMS.Message.Request.Request();
request.send();
};
jsOMS.UI.Button.prototype.runFill = function(e, data)
{
};
jsOMS.UI.Button.prototype.runShow = function(e)
{
};
jsOMS.UI.Button.prototype.runHide = function(e)
{
this.actions[action['type']](action, function ()
{
self.app.eventManager.triggerDone(e.getAttribute('id'), e.getAttribute('id') + action['type']);
});
};
}(window.jsOMS = window.jsOMS || {}));

View File

@ -95,22 +95,25 @@
jsOMS.UI.FormManager.prototype.submit = function (form)
{
/* Handle injects */
let self = this,
let self = this,
injects = form.getSubmitInjects(),
counter = 0;
for (let property in injects) {
counter++;
this.app.requestManager.addGroup(counter, form.getId());
this.app.eventManager.addGroup(counter, form.getId());
injects[property](form.getElement(), counter, form.getId());
}
this.app.requestManager.setDone(form.getId(), function() {self.submitForm(form)});
this.app.requestManager.triggerDone('?', form.getId());
this.app.eventManager.setDone(form.getId(), function ()
{
self.submitForm(form);
});
this.app.eventManager.triggerDone('?', form.getId());
};
jsOMS.UI.FormManager.prototype.submitForm = function(form)
jsOMS.UI.FormManager.prototype.submitForm = function (form)
{
if (!form.isValid()) {
this.app.logger.debug('Form "' + form.getId() + '" has invalid values.');

View File

@ -23,6 +23,7 @@
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);
};
/**
@ -41,6 +42,7 @@
this.formManager.bind();
this.tabManager.bind();
this.tableManager.bind();
this.button.bind();
} else {
let tag = document.getElementById(id);
@ -51,6 +53,10 @@
case 'table':
this.tableManager.bind(id);
break;
case 'button':
case 'a':
this.button.bind(id);
break;
}
}
};
@ -70,6 +76,11 @@
return this.formManager;
};
jsOMS.UI.UIManager.prototype.getButton = function ()
{
return this.button;
};
/**
* Get tab manager.
*