sync
[roojs1] / Roo / bootstrap / CardUploader.js
index eb26195..b6a0f39 100644 (file)
@@ -23,41 +23,63 @@ Roo.bootstrap.CardUploader = function(config){
     Roo.bootstrap.CardUploader.superclass.constructor.call(this, config);
     
     
-    this.imageCollection  = new Roo.util.MixedCollection(function(r) { return r.id });
+    this.fileCollection   = new Roo.util.MixedCollection(false,function(r) {
+        return r.data.id
+        });
     
     
 };
 
-Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
+Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input,  {
     
      
     errorTimeout : 3000,
      
     images : false,
    
-    imageCollection : false,
+    fileCollection : false,
+    allowBlank : true,
     
     getAutoCreate : function()
     {
-        var btn = Roo.bootstrap.Button.prototype.getAutoCreate.call(this);
         
-        var cfg = {
-            cls : 'roo-card-uploader-wrap',
+        var cfg =  {
+            cls :'form-group' ,
             cn : [
-                btn,
+               
                 {
-                    tag : 'input',
-                    cls : 'd-none roo-card-upload-selector',
+                    tag: 'label',
+                   //cls : 'input-group-addon',
+                    html : this.fieldLabel
+
+                },
+
+                {
+                    tag: 'input',
+                    type : 'hidden',
+                    value : this.value,
+                    cls : 'd-none  form-control'
+                },
+                
+                {
+                    tag: 'input',
+                    multiple : 'multiple',
                     type : 'file',
-                    multiple : 'multiple'
+                    cls : 'd-none  roo-card-upload-selector'
+                },
+                
+                {
+                    cls : 'roo-card-uploader-button-container w-100 mb-2'
                 },
                 {
                     cls : 'card-columns roo-card-uploader-container'
                 }
+
             ]
-            
-        }
+        };
+           
         
+         
         
         return cfg;
     },
@@ -67,10 +89,34 @@ Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
         return this.containerEl;
     },
    
-    
+    getButtonContainer : function() /// what children are added to.
+    {
+        return this.el.select(".roo-card-uploader-button-container").first();
+    },
+   
     initEvents : function()
     {
-        Roo.bootstrap.Button.prototype.initEvents.call(this);
+        
+        Roo.bootstrap.Input.prototype.initEvents.call(this);
+        
+        var t = this;
+        this.addxtype({
+            xns: Roo.bootstrap,
+
+            xtype : 'Button',
+            container_method : 'getButtonContainer' ,            
+            html : 'Upload File / Images', // fix changable?
+            cls : 'w-100 ',
+            listeners : {
+                'click' : function(btn, e) {
+                    t.onClick(e);
+                }
+            }
+        })
+        
+        
+        
+        
         this.urlAPI = (window.createObjectURL && window) || 
                                 (window.URL && URL.revokeObjectURL && URL) || 
                                 (window.webkitURL && webkitURL);
@@ -80,7 +126,7 @@ Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
          
         this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
         
-         this.selectorEl.on('change', this.onFileSelected, this);
+        this.selectorEl.on('change', this.onFileSelected, this);
         if (this.images) {
             var t = this;
             this.images.forEach(function(img) {
@@ -145,7 +191,8 @@ Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
             is_uploaded : false,
             src : url,
             title : file.name,
-            mimetype : file.mimetype,
+            mimetype : file.type,
+            preview : false,
         })
         
     },
@@ -155,50 +202,94 @@ Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
         // hidden input element?
         // if the file is not an image...
         //then we need to use something other that and header_image
-        
+        var t = this;
         //   remove.....
-        
-        var cn = this.addxtype(
+        var footer = [
             {
-                 
                 xns : Roo.bootstrap,
-                xtype : 'Card',
-                closeable : true,
-                header_image : data.src,
-                data : data,
-                
-                listeners : {
-                    click : function() {
-                        // view..
-                    }
-                },
-                items : [
+                xtype : 'CardFooter',
+                items: [
                     {
                         xns : Roo.bootstrap,
-                        xtype : 'CardFooter',
-                        items: [
+                        xtype : 'Element',
+                        cls : 'd-flex',
+                        items : [
+                            
                             {
                                 xns : Roo.bootstrap,
-                                xtype : 'Element',
-                                html : data.title
+                                xtype : 'Button',
+                                html : String.format("<small>{0}</small>", data.title),
+                                cls : 'col-11 text-left',
+                                size: 'sm',
+                                weight: 'link',
+                                fa : 'download',
+                                listeners : {
+                                    click : function() {
+                                        this.downloadCard(data.id)
+                                    }
+                                }
                             },
+                          
                             {
                                 xns : Roo.bootstrap,
                                 xtype : 'Button',
-                                html : 'Delete',
-                                fa : 'cross'
+                                
+                                size : 'sm',
+                                weight: 'danger',
+                                cls : 'col-1',
+                                fa : 'times',
+                                listeners : {
+                                    click : function() {
+                                        t.removeCard(data.id)
+                                    }
+                                }
                             }
-                            
-                        ],
+                        ]
+                    }
+                    
+                ],
+            }
+            
+        ];
+
+        var cn = this.addxtype(
+            {
+                 
+                xns : Roo.bootstrap,
+                xtype : 'Card',
+                closeable : true,
+                header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
+                header_image : data.mimetype.match(/image/) ? data.src  : data.preview,
+                header_image_fit_square: true,
+                data : data,
+                html : false,
+                 
+                items : footer,
+                initEvents : function() {
+                    Roo.bootstrap.Card.prototype.initEvents.call(this);
+                    this.imgEl = this.el.select('.card-img-top').first();
+                    if (this.imgEl) {
+                        this.imgEl.on('click', function() { t.previewCard( data.id); }, this);
+                        this.imgEl.set({ 'pointer' : 'cursor' });
+                                  
                     }
                     
-                ]
                   
+                }
                 
             }
         );
         this.items.push(cn);
+        this.fileCollection.add(cn);
+        
+    },
+    removeCard : function(id)
+    {
         
+        var card  = this.fileCollection.get(id);
+        this.fileCollection.remove(card);
+        this.items = this.items.filter(function(e) { return e != card });
+        card.el.dom.parentNode.removeChild(card.el.dom);
         
     }