Builders and collections for fields and widgets

Introduced IPA.field_container and IPA.widget_container.

IPA.field_container: collection for fields. Can set logical container (facet, dialog...) to fields.
IPA.widget_container: collection for widgets. Has basic searching capability withing widget tree.

Introduced field_builder, widget_builder, section_builder, details_builder. All are used for building fields and widgets. Field_builder and widget_builder have the main building logic. Section_builder can create content based on current section spec. Details builder defines a strategy for building content.

https://fedorahosted.org/freeipa/ticket/2040
This commit is contained in:
Petr Vobornik
2011-11-22 16:00:48 +01:00
committed by Endi S. Dewata
parent 863666fbe8
commit 6cdf09812d
3 changed files with 373 additions and 0 deletions

View File

@@ -29,6 +29,139 @@
IPA.expanded_icon = 'expanded-icon';
IPA.collapsed_icon = 'collapsed-icon';
IPA.details_builder = function(spec) {
var that = {};
that.widgets = spec.container.widgets;
that.fields = spec.container.fields;
that.widget_builder = spec.widget_builder || IPA.widget_builder();
that.field_builder = spec.field_builder || IPA.field_builder();
that.section_builder = spec.section_builder || IPA.section_builder();
that.build_widget = function(spec) {
if (!spec) return;
that.widget_builder.build_widget(spec, that.widgets);
};
that.build_widgets = function(specs) {
if (!specs) return;
that.widget_builder.build_widgets(specs, that.widgets);
};
that.build_field = function(spec) {
if (!spec) return;
that.field_builder.build_field(spec, that.fields);
};
that.build_fields = function(specs) {
if (!specs) return;
that.field_builder.build_fields(specs, that.fields);
};
that.build_sections = function(specs) {
if (!specs) return;
that.section_builder.build_sections(specs);
};
that.build = function(spec) {
if (spec.sections) {
that.build_sections(spec.sections);
} else if (spec.fields && !spec.widgets) {
var sections = [
{
fields: spec.fields
}
];
that.build_sections(sections);
} else {
that.build_fields(spec.fields);
that.build_widgets(spec.widgets);
}
};
return that;
};
IPA.section_builder = function(spec) {
spec = spec || {};
var that = {};
that.container = spec.container;
that.section_factory = spec.section_factory || IPA.details_table_section;
that.field_builder = spec.field_builder;
that.widget_builder = spec.widget_builder;
that.build_sections = function(sections) {
if(!sections) return;
for (var i=0; i < sections.length; i++) {
that.build_section(sections[i], i);
}
};
that.build_section = function(section_spec, index) {
section_spec.entity = that.entity;
if (!section_spec.label && section_spec.name) {
var obj_messages = IPA.messages.objects[that.container.entity.name];
section_spec.label = obj_messages[section_spec.name];
}
if(!section_spec.name) section_spec.name = 'section'+index;
section_spec.factory = section_spec.factory || that.section_factory;
var section = section_spec.factory(section_spec);
that.container.widgets.add_widget(section);
that.create_fields(section, section_spec.fields);
};
that.create_fields = function(section, fields_spec) {
for (var i=0; i < fields_spec.length; i++) {
that.create_field(section, fields_spec[i]);
}
};
that.create_field = function(section, field_spec) {
var widget = that.widget_builder.build_widget(field_spec, section.widgets);
//spec.factory refers to widget factory
if(field_spec.factory) delete field_spec.factory;
var field = that.field_builder.build_field(field_spec, that.container.fields);
if(widget && field) {
field.widget_name = section.name+'.'+widget.name;
}
};
return that;
};
IPA.details_section = function(spec) {
spec = spec || {};

View File

@@ -572,6 +572,103 @@ IPA.link_field = function(spec) {
return that;
};
IPA.field_container = function(spec) {
spec = spec || {};
var that = {};
that.container = spec.container; //usually facet or dialog
that.fields = $.ordered_map();
that.get_field = function(name) {
return that.fields.get(name);
};
that.get_fields = function(name) {
return that.fields.values;
};
that.add_field = function(field) {
field.container = that.container;
that.fields.put(field.name, field);
};
that.widgets_created = function() {
var fields = that.fields.values;
for (var i=0; i<fields.length; i++) {
fields[i].widgets_created();
}
};
that.container_add_field = that.add_field;
return that;
};
IPA.field_builder = function(spec) {
spec = spec || {};
var that = {};
that.default_factory = spec.default_factory || IPA.field;
that.container = spec.container;
that.field_options = spec.field_options || {};
that.get_field_factory = function(spec) {
var factory;
if (spec.factory) {
factory = spec.factory;
} else if(spec.type) {
factory = IPA.field_factories[spec.type];
}
if (!factory) {
factory = that.default_factory;
}
return factory;
};
that.build_field = function(spec, container) {
container = container || that.container;
if(!(spec instanceof Object)) {
spec = { name: spec };
}
if(that.field_options) {
$.extend(spec, that.field_options);
}
var factory = that.get_field_factory(spec);
var field = factory(spec);
if(container) {
container.add_field(field);
}
return field;
};
that.build_fields = function(specs, container) {
container = container || that.container;
for(var i=0; i<specs.length; i++) {
that.build_field(specs[i], container);
}
};
return that;
};
IPA.field_factories['field'] = IPA.field;
IPA.field_factories['text'] = IPA.field;
IPA.field_factories['password'] = IPA.field;

View File

@@ -1830,6 +1830,149 @@ IPA.html_util = function() {
return that;
}();
IPA.widget_container = function(spec) {
spec = spec || {};
var that = {};
that.new_container_for_child = spec.new_container_for_child !== undefined ?
spec.new_container_for_child : true;
that.widgets = $.ordered_map();
that.widget_builder = spec.widget_builder || IPA.widget_builder();
that.add_widget = function(widget) {
that.widgets.put(widget.name, widget);
};
that.get_widget = function(path) {
var path_len = path.length;
var i = path.indexOf('.');
var name, child_path, widget, child;
if (i >= 0) {
name = path.substring(0, i);
child_path = path.substring(i + 1);
child = that.widgets.get(name);
widget = child.widgets.get_widget(child_path);
} else {
widget = that.widgets.get(path);
}
return widget;
};
that.get_widgets = function() {
return that.widgets.values;
};
that.create = function(container) {
var widgets = that.widgets.values;
for (var i=0; i<widgets.length; i++) {
var widget = widgets[i];
var child_container = container;
if(that.new_container_for_child) {
child_container = $('<div/>', {
name: widget.name,
title: widget.label,
'class': widget['class']
}).appendTo(container);
}
widget.create(child_container);
if(i < widgets.length - 1) {
that.create_widget_delimiter(container);
}
}
};
that.clear = function() {
var widgets = that.widgets.values;
for (var i=0; i<widgets.length; i++) {
widgets[i].clear();
}
};
that.create_widget_delimiter = function(container) {
};
that.widget_container_create = that.create;
that.widget_container_clear = that.clear;
return that;
};
IPA.widget_builder = function(spec) {
spec = spec || {};
var that = {};
that.default_factory = spec.default_factory || IPA.text_widget;
that.container = spec.container;
that.widget_options = spec.widget_options || {};
that.get_widget_factory = function(spec) {
var factory;
if (spec.factory) {
factory = spec.factory;
} else if(spec.type) {
factory = IPA.widget_factories[spec.type];
}
if (!factory) {
factory = that.default_factory;
}
return factory;
};
that.build_widget = function(spec, container) {
container = container || that.container;
if(!(spec instanceof Object)) {
spec = { name: spec };
}
if(that.widget_options) {
$.extend(spec, that.widget_options);
}
var factory = that.get_widget_factory(spec);
var widget = factory(spec);
if(container) {
container.add_widget(widget);
}
if(spec.widgets) {
that.build_widgets(spec.widgets, widget.widgets);
}
return widget;
};
that.build_widgets = function(specs, container) {
container = container || that.container;
for(var i=0; i<specs.length; i++) {
that.build_widget(specs[i], container);
}
};
return that;
};
IPA.widget_factories['text'] = IPA.text_widget;
IPA.widget_factories['password'] = IPA.password_widget;
IPA.widget_factories['checkbox'] = IPA.checkbox_widget;