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 
 24 (function ($, rf) {
 25 
 26     rf.ui = rf.ui || {};
 27 
 28     var __DEFAULT_OPTIONS = {
 29         expandSingle : true,
 30         bubbleSelection : true
 31     };
 32 
 33     rf.ui.PanelMenu = rf.BaseComponent.extendClass({
 34             // class name
 35             name:"PanelMenu",
 36 
 37             /**
 38              * @class PanelMenu
 39              * @name PanelMenu
 40              *
 41              * @constructor
 42              * @param {String} componentId - component id
 43              * @param {Hash} options - params
 44              * */
 45             init : function (componentId, options) {
 46                 $super.constructor.call(this, componentId);
 47                 this.items = {};
 48                 this.attachToDom();
 49 
 50                 this.options = $.extend(this.options, __DEFAULT_OPTIONS, options || {});
 51                 this.activeItem = this.__getValueInput().value;
 52                 this.nestingLevel = 0;
 53 
 54                 this.__addUserEventHandler("collapse");
 55                 this.__addUserEventHandler("expand");
 56             },
 57 
 58             addItem: function(item) {
 59                 this.items[item.itemName] = item;
 60             },
 61 
 62             deleteItem: function(item) {
 63                 delete this.items[item.itemName];
 64             },
 65 
 66             getSelectedItem: function() {
 67                 return this.getItem(this.selectedItem());
 68             },
 69 
 70             getItem: function (name) {
 71                 return this.items[name];
 72             },
 73 
 74             /***************************** Public Methods  ****************************************************************/
 75             /**
 76              * @methodOf
 77              * @name PanelMenu#selectItem
 78              *
 79              * TODO ...
 80              *
 81              * @param {String} name
 82              * @return {void} TODO ...
 83              */
 84             selectItem: function (name) {
 85                 // TODO implement
 86             },
 87 
 88             /**
 89              * @methodOf
 90              * @name PanelMenu#selectedItem
 91              *
 92              * TODO ...
 93              *
 94              * @return {String} TODO ...
 95              */
 96             selectedItem: function (id) {
 97                 if (typeof id != "undefined") {
 98                     var valueInput = this.__getValueInput();
 99                     var prevActiveItem = valueInput.value;
100 
101                     this.activeItem = id;
102                     valueInput.value = id;
103 
104                     for (var itemName in this.items) {
105                         var item = this.items[itemName];
106                         if (item.__isSelected()) {
107                             item.__unselect();
108                         }
109                     }
110 
111                     return prevActiveItem;
112                 } else {
113                     return this.activeItem;
114                 }
115             },
116 
117             __getValueInput : function() {
118                 return document.getElementById(this.id + "-value");
119             },
120 
121             /**
122              * @methodOf
123              * @name PanelMenu#expandAll
124              *
125              * TODO ...
126              *
127              * @return {void} TODO ...
128              */
129             expandAll: function () {
130                 // TODO implement
131             },
132 
133             /**
134              * @methodOf
135              * @name PanelMenu#collapseAll
136              *
137              * TODO ...
138              *
139              * @return {void} TODO ...
140              */
141             collapseAll: function () {
142                 // TODO implement
143             },
144 
145             /**
146              * @methodOf
147              * @name PanelMenu#expandGroup
148              *
149              * TODO ...
150              *
151              * @param {String} groupName
152              * @return {void} TODO ...
153              */
154             expandGroup: function (groupName) {
155                 // TODO implement
156             },
157 
158             /**
159              * @methodOf
160              * @name PanelMenu#collapseGroup
161              *
162              * TODO ...
163              *
164              * @param {String} groupName
165              * @return {void} TODO ...
166              */
167             collapseGroup: function (groupName) {
168                 // TODO implement
169             },
170 
171 
172             /***************************** Private Methods ****************************************************************/
173 
174 
175             __panelMenu  : function () {
176                 return $(rf.getDomElement(this.id));
177             },
178 
179             __childGroups : function () {
180                 return this.__panelMenu().children(".rf-pm-top-gr")
181             },
182 
183             /**
184              * @private
185              * */
186             __addUserEventHandler : function (name) {
187                 var handler = this.options["on" + name];
188                 if (handler) {
189                     //TODO nick - this will cause slowdowns in IE
190                     rf.Event.bindById(this.id, name, handler);
191                 }
192             },
193 
194             __isActiveItem: function(item) {
195                 return item.itemName == this.activeItem;
196             },
197 
198             __collapseGroups : function (source) {
199                 var topGroup = source.__rfTopGroup();
200                 this.__childGroups().each(function (index, group) {
201                     if (group.id != source.getEventElement() && (!topGroup || group.id != topGroup.id)) {
202                         rf.component(group).__collapse();
203                     }
204                 });
205 
206             },
207 
208             destroy: function () {
209                 rf.Event.unbindById(this.id, "." + this.namespace);
210                 $super.destroy.call(this);
211             }
212         });
213 
214     // define super class link
215     var $super = rf.ui.PanelMenu.$super;
216 
217 })(RichFaces.jQuery, RichFaces);
218