roojs-ui.js
[roojs1] / Roo / form / ComboNested.js
index b07ac43..b7aa8e5 100644 (file)
@@ -47,6 +47,11 @@ Roo.form.ComboNested = function(config){
 
 Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
    
+    /*
+     * @config {Number} max Number of columns to show
+     */
+    
+    maxColumns : 3,
    
     list : null, // the outermost div..
     innerLists : null, // the
@@ -92,18 +97,25 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
         this.innerLists = [];
         this.views = [];
         this.stores = [];
-        for (var i =0 ; i < 3; i++) {
+        for (var i =0 ; i < this.maxColumns; i++) {
             this.onRenderList( cls, i);
         }
         
-        if(this.allowBlank && !this.disableClear){
-            this.footer = this.list.createChild({cls:cls+'-ft'});
-            this.pageTb = new Roo.Toolbar(this.footer);  
-        }
-       
+        // always needs footer, as we are going to have an 'OK' button.
+        this.footer = this.list.createChild({cls:cls+'-ft'});
+        this.pageTb = new Roo.Toolbar(this.footer);  
+        var _this = this;
+        this.pageTb.add(  {
+            
+            text: 'Done',
+            handler: function()
+            {
+                _this.collapse();
+            }
+        });
         
-        if (this.pageTb && this.allowBlank && !this.disableClear) {
-            var _this = this;
+        if ( this.allowBlank && !this.disableClear) {
+            
             this.pageTb.add(new Roo.Toolbar.Fill(), {
                 cls: 'x-btn-icon x-btn-clear',
                 text: '&#160;',
@@ -124,8 +136,10 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
     {
         
         var lw = Math.floor(
-                ((this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / 3
-            );;
+                ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
+        );
+        
+        this.list.setWidth(lw); // default to '1'
 
         var il = this.innerLists[i] = this.list.createChild({cls:cls+'-inner'});
         //il.on('mouseover', this.onViewOver, this, { list:  i });
@@ -134,20 +148,26 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
         il.setStyle({ 'overflow-x' : 'hidden'});
 
         if(!this.tpl){
-            this.tpl = '<div class="'+cls+'-item">{' + this.displayField + '}</div>';
+            this.tpl = new Roo.Template({
+                html :  '<div class="'+cls+'-item '+cls+'-item-{cn:this.isEmpty}">{' + this.displayField + '}</div>',
+                isEmpty: function (value, allValues) {
+                    //Roo.log(value);
+                    var dl = typeof(value.data) != 'undefined' ? value.data.length : value.length; ///json is a nested response..
+                    return dl ? 'has-children' : 'no-children'
+                }
+            });
         }
         
         var store  = this.store;
         if (i > 0) {
             store  = new Roo.data.SimpleStore({
+                //fields : this.store.reader.meta.fields,
                 reader : this.store.reader,
                 data : [ ]
             });
         }
         this.stores[i]  = store;
-                
-        
-        
+                  
         var view = this.views[i] = new Roo.View(
             il,
             this.tpl,
@@ -161,17 +181,18 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
         view.getEl().setStyle({
             position: i < 1 ? 'relative' : 'absolute',
             top: 0,
-            left: (i * lw ) + 'px'           
+            left: (i * lw ) + 'px',
+            display : i > 0 ? 'none' : 'block'
         });
         view.on('selectionchange', this.onSelectChange, this, {list : i });
-        
+        view.on('dblclick', this.onDoubleClick, this, {list : i });
         //view.on('click', this.onViewClick, this, { list : i });
 
         store.on('beforeload', this.onBeforeLoad, this);
-        store.on('load', this.onLoad, this);
+        store.on('load',  this.onLoad, this, { list  : i});
         store.on('loadexception', this.onLoadException, this);
 
-          
+        // hide the other vies..
         
         
         
@@ -188,11 +209,13 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
             var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
             // only adjust heights on other ones..
             if (i < 1) {
+                
                 el.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
                 il.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
+                mh = Math.max(el.getHeight(), mh);
             }
             
-            mh = Math.max(el.getHeight(), mh);
+            
         }, this);
         
         this.list.beginUpdate();
@@ -204,11 +227,21 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
      
     
     // -- store handlers..
-    
+    // private
+    onBeforeLoad : function()
+    {
+        if(!this.hasFocus){
+            return;
+        }
+        this.innerLists[0].update(this.loadingText ?
+               '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
+        this.restrictHeight();
+        this.selectedIndex = -1;
+    },
     // private
     onLoad : function(a,b,c,d)
     {
-         
+        
         if(!this.hasFocus){
             return;
         }
@@ -219,10 +252,16 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
         } else {
             this.onEmptyResults();
         }
-        //this.stores[1].loadData([]);
-        //this.stores[2].loadData([]);
+        /*
+        this.stores[1].loadData([]);
+        this.stores[2].loadData([]);
+        this.views
+        */    
+    
         //this.el.focus();
     },
+    
+    
     // private
     onLoadException : function()
     {
@@ -240,19 +279,127 @@ Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
     onSelectChange : function (view, sels, opts )
     {
         var ix = view.getSelectedIndexes();
-        if (!ix.length|| opts.list > 2) {
-            this.stores[opts.list+1].loadData( [] );
+         
+        if (opts.list > this.maxColumns - 2) {
+            this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
+            return;
+        }
+        
+        if (!ix.length) {
+            this.setFromData({});
+            var str = this.stores[opts.list+1];
+            str.removeAll();
             return;
         }
         
-        Roo.log(ix);
         var rec = view.store.getAt(ix[0]);
-        Roo.log(rec);
+        if (!this.isLoading) {
+            this.setFromData(rec.data);
+        }
+        
         
-        this.stores[opts.list+1].loadData( typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn);
+        var lw = Math.floor(
+                ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
+        );
+        
+        this.stores[opts.list+1].loadDataFromChildren( rec );
+        var dl = this.stores[opts.list+1]. getTotalCount();
         this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
-        this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());        
+        this.views[opts.list+1].getEl().setStyle({ display : dl ? 'block' : 'none' });
+        this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
+        this.list.setWidth(lw * (opts.list + (dl ? 2 : 1)));
+        
+        if (this.isLoading) {
+            this.selectActive(opts.list);
+        }
+         
+    },
+    onDoubleClick : function()
+    {
+        this.collapse(); //??
+    },
+    
+     
+    
+    findRecord : function (prop,value)
+    {
+        return this.findRecordInStore(this.store, prop,value);
+    },
+    
+    // private
+    findRecordInStore : function(store, prop, value)
+    {
+        var cstore = new Roo.data.SimpleStore({
+            //fields : this.store.reader.meta.fields, // we need array reader.. for
+            reader : this.store.reader,
+            data : [ ]
+        });
+        var _this = this;
+        var record  = false;
+        if(store.getCount() < 1){
+            return false;
+        }
+        store.each(function(r){
+            if(r.data[prop] == value){
+                record = r;
+                return false;
+            }
+            if (r.data.cn && r.data.cn.length) {
+                cstore.loadDataFromChildren( r);
+                var cret = _this.findRecordInStore(cstore, prop, value);
+                if (cret !== false) {
+                    record = cret;
+                    return false;
+                }
+            }
+             
+            return true;
+        });
+        
+        return record;
+    },
+    
+    
+    
+    selectActive : function (lvl)
+    {
+        var cstore = new Roo.data.SimpleStore({
+            //fields : this.store.reader.meta.fields, // we need array reader.. for
+            reader : this.store.reader,
+            data : [ ]
+        });
+        // just need to determine which of the current level is selected if any..
+        var value = this.getValue();
+        var prop = this.hiddenName;
+        var store = this.stores[lvl];
+        if(store.getCount() < 1){
+            return;
+        }
+        
+        store.each(function(r){
+            // selected is at this level
+            if(r.data[prop] == value){
+                var ix = store.getIndexOf(r);
+                this.views[lvl].select(ix, false, true); // do not trigger set active..
+                return false;
+            }
+            
+            if (r.data.cn && r.data.cn.length) {
+                cstore.loadDataFromChildren(r);
+                var cret = _this.findRecordInStore(cstore, prop, value);
+                if (cret !== false) {
+                    var ix = store.getIndexOf(r);
+                    this.views[lvl].select(ix, false, false); // will trigger select change..
+                    return false;
+                }
+            }
+             
+            return true;
+        });
+        
     }
     
     
+    
+    
 });
\ No newline at end of file