/*jsl:import ipa.js */ /* Authors: * Endi Sukma Dewata * Adam Young * Pavel Zuna * * Copyright (C) 2010 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 */ IPA.checkbox_column_width = 22; IPA.widget = function(spec) { spec = spec || {}; var that = {}; that.id = spec.id; that.name = spec.name; that.label = spec.label; that.tooltip = spec.tooltip; that.disabled = spec.disabled; that.hidden = spec.hidden; that.conditional = spec.conditional; // 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; that.height = spec.height; that.undo = typeof spec.undo == 'undefined' ? true : spec.undo; that.join = spec.join; that.param_info = spec.param_info; that.metadata = spec.metadata; that.values = []; that.dirty = false; that.valid = true; that.__defineGetter__("entity_name", function(){ return that._entity_name; }); that.__defineSetter__("entity_name", function(entity_name){ that._entity_name = entity_name; }); /*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 || !values.length) { return; } var value = values[0]; if (!value) { return; } if (that.metadata) { if (that.metadata.type == 'int') { if (!value.match(/^-?\d+$/)) { that.valid = false; // TODO: I18n that.show_error('must be an integer'); return; } if (that.metadata.minvalue && value < that.metadata.minvalue) { that.valid = false; // TODO: I18n that.show_error('minimum value is '+that.metadata.minvalue); return; } if (that.metadata.maxvalue && value > that.metadata.maxvalue) { that.valid = false; // TODO: I18n that.show_error('maximum value is '+that.metadata.maxvalue); return; } } } if (that.param_info) { if (that.param_info.pattern) { var regex = new RegExp(that.param_info.pattern); if (!value.match(regex)) { that.valid = false; that.show_error(that.param_info.pattern_errmsg); return; } } } }; that.init = function() { if (that.entity_name) { that.param_info = IPA.get_entity_param(that.entity_name, that.name); if (that.param_info) { if (that.label === undefined) { that.label = that.param_info.label; } if (that.tooltip === undefined) { that.tooltip = that.param_info.doc; } } } }; that.create = function(container) { that.container = container; }; that.setup = function(container) { that.container = container; }; /** * This function stores the entire record and the values * of the field, then invoke reset() to update the UI. */ that.load = function(record) { that.record = record; var value = record[that.name]; if (value instanceof Array) { that.values = value; } else { that.values = value ? [value] : []; } that.writable = true; if (that.param_info) { if (that.param_info.primary_key) { that.writable = false; } if (that.param_info.flags && '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(); }; that.reset = function() { that.set_dirty(false); that.update(); }; that.update = function() { }; /** * This function saves the values entered in the UI. * It returns the values in an array, or null if * the field should not be saved. */ that.save = function() { return that.values; }; /** * This function compares the original values and the * values entered in the UI. If the values have changed * it will return true. */ that.is_dirty = function() { return that.dirty; }; that.create_undo = function(container) { that.undo_span = $('', { name: 'undo', style: 'display: none;', 'class': 'ui-state-highlight ui-corner-all undo', html: 'undo' }).appendTo(container); }; that.set_dirty = function(dirty) { that.dirty = dirty; if (that.undo) { if (dirty) { that.show_undo(); } else { that.hide_undo(); } } }; that.get_undo = function() { return $(that.undo_span); }; that.show_undo = function() { $(that.undo_span).css('display', 'inline'); }; that.hide_undo = function() { $(that.undo_span).css('display', 'none'); }; that.get_error_link = function() { return $('span[name="error_link"]', that.container); }; that.show_error = function(message) { var error_link = that.get_error_link(); error_link.html(message); error_link.css('display', 'block'); }; that.hide_error = function() { var error_link = that.get_error_link(); error_link.css('display', 'none'); }; that.set_enabled = function() { }; that.refresh = function() { }; // methods that should be invoked by subclasses that.widget_init = that.init; that.widget_create = that.create; that.widget_setup = that.setup; that.widget_load = that.load; that.widget_reset = that.reset; that.widget_save = that.save; that.widget_set_dirty = that.set_dirty; return that; }; IPA.text_widget = function(spec) { spec = spec || {}; var that = IPA.widget(spec); that.size = spec.size || 30; that.type = spec.type || 'text'; that.create = function(container) { $('