/*jsl:import ipa.js */ /* Authors: * Endi Sukma Dewata * Petr Vobornik * * 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(['./ipa', './jquery', './text', './field', './widget'], function(IPA, $, text) { /** * Opened dialogs * * @class * @singleton */ IPA.opened_dialogs = { /** Opened dialogs */ dialogs: [], /** Get top dialog */ top_dialog: function() { var top = null; if (this.dialogs.length) top = this.dialogs[this.dialogs.length - 1]; return top; }, /** Focus to dialog */ focus_top: function() { var top = this.top_dialog(); if (top) { top.focus_first_element(); } }, /** Add dialog */ add_dialog: function(dialog) { this.dialogs.push(dialog); }, /** Remove dialog */ remove_dialog: function(dialog) { var index = this.dialogs.indexOf(dialog); if (index > -1) this.dialogs.splice(index, 1); } }; /** * Dialog button * @class */ IPA.dialog_button = function(spec) { spec = spec || {}; var that = IPA.object(); /** @property {string} name Name */ that.name = spec.name; /** @property {string} label Label */ that.label = text.get(spec.label || spec.name); /** @property {Function} click Click handler */ that.click = spec.click || click; /** @property {boolean} visible=true Button should be visible */ that.visible = spec.visible !== undefined ? spec.visible : true; /** @property {boolean} enabled=true Button is enabled */ that.enabled = spec.enabled !== undefined ? spec.enabled : true; /** @property {jQuery} element Button element */ that.element = null; function click() { } /** * Enabled setter * @param {boolean} enabled */ that.set_enabled = function(enabled) { that.enabled = enabled; if (that.element) { that.element.prop('disabled', !enabled); } }; /** * Enabled getter * @return {boolean} */ that.is_enabled = function() { return that.enabled; }; return that; }; /** * This is a base class for dialog boxes. * @class */ IPA.dialog = function(spec) { spec = spec || {}; var that = IPA.object(); /** @property {entity.entity} entity Entity */ that.entity = IPA.get_entity(spec.entity); /** @property {string} name="dialog" Name */ that.name = spec.name || 'dialog'; /** @property {string} id ID */ that.id = spec.id; /** @property {string} title Dialog title */ that.title = text.get(spec.title); /** @property {number} width=500 Dialog width */ that.width = spec.width || 500; /** @property {number} height Dialog height */ that.height = spec.height; /** * Close dialog on Escape key press * @property {boolean} close_on_escape=true */ that.close_on_escape = spec.close_on_escape !== undefined ? spec.close_on_escape : true; // FIXME: remove facet reference // Purpose of facet reference is to obtain pkeys or ability to reload // facet. Such usage makes the code more spaghetti. It should be replaced. /** * Facet * @property {facet.facet} */ that.facet = spec.facet; /** @property {IPA.widget_container} widgets Widgets */ that.widgets = IPA.widget_container(); /** @property {IPA.field_container} fields Fields */ that.fields = IPA.field_container({ container: that }); /** @property {ordered_map} buttons Buttons */ that.buttons = $.ordered_map(); /** @property {details.facet_policies} policies Policies */ that.policies = IPA.facet_policies({ container: that, policies: spec.policies }); /** Create and add button */ that.create_button = function(spec) { var factory = spec.$factory || IPA.dialog_button; var button = factory(spec); that.add_button(button); return button; }; /** * Add button * @param {IPA.dialog_button} button */ that.add_button = function(button) { that.buttons.put(button.name, button); }; /** * Get button * @param {string} name */ that.get_button = function(name) { return that.buttons.get(name); }; /** * Add field * @param {IPA.field} field */ that.field = function(field) { that.fields.add_field(field); return that; }; /** Validate dialog fields */ that.validate = function() { var valid = true; var fields = that.fields.get_fields(); for (var i=0; i', { 'class': 'rcue-dialog-background', keydown: that.on_key_down }); var container_node = $('
', { 'class': 'rcue-dialog-container' }).appendTo(that.dom_node); that.dialog_node = $('
', { 'class': 'rcue-dialog', id: that.get_id(), 'data-name' : that.name, role: 'dialog', tabIndex: -1 // make the div focusable }).appendTo(container_node); that.header_node = $('
'); that.create_header(); that.header_node.appendTo(that.dialog_node); that.body_node = $('
', { 'class': 'rcue-dialog-body' }); // for backwards compatibility that.container = that.body_node; that.create_content(); that.body_node.appendTo(that.dialog_node); that.footer_node = $('