/*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.entity = spec.entity; 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; that.optional = spec.optional || false; // read_only is set when widget is created that.read_only = spec.read_only; // writable is set during load that.writable = true; 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.dirty_changed = IPA.observer(); function set_param_info(){ if (!that.param_info && that.entity){ 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; } } } function meta_validate(meta, value){ var message; if (meta.type == 'int') { if (!value.match(/^-?\d+$/)) { that.valid = false; that.show_error(IPA.messages.widget.validation.integer); return; } if (meta.minvalue !== undefined && value < meta.minvalue) { that.valid = false; message = IPA.messages.widget.validation.min_value; message = message.replace('${value}', meta.minvalue); that.show_error(message); return; } if (meta.maxvalue !== undefined && value > meta.maxvalue) { that.valid = false; message = IPA.messages.widget.validation.max_value; message = message.replace('${value}', meta.maxvalue); that.show_error(message); return; } } if (meta.pattern) { var regex = new RegExp(meta.pattern); if (!value.match(regex)) { that.valid = false; that.show_error(meta.pattern_errmsg); return; } } } that.create_error_link = function(container){ container.append(' '); $('', { name: 'error_link', html: IPA.messages.widget.validation.error, 'class': 'ui-state-error ui-corner-all', style: 'display:none' }).appendTo(container); }; that.check_required = function(){ var values = that.save(); if (!values || !values.length || values[0] === '' ) { if (that.param_info && that.param_info.required && !that.optional && !that.read_only && that.writable) { 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() { hide_error(); that.valid = true; var values = that.save(); if (!values) { return; } if (values.length === 0) { return; } var value = values[0]; if (!value) { return; } if (that.metadata) { meta_validate(that.metadata,value); } if (that.param_info) { meta_validate(that.param_info,value); } }; /** * This function compares the original values and the * values entered in the UI. If the values have changed * it will return true. */ that.test_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', { name: 'undo', style: 'display: none;', 'class': 'ui-state-highlight ui-corner-all undo', html: 'undo' }).appendTo(container); }; that.set_dirty = function(dirty) { var old = that.dirty; that.dirty = dirty; if (that.undo) { if (dirty) { that.show_undo(); } else { that.hide_undo(); } } if(old !== dirty) { that.dirty_changed.notify([], that); } }; 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'); }; function hide_error() { var error_link = that.get_error_link(); error_link.css('display', 'none'); } that.set_enabled = function() { }; that.refresh = function() { }; /*widget initialization*/ set_param_info(); // methods that should be invoked by subclasses that.widget_create = that.create; that.widget_load = that.load; that.widget_reset = that.reset; that.widget_save = that.save; that.widget_set_dirty = that.set_dirty; that.widget_test_dirty = that.test_dirty; return that; }; /*uses a browser specific technique to select a range.*/ IPA.select_range = function(input,start, end) { input.focus(); if (input[0].setSelectionRange) { input[0].setSelectionRange(start, end); } else if (input[0].createTextRange) { var range = input[0].createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } }; IPA.text_widget = function(spec) { spec = spec || {}; var that = IPA.widget(spec); that.size = spec.size || 30; that.type = spec.type || 'text'; that.select_range = function(start, end){ IPA.select_range(that.input, start, end); }; that.create = function(container) { that.widget_create(container); container.addClass('text-widget'); $('