Roo/form/ComboNested.js
[roojs1] / Roo / form / ComboNested.js
1 /*
2  * RooJS Library 1.1.1
3  * Copyright(c) 2008-2011  Alan Knowles
4  *
5  * License - LGPL
6  */
7  
8
9 /**
10  * @class Roo.form.ComboNested
11  * @extends Roo.form.ComboBox
12  * A combobox for that allows selection of nested items in a list,
13  * eg.
14  *
15  *  Book
16  *    -> red
17  *    -> green
18  *  Table
19  *    -> square
20  *      ->red
21  *      ->green
22  *    -> rectangle
23  *      ->green
24  *      
25  * 
26  * @constructor
27  * Create a new ComboNested
28  * @param {Object} config Configuration options
29  */
30 Roo.form.ComboNested = function(config){
31     Roo.form.ComboCheck.superclass.constructor.call(this, config);
32     // should verify some data...
33     // like
34     // hiddenName = required..
35     // displayField = required
36     // valudField == required
37     var req= [ 'hiddenName', 'displayField', 'valueField' ];
38     var _t = this;
39     Roo.each(req, function(e) {
40         if ((typeof(_t[e]) == 'undefined' ) || !_t[e].length) {
41             throw "Roo.form.ComboNested : missing value for: " + e;
42         }
43     });
44      
45     
46 };
47
48 Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
49    
50     /*
51      * @config {Number} max Number of columns to show
52      */
53     
54     maxColumns : 3,
55    
56     list : null, // the outermost div..
57     innerLists : null, // the
58     views : null,
59     stores : null,
60     // private
61     onRender : function(ct, position)
62     {
63         Roo.form.ComboBox.superclass.onRender.call(this, ct, position); // skip parent call - got to above..
64         
65         if(this.hiddenName){
66             this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id:  (this.hiddenId||this.hiddenName)},
67                     'before', true);
68             this.hiddenField.value =
69                 this.hiddenValue !== undefined ? this.hiddenValue :
70                 this.value !== undefined ? this.value : '';
71
72             // prevent input submission
73             this.el.dom.removeAttribute('name');
74              
75              
76         }
77         
78         if(Roo.isGecko){
79             this.el.dom.setAttribute('autocomplete', 'off');
80         }
81
82         var cls = 'x-combo-list';
83
84         this.list = new Roo.Layer({
85             shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
86         });
87
88         var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
89         this.list.setWidth(lw);
90         this.list.swallowEvent('mousewheel');
91         this.assetHeight = 0;
92
93         if(this.title){
94             this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
95             this.assetHeight += this.header.getHeight();
96         }
97         this.innerLists = [];
98         this.views = [];
99         this.stores = [];
100         for (var i =0 ; i < this.maxColumns; i++) {
101             this.onRenderList( cls, i);
102         }
103         
104         // always needs footer, as we are going to have an 'OK' button.
105         this.footer = this.list.createChild({cls:cls+'-ft'});
106         this.pageTb = new Roo.Toolbar(this.footer);  
107         var _this = this;
108         this.pageTb.add(  {
109             
110             text: 'Done',
111             handler: function()
112             {
113                 _this.collapse();
114             }
115         });
116         
117         if ( this.allowBlank && !this.disableClear) {
118             
119             this.pageTb.add(new Roo.Toolbar.Fill(), {
120                 cls: 'x-btn-icon x-btn-clear',
121                 text: '&#160;',
122                 handler: function()
123                 {
124                     _this.collapse();
125                     _this.clearValue();
126                     _this.onSelect(false, -1);
127                 }
128             });
129         }
130         if (this.footer) {
131             this.assetHeight += this.footer.getHeight();
132         }
133         
134     },
135     onRenderList : function (  cls, i)
136     {
137         
138         var lw = Math.floor(
139                 ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
140         );
141         
142         this.list.setWidth(lw); // default to '1'
143
144         var il = this.innerLists[i] = this.list.createChild({cls:cls+'-inner'});
145         //il.on('mouseover', this.onViewOver, this, { list:  i });
146         //il.on('mousemove', this.onViewMove, this, { list:  i });
147         il.setWidth(lw);
148         il.setStyle({ 'overflow-x' : 'hidden'});
149
150         if(!this.tpl){
151             this.tpl = new Roo.Template({
152                 html :  '<div class="'+cls+'-item '+cls+'-item-{cn:this.isEmpty}">{' + this.displayField + '}</div>',
153                 isEmpty: function (value, allValues) {
154                     //Roo.log(value);
155                     var dl = typeof(value.data) != 'undefined' ? value.data.length : value.length; ///json is a nested response..
156                     return dl ? 'has-children' : 'no-children'
157                 }
158             });
159         }
160         
161         var store  = this.store;
162         if (i > 0) {
163             store  = new Roo.data.SimpleStore({
164                 //fields : this.store.reader.meta.fields,
165                 reader : this.store.reader,
166                 data : [ ]
167             });
168         }
169         this.stores[i]  = store;
170                 
171         
172         
173         var view = this.views[i] = new Roo.View(
174             il,
175             this.tpl,
176             {
177                 singleSelect:true,
178                 store: store,
179                 selectedClass: this.selectedClass
180             }
181         );
182         view.getEl().setWidth(lw);
183         view.getEl().setStyle({
184             position: i < 1 ? 'relative' : 'absolute',
185             top: 0,
186             left: (i * lw ) + 'px',
187             display : i > 0 ? 'none' : 'block'
188         });
189         view.on('selectionchange', this.onSelectChange, this, {list : i });
190         view.on('dblclick', this.onDoubleClick, this, {list : i });
191         //view.on('click', this.onViewClick, this, { list : i });
192
193         store.on('beforeload', this.onBeforeLoad, this);
194         store.on('load',  this.onLoad, this, { list  : i});
195         store.on('loadexception', this.onLoadException, this);
196
197         // hide the other vies..
198         
199         
200         
201     },
202     onResize : function()  {},
203     
204     restrictHeight : function()
205     {
206         var mh = 0;
207         Roo.each(this.innerLists, function(il,i) {
208             var el = this.views[i].getEl();
209             el.dom.style.height = '';
210             var inner = el.dom;
211             var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
212             // only adjust heights on other ones..
213             if (i < 1) {
214                 
215                 el.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
216                 il.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
217                 mh = Math.max(el.getHeight(), mh);
218             }
219             
220             
221         }, this);
222         
223         this.list.beginUpdate();
224         this.list.setHeight(mh+this.list.getFrameWidth('tb')+this.assetHeight);
225         this.list.alignTo(this.el, this.listAlign);
226         this.list.endUpdate();
227         
228     },
229      
230     
231     // -- store handlers..
232     // private
233     onBeforeLoad : function()
234     {
235         if(!this.hasFocus){
236             return;
237         }
238         this.innerLists[0].update(this.loadingText ?
239                '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
240         this.restrictHeight();
241         this.selectedIndex = -1;
242     },
243     // private
244     onLoad : function(a,b,c,d)
245     {
246         
247         if(!this.hasFocus){
248             return;
249         }
250         
251         if(this.store.getCount() > 0) {
252             this.expand();
253             this.restrictHeight();   
254         } else {
255             this.onEmptyResults();
256         }
257         /*
258         this.stores[1].loadData([]);
259         this.stores[2].loadData([]);
260         this.views
261         */    
262     
263         //this.el.focus();
264     },
265     
266     
267     // private
268     onLoadException : function()
269     {
270         this.collapse();
271         Roo.log(this.store.reader.jsonData);
272         if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
273             Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
274         }
275         
276         
277     } ,
278      
279      
280
281     onSelectChange : function (view, sels, opts )
282     {
283         var ix = view.getSelectedIndexes();
284         
285         
286         if (opts.list > 2) {
287              
288             this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
289             return;
290         }
291         
292         if (!ix.length) {
293             this.setFromData({});
294             this.stores[opts.list+1].loadData( [] );
295             return;
296         }
297         
298         var rec = view.store.getAt(ix[0]);
299         this.setFromData(rec.data);
300         
301         var lw = Math.floor(
302                 ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
303         );
304         var data =  typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn;
305         var dl = typeof(data.data) != 'undefined' ? data.total : data.length; ///json is a nested response..
306         this.stores[opts.list+1].loadData( data );
307         this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
308         this.views[opts.list+1].getEl().setStyle({ display : dl ? 'block' : 'none' });
309         this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
310         this.list.setWidth(lw * (opts.list + (dl ? 2 : 1))); 
311     },
312     onDoubleClick : function()
313     {
314         this.collapse(); //??
315     },
316     
317      
318     
319     findRecord : function (prop,value)
320     {
321         return this.findRecordInStore(this.store, prop,value);
322     },
323     
324      // private
325     findRecordInStore : function(store, prop, value)
326     {
327         var cstore = new Roo.data.SimpleStore({
328             //fields : this.store.reader.meta.fields, // we need array reader.. for
329             reader : this.store.reader,
330             data : [ ]
331         });
332         var _this = this;
333         var record  = false;
334         if(store.getCount() > 0){
335            store.each(function(r){
336                 if(r.data[prop] == value){
337                     record = r;
338                     return false;
339                 }
340                 if (r.data.cn && r.data.cn.length) {
341                     cstore.loadData( r.data.cn);
342                     var cret = _this.findRecordInStore(cstore, prop, value);
343                     if (cret !== false) {
344                         record = cret;
345                         return false;
346                     }
347                 }
348                 
349                 return true;
350             });
351         }
352         return record;
353     }
354     
355     
356     
357     
358 });