/*jsl:import ipa.js */ /* Authors: * Endi Sukma Dewata * * 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.widget = function(spec) { spec = spec || {}; var that = {}; that.id = spec.id; that.name = spec.name; that.label = spec.label; that.tooltip = spec.tooltip; that.read_only = spec.read_only; 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.init = spec.init || init; that.create = spec.create || create; that.setup = spec.setup || setup; that.load = spec.load || load; that.save = spec.save || save; that.update = spec.update || update; that.validate_input = spec.validate_input || validate_input; that.valid = true; that.param_info = spec.param_info; 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. */ function validate_input(text) { if (!(that.param_info && that.param_info.pattern)) { that.valid = true; return; } var error_link = that.get_error_link(); if (!error_link) { that.valid = true; return; } var regex = new RegExp( that.param_info.pattern ); //If the field is empty, don't validate if ( !text || text.match(regex) ) { error_link.css('display', 'none'); that.valid = true; }else{ error_link.css('display', 'block'); if (that.param_info.pattern_errmsg) { error_link.html(that.param_info.pattern_errmsg); } that.valid = false; } } function init() { if (that.entity_name) { that.param_info = IPA.get_param_info(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; } } } } function create(container) { } function setup(container) { that.container = container; } /** * This function stores the entire record and the values * of the field, then invoke reset() to update the UI. */ function load(record) { that.record = record; var value = record[that.name]; if (value instanceof Array) { that.values = value; } else { that.values = value ? [value] : []; } that.reset(); } that.reset = function() { that.hide_undo(); that.update(); }; function update() { } /** * 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. */ function save() { 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() { 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', { type: 'text', name: that.name, size: that.size, title: that.tooltip }).appendTo(container); $('', { 'name': 'undo', 'style': 'display: none;', 'html': 'undo' }).appendTo(container); $("",{ 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); var input = $('input[name="'+that.name+'"]', that.container); input.keyup(function() { if(that.undo){ that.show_undo(); } var value = $('input[name="'+that.name+'"]', that.container).val(); that.validate_input(value); }); var undo = that.get_undo(); undo.click(function() { that.reset(); }); that.input = input; }; that.load = function(record) { var value = record[that.name]; if (value instanceof Array) { that.values = value; } else { that.values = value ? [value] : ['']; } if (that.read_only) { var input = $('input[name="'+that.name+'"]', that.container); var label = $('