From a17bf5e754a8614fea5effcc3f7e580a5483932c Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Tue, 22 Nov 2011 14:58:10 +0100 Subject: Splitting widget into widget and field Splitting IPA.widget into IPA.field (logical part) and IPA.widget, IPA.input_widget (visual part). https://fedorahosted.org/freeipa/ticket/2040 --- install/ui/widget.js | 308 ++++++--------------------------------------------- 1 file changed, 34 insertions(+), 274 deletions(-) (limited to 'install/ui/widget.js') diff --git a/install/ui/widget.js b/install/ui/widget.js index e9fffaf1..59a4091d 100644 --- a/install/ui/widget.js +++ b/install/ui/widget.js @@ -32,55 +32,43 @@ IPA.widget = function(spec) { var that = {}; - - that.entity = spec.entity; - that.id = spec.id; that.name = spec.name; + that.id = spec.id; that.label = spec.label; that.tooltip = spec.tooltip; + that.entity = spec.entity; //some old widgets still need it - that.disabled = spec.disabled; - that.hidden = spec.hidden; + that.create = function(container) { + container.addClass('widget'); + that.container = container; + }; - // override the required flag in metadata - that.required = spec.required; + that.clear = function() { + }; - // read_only is set when widget is created - that.read_only = spec.read_only; + that.widget_create = that.create; - // writable is set during load - that.writable = true; + return that; +}; - that.width = spec.width; - that.height = spec.height; +IPA.input_widget = function(spec) { - that.undo = spec.undo === undefined ? true : spec.undo; - that.join = spec.join; + spec = spec || {}; - that.metadata = spec.metadata; + var that = IPA.widget(spec); - that.priority = spec.priority; + that.width = spec.width; + that.height = spec.height; - that.values = []; - that.dirty = false; - that.valid = true; + that.undo = spec.undo === undefined ? true : spec.undo; + that.writable = spec.writable; + that.read_only = spec.read_only; - that.dirty_changed = IPA.observer(); + //events + //each widget can contain several events that.value_changed = IPA.observer(); + that.undo_clicked = IPA.observer(); - var init = function() { - if (!that.metadata && that.entity) { - that.metadata = IPA.get_entity_param(that.entity.name, that.name); - } - if (that.metadata) { - if (that.label === undefined) { - that.label = that.metadata.label; - } - if (that.tooltip === undefined) { - that.tooltip = that.metadata.doc; - } - } - }; that.create_error_link = function(container) { container.append(' '); @@ -101,225 +89,16 @@ IPA.widget = function(spec) { }).appendTo(container); }; - that.is_required = function() { - if (that.read_only) return false; - if (!that.writable) return false; - - if (that.required !== undefined) return that.required; - return that.metadata && that.metadata.required; - }; - - that.set_required = function(required) { - that.required = required; - - that.update_required(); - }; - - that.update_required = function() { - if (that.required_indicator) { - that.required_indicator.css('display', that.is_required() ? 'inline' : 'none'); - } - }; - - that.validate_required = function() { - var values = that.save(); - if (!values || !values.length || values[0] === '') { - if (that.is_required()) { - that.valid = false; - that.show_error(IPA.messages.widget.validation.required); - return false; - } - } - return true; - }; - - /*returns true and clears the error message if the field value passes - the validation pattern. If the field value does not pass validation, - displays the error message and returns false. */ - that.validate = function() { - that.hide_error(); - that.valid = true; - - var values = that.save(); - if (!values) { - return that.valid; - } - if (values.length === 0) { - return that.valid; - } - var value = values[0]; - if (!value) { - return that.valid; - } - - if (!that.metadata) { - return that.valid; - } - - var message; - - if (that.metadata.type == 'int') { - if (!value.match(/^-?\d+$/)) { - that.valid = false; - that.show_error(IPA.messages.widget.validation.integer); - return that.valid; - } - - if (that.metadata.minvalue !== undefined && value < that.metadata.minvalue) { - that.valid = false; - message = IPA.messages.widget.validation.min_value; - message = message.replace('${value}', that.metadata.minvalue); - that.show_error(message); - return that.valid; - } - - if (that.metadata.maxvalue !== undefined && value > that.metadata.maxvalue) { - that.valid = false; - message = IPA.messages.widget.validation.max_value; - message = message.replace('${value}', that.metadata.maxvalue); - that.show_error(message); - return that.valid; - } - } - - if (that.metadata.pattern) { - var regex = new RegExp(that.metadata.pattern); - if (!value.match(regex)) { - that.valid = false; - that.show_error(that.metadata.pattern_errmsg); - return that.valid; - } - } - - return that.valid; - }; - - - /** - * This function compares the original values and the - * values entered in the UI. If the values have changed - * it will return true. - */ - that.test_dirty = function() { - - if (that.read_only) { - return false; - } - - var values = that.save(); - - if (!values) { // ignore null values - return false; - } - - if (!that.values) { - - if (values instanceof Array) { - - if ((values.length === 0) || - (values.length === 1) && - (values[0] === '')) { - return false; - } - } - - return true; - } - - if (values.length != that.values.length) { - return true; - } - - values.sort(); - that.values.sort(); - - for (var i=0; i