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.data.id });
27     
28     
29 };
30
31 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input,  {
32     
33      
34     errorTimeout : 3000,
35      
36     images : false,
37    
38     imageCollection : false,
39     
40     getAutoCreate : function()
41     {
42         
43         var cfg =  {
44             cls :'form-group' ,
45             cn : [
46                 {
47                     tag : 'i',
48                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
49                     tooltip : 'This field is required'
50                 },
51                 {
52                     tag: 'label',
53                    //cls : 'input-group-addon',
54                     html : this.fieldLabel
55
56                 },
57
58                 {
59                     tag: 'input',
60                     id : id,
61                     type : 'hidden',
62                     value : this.value,
63                     cls : 'd-none'
64                 },
65                 {
66                     cls : 'roo-card-uploader-button-container'
67                 },
68                 {
69                     cls : 'card-columns roo-card-uploader-container'
70                 }
71
72             ]
73         };
74            
75         
76         var input = Roo.bootstrap.Input.prototype.getAutoCreate.call(this);
77         
78         
79         btn.cls += ' w-100 mb-2';
80         var cfg = {
81             cls : 'roo-card-uploader-wrap',
82             cn : [
83                 btn,
84                 {
85                     tag : 'input',
86                     cls : 'd-none roo-card-upload-selector',
87                     type : 'file',
88                     multiple : 'multiple'
89                 },
90                
91             ]
92             
93         }
94         
95         
96         return cfg;
97     },
98     
99     getChildContainer : function() /// what children are added to.
100     {
101         return this.el;
102     },
103    
104     
105     initEvents : function()
106     {
107         
108         Roo.bootstrap.Input.prototype.initEvents.call(this);
109         
110         this.addxtype({
111             xns: Roo.bootstrap,
112             xtype : 'Button',
113             
114             
115         })
116         
117         
118         
119         
120         this.urlAPI = (window.createObjectURL && window) || 
121                                 (window.URL && URL.revokeObjectURL && URL) || 
122                                 (window.webkitURL && webkitURL);
123                         
124          
125          
126          
127         this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
128         
129          this.selectorEl.on('change', this.onFileSelected, this);
130         if (this.images) {
131             var t = this;
132             this.images.forEach(function(img) {
133                 t.addCard(img)
134             });
135             this.images = false;
136         }
137         this.containerEl = this.el.select('.roo-card-uploader-container', true).first();
138          
139        
140     },
141     
142    
143     onClick : function(e)
144     {
145         e.preventDefault();
146          
147         this.selectorEl.dom.click();
148          
149     },
150     
151     onFileSelected : function(e)
152     {
153         e.preventDefault();
154         
155         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
156             return;
157         }
158         
159         Roo.each(this.selectorEl.dom.files, function(file){    
160             this.addFile(file);
161         }, this);
162          
163     },
164     
165       
166     
167       
168     
169     addFile : function(file)
170     {
171            
172         if(typeof(file) === 'string'){
173             throw "Add file by name?"; // should not happen
174             return;
175         }
176         
177         if(!file || !this.urlAPI){
178             return;
179         }
180         
181         // file;
182         // file.type;
183         
184         var _this = this;
185         
186         
187         var url = _this.urlAPI.createObjectURL( file);
188            
189         this.addCard({
190             id : Roo.bootstrap.CardUploader.ID--,
191             is_uploaded : false,
192             src : url,
193             title : file.name,
194             mimetype : file.type,
195             preview : false,
196         })
197         
198     },
199     
200     addCard : function (data)
201     {
202         // hidden input element?
203         // if the file is not an image...
204         //then we need to use something other that and header_image
205         var t = this;
206         //   remove.....
207         var footer = [
208             {
209                 xns : Roo.bootstrap,
210                 xtype : 'CardFooter',
211                 items: [
212                     {
213                         xns : Roo.bootstrap,
214                         xtype : 'Element',
215                         cls : 'd-flex',
216                         items : [
217                             
218                             {
219                                 xns : Roo.bootstrap,
220                                 xtype : 'Button',
221                                 html : String.format("<small>{0}</small>", data.title),
222                                 cls : 'col-11 text-left',
223                                 size: 'sm',
224                                 weight: 'link',
225                                 fa : 'download',
226                                 listeners : {
227                                     click : function() {
228                                         this.downloadCard(data.id)
229                                     }
230                                 }
231                             },
232                           
233                             {
234                                 xns : Roo.bootstrap,
235                                 xtype : 'Button',
236                                 
237                                 size : 'sm',
238                                 weight: 'danger',
239                                 cls : 'col-1',
240                                 fa : 'times',
241                                 listeners : {
242                                     click : function() {
243                                         this.removeCard(data.id)
244                                     }
245                                 }
246                             }
247                         ]
248                     }
249                     
250                 ],
251             }
252             
253         ];
254
255         var cn = this.addxtype(
256             {
257                  
258                 xns : Roo.bootstrap,
259                 xtype : 'Card',
260                 closeable : true,
261                 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
262                 header_image : data.mimetype.match(/image/) ? data.src  : data.preview,
263                 header_image_fit_square: true,
264                 data : data,
265                 html : false,
266                  
267                 items : footer,
268                 initEvents : function() {
269                     Roo.bootstrap.Card.prototype.initEvents.call(this);
270                     this.imgEl = this.el.select('.card-img-top').first();
271                     if (this.imgEl) {
272                         this.imgEl.on('click', function() { t.previewCard( data.id); }, this);
273                         this.imgEl.set({ 'pointer' : 'cursor' });
274                                   
275                     }
276                     
277                   
278                 }
279                 
280             }
281         );
282         this.items.push(cn);
283         
284         
285     }
286     
287     
288 });
289
290
291 Roo.bootstrap.CardUploader.ID = -1;