From b9ef6ab0c412913234f05f788b3fcd3c3277eb69 Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Mon, 26 Nov 2012 14:28:32 +0100 Subject: Move of core Web UI files to AMD directory SSIA https://fedorahosted.org/freeipa/ticket/112 --- install/ui/src/freeipa/field.js | 951 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 951 insertions(+) create mode 100644 install/ui/src/freeipa/field.js (limited to 'install/ui/src/freeipa/field.js') diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js new file mode 100644 index 000000000..ff807dcfc --- /dev/null +++ b/install/ui/src/freeipa/field.js @@ -0,0 +1,951 @@ +/*jsl:import ipa.js */ +/* Authors: + * Endi Sukma Dewata + * Adam Young + * Pavel Zuna + * Petr Vobornik + * + * Copyright (C) 2011 Red Hat + * see file 'COPYING' for use and warranty information + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* REQUIRES: ipa.js, widget.js */ + +IPA.field = function(spec) { + spec = spec || {}; + + var that = {}; + + that.entity = IPA.get_entity(spec.entity); + that.container = null; + that.name = spec.name; + that.param = spec.param || spec.name; + that.label = spec.label; + that.tooltip = spec.tooltip; + that.measurement_unit = spec.measurement_unit; + that.formatter = spec.formatter; + + that.widget = null; + that.widget_name = spec.widget; + + // override the required flag in metadata + that.required = spec.required; + + // read_only is set when widget is created + that.read_only = spec.read_only; + + // writable is set during load + that.writable = true; + + that.enabled = spec.enabled === undefined ? true : spec.enabled; + that.flags = spec.flags || []; + + that.undo = spec.undo === undefined ? true : spec.undo; + + that.metadata = spec.metadata; + that.validators = spec.validators || []; + + that.priority = spec.priority; + + that.values = []; + that.dirty = false; + that.valid = true; + + that.dirty_changed = IPA.observer(); + + var init = function() { + if (!that.metadata && that.entity) { + that.metadata = IPA.get_entity_param(that.entity.name, that.param); + } + if (that.metadata) { + if (that.label === undefined) { + that.label = that.metadata.label; + } + if (that.tooltip === undefined) { + that.tooltip = that.metadata.doc; + } + } + + that.validators.push(IPA.metadata_validator()); + }; + + 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.widget && that.widget.set_required) { + that.widget.set_required(that.is_required()); + } + }; + + that.validate_required = function() { + var values = that.save(); + if (IPA.is_empty(values) && that.is_required() && that.enabled) { + that.valid = false; + var message = IPA.get_message('widget.validation.required', + "Required field"); + that.show_error(message); + 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; + + if (!that.enabled) return that.valid; + + var values = that.save(); + + if (IPA.is_empty(values)) { + return that.valid; + } + + var value = values[0]; + + for (var i=0; i -1; + + // Some objects in LDAP may not have set proper object class and + // therefore server doesn't send proper attribute rights. Flag + // 'w_if_no_aci' should be used when we want to ensure that UI + // shows edit interface in such cases. Usable only when user can + // modify object classes. + // For all others, lack of rights means no write. + if ((!rights && !(that.flags.indexOf('w_if_no_aci') > -1 && write_oc)) || + (rights && rights.indexOf('w') < 0)) { + that.writable = false; + } + } + }; + + that.reset = function() { + that.set_widget_flags(); + that.update_required(); + that.update(); + that.validate(); + that.set_dirty(false); + }; + + that.update = function() { + + if (!that.widget || !that.widget.update) return; + + var formatted_values; + + // The formatter is currently only used on read-only fields only + // because it cannot parse formatted values back to internal values. + if (that.formatter && that.read_only) { + formatted_values = []; + for (var i=0; that.values && i Number(metadata.maxvalue)) { + message = IPA.messages.widget.validation.max_value; + message = message.replace('${value}', metadata.maxvalue); + return that.false_result(message); + } + } + + if (metadata.pattern) { + var regex = new RegExp(metadata.pattern); + if (!value.match(regex)) { + return that.false_result(metadata.pattern_errmsg); + } + } + + return that.true_result(); + }; + + return that; +}; + +IPA.unsupported_validator = function(spec) { + + var that = IPA.validator(spec); + + that.unsupported = spec.unsupported || []; + that.message = spec.message || IPA.messages.widget.validation.unsupported; + + that.validate = function(value, context) { + + if (IPA.is_empty(value)) return that.true_result(); + + if (that.unsupported.indexOf(value) > -1) return that.false_result(); + + return that.true_result(); + }; + + return that; +}; + +IPA.same_password_validator = function(spec) { + + spec = spec || {}; + + var that = IPA.validator(spec); + that.other_field = spec.other_field; + + that.message = spec.message || IPA.get_message('password.password_must_match', + "Passwords must match"); + that.validate = function(value, context) { + + var other_field = context.container.fields.get_field(that.other_field); + var other_value = other_field.save(); + var this_value = context.save(); + + if (IPA.array_diff(this_value, other_value)) return that.false_result(); + + return that.true_result(); + }; + + return that; +}; + +IPA.checkbox_field = function(spec) { + + spec = spec || {}; + + var that = IPA.field(spec); + + that.checked = spec.checked || false; + that.boolean_formatter = IPA.boolean_formatter(); + + that.load = function(record) { + + that.record = record; + + that.values = that.get_value(record, that.param); + + var value = that.boolean_formatter.parse(that.values); + if (value === '') value = that.widget.checked; //default value + + that.values = [value]; + + that.load_writable(record); + + that.reset(); + }; + + that.widgets_created = function() { + + that.field_widgets_created(); + that.widget.checked = that.checked; + }; + + // a checkbox will always have a value, so it's never required + that.is_required = function() { + return false; + }; + + that.checkbox_load = that.load; + + return that; +}; + +IPA.checkboxes_field = function(spec) { + + spec = spec || {}; + + var that = IPA.field(spec); + + return that; +}; + +IPA.radio_field = function(spec) { + + spec = spec || {}; + + var that = IPA.field(spec); + + // a radio will always have a value, so it's never required + that.is_required = function() { + return false; + }; + + that.widgets_created = function() { + + that.field_widgets_created(); + }; + + return that; +}; + +IPA.multivalued_field = function(spec) { + + spec = spec || {}; + + var that = IPA.field(spec); + + that.load = function(record) { + + that.field_load(record); + }; + + that.test_dirty = function() { + var dirty = that.field_test_dirty(); + dirty = dirty || that.widget.test_dirty(); //also checks order + return dirty; + }; + + that.validate = function() { + + var values = that.save(); + + return that.validate_core(values); + }; + + that.validate_core = function(values) { + + that.hide_error(); + that.valid = true; + + if (IPA.is_empty(values)) { + return that.valid; + } + + for (var i=0; i