roojs-bootstrap.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 {Array}  images  an array of ?? Img objects ??? when loading existing files..
12  * @cfg {Array}  html The button text.
13
14  *
15  * @constructor
16  * Create a new CardUploader
17  * @param {Object} config The config object
18  */
19
20 Roo.bootstrap.CardUploader = function(config){
21     
22  
23     
24     Roo.bootstrap.CardUploader.superclass.constructor.call(this, config);
25     
26     
27     this.fileCollection   = new Roo.util.MixedCollection(false,function(r) {
28         return r.data.id
29         });
30     
31     
32 };
33
34 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input,  {
35     
36      
37     errorTimeout : 3000,
38      
39     images : false,
40    
41     fileCollection : false,
42     allowBlank : true,
43     
44     getAutoCreate : function()
45     {
46         
47         var cfg =  {
48             cls :'form-group' ,
49             cn : [
50                
51                 {
52                     tag: 'label',
53                    //cls : 'input-group-addon',
54                     html : this.fieldLabel
55
56                 },
57
58                 {
59                     tag: 'input',
60                     type : 'hidden',
61                     nane : this.name,
62                     value : this.value,
63                     cls : 'd-none  form-control'
64                 },
65                 
66                 {
67                     tag: 'input',
68                     multiple : 'multiple',
69                     type : 'file',
70                     cls : 'd-none  roo-card-upload-selector'
71                 },
72                 
73                 {
74                     cls : 'roo-card-uploader-button-container w-100 mb-2'
75                 },
76                 {
77                     cls : 'card-columns roo-card-uploader-container'
78                 }
79
80             ]
81         };
82            
83          
84         return cfg;
85     },
86     
87     getChildContainer : function() /// what children are added to.
88     {
89         return this.containerEl;
90     },
91    
92     getButtonContainer : function() /// what children are added to.
93     {
94         return this.el.select(".roo-card-uploader-button-container").first();
95     },
96    
97     initEvents : function()
98     {
99         
100         Roo.bootstrap.Input.prototype.initEvents.call(this);
101         
102         var t = this;
103         this.addxtype({
104             xns: Roo.bootstrap,
105
106             xtype : 'Button',
107             container_method : 'getButtonContainer' ,            
108             html :  this.html, // fix changable?
109             cls : 'w-100 ',
110             listeners : {
111                 'click' : function(btn, e) {
112                     t.onClick(e);
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             is_deleted : 0
197         })
198         
199     },
200     
201     addCard : function (data)
202     {
203         // hidden input element?
204         // if the file is not an image...
205         //then we need to use something other that and header_image
206         var t = this;
207         //   remove.....
208         var footer = [
209             {
210                 xns : Roo.bootstrap,
211                 xtype : 'CardFooter',
212                 items: [
213                     {
214                         xns : Roo.bootstrap,
215                         xtype : 'Element',
216                         cls : 'd-flex',
217                         items : [
218                             
219                             {
220                                 xns : Roo.bootstrap,
221                                 xtype : 'Button',
222                                 html : String.format("<small>{0}</small>", data.title),
223                                 cls : 'col-11 text-left',
224                                 size: 'sm',
225                                 weight: 'link',
226                                 fa : 'download',
227                                 listeners : {
228                                     click : function() {
229                                         this.downloadCard(data.id)
230                                     }
231                                 }
232                             },
233                           
234                             {
235                                 xns : Roo.bootstrap,
236                                 xtype : 'Button',
237                                 
238                                 size : 'sm',
239                                 weight: 'danger',
240                                 cls : 'col-1',
241                                 fa : 'times',
242                                 listeners : {
243                                     click : function() {
244                                         t.removeCard(data.id)
245                                     }
246                                 }
247                             }
248                         ]
249                     }
250                     
251                 ] 
252             }
253             
254         ];
255
256         var cn = this.addxtype(
257             {
258                  
259                 xns : Roo.bootstrap,
260                 xtype : 'Card',
261                 closeable : true,
262                 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
263                 header_image : data.mimetype.match(/image/) ? data.src  : data.preview,
264                 header_image_fit_square: true, // fixme  - we probably need to use the 'Img' element to do stuff like this.
265                 data : data,
266                 html : false,
267                  
268                 items : footer,
269                 initEvents : function() {
270                     Roo.bootstrap.Card.prototype.initEvents.call(this);
271                     this.imgEl = this.el.select('.card-img-top').first();
272                     if (this.imgEl) {
273                         this.imgEl.on('click', function() { t.previewCard( data.id); }, this);
274                         this.imgEl.set({ 'pointer' : 'cursor' });
275                                   
276                     }
277                     
278                   
279                 }
280                 
281             }
282         );
283         // dont' really need ot update items.
284         // this.items.push(cn);
285         this.fileCollection.add(cn);
286         this.updateInput();
287         
288     },
289     removeCard : function(id)
290     {
291         
292         var card  = this.fileCollection.get(id);
293         card.data.is_deleted = 1;
294         card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
295         this.fileCollection.remove(card);
296         //this.items = this.items.filter(function(e) { return e != card });
297         // dont' really need ot update items.
298         card.el.dom.parentNode.removeChild(card.el.dom);
299         
300     },
301     reset: function()
302     {
303         this.fileCollection.each(function(card) {
304             card.el.dom.parentNode.removeChild(card.el.dom);    
305         });
306         this.fileCollection.clear();
307         this.updateInput();
308     },
309     
310     updateInput : function()
311     {
312         var i =0;
313         var data = [];
314         var dom = this.inputEl().dom;
315         var fc = this.fileCollection;
316         var next = function() {
317             if (i >= fc.length) {
318                 dom.value = JSON.stringify(data);
319                 return;
320             }
321             var reader = new FileReader();
322             reader.onloadend = function(evt) {  
323                 // file is loaded
324                 var ee = Roo.apply({}, fc[i]);
325                 ee.src = evt.target.result;
326                 data.push(ee);
327                 i++;
328                 next();
329             };
330             reader.readAsDataURL(fc[i].src);
331             
332         }
333         next();
334         
335         
336     }
337     
338     
339 });
340
341
342 Roo.bootstrap.CardUploader.ID = -1;