/*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 . */ define(['dojo/_base/array', 'dojo/_base/lang', './builder', './ipa', './jquery', './phases', './reg', './text' ], function(array, lang, builder, IPA, $, phases, reg, text) { var exp = {}; IPA.checkbox_column_width = 22; IPA.required_indicator = '*'; IPA.widget = function(spec) { spec = spec || {}; var that = IPA.object(); that.name = spec.name; that.id = spec.id; that.label = text.get(spec.label); that.tooltip = text.get(spec.tooltip); that.measurement_unit = spec.measurement_unit; that.entity = IPA.get_entity(spec.entity); //some old widgets still need it that.facet = spec.facet; that.create = function(container) { container.addClass('widget'); that.container = container; }; that.clear = function() { }; that.set_visible = function(visible) { if (visible) { that.container.show(); } else { that.container.hide(); } }; that.build_child = function(spec, context, overrides) { var def_c = { entity: that.entity, facet: that.facet }; context = lang.mixin(def_c, context); var child = builder.build('widget', spec, context, overrides); return child; }; that.widget_create = that.create; return that; }; IPA.input_widget = function(spec) { spec = spec || {}; var that = IPA.widget(spec); that.width = spec.width; that.height = spec.height; that.undo = spec.undo === undefined ? true : spec.undo; that.writable = spec.writable === undefined ? true : spec.writable; that.read_only = spec.read_only; that.hidden = spec.hidden; //events //each widget can contain several events that.value_changed = IPA.observer(); that.undo_clicked = IPA.observer(); that.updated = IPA.observer(); that.create_error_link = function(container) { container.append(' '); $('', { name: 'error_link', 'class': 'ui-state-error ui-corner-all', style: 'display:none' }).appendTo(container); }; that.create_required = function(container) { that.required_indicator = $('', { 'class': 'required-indicator', text: IPA.required_indicator, style: 'display: none;' }).appendTo(container); }; 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 []; }; /** * This function creates an undo link in the container. * On_undo is a link click callback. It can be specified to custom * callback. If a callback isn't set, default callback is used. If * spefified to value other than a function, no callback is registered. */ that.create_undo = function(container, on_undo) { container.append(' '); that.undo_span = $('', { name: 'undo', style: 'display: none;', 'class': 'ui-state-highlight ui-corner-all undo', html: text.get('@i18n:widget.undo') }).appendTo(container); if(on_undo === undefined) { on_undo = function() { that.undo_clicked.notify([], that); }; } if(typeof on_undo === 'function') { that.undo_span.click(on_undo); } }; that.get_undo = function() { return $(that.undo_span); }; that.show_undo = function() { that.get_undo().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_required = function(required) { that.required = required; if (that.required_indicator) { that.required_indicator.css('display', that.required ? 'inline' : 'none'); } }; that.on_value_changed = function() { var value = that.save(); that.value_changed.notify([value], that); }; that.focus_input = function() {}; that.set_deleted = function() {}; // methods that should be invoked by subclasses that.widget_hide_error = that.hide_error; that.widget_show_error = that.show_error; 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.input_widget(spec); that.size = spec.size || 30; that.input_type = spec.input_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'); that.display_control = $('