1 /*
  2  * JBoss, Home of Professional Open Source
  3  * Copyright ${year}, Red Hat, Inc. and individual contributors
  4  * by the @authors tag. See the copyright.txt in the distribution for a
  5  * full listing of individual contributors.
  6  *
  7  * This is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU Lesser General Public License as
  9  * published by the Free Software Foundation; either version 2.1 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * This software is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this software; if not, write to the Free
 19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21  */
 22 
 23 (function ($, rf) {
 24 
 25     rf.ui = rf.ui || {};
 26 
 27     var ITEMS_SWITCHER = {
 28 
 29         /**
 30          * @param {TogglePanelItem} oldPanel
 31          * @param {TogglePanelItem} newPanel
 32          *
 33          * @return {void}
 34          * */
 35         exec : function (oldPanel, newPanel) {
 36             if (newPanel.switchMode == "server") {
 37                 return this.execServer(oldPanel, newPanel);
 38             } else if (newPanel.switchMode == "ajax") {
 39                 return this.execAjax(oldPanel, newPanel);
 40             } else if (newPanel.switchMode == "client") {
 41                 return this.execClient(oldPanel, newPanel);
 42             } else {
 43                 rf.log.error("SwitchItems.exec : unknown switchMode (" + newPanel.switchMode + ")");
 44             }
 45         },
 46 
 47         /**
 48          * @protected
 49          * @param {TogglePanelItem} oldPanel
 50          * @param {TogglePanelItem} newPanel
 51          *
 52          * @return {Boolean} false
 53          * */
 54         execServer : function (oldPanel, newPanel) {
 55             if (oldPanel) {
 56                 var continueProcess = oldPanel.__leave();
 57                 if (!continueProcess) {
 58                     return false;
 59                 }
 60             }
 61 
 62             this.__setActiveItem(newPanel);
 63 
 64             var params = {};
 65 
 66             params[newPanel.getTogglePanel().id] = newPanel.name;
 67             params[newPanel.id] = newPanel.id;
 68 
 69             $.extend(params, newPanel.getTogglePanel().options["ajax"] || {});
 70 
 71             rf.submitForm(this.__getParentForm(newPanel), params);
 72 
 73             return false;
 74         },
 75 
 76         /**
 77          * @protected
 78          * @param {TogglePanelItem} oldPanel
 79          * @param {TogglePanelItem} newPanel
 80          *
 81          * @return {Boolean} false
 82          * */
 83         execAjax : function (oldPanel, newPanel) {
 84             var options = $.extend({}, newPanel.getTogglePanel().options["ajax"], {});
 85 
 86             this.__setActiveItem(newPanel);
 87             rf.ajax(newPanel.id, null, options);
 88 
 89             if (oldPanel) {
 90                 this.__setActiveItem(oldPanel);
 91             }
 92 
 93             return false;
 94         },
 95 
 96         /**
 97          * @protected
 98          * @param {TogglePanelItem} oldPanel
 99          * @param {TogglePanelItem} newPanel
100          *
101          * @return {undefined}
102          *             - false - if process has been terminated
103          *             - true  - in other cases
104          * */
105         execClient : function (oldPanel, newPanel) {
106             if (oldPanel) {
107                 var continueProcess = oldPanel.__leave();
108                 if (!continueProcess) {
109                     return false;
110                 }
111             }
112 
113             this.__setActiveItem(newPanel);
114 
115             newPanel.__enter();
116             newPanel.getTogglePanel().__fireItemChange(oldPanel, newPanel);
117 
118             return true;
119         },
120 
121         /**
122          * @private
123          * */
124         __getParentForm : function (comp) {
125             return $(rf.getDomElement(comp.id)).parents('form:first');
126         },
127 
128         /**
129          * @private
130          * */
131         __setActiveItem : function (item) {
132             rf.getDomElement(item.togglePanelId + "-value").value = item.getName(); // todo it is should be toogle panel method
133             item.getTogglePanel().activeItem = item.getName();
134         }
135     };
136 
137 
138     rf.ui.TabPanel = rf.ui.TogglePanel.extendClass({
139             // class name
140             name:"TabPanel",
141 
142             /**
143              * @class TabPanel
144              * @name TabPanel
145              *
146              * @constructor
147              * @param {String} componentId - component id
148              * @param {Hash} options - params
149              * */
150             init : function (componentId, options) {
151                 rf.ui.TogglePanel.call(this, componentId, options);
152                 this.items = [];
153 
154                 this.isKeepHeight = options["isKeepHeight"] || false;
155 
156                 this.element = document.getElementById(componentId);
157                 var $element = $(this.element);
158 
159                 $element.on("click", ".rf-tab-hdr-act", $.proxy(this.__clickListener, this))
160                 $element.on("click", ".rf-tab-hdr-inact", $.proxy(this.__clickListener, this))
161             },
162 
163             __clickListener: function(event) {
164                 var header = $(event.target);
165                 if (! header.hasClass("rf-tab-hdr")) {
166                     header = header.parents(".rf-tab-hdr").first();
167                 }
168                 var tabname = header.data('tabname');
169                 this.switchToItem(tabname);
170             },
171 
172             __itemsSwitcher : function () {
173                 return ITEMS_SWITCHER;
174             }
175 
176         });
177 })(RichFaces.jQuery, RichFaces);
178