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         this.format();
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     },
93     
94     addItem : function(cfg)
95     {
96         var item = new Roo.bootstrap.NavProgressItem(cfg);
97         
98         item.parentId = this.id;
99         item.render(this.el.select('.roo-navigation-bar', true).first(), null);
100         
101         if(cfg.html){
102             var top = new Roo.bootstrap.Element({
103                 tag : 'div',
104                 cls : 'roo-navigation-bar-text',
105                 html : (typeof(cfg.position) != 'undefined' && cfg.position == 'top') ? cfg.html : ''
106             });
107             
108             var bottom = new Roo.bootstrap.Element({
109                 tag : 'div',
110                 cls : 'roo-navigation-bar-text',
111                 html : (typeof(cfg.position) != 'undefined' && cfg.position == 'top') ? '' : cfg.html
112             });
113             
114             top.onRender(this.el.select('.roo-navigation-top-bar', true).first(), null);
115             bottom.onRender(this.el.select('.roo-navigation-bottom-bar', true).first(), null);
116             
117             item.topEl = top;
118             item.bottomEl = bottom;
119         }
120         
121         this.barItems.push(item);
122         
123         return item;
124     },
125     
126     getActive : function()
127     {
128         var active = false;
129         
130         Roo.each(this.barItems, function(v){
131             
132             if (!v.isActive()) {
133                 return;
134             }
135             
136             active = v;
137             return false;
138             
139         });
140         
141         return active;
142     },
143     
144     setActiveItem : function(item)
145     {
146         var prev = false;
147         
148         Roo.each(this.barItems, function(v){
149             if (v.rid == item.rid) {
150                 return ;
151             }
152             
153             if (v.isActive()) {
154                 v.setActive(false);
155                 prev = v;
156             }
157         });
158
159         item.setActive(true);
160         
161         this.fireEvent('changed', this, item, prev);
162     },
163     
164     getBarItem: function(rid)
165     {
166         var ret = false;
167         
168         Roo.each(this.barItems, function(e) {
169             if (e.rid != rid) {
170                 return;
171             }
172             
173             ret =  e;
174             return false;
175         });
176         
177         return ret;
178     },
179     
180     indexOfItem : function(item)
181     {
182         var index = false;
183         
184         Roo.each(this.barItems, function(v, i){
185             
186             if (v.rid != item.rid) {
187                 return;
188             }
189             
190             index = i;
191             return false
192         });
193         
194         return index;
195     },
196     
197     setActiveNext : function()
198     {
199         var i = this.indexOfItem(this.getActive());
200         
201         if (i > this.barItems.length) {
202             return;
203         }
204         
205         this.setActiveItem(this.barItems[i+1]);
206     },
207     
208     setActivePrev : function()
209     {
210         var i = this.indexOfItem(this.getActive());
211         
212         if (i  < 1) {
213             return;
214         }
215         
216         this.setActiveItem(this.barItems[i-1]);
217     },
218     
219     format : function()
220     {
221         if(!this.barItems.length){
222             return;
223         }
224         
225         var width = 100 / this.barItems.length;
226         
227         Roo.each(this.barItems, function(i){
228             i.el.setStyle('width', width + '%');
229             i.topEl.el.setStyle('width', width + '%');
230             i.bottomEl.el.setStyle('width', width + '%');
231         }, this);
232         
233     }
234     
235 });