/*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.label = spec.label; that.tooltip = spec.tooltip; 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.undo = spec.undo === undefined ? true : spec.undo; that.join = spec.join; 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.name); } 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 (that.is_empty(values) && 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 (that.is_empty(values)) { return that.valid; } var value = values[0]; for (var i=0; i metadata.maxvalue) { message = IPA.messages.widget.validation.max_value; message = message.replace('${value}', metadata.maxvalue); return { valid: false, message: message }; } } if (metadata.pattern) { var regex = new RegExp(metadata.pattern); if (!value.match(regex)) { return { valid: false, message: metadata.pattern_errmsg }; } } return { valid: true }; }; return that; }; IPA.checkbox_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.checked = spec.checked || false; 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); that.checkbox_load = that.load; /* // a checkbox will always have a value, so it's never required that.is_required = function() { return false; }; */ 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.widgets_created = function() { that.field_widgets_created(); }; 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.widget_value_changed = function() { that.set_dirty(that.test_dirty()); //that.validate(); disabling validation for now }; return that; }; IPA.select_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.widgets_created = function() { that.field_widgets_created(); }; return that; }; IPA.combobox_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.widgets_created = function() { that.field_widgets_created(); that.widget.input_field_changed.attach(that.on_input_field_changed); }; that.on_input_field_changed = function() { that.validate(); }; return that; }; IPA.link_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.other_entity = IPA.get_entity(spec.other_entity); function other_pkeys () { return that.entity.get_primary_key(); } that.other_pkeys = spec.other_pkeys || other_pkeys; that.on_link_clicked = function() { IPA.nav.show_entity_page( that.other_entity, 'default', that.other_pkeys()); }; that.load = function(record) { that.field_load(record); that.check_entity_link(); }; that.check_entity_link = function() { IPA.command({ entity: that.other_entity.name, method: 'show', args: that.other_pkeys(), options: {}, retry: false, on_success: function (result) { that.widget.is_link = result.result !== undefined; that.widget.update(that.values); } }).execute(); }; that.widgets_created = function() { that.field_widgets_created(); that.widget.link_clicked.attach(that.on_link_clicked); }; return that; }; IPA.enable_field = function(spec) { spec = spec || {}; var that = IPA.radio_field(spec); that.enable_method = spec.enable_method || 'enable'; that.disable_method = spec.enable_method || 'disable'; that.enable_option = spec.enable_option || 'TRUE'; that.get_update_info = function() { var info = IPA.update_info_builder.new_update_info(); if(that.test_dirty()) { var values = that.save(); var method = that.disable_method; if(values[0] === that.enable_option) { method = that.enable_method; } var command = IPA.command({ entity: that.entity.name, method: method, args: that.entity.get_primary_key(), options: {all: true, rights: true} }); info.append_command(command, that.priority); } return info; }; 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