Added event listener system to etemplate2

This commit is contained in:
Andreas Stöckel 2011-08-16 18:18:18 +00:00
parent 03b4704685
commit 63d0baf497
2 changed files with 87 additions and 7 deletions

View File

@ -193,7 +193,13 @@ var et2_widget = Class.extend({
{ {
if (!_obj.attributes[key].ignore) if (!_obj.attributes[key].ignore)
{ {
this.setAttribute(key, _obj.getAttribute(key)); var value = _obj.getAttribute(key);
// Check whether the attribute is undefined
if (typeof value != "undefined")
{
this.setAttribute(key, value);
}
} }
} }

View File

@ -23,6 +23,7 @@
et2_selectbox; et2_selectbox;
et2_styles; et2_styles;
et2_html; et2_html;
et2_tabs;
// Requirements for the etemplate2 object // Requirements for the etemplate2 object
et2_xml; et2_xml;
@ -49,6 +50,9 @@ function etemplate2(_container, _menuaction)
// Preset the object variable // Preset the object variable
this.widgetContainer = null; this.widgetContainer = null;
// Associative array with the event listeners
this.listeners = {};
} }
/** /**
@ -132,6 +136,9 @@ etemplate2.prototype.submit = function()
// Get the form values // Get the form values
var values = this.widgetContainer.getValues(); var values = this.widgetContainer.getValues();
// Trigger the submit event
if (this.fireEvent("submit", [values]))
{
// Create the request object // Create the request object
if (typeof egw_json_request != "undefined") if (typeof egw_json_request != "undefined")
{ {
@ -140,9 +147,76 @@ etemplate2.prototype.submit = function()
} }
else else
{ {
console.log(values); et2_debug("info", "Form got submitted with values: ", values);
} }
} }
}
/**
* Adds an callback function to the given event slot
*
* @param _event is the name of the event
* @param _callback is the function which should be called once the event gets
* fired.
* @param _context is the context in which the function should be executed.
*/
etemplate2.prototype.addListener = function(_event, _callback, _context)
{
// Add the event slot if it does not exist yet
if (typeof this.listeners[_event] == "undefined")
{
this.listeners[_event] = [];
}
this.listeners[_event].push({
"callback": _callback,
"context": _context
});
}
/**
* Removes the given callback function from the given event slot.
*/
etemplate2.prototype.removeListener = function(_event, _callback)
{
if (typeof this.listeners[_event] != "undefined")
{
var events = this.listeners[_event];
for (var i = events.length - 1; i >= 0; i--)
{
if (events[i].callback == _callback)
{
events.splice(i, 1);
}
}
}
}
/**
* Fires the given event. The return values are conected via AND
*/
etemplate2.prototype.fireEvent = function(_event, _args)
{
if (typeof _args == "undefined")
{
_args = [];
}
var result = true;
if (typeof this.listeners[_event] != "undefined")
{
var events = this.listeners[_event];
for (var i = 0; i < events.length; i++)
{
result = result && events[i].callback.apply(events[i].context, _args);
}
}
return result;
}
/** /**
* Function which handles the EGW JSON et2_load response * Function which handles the EGW JSON et2_load response