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