From bd493d47a736fab4e74efbe9b603dcc8f1986512 Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Wed, 2 Feb 2011 16:18:35 -0600 Subject: Added multi-valued text widget. A multi-valued text widget has been created to replace the old IPA.details_field. The old code was designed to handle all data types, and it uses one
tag for each value, so the code is still incomplete and complex. The new code was designed to handle only multi-valued text attributes, and it uses one
tag for all values, so it's easier to maintain. There are already other widgets that can be used to handle other data types. The new code supports line-level undo and line-out for removal like the old code, but there are some changes: - Undoing a newly added line will remove the entire line. - Editing the value of a removed line will cancel the removal. - It provides 'undo all' link to reset the entire attribute. The old code will be cleaned up in a subsequent patch. --- install/ui/widget.js | 302 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 294 insertions(+), 8 deletions(-) (limited to 'install/ui/widget.js') diff --git a/install/ui/widget.js b/install/ui/widget.js index 8f3eeb62..db9d1744 100644 --- a/install/ui/widget.js +++ b/install/ui/widget.js @@ -2,6 +2,7 @@ /* Authors: * Endi Sukma Dewata * Adam Young + * Pavel Zuna * * Copyright (C) 2010 Red Hat * see file 'COPYING' for use and warranty information @@ -34,8 +35,13 @@ IPA.widget = function(spec) { that.tooltip = spec.tooltip; that.disabled = spec.disabled; + + // read_only is set during initialization that.read_only = spec.read_only; + // writable is set during load + that.writable = true; + that._entity_name = spec.entity_name; that.width = spec.width; @@ -80,7 +86,7 @@ IPA.widget = function(spec) { if ( !text || text.match(regex) ) { error_link.css('display', 'none'); that.valid = true; - }else{ + } else { error_link.css('display', 'block'); if (that.param_info.pattern_errmsg) { error_link.html(that.param_info.pattern_errmsg); @@ -127,6 +133,23 @@ IPA.widget = function(spec) { that.values = value ? [value] : []; } + that.writable = true; + + if (that.param_info.primary_key) { + that.writable = false; + } + + if ('no_update' in that.param_info.flags) { + that.writable = false; + } + + if (that.record.attributelevelrights) { + var rights = that.record.attributelevelrights[that.name]; + if (!rights || rights.indexOf('w') < 0) { + that.writable = false; + } + } + that.reset(); } @@ -272,21 +295,21 @@ IPA.text_widget = function(spec) { container.append(' '); - $("",{ - name:'error_link', - html:"Text does not match field pattern", - "class":"ui-state-error ui-corner-all", - style:"display:none" + $('', { + name: 'error_link', + html: 'Text does not match field pattern', + 'class': 'ui-state-error ui-corner-all', + style: 'display:none' }).appendTo(container); }; that.setup = function(container) { - this.widget_setup(container); + that.widget_setup(container); var input = $('input[name="'+that.name+'"]', that.container); input.keyup(function() { - if(that.undo){ + if (that.undo) { that.show_undo(); } var value = $(this).val(); @@ -343,6 +366,269 @@ IPA.text_widget = function(spec) { return that; }; +IPA.multivalued_text_widget = function(spec) { + + spec = spec || {}; + + var that = IPA.widget(spec); + + that.size = spec.size || 30; + + that.get_undo = function(index) { + if (index === undefined) { + return $('span[name="undo_all"]', that.container); + + } else { + var row = that.get_row(index); + return $('span[name="undo"]', row); + } + }; + + that.show_undo = function(index) { + var undo = that.get_undo(index); + undo.css('display', 'inline'); + }; + + that.hide_undo = function(index) { + var undo = that.get_undo(index); + undo.css('display', 'none'); + }; + + that.create = function(container) { + + var dd = $('
', { + 'class': 'first' + }).appendTo(container); + + var div = $('
', { + name: 'value' + }).appendTo(dd); + + $('', { + type: 'text', + name: that.name, + disabled: that.disabled, + size: that.size, + title: that.tooltip + }).appendTo(div); + + div.append(' '); + + $('', { + name: 'remove', + href: 'jslink', + title: 'Remove', + html: 'Remove' + }).appendTo(div); + + if (that.undo) { + div.append(' '); + that.create_undo(div); + } + + div.append(' '); + + $('', { + name: 'error_link', + html: 'Text does not match field pattern', + 'class': 'ui-state-error ui-corner-all', + style: 'display:none' + }).appendTo(div); + + $('', { + name: 'add', + href: 'jslink', + title: 'Add', + html: 'Add' + }).appendTo(dd); + + dd.append(' '); + + $('', { + name: 'undo_all', + style: 'display: none;', + 'class': 'ui-state-highlight ui-corner-all undo', + html: 'undo all' + }).appendTo(dd); + }; + + that.setup = function(container) { + + that.widget_setup(container); + + that.template = $('div[name=value]', that.container); + that.template.detach(); + + var undo = that.get_undo(); + undo.click(function() { + that.reset(); + }); + + var add_link = $('a[name=add]', that.container); + add_link.click(function() { + that.add_row(''); + var input = $('input[name="'+that.name+'"]:last', that.container); + input.focus(); + return false; + }); + }; + + that.save = function() { + var values = []; + if (that.read_only || !that.writable) { + $('label[name="'+that.name+'"]', that.container).each(function() { + var input = $(this); + var value = $.trim(input.html()); + values.push(value); + }); + + } else { + $('input[name="'+that.name+'"]', that.container).each(function() { + var input = $(this); + if (input.is('.strikethrough')) return; + + var value = $.trim(input.val()); + values.push(value); + }); + } + return values; + }; + + that.add_row = function(value) { + + var add_link = $('a[name=add]', that.container); + + var row = that.template.clone(); + row.insertBefore(add_link); + + var input = $('input[name="'+that.name+'"]', row); + var remove_link = $('a[name=remove]', row); + var undo_link = $('span[name=undo]', row); + + if (that.read_only || !that.writable) { + var label = $('