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 : 'ul',
56                     cls : 'roo-navigation-bar'
57                 },
58                 {
59                     tag : 'div',
60                     cls : 'roo-navigation-bottom-bar'
61                 }
62             ]
63             
64         };
65         
66         return cfg;
67         
68     },
69     
70     initEvents: function() 
71     {
72         Roo.log('initEvent????');
73         
74     },
75     
76     onRender : function(ct, position) 
77     {
78         Roo.log('render bar???');
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, null);
96         
97         this.barItems.push(item);
98         
99         this.formatBullets();
100         
101         return item;
102     },
103     
104     getActive : function()
105     {
106         var active = false;
107         
108         Roo.each(this.barItems, function(v){
109             
110             if (!v.isActive()) {
111                 return;
112             }
113             
114             active = v;
115             return false;
116             
117         });
118         
119         return active;
120     },
121     
122     setActiveItem : function(item)
123     {
124         var prev = false;
125         
126         Roo.each(this.barItems, function(v){
127             if (v.rid == item.rid) {
128                 return ;
129             }
130             
131             if (v.isActive()) {
132                 v.setActive(false);
133                 prev = v;
134             }
135         });
136
137         item.setActive(true);
138         
139         this.fireEvent('changed', this, item, prev);
140     },
141     
142     getBarItem: function(rid)
143     {
144         var ret = false;
145         
146         Roo.each(this.barItems, function(e) {
147             if (e.rid != rid) {
148                 return;
149             }
150             
151             ret =  e;
152             return false;
153         });
154         
155         return ret;
156     },
157     
158     indexOfItem : function(item)
159     {
160         var index = false;
161         
162         Roo.each(this.barItems, function(v, i){
163             
164             if (v.rid != item.rid) {
165                 return;
166             }
167             
168             index = i;
169             return false
170         });
171         
172         return index;
173     },
174     
175     setActiveNext : function()
176     {
177         var i = this.indexOfItem(this.getActive());
178         
179         if (i > this.barItems.length) {
180             return;
181         }
182         
183         this.setActiveItem(this.barItems[i+1]);
184     },
185     
186     setActivePrev : function()
187     {
188         var i = this.indexOfItem(this.getActive());
189         
190         if (i  < 1) {
191             return;
192         }
193         
194         this.setActiveItem(this.barItems[i-1]);
195     },
196     
197     formatBullets : function()
198     {
199         if(!this.barItems.length){
200             return;
201         }
202         
203         var width = 100 / this.barItems.length;
204         
205         Roo.each(this.barItems, function(i){
206             i.el.setStyle('width', width + '%');
207         }, this);
208     }
209     
210 });