Roo/bootstrap/NavGroup.js
[roojs1] / Roo / bootstrap / NavGroup.js
1 /*
2  * - LGPL
3  *
4  * nav group
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.NavGroup
10  * @extends Roo.bootstrap.Component
11  * Bootstrap NavGroup class
12  * @cfg {String} align left | right
13  * @cfg {Boolean} inverse false | true
14  * @cfg {String} type (nav|pills|tab) default nav
15  * @cfg {String} navId - reference Id for navbar.
16
17  * 
18  * @constructor
19  * Create a new nav group
20  * @param {Object} config The config object
21  */
22
23 Roo.bootstrap.NavGroup = function(config){
24     Roo.bootstrap.NavGroup.superclass.constructor.call(this, config);
25     this.navItems = [];
26    
27     Roo.bootstrap.NavGroup.register(this);
28      this.addEvents({
29         /**
30              * @event changed
31              * Fires when the active item changes
32              * @param {Roo.bootstrap.NavGroup} this
33              * @param {Roo.bootstrap.Navbar.Item} selected The item selected
34              * @param {Roo.bootstrap.Navbar.Item} prev The previously selected item 
35          */
36         'changed': true
37      });
38     
39 };
40
41 Roo.extend(Roo.bootstrap.NavGroup, Roo.bootstrap.Component,  {
42     
43     align: '',
44     inverse: false,
45     form: false,
46     type: 'nav',
47     navId : '',
48     // private
49     
50     navItems : false, 
51     
52     getAutoCreate : function()
53     {
54         var cfg = Roo.apply({}, Roo.bootstrap.NavGroup.superclass.getAutoCreate.call(this));
55         
56         cfg = {
57             tag : 'ul',
58             cls: 'nav' 
59         }
60         
61         if (['tabs','pills'].indexOf(this.type)!==-1) {
62             cfg.cls += ' nav-' + this.type
63         } else {
64             if (this.type!=='nav') {
65                 Roo.log('nav type must be nav/tabs/pills')
66             }
67             cfg.cls += ' navbar-nav'
68         }
69         
70         if (this.parent().sidebar) {
71             cfg = {
72                 tag: 'ul',
73                 cls: 'dashboard-menu sidebar-menu'
74             }
75             
76             return cfg;
77         }
78         
79         if (this.form === true) {
80             cfg = {
81                 tag: 'form',
82                 cls: 'navbar-form'
83             }
84             
85             if (this.align === 'right') {
86                 cfg.cls += ' navbar-right';
87             } else {
88                 cfg.cls += ' navbar-left';
89             }
90         }
91         
92         if (this.align === 'right') {
93             cfg.cls += ' navbar-right';
94         }
95         
96         if (this.inverse) {
97             cfg.cls += ' navbar-inverse';
98             
99         }
100         
101         
102         return cfg;
103     },
104     /**
105     * sets the active Navigation item
106     * @param {Roo.bootstrap.NavItem} the new current navitem
107     */
108     setActiveItem : function(item)
109     {
110         Roo.log('init run!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
111         var prev = false;
112         Roo.each(this.navItems, function(v){
113             if (v == item) {
114                 return ;
115             }
116             if (v.isActive()) {
117                 v.setActive(false, true);
118                 prev = v;
119                 
120             }
121             
122         });
123
124         item.setActive(true, true);
125         this.fireEvent('changed', this, item, prev);
126         
127         
128     },
129     /**
130     * gets the active Navigation item
131     * @return {Roo.bootstrap.NavItem} the current navitem
132     */
133     getActive : function()
134     {
135         
136         var prev = false;
137         Roo.each(this.navItems, function(v){
138             
139             if (v.isActive()) {
140                 prev = v;
141                 
142             }
143             
144         });
145         return prev;
146     },
147     
148     indexOfNav : function()
149     {
150         
151         var prev = false;
152         Roo.each(this.navItems, function(v,i){
153             
154             if (v.isActive()) {
155                 prev = i;
156                 
157             }
158             
159         });
160         return prev;
161     },
162     /**
163     * adds a Navigation item
164     * @param {Roo.bootstrap.NavItem} the navitem to add
165     */
166     addItem : function(cfg)
167     {
168         var cn = new Roo.bootstrap.NavItem(cfg);
169         this.register(cn);
170         cn.parentId = this.id;
171         cn.onRender(this.el, null);
172         return cn;
173     },
174     /**
175     * register a Navigation item
176     * @param {Roo.bootstrap.NavItem} the navitem to add
177     */
178     register : function(item)
179     {
180         this.navItems.push( item);
181         item.navId = this.navId;
182     
183     },
184     
185     /**
186     * clear all the Navigation item
187     */
188    
189     clearAll : function()
190     {
191         this.navItems = [];
192         this.el.dom.innerHTML = '';
193     },
194     
195     getNavItem: function(tabId)
196     {
197         var ret = false;
198         Roo.each(this.navItems, function(e) {
199             if (e.tabId == tabId) {
200                ret =  e;
201                return false;
202             }
203             return true;
204             
205         });
206         return ret;
207     },
208     
209     setActiveNext : function()
210     {
211         var i = this.indexOfNav(this.getActive());
212         if (i > this.navItems.length) {
213             return;
214         }
215         this.setActiveItem(this.navItems[i+1]);
216     },
217     setActivePrev : function()
218     {
219         var i = this.indexOfNav(this.getActive());
220         if (i  < 1) {
221             return;
222         }
223         this.setActiveItem(this.navItems[i-1]);
224     },
225     clearWasActive : function(except) {
226         Roo.each(this.navItems, function(e) {
227             if (e.tabId != except.tabId && e.was_active) {
228                e.was_active = false;
229                return false;
230             }
231             return true;
232             
233         });
234     },
235     getWasActive : function ()
236     {
237         var r = false;
238         Roo.each(this.navItems, function(e) {
239             if (e.was_active) {
240                r = e;
241                return false;
242             }
243             return true;
244             
245         });
246         return r;
247     }
248     
249     
250 });
251
252  
253 Roo.apply(Roo.bootstrap.NavGroup, {
254     
255     groups: {},
256      /**
257     * register a Navigation Group
258     * @param {Roo.bootstrap.NavGroup} the navgroup to add
259     */
260     register : function(navgrp)
261     {
262         this.groups[navgrp.navId] = navgrp;
263         
264     },
265     /**
266     * fetch a Navigation Group based on the navigation ID
267     * @param {string} the navgroup to add
268     * @returns {Roo.bootstrap.NavGroup} the navgroup 
269     */
270     get: function(navId) {
271         if (typeof(this.groups[navId]) == 'undefined') {
272             return false;
273             //this.register(new Roo.bootstrap.NavGroup({ navId : navId }));
274         }
275         return this.groups[navId] ;
276     }
277     
278     
279     
280 });
281
282