/* 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 */ function ipa_widget(spec) { spec = spec || {}; var that = {}; that.id = spec.id; that.name = spec.name; that.label = spec.label; 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.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.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)) { return true; } var error_link = that.get_error_link(); if (!error_link) { return true; } 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'); return true; }else{ error_link.css('display', 'block'); if (that.param_info.pattern_errmsg) { error_link.html(that.param_info.pattern_errmsg); } return false; } } function init() { if (that.entity_name && !that.label){ that.param_info = ipa_get_param_info(that.entity_name, that.name); if ((that.param_info) && (that.label === undefined)){ that.label = that.param_info.label; } } } function create(container) { } function setup(container) { that.container = container; } function load(record) { that.record = record; that.reset(); } that.reset = function() { that.hide_undo(); that.update(); }; function update() { } function save() { return []; } that.is_dirty = function() { var values = that.save(); if (!values && !that.values) return false; if (!values || !that.values) return true; if (values.length != that.values.length) return true; for (var i=0; i', { 'type': 'text', 'name': that.name, 'size': that.size }).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.load = function(record) { that.values = record[that.name] || ['']; if (that.read_only) { var input = $('input[name="'+that.name+'"]', that.container); var label = $('