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     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 || 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.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                 reader : this.store.reader,
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.onStoreLoad, 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     
225     // private
226     onLoad : function(a,b,c,d)
227     {
228         
229         if(!this.hasFocus){
230             return;
231         }
232         
233         if(this.store.getCount() > 0) {
234             this.expand();
235             this.restrictHeight();   
236         } else {
237             this.onEmptyResults();
238         }
239         /*
240         this.stores[1].loadData([]);
241         this.stores[2].loadData([]);
242         this.views
243         */    
244     
245         //this.el.focus();
246     },
247     onStoreLoad : function ()
248     {
249         Roo.log(arguments);
250     },
251     
252     // private
253     onLoadException : function()
254     {
255         this.collapse();
256         Roo.log(this.store.reader.jsonData);
257         if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
258             Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
259         }
260         
261         
262     } ,
263      
264      
265
266     onSelectChange : function (view, sels, opts )
267     {
268         var ix = view.getSelectedIndexes();
269         
270         
271         if (opts.list > 1) {
272              
273             this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
274             return;
275         }
276         
277         if (!ix.length) {
278             this.setFromData({});
279             this.stores[opts.list+1].loadData( [] );
280             return;
281         }
282         
283         var rec = view.store.getAt(ix[0]);
284         this.setFromData(rec.data)
285         
286         var lw = Math.floor(
287                 ((this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / 3
288         );
289         
290         this.stores[opts.list+1].loadData( typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn);
291         this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
292         this.views[opts.list+1].getEl().setStyle({ display : rec.data.cn.length ? 'block' : 'none' });
293         this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
294         this.list.setWidth(lw * (opts.list + (rec.data.cn.length ? 2 : 1))); 
295     },
296     onDoubleClick : function()
297     {
298         this.collapse(); //??
299     },
300     
301     setValue : function(v){
302         var text = v;
303         if(this.valueField){
304             var r = this.findRecord(this.valueField, v);
305             if(r){
306                 text = r.data[this.displayField];
307             }else if(this.valueNotFoundText !== undefined){
308                 text = this.valueNotFoundText;
309             }
310         }
311         this.lastSelectionText = text;
312         if(this.hiddenField){
313             this.hiddenField.value = v;
314         }
315         Roo.form.ComboBox.superclass.setValue.call(this, text);
316         this.value = v;
317     },
318 });