From d99ebc0f3798c84e612c79c43eb85c31b20ab1ce Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Tue, 2 Nov 2010 20:16:55 -0500 Subject: HBAC Details Page The UI framework has been extended to include a collection of widgets: - ipa_widget: base class - ipa_text_widget: text field - ipa_radio_widget: radio button - ipa_textarea_widget: textarea - ipa_button_widget: button - ipa_column_widget: column for table - ipa_table_widget: table These widgets can be used to create input controls. They can also be extended to create custom controls. The framework has also been enhanced to support custom layouts. This can be used to change the look of the application without changing the code. Initially this is only available in details section. Layout consists of a collection of HTML templates. Each template is a complete and valid HTML file representing a portion of a page. The template will be loaded and initialized by the code, then filled with the data from the server. The layouts are located in install/static/layouts/ folder. By default, if no templates are used, the fields in the details page are rendered vertically using dd/dt/dd tags. For pages that require different layout, a custom UI needs to be developed. There are two ways to do that: - write a custom widget to generate the UI dynamically - create an HTML template and write the initialization code For components that are quite complex or used frequently, it's might be better to use the first method. For simple pages that are used only in one location or need to support customization, the second method might be preferable. Other benefits of templates: - cleaner code and UI separation - more flexibility in customization - new pages can be developed quickly and require less coding - multiple templates can be used with the same initialization code - easier to maintain The HBAC details page has been implemented using both methods. By default it will use custom widgets to generate the page. To use a custom layout, add the following parameter to the URL, then reload the page: &layout= Currently the only available layout is 'default' which produces the same look as the custom widgets. The HBAC details page is usable, but it still needs additional work. The access time is not working yet. There is no undo button, hint, or validation yet. The table in the association facet has also been changed to use ipa_association_widget which is derived from ipa_table_widget. The Makefile has been updated to include the layouts. The unit tests have been updated as well. --- install/static/details.js | 366 +++++++++++++++++++++++++++++----------------- 1 file changed, 232 insertions(+), 134 deletions(-) (limited to 'install/static/details.js') diff --git a/install/static/details.js b/install/static/details.js index e4cbec77..e69a5dac 100644 --- a/install/static/details.js +++ b/install/static/details.js @@ -30,39 +30,44 @@ function ipa_details_field(spec) { spec = spec || {}; - var that = {}; - that.name = spec.name; - that.label = spec.label; + spec.create = spec.create || create; + spec.setup = spec.setup || setup; + spec.load = spec.load || load; + spec.save = spec.save || save; + + var that = ipa_widget(spec); + + function create(container) { + } - that.setup = spec.setup || setup; - that.load = spec.load || load; - that.save = spec.save || save; + function setup(container) { - function setup(container, dl, section) { + var dl = $('dl', container); - var obj_name = container.attr('title'); - var title = this.name; + var title = that.name; var label = ''; - var param_info = ipa_get_param_info(obj_name, this.name); + var param_info = ipa_get_param_info(that.entity_name, that.name); if (param_info) label = param_info['label']; if (!label) - label = this.label; + label = that.label; $('
', { - id: this.name, + id: that.name, title: title, html: label + ':' }).appendTo(dl); } - function load(container, dt, entry_attrs) { + function load(container, result) { - var obj_name = container.attr('id'); var multivalue = false; var hint_span = null; var dd; - var param_info = ipa_get_param_info(obj_name, this.name); + var dt = $('dt[title='+that.name+']', container); + if (!dt.length) return; + + var param_info = ipa_get_param_info(that.entity_name, that.name); if (param_info) { if (param_info['multivalue'] || param_info['class'] == 'List') multivalue = true; @@ -74,35 +79,35 @@ function ipa_details_field(spec) { } } - var value = entry_attrs[this.name]; + var value = result[that.name]; if (value) { dd = ipa_create_first_dd( - this.name, ipa_create_input(obj_name, this.name, value[0],hint_span) + that.name, ipa_create_input(that.entity_name, that.name, value[0],hint_span) ); dt.after(dd); var last_dd = dd; for (var i = 1; i < value.length; ++i) { dd = ipa_create_other_dd( - this.name, ipa_create_input(obj_name, this.name, value[i],hint_span) + that.name, ipa_create_input(that.entity_name, that.name, value[i],hint_span) ); last_dd.after(dd); last_dd = dd; } if (multivalue) { dd = ipa_create_other_dd( - this.name, _ipa_a_add_template.replace('A', this.name) + that.name, _ipa_a_add_template.replace('A', that.name) ); last_dd.after(dd); } } else { if (multivalue) { dd = ipa_create_first_dd( - this.name, _ipa_a_add_template.replace('A', this.name) /*.append(hint_span)*/ + that.name, _ipa_a_add_template.replace('A', that.name) /*.append(hint_span)*/ ); dt.after(dd); } else { dd = ipa_create_first_dd( - this.name, ipa_create_input(obj_name, this.name, '') /*.append(hint_span)*/ + that.name, ipa_create_input(that.entity_name, that.name, '') /*.append(hint_span)*/ ); dt.after(dd); } @@ -139,10 +144,35 @@ function ipa_details_section(spec){ var that = {}; that.name = spec.name || ''; that.label = spec.label || ''; + that.template = spec.template; + that._entity_name = spec.entity_name; + + that.setup = spec.setup || ipa_details_section_setup; + that.create = spec.create || ipa_details_section_create; + that.load = spec.load || ipa_details_section_load; that.fields = []; that.fields_by_name = {}; + that.super = function(name) { + var method = that[name]; + return function () { + return method.apply(that, arguments); + }; + }; + + that.__defineGetter__("entity_name", function(){ + return that._entity_name; + }); + + that.__defineSetter__("entity_name", function(entity_name){ + that._entity_name = entity_name; + + for (var i=0; i', { + 'id': spec.id, + 'html': spec.label, + 'class': 'ui-state-default ui-corner-all input_link' + }); - if (that.pkey||unspecified){ - ipa_details_load(container, that.pkey, null, null); - } - } + if (spec.click) button.click(spec.click); + if (spec.class) button.addClass(spec.class); + if (spec.icon) button.append(' '); - if (that.init) that.init(); + return button; +} - return that; +function ipa_details_is_dirty() { + var pkey = $.bbq.getState(this.entity_name + '-pkey', true) || ''; + return pkey != this.pkey; } -function ipa_make_button(which,text,details_class){ +function ipa_details_setup(container, unspecified) { - var button_class= details_class + - " ui-state-default ui-corner-all input_link "; - return $('',{ - "class": button_class - }). - append(' '). - append(text); + var facet = this; + + facet.setup_views(container); + + facet.pkey = $.bbq.getState(facet.entity_name + '-pkey', true) || ''; + if (!facet.pkey && !unspecified) return; + + function on_success(data, text_status, xhr) { + var result = data.result.result; + + ipa_details_cache[facet.entity_name] = $.extend(true, {}, result); + facet.create(container, result); + } + + function on_failure(xhr, text_status, error_thrown) { + var details = $('.details', container).empty(); + details.append('

Error: '+error_thrown.name+'

'); + details.append('

'+error_thrown.title+'

'); + details.append('

'+error_thrown.message+'

'); + } + + var params = []; + if (facet.pkey) params.push(facet.pkey); + + ipa_cmd( + 'show', params, {all: true}, on_success, on_failure, facet.entity_name + ); } -function ipa_details_create(container, sections) +function ipa_details_create(container, result) { + var facet = this; + if (!container) { alert('ERROR: ipa_details_create: Missing container argument!'); return; } - var obj_name = container.attr('id'); - container.attr('title', obj_name); - container.addClass('details-container'); + var entity_name = container.attr('id'); + container.attr('title', entity_name); var details = $('
', { 'class': 'details' @@ -272,81 +358,97 @@ function ipa_details_create(container, sections) 'class': 'details-buttons' }).appendTo(details); - buttons.append(ipa_make_button('ui-icon-refresh','Reset','details-reset')); - buttons.append(ipa_make_button('ui-icon-check','Update','details-update')); - - details.append('
'); - - for (var i = 0; i < sections.length; ++i) { - var section = sections[i]; - ipa_details_section_setup(container, details, section); - } + buttons.append(ipa_button({ + 'label': 'Reset', + 'icon': 'ui-icon-refresh', + 'class': 'details-reset', + 'click': function() { + ipa_details_reset(container); + return false; + } + })); -} + var pkey_name = IPA.metadata[facet.entity_name].primary_key; + buttons.append(ipa_button({ + 'label': 'Update', + 'icon': 'ui-icon-check', + 'class': 'details-update', + 'click': function() { + ipa_details_update(container, ipa_details_cache[facet.entity_name][pkey_name][0]); + return false; + } + })); -function ipa_details_section_setup(container, details, section) -{ - var id = section.name; - var name = section.label; - var fields = section.fields; + details.append('
'); + details.append('
'); - if (!fields) - return; + for (var i = 0; i < facet.sections.length; ++i) { + var section = facet.sections[i]; - details.append($("

",{ - click: function(){_h2_on_click(this)}, - html:"− "+name - })); + details.append($('

',{ + click: function(){_h2_on_click(this)}, + html:"− "+section.label + })); - var dl = $('
',{ - id:id, - "class":"entryattrs" - }).appendTo(details); + var div = $('
', { + 'id': facet.entity_name+'-'+facet.name+'-'+section.name, + 'class': 'details-section' + }).appendTo(details); - for (var i = 0; i < fields.length; ++i) { - var field = fields[i]; + section.setup(div, result); - field.setup(container, dl, section); + details.append('
'); } - - details.append('
'); } -function ipa_details_load(container, pkey, on_win, on_fail) -{ - var obj_name = container.attr('id'); - - function load_on_win(data, text_status, xhr) { - if (on_win) - on_win(data, text_status, xhr); - if (data.error) - return; +function ipa_details_section_setup(container, result) { + var section = this; + var fields = section.get_fields(); - var result = data.result.result; - ipa_details_cache[obj_name] = $.extend(true, {}, result); - ipa_details_display(container, result); + if (section.template) { + var template = IPA.get_template(section.template); + container.load(template, function(data, text_status, xhr) { + for (var i = 0; i < fields.length; ++i) { + var field = fields[i]; + field.create(container); + field.setup(container); + field.load(container, result); + } + }); + return; } - function load_on_fail(xhr, text_status, error_thrown) { - if (on_fail) - on_fail(xhr, text_status, error_thrown); + section.create(container); - var details = $('.details', container).empty(); - details.append('

Error: '+error_thrown.name+'

'); - details.append('

'+error_thrown.title+'

'); - details.append('

'+error_thrown.message+'

'); + for (var i = 0; i < fields.length; ++i) { + var field = fields[i]; + field.create(container); + field.setup(container); + field.load(container, result); } +} - var params = [pkey]; - if (!pkey){ - params = []; +function ipa_details_section_create(container, result) { + var section = this; + + var dl = $('
', { + 'id': section.name, + 'class': 'entryattrs' + }).appendTo(container); +} + +function ipa_details_section_load(container, result) { + var section = this; + var fields = section.get_fields(); + + for (var j=0; j * * arguments: - * entry_attrs - 'result' field as returned by ipa *-show commnads + * result - 'result' field as returned by ipa *-show commnads * (basically an associative array with attr:value pairs) */ -function ipa_details_display(container, entry_attrs) +function ipa_details_display(container, result) { - var obj_name = container.attr('id'); + var entity_name = container.attr('id'); /* remove all
tags i.e. all attribute values */ $('dd', container).remove(); /* go through all
tags and pair them with newly created
s */ - var facet = ipa_entity_get_details_facet(obj_name); + var facet = ipa_entity_get_details_facet(entity_name); var sections = facet.get_sections(); for (var i=0; i",{html:value.toString()}); - var param_info = ipa_get_param_info(obj_name, attr); + var param_info = ipa_get_param_info(entity_name, attr); if (!param_info) { /* no information about the param is available, default to text input */ input = _ipa_create_text_input(attr, value, null); @@ -606,7 +704,7 @@ function _ipa_create_text_input(attr, value, param_info) style:"display:none", click: function(){ var key = this.previousElementSibling.name; - var entity_divs = $(this).parents('.details-container'); + var entity_divs = $(this).parents('.entity-container'); var entry_attrs = ipa_details_cache[entity_divs[0].id]; index = calculate_dd_index($(this)); @@ -649,7 +747,7 @@ function _ipa_add_on_click(obj) var jobj = $(obj); var attr = jobj.attr('title'); var par = jobj.parent(); - var obj_name = jobj.closest('.details-container').attr('title'); + var obj_name = jobj.closest('.entity-container').attr('title'); var param_info = ipa_get_param_info(obj_name, ''); var input = _ipa_create_text_input(attr, '', param_info); -- cgit