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-bar-top'
53                 },
54                 {
55                     tag : 'ul',
56                     cls : 'roo-navigation-bar'
57                 },
58                 {
59                     tag : 'div',
60                     cls : 'roo-navigation-bar-bottom'
61                 }
62             ]
63             
64         };
65         
66         return cfg;
67         
68     },
69     
70     initEvents: function() 
71     {
72         this.topEl = this.el.select('.roo-navigation-bar-top', true).first();
73         this.iconEl = this.el.select('.roo-navigation-bar', true).first();
74         this.bottomEl = this.el.select('.roo-navigation-bar-bottom', true).first();
75         
76     },
77     
78     onRender : function(ct, position) 
79     {
80         Roo.bootstrap.NavProgressBar.superclass.onRender.call(this, ct, position);
81         
82         if(this.bullets.length){
83             Roo.each(this.bullets, function(b){
84                this.addItem(b);
85             }, this);
86         }
87         
88     },
89     
90     addItem : function(cfg)
91     {
92         var item = new Roo.bootstrap.NavProgressItem(cfg);
93         
94         item.parentId = this.id;
95         item.render(this.el.select('.roo-navigation-bar', true).first(), null);
96         
97         if(cfg.html){
98             var html = new Roo.bootstrap.Element({
99                 tag : 'div',
100                 cls : 'roo-navigation-bar-text',
101                 html : cfg.html
102             });
103             
104             var pos = (['top', 'bottom'].indexOf(cfg.position) == -1) ? cfg.position : 'bottom';
105             
106             html.onRender(this.el.select('.roo-navigation-bar-' + pos, true).first(), null);
107         }
108         
109         this.barItems.push(item);
110         
111         this.formatBullets();
112         
113         return item;
114     },
115     
116     getActive : function()
117     {
118         var active = false;
119         
120         Roo.each(this.barItems, function(v){
121             
122             if (!v.isActive()) {
123                 return;
124             }
125             
126             active = v;
127             return false;
128             
129         });
130         
131         return active;
132     },
133     
134     setActiveItem : function(item)
135     {
136         var prev = false;
137         
138         Roo.each(this.barItems, function(v){
139             if (v.rid == item.rid) {
140                 return ;
141             }
142             
143             if (v.isActive()) {
144                 v.setActive(false);
145                 prev = v;
146             }
147         });
148
149         item.setActive(true);
150         
151         this.fireEvent('changed', this, item, prev);
152     },
153     
154     getBarItem: function(rid)
155     {
156         var ret = false;
157         
158         Roo.each(this.barItems, function(e) {
159             if (e.rid != rid) {
160                 return;
161             }
162             
163             ret =  e;
164             return false;
165         });
166         
167         return ret;
168     },
169     
170     indexOfItem : function(item)
171     {
172         var index = false;
173         
174         Roo.each(this.barItems, function(v, i){
175             
176             if (v.rid != item.rid) {
177                 return;
178             }
179             
180             index = i;
181             return false
182         });
183         
184         return index;
185     },
186     
187     setActiveNext : function()
188     {
189         var i = this.indexOfItem(this.getActive());
190         
191         if (i > this.barItems.length) {
192             return;
193         }
194         
195         this.setActiveItem(this.barItems[i+1]);
196     },
197     
198     setActivePrev : function()
199     {
200         var i = this.indexOfItem(this.getActive());
201         
202         if (i  < 1) {
203             return;
204         }
205         
206         this.setActiveItem(this.barItems[i-1]);
207     },
208     
209     formatBullets : function()
210     {
211         if(!this.barItems.length){
212             return;
213         }
214         
215         var width = 100 / this.barItems.length;
216         
217         Roo.each(this.barItems, function(i){
218             i.el.setStyle('width', width + '%');
219         }, this);
220     }
221     
222 });