diff --git a/Event/EventManager.js b/Event/EventManager.js index 6283ac7..088fd55 100644 --- a/Event/EventManager.js +++ b/Event/EventManager.js @@ -1,6 +1,69 @@ -(function (jsOMS, undefined) { +/** + * Request manager class. + * + * Used for pooling requests. + * + * @author OMS Development Team + * @author Dennis Eichhorn + * @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 + */ + 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 || {})); diff --git a/UI/Button.js b/UI/Button.js index b4ae904..11b8a24 100644 --- a/UI/Button.js +++ b/UI/Button.js @@ -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 || {})); diff --git a/UI/FormManager.js b/UI/FormManager.js index 2e776d5..1996e4f 100644 --- a/UI/FormManager.js +++ b/UI/FormManager.js @@ -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.'); diff --git a/UI/UIManager.js b/UI/UIManager.js index 2dc9270..91c6ec5 100644 --- a/UI/UIManager.js +++ b/UI/UIManager.js @@ -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. *