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     rf.ui.Tab = rf.ui.TogglePanelItem.extendClass({
 28             // class name
 29             name:"Tab",
 30 
 31             /**
 32              * @class AccordionItem
 33              * @name AccordionItem
 34              *
 35              * @constructor
 36              * @param {String} componentId - component id
 37              * @param {Hash} options - params
 38              * */
 39             init : function (componentId, options) {
 40                 $super.constructor.call(this, componentId, options);
 41                 this.attachToDom();
 42                 this.index = options["index"];
 43                 this.getTogglePanel().getItems()[this.index] = this;
 44             },
 45 
 46             /**
 47              * @param newState {string} = inactive | active | disabled
 48              *     in that case looking header by css class appropriate to this state
 49              *
 50              * @return {jQuery Object}
 51              * */
 52             __header : function (newState) {
 53                 var header = $(rf.getDomElement(this.id + ":header"));
 54 
 55                 for (var state in stateMap) {
 56                     if (state !== newState) {
 57                         header.removeClass(stateMap[state]);
 58                     }
 59                     if (!header.hasClass(stateMap[newState])) {
 60                         header.addClass(stateMap[newState]);
 61                     }
 62                 }
 63                 return header;
 64             },
 65 
 66             /**
 67              * @return {jQuery Object}
 68              * */
 69             __content : function () {
 70                 if (!this.__content_) {
 71                     this.__content_ = $(rf.getDomElement(this.id));
 72                 }
 73                 return this.__content_;
 74             },
 75 
 76             /**
 77              * @private
 78              *
 79              * used in TogglePanel
 80              * */
 81             __enter : function () {
 82 
 83                 this.__content().show();
 84                 this.__header("active");
 85 
 86                 return this.__fireEnter();
 87             },
 88 
 89             __fireLeave : function () {
 90                 return rf.Event.fireById(this.id + ":content", "leave");
 91             },
 92 
 93             __fireEnter : function () {
 94                 return rf.Event.fireById(this.id + ":content", "enter");
 95             },
 96 
 97             __addUserEventHandler : function (name) {
 98                 var handler = this.options["on" + name];
 99                 if (handler) {
100                     var userHandler = rf.Event.bindById(this.id + ":content", name, handler);
101                 }
102             },
103 
104             getHeight : function (recalculate) {
105                 if (recalculate || !this.__height) {
106                     this.__height = $(rf.getDomElement(this.id)).outerHeight(true)
107                 }
108 
109                 return this.__height;
110             },
111 
112             /**
113              * @private
114              *
115              * used in TogglePanel
116              * */
117             __leave : function () {
118                 var continueProcess = this.__fireLeave();
119                 if (!continueProcess) {
120                     return false;
121                 }
122 
123                 this.__content().hide();
124                 this.__header("inactive");
125 
126                 return true;
127             },
128 
129             /***************************** Private Methods ********************************************************/
130 
131 
132             destroy: function () {
133                 var parent = this.getTogglePanel();
134                 if (parent && parent.getItems && parent.getItems()[this.index]) {
135                     delete parent.getItems()[this.index];
136                 }
137 
138                 rf.Event.unbindById(this.id);
139 
140                 $super.destroy.call(this);
141             }
142         });
143 
144     // define super class link
145     var $super = rf.ui.Tab.$super;
146 
147     var stateMap = {
148         active: 'rf-tab-hdr-act',
149         inactive: 'rf-tab-hdr-inact',
150         disabled: 'rf-tab-hdr-dis'
151     };
152 })(RichFaces.jQuery, RichFaces);
153