Roo/bootstrap/CardUploader.js
[roojs1] / Roo / bootstrap / CardUploader.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.bootstrap.CardUploader
8  * @extends Roo.bootstrap.Button
9  * Bootstrap Card Uploader class - it's a button which when you add files to it, adds cards below with preview and the name...
10  * @cfg {Number} errorTimeout default 3000
11  * @cfg {Boolean} name  the field name for each image..
12  * @cfg {Array}  images  an array of ?? Img objects ??? when loading existing files..
13  *
14  * @constructor
15  * Create a new CardUploader
16  * @param {Object} config The config object
17  */
18
19 Roo.bootstrap.CardUploader = function(config){
20     
21  
22     
23     Roo.bootstrap.CardUploader.superclass.constructor.call(this, config);
24     
25     
26     this.imageCollection  = new Roo.util.MixedCollection(function(r) { return r.id });
27     
28     
29 };
30
31 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button,  {
32     
33      
34     errorTimeout : 3000,
35      
36     images : false,
37    
38     imageCollection : false,
39     
40     getAutoCreate : function()
41     {
42         var btn = Roo.bootstrap.Button.prototype.getAutoCreate.call(this);
43         
44         var cfg = {
45             cls : 'roo-card-uploader-wrap',
46             cn : [
47                 btn,
48                 {
49                     tag : 'input',
50                     cls : 'd-none roo-card-upload-selector',
51                     type : 'file',
52                     multiple : 'multiple'
53                 },
54                 {
55                     cls : 'card-columns roo-card-uploader-container'
56                 }
57             ]
58             
59         }
60         
61         
62         return cfg;
63     },
64     
65     getChildContainer : function() /// what children are added to.
66     {
67         return this.containerEl;
68     },
69    
70     
71     initEvents : function()
72     {
73         Roo.bootstrap.Button.prototype.initEvents.call(this);
74         this.urlAPI = (window.createObjectURL && window) || 
75                                 (window.URL && URL.revokeObjectURL && URL) || 
76                                 (window.webkitURL && webkitURL);
77                         
78          
79          
80          
81         this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
82         
83          this.selectorEl.on('change', this.onFileSelected, this);
84         if (this.images) {
85             var t = this;
86             this.images.forEach(function(img) {
87                 t.addCard(img)
88             });
89             this.images = false;
90         }
91         this.containerEl = this.el.select('.roo-card-uploader-container', true).first();
92          
93        
94     },
95     
96    
97     onClick : function(e)
98     {
99         e.preventDefault();
100          
101         this.selectorEl.dom.click();
102          
103     },
104     
105     onFileSelected : function(e)
106     {
107         e.preventDefault();
108         
109         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
110             return;
111         }
112         
113         Roo.each(this.selectorEl.dom.files, function(file){    
114             this.addFile(file);
115         }, this);
116          
117     },
118     
119       
120     
121       
122     
123     addFile : function(file)
124     {
125            
126         if(typeof(file) === 'string'){
127             throw "Add file by name?"; // should not happen
128             return;
129         }
130         
131         if(!file || !this.urlAPI){
132             return;
133         }
134         
135         // file;
136         // file.type;
137         
138         var _this = this;
139         
140         
141         var url = _this.urlAPI.createObjectURL( file);
142            
143         this.addCard({
144             id : Roo.bootstrap.CardUploader.ID--,
145             is_uploaded : false,
146             src : url,
147             title : file.name,
148             mimetype : file.mimetype,
149         })
150         
151     },
152     
153     addCard : function (data)
154     {
155         // hidden input element?
156         // if the file is not an image...
157         //then we need to use something other that and header_image
158         
159         //   remove.....
160         
161         var cn = this.addxtype(
162             {
163                  
164                 xns : Roo.bootstrap,
165                 xtype : 'Card',
166                 closeable : true,
167                 header_image : data.src,
168                 data : data,
169                 html : false,
170                 listeners : {
171                     click : function() {
172                         // view..
173                     }
174                 },
175                 items : [
176                     {
177                         xns : Roo.bootstrap,
178                         xtype : 'CardFooter',
179                         items: [
180                             {
181                                 xns : Roo.bootstrap,
182                                 xtype : 'Element',
183                                 cls : 'd-flex',
184                                 items : [
185                                     
186                                     {
187                                         xns : Roo.bootstrap,
188                                         xtype : 'Button',
189                                         html : String.format("<small>{0}</small>", data.title),
190                                         cls : 'col-8 text-left',
191                                         size: 'sm',
192                                         weight: 'info',
193                                         fa : 'download',
194                                         listeners : {
195                                             click : function() {
196                                                 // download
197                                             }
198                                         }
199                                     },
200                                     {
201                                         xns : Roo.bootstrap,
202                                         xtype : 'Element',
203                                         cls : 'col-1'
204                                     },
205                                     {
206                                         xns : Roo.bootstrap,
207                                         xtype : 'Button',
208                                         html : 'Delete',
209                                         size : 'sm',
210                                         weight: 'danger',
211                                         cls : 'col-3',
212                                         fa : 'times'
213                                     }
214                                 ]
215                             }
216                             
217                         ],
218                     }
219                     
220                 ]
221                   
222                 
223             }
224         );
225         this.items.push(cn);
226         
227         
228     }
229     
230     
231 });
232
233
234 Roo.bootstrap.CardUploader.ID = -1;