      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.TogglePanelItem = rf.BaseComponent.extendClass({
     28
     29             // class name
     30             name:"TogglePanelItem",
     31
     32             init : function (componentId, options) {
     33                 $super.constructor.call(this, componentId);
     34                 this.attachToDom(this.id);
     35
     36                 this.options = $.extend(this.options, options || {});
     37                 this.name = this.options.name;
     38                 this.togglePanelId = this.options.togglePanelId;
     39                 this.switchMode = this.options.switchMode;
     40                 this.disabled = this.options.disabled || false;
     41
     42                 this.index = options["index"];
     43                 this.getTogglePanel().getItems()[this.index] = this;
     44
     45                 this.__addUserEventHandler("enter");
     46                 this.__addUserEventHandler("leave");
     47             },
     48
     49             /***************************** Public Methods *****************************************************************/
     50             /**
     51              * @methodOf TogglePanelItem
     52              * @name TogglePanelItem#getName
     53              *
     54              * @return {String} panel item name
     55              */
     56             getName: function () {
     57                 return this.options.name;
     58             },
     59
     60             /**
     61              * @methodOf
     62              * @name TogglePanelItem#getTogglePanel
     63              *
     64              * @return {TogglePanel} parent TogglePanel
     65              * */
     66             getTogglePanel : function () {
     67                 return rf.component(this.togglePanelId);
     68             },
     69
     70             /**
     71              * @methodOf
     72              * @name TogglePanelItem#isSelected
     73              *
     74              * @return {Boolean} true if this panel item is selected in the parent toggle panel
     75              * */
     76             isSelected : function () {
     77                 return this.getName() == this.getTogglePanel().getSelectItem();
     78             },
     79
     80
     81             /***************************** Private Methods ****************************************************************/
     82
     83             /**
     84              * @private
     85              * */
     86             __addUserEventHandler : function (name) {
     87                 var handler = this.options["on" + name];
     88                 if (handler) {
     89                     rf.Event.bindById(this.id, name, handler);
     90                 }
     91             },
     92
     93             /**
     94              * @private
     95              *
     96              * used in TogglePanel
     97              * */
     98             __enter : function () {
     99                 rf.getDomElement(this.id).style.display = "block";
    100
    101                 return this.__fireEnter();
    102             },
    103
    104             /**
    105              * @private
    106              *
    107              * used in TogglePanel
    108              * */
    109             __leave : function () {
    110                 var continueProcess = this.__fireLeave();
    111                 if (!continueProcess) {
    112                     return false;
    113                 }
    114
    115                 rf.getDomElement(this.id).style.display = "none";
    116                 return true;
    117             },
    118
    119             __fireLeave : function () {
    120                 return rf.Event.fireById(this.id, "leave");
    121             },
    122
    123             __fireEnter : function () {
    124                 return rf.Event.fireById(this.id, "enter");
    125             },
    126
    127             // class stuff
    128             destroy: function () {
    129                 var parent = this.getTogglePanel();
    130                 if (parent) {
    131                     delete parent.getItems()[this.index];
    132                 }
    133
    134                 $super.destroy.call(this);
    135             }
    136         });
    137
    138     // define super class link
    139     var $super = rf.ui.TogglePanelItem.$super;
    140 })(RichFaces.jQuery, RichFaces);
    141