2013-02-11 09:10:25 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 widget browser
|
|
|
|
* View & play with et2 widgets - javascript
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright 2013 Nathan Gray
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage tools
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* widget_browser shows a list of widgets, and allows you to view them one at a time.
|
|
|
|
* You can view and edit defined properties to see the effect.
|
|
|
|
*/
|
|
|
|
function widget_browser(list_div, widget_div)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Initialize etemplate2
|
|
|
|
this.et2 = new etemplate2(widget_div, "etemplate::ajax_process_content");
|
|
|
|
|
|
|
|
// Normally this would be passed from app
|
2013-04-13 21:00:13 +02:00
|
|
|
var _data = {};
|
2013-02-11 09:10:25 +01:00
|
|
|
|
|
|
|
// Create the basic widget container and attach it to the DOM
|
|
|
|
// Not really needed, but let's be consitent with et2
|
|
|
|
this.et2.widgetContainer = new et2_container(null);
|
|
|
|
this.et2.widgetContainer.setApiInstance(egw('etemplate', egw.elemWindow(this.et2.DOMContainer)));
|
|
|
|
this.et2.widgetContainer.setInstanceManager(this.et2);
|
|
|
|
this.et2.widgetContainer.setParentDOMNode(this.et2.DOMContainer);
|
|
|
|
this.et2.widgetContainer.setArrayMgrs(this.et2._createArrayManagers(_data));
|
|
|
|
|
|
|
|
// Set up UI
|
|
|
|
this.list_div = $j(list_div);
|
|
|
|
this.widget_div = $j(widget_div);
|
|
|
|
this.attribute_list = null;
|
|
|
|
|
|
|
|
// Create and popuplate the widget list
|
|
|
|
this._init_list();
|
2014-03-10 16:08:18 +01:00
|
|
|
|
2013-02-11 09:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the widget registry and create a list.
|
|
|
|
* The user can pick a widget, and we'll instanciate it.
|
|
|
|
*/
|
|
|
|
widget_browser.prototype._init_list = function()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Create list
|
|
|
|
var list = $j(document.createElement('ul'))
|
|
|
|
.attr('id', 'widgets')
|
|
|
|
.click(function(e) {self.select_widget(e);})
|
|
|
|
.appendTo(this.list_div);
|
2013-05-21 22:22:51 +02:00
|
|
|
|
|
|
|
// Sort the registry
|
|
|
|
var types = [];
|
2013-02-11 09:10:25 +01:00
|
|
|
for(var type in et2_registry)
|
|
|
|
{
|
2013-05-21 22:22:51 +02:00
|
|
|
types.push(type);
|
|
|
|
}
|
|
|
|
types.sort();
|
|
|
|
for(var i = 0; i < types.length; i++)
|
|
|
|
{
|
|
|
|
list.append('<li>'+types[i]+'</li>');
|
2014-03-10 16:08:18 +01:00
|
|
|
this.dump_attributes(types[i]);
|
2013-02-11 09:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build attribute table
|
|
|
|
attribute_table = $j(document.createElement('table'));
|
|
|
|
attribute_table.append('<thead class = "ui-widget-header"><td>'+egw().lang('Name')+"</td><td>"+egw().lang("Data type") +
|
|
|
|
"</td><td>"+egw().lang("Value") + "</td></thead>");
|
|
|
|
this.attribute_list = $j(document.createElement('tbody'))
|
|
|
|
.appendTo(attribute_table);
|
|
|
|
|
|
|
|
this.list_div.append(
|
|
|
|
$j(document.createElement('div'))
|
|
|
|
.attr('id', 'widget_attributes')
|
|
|
|
.append(attribute_table)
|
|
|
|
);
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2013-02-11 09:10:25 +01:00
|
|
|
|
2014-03-10 16:08:18 +01:00
|
|
|
widget_browser.prototype.dump_attributes = function(_type)
|
|
|
|
{
|
|
|
|
console.log(_type);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var attrs = {};
|
|
|
|
window.wb_widget = this.widget = et2_createWidget(_type, attrs, this.et2.widgetContainer);
|
|
|
|
this.widget.loadingFinished();
|
|
|
|
|
|
|
|
if(this.widget !== null && this.widget.attributes)
|
|
|
|
{
|
|
|
|
for(var attr in this.widget.attributes)
|
|
|
|
{
|
|
|
|
console.log(attr, this.widget.attributes[attr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.log('*** '+_type+' error '+(typeof e.message != 'undefined' ? e.message : e));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (this.widget)
|
|
|
|
{
|
|
|
|
this.widget.destroy();
|
|
|
|
delete this.widget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-11 09:10:25 +01:00
|
|
|
/**
|
|
|
|
* User selected a widget from the list
|
2014-03-10 16:08:18 +01:00
|
|
|
*
|
2013-02-11 09:10:25 +01:00
|
|
|
* Create an instance of the widget, get its attributes, and display it.
|
|
|
|
*/
|
|
|
|
widget_browser.prototype.select_widget = function(e,f)
|
|
|
|
{
|
|
|
|
// UI prettyness - clear selected
|
|
|
|
$j(e.target).parent().children().removeClass("ui-state-active");
|
|
|
|
|
|
|
|
// Clear previous widget
|
|
|
|
if(this.widget)
|
|
|
|
{
|
2013-02-22 01:25:41 +01:00
|
|
|
this.et2.widgetContainer.removeChild(this.widget);
|
2013-02-11 09:10:25 +01:00
|
|
|
this.widget.free();
|
2013-02-22 01:25:41 +01:00
|
|
|
this.widget = null;
|
2013-02-11 09:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the type of widget
|
|
|
|
var type = $j(e.target).text();
|
|
|
|
if(!type || e.target.nodeName != 'LI')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UI prettyness - show item as selected
|
|
|
|
$j(e.target).addClass('ui-state-active');
|
|
|
|
|
|
|
|
// Widget attributes
|
|
|
|
var attrs = {};
|
|
|
|
|
|
|
|
|
2013-04-16 20:48:06 +02:00
|
|
|
window.wb_widget = this.widget = et2_createWidget(type, attrs, this.et2.widgetContainer);
|
2013-02-11 09:10:25 +01:00
|
|
|
this.widget.loadingFinished();
|
|
|
|
|
|
|
|
// Attribute list
|
|
|
|
this.attribute_list.empty();
|
|
|
|
if(this.widget !== null && this.widget.attributes)
|
|
|
|
{
|
|
|
|
for(var attr in this.widget.attributes)
|
|
|
|
{
|
|
|
|
if(this.widget.attributes[attr].ignore) continue;
|
|
|
|
this.create_attribute(attr, this.widget.attributes[attr])
|
|
|
|
.appendTo(this.attribute_list);
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2013-02-11 09:10:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the UI (DOM) elements for a single widget attribute
|
|
|
|
*
|
|
|
|
* @param name Name of the attribute
|
|
|
|
* @param settings attribute attributes (Human name, description, etc)
|
|
|
|
*/
|
|
|
|
widget_browser.prototype.create_attribute = function(name, settings)
|
|
|
|
{
|
|
|
|
var set_function_name = "set_"+name;
|
|
|
|
var row = $j(document.createElement("tr"))
|
|
|
|
.addClass(typeof this.widget[set_function_name] == 'function' ? 'ui-state-default':'ui-state-disabled')
|
|
|
|
// Human Name
|
|
|
|
.append($j(document.createElement('td'))
|
|
|
|
.text(settings.name)
|
|
|
|
)
|
|
|
|
// Data type
|
|
|
|
.append($j(document.createElement('td'))
|
|
|
|
.text(settings.type)
|
|
|
|
);
|
|
|
|
// Add attribute name & description as a tooltip
|
|
|
|
if(settings.description)
|
|
|
|
{
|
|
|
|
egw().tooltipBind(row,settings.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value
|
|
|
|
var value = $j(document.createElement('td')).appendTo(row);
|
|
|
|
if(row.hasClass('ui-state-disabled'))
|
|
|
|
{
|
|
|
|
// No setter - just use text
|
|
|
|
value.text(this.widget.options[name]);
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setter function - maybe editable?
|
|
|
|
var self = this;
|
2013-02-25 19:52:45 +01:00
|
|
|
var input = null;
|
2013-02-11 09:10:25 +01:00
|
|
|
switch(settings.type)
|
|
|
|
{
|
|
|
|
case 'string':
|
2013-02-25 19:52:45 +01:00
|
|
|
input = $j('<input/>')
|
2013-02-11 09:10:25 +01:00
|
|
|
.change(function(e) {
|
|
|
|
self.widget[set_function_name].apply(self.widget, [$j(e.target).val()]);
|
|
|
|
});
|
2013-02-25 19:52:45 +01:00
|
|
|
input.val(this.widget.options[name]);
|
2013-02-11 09:10:25 +01:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
2013-02-25 19:52:45 +01:00
|
|
|
input = $j('<input type="checkbox"/>')
|
|
|
|
.attr("checked", this.widget.options[name])
|
2013-02-11 09:10:25 +01:00
|
|
|
.change(function(e) {
|
2013-02-25 19:52:45 +01:00
|
|
|
self.widget[set_function_name].apply(self.widget, [e.target.checked]);
|
2013-02-11 09:10:25 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value.text(this.widget.options[name]);
|
|
|
|
return row;
|
|
|
|
}
|
2013-04-13 21:00:13 +02:00
|
|
|
input.appendTo(value);
|
2013-02-11 09:10:25 +01:00
|
|
|
|
|
|
|
return row;
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2013-10-10 13:58:19 +02:00
|
|
|
|
|
|
|
egw_LAB.wait(function() {
|
|
|
|
var wb = new widget_browser(
|
|
|
|
document.getElementById("widget_list"),
|
|
|
|
document.getElementById("widget_container")
|
|
|
|
);
|
|
|
|
});
|