First implementation of data-action

Remaining todos are passing results from one action to the next and
allowing the same action time multiple times.
This commit is contained in:
Dennis Eichhorn 2016-06-26 16:25:51 +02:00
parent 30ba6233a0
commit 804bf24513

View File

@ -14,66 +14,116 @@
/** @namespace jsOMS.UI */ /** @namespace jsOMS.UI */
jsOMS.Autoloader.defineNamespace('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.Button = function (app) jsOMS.UI.Button = function (app)
{ {
this.app = app; this.app = app;
this.actions = {}; this.actions = {};
}; };
/**
* Bind button.
*
* @param {string} [id] Button id (optional)
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.Button.prototype.bind = function (id) jsOMS.UI.Button.prototype.bind = function (id)
{ {
if (typeof id !== 'undefined') { if (typeof id !== 'undefined') {
this.bindButton(id); this.bindButton(document.getElementById(id));
} else { } else {
let buttons = document.getElementsByTagName('button'), let buttons = document.getElementsByTagName('button'),
length = buttons.length; length = buttons.length;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (typeof buttons[i].getAttribute('data-action') !== 'undefined' && buttons[i].getAttribute('id') !== null) { if (buttons[i].getAttribute('data-action') !== null) {
this.bindButton(buttons[i].getAttribute('id')); this.bindButton(buttons[i]);
} }
} }
} }
}; };
jsOMS.UI.Button.prototype.bindButton = function (id) /**
* Bind button.
*
* @param {Element} e Button element
*
* @method
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.Button.prototype.bindButton = function (e)
{ {
let e = document.getElementById(id), let actions = JSON.parse(e.getAttribute('data-action')),
actions = JSON.parse(e.getAttribute('data-action')),
actionLength = actions.length, actionLength = actions.length,
self = this; self = this;
// todo: carefull this means a type has to be unique in a button. no multiple actions with same type!!! CHANGE! // For everey action an event is registered
for (let i = 1; i < actionLength; i++) { for (let i = 1; i < actionLength; i++) {
this.app.eventManager.addGroup(actions[i - 1].type, id + actions[i - 1].type); // todo: right now one event type can only exist once... needs fixing!!!
this.app.eventManager.setDone(id + actions[i - 1].type, function () this.app.eventManager.addGroup(actions[i - 1].type, e.id + actions[i - 1].type);
this.app.eventManager.setDone(e.id + actions[i - 1].type, function ()
{ {
// todo: how to pass result from previous action to next action?! // todo: how to pass result from previous action to next action?!
self.runAction(document.getElementById(id), actions[i]); self.runAction(e, actions[i]);
}); });
} }
e.addEventListener('click', function (event) // Register event for first action
e.addEventListener('click', function ()
{ {
self.runAction(document.getElementById(id), actions[0]); self.runAction(this, actions[0]);
}); });
}; };
/**
* 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.Button.prototype.add = function (name, callback) jsOMS.UI.Button.prototype.add = function (name, callback)
{ {
this.actions[name] = callback; this.actions[name] = callback;
}; };
/**
* 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.Button.prototype.runAction = function (e, action) jsOMS.UI.Button.prototype.runAction = function (e, action)
{ {
let self = this; let self = this;
// todo: why am i accessing this.actions? shouldn't the callback be stored in the event manager?
// todo: what happens if i click the same button again? isn't it now removed from the eventmanager?
this.actions[action.type](action, function () this.actions[action.type](action, function ()
{ {
// todo: the event manager needs optional parameter to pass data to the callback. self.app.eventManager.trigger(e.id, e.id + action.type, false);
self.app.eventManager.triggerDone(e.getAttribute('id'), e.getAttribute('id') + action.type);
}); });
}; };
}(window.jsOMS = window.jsOMS || {})); }(window.jsOMS = window.jsOMS || {}));