Roo/bootstrap/NavProgressBar.js
[roojs1] / Roo / bootstrap / NavProgressBar.js
1 /*
2  * - LGPL
3  *
4  * nav progress bar
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.NavProgressBar
10  * @extends Roo.bootstrap.Component
11  * Bootstrap NavProgressBar class
12  * 
13  * @constructor
14  * Create a new nav progress bar
15  * @param {Object} config The config object
16  */
17
18 Roo.bootstrap.NavProgressBar = function(config){
19     Roo.bootstrap.NavProgressBar.superclass.constructor.call(this, config);
20
21     this.bullets = this.bullets || [];
22    
23 //    Roo.bootstrap.NavProgressBar.register(this);
24      this.addEvents({
25         /**
26              * @event changed
27              * Fires when the active item changes
28              * @param {Roo.bootstrap.NavProgressBar} this
29              * @param {Roo.bootstrap.NavProgressItem} selected The item selected
30              * @param {Roo.bootstrap.NavProgressItem} prev The previously selected item 
31          */
32         'changed': true
33      });
34     
35 };
36
37 Roo.extend(Roo.bootstrap.NavProgressBar, Roo.bootstrap.Component,  {
38     
39     bullets : [],
40     barItems : [],
41     
42     getAutoCreate : function()
43     {
44         var cfg = Roo.apply({}, Roo.bootstrap.NavProgressBar.superclass.getAutoCreate.call(this));
45         
46         cfg = {
47             tag : 'div',
48             cls : 'roo-navigation-bar-group',
49             cn : [
50                 {
51                     tag : 'div',
52                     cls : 'roo-navigation-top-bar'
53                 },
54                 {
55                     tag : 'div',
56                     cls : 'roo-navigation-bullets-bar',
57                     cn : [
58                         {
59                             tag : 'ul',
60                             cls : 'roo-navigation-bar'
61                         }
62                     ]
63                 },
64                 
65                 {
66                     tag : 'div',
67                     cls : 'roo-navigation-bottom-bar'
68                 }
69             ]
70             
71         };
72         
73         return cfg;
74         
75     },
76     
77     initEvents: function() 
78     {
79         
80     },
81     
82     onRender : function(ct, position) 
83     {
84         Roo.bootstrap.NavProgressBar.superclass.onRender.call(this, ct, position);
85         
86         if(this.bullets.length){
87             Roo.each(this.bullets, function(b){
88                this.addItem(b);
89             }, this);
90         }
91         
92         this.format();
93         
94     },
95     
96     addItem : function(cfg)
97     {
98         var item = new Roo.bootstrap.NavProgressItem(cfg);
99         
100         item.parentId = this.id;
101         item.render(this.el.select('.roo-navigation-bar', true).first(), null);
102         
103         if(cfg.html){
104             var top = new Roo.bootstrap.Element({
105                 tag : 'div',
106                 cls : 'roo-navigation-bar-text',
107                 items : [
108                     {
109                         tag : 'span',
110                         html : (typeof(cfg.position) != 'undefined' && cfg.position == 'top') ? cfg.html : ''
111                     }
112                 ]
113             });
114             
115             var bottom = new Roo.bootstrap.Element({
116                 tag : 'div',
117                 cls : 'roo-navigation-bar-text',
118                 cn : [
119                     {
120                         tag : 'span',
121                         html : (typeof(cfg.position) != 'undefined' && cfg.position == 'top') ? '' : cfg.html
122                     }
123                 ]
124             });
125             
126             top.onRender(this.el.select('.roo-navigation-top-bar', true).first(), null);
127             bottom.onRender(this.el.select('.roo-navigation-bottom-bar', true).first(), null);
128             
129             item.topEl = top;
130             item.bottomEl = bottom;
131         }
132         
133         this.barItems.push(item);
134         
135         return item;
136     },
137     
138     getActive : function()
139     {
140         var active = false;
141         
142         Roo.each(this.barItems, function(v){
143             
144             if (!v.isActive()) {
145                 return;
146             }
147             
148             active = v;
149             return false;
150             
151         });
152         
153         return active;
154     },
155     
156     setActiveItem : function(item)
157     {
158         var prev = false;
159         
160         Roo.each(this.barItems, function(v){
161             if (v.rid == item.rid) {
162                 return ;
163             }
164             
165             if (v.isActive()) {
166                 v.setActive(false);
167                 prev = v;
168             }
169         });
170
171         item.setActive(true);
172         
173         this.fireEvent('changed', this, item, prev);
174     },
175     
176     getBarItem: function(rid)
177     {
178         var ret = false;
179         
180         Roo.each(this.barItems, function(e) {
181             if (e.rid != rid) {
182                 return;
183             }
184             
185             ret =  e;
186             return false;
187         });
188         
189         return ret;
190     },
191     
192     indexOfItem : function(item)
193     {
194         var index = false;
195         
196         Roo.each(this.barItems, function(v, i){
197             
198             if (v.rid != item.rid) {
199                 return;
200             }
201             
202             index = i;
203             return false
204         });
205         
206         return index;
207     },
208     
209     setActiveNext : function()
210     {
211         var i = this.indexOfItem(this.getActive());
212         
213         if (i > this.barItems.length) {
214             return;
215         }
216         
217         this.setActiveItem(this.barItems[i+1]);
218     },
219     
220     setActivePrev : function()
221     {
222         var i = this.indexOfItem(this.getActive());
223         
224         if (i  < 1) {
225             return;
226         }
227         
228         this.setActiveItem(this.barItems[i-1]);
229     },
230     
231     format : function()
232     {
233         if(!this.barItems.length){
234             return;
235         }
236      
237         var width = 100 / this.barItems.length;
238         
239         Roo.each(this.barItems, function(i){
240             i.el.setStyle('width', width + '%');
241             i.topEl.el.setStyle('width', width + '%');
242             i.bottomEl.el.setStyle('width', width + '%');
243         }, this);
244         
245     }
246     
247 });