Fix #6465 - drag drop for cards
[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.fileCollection   = new Roo.util.MixedCollection(false,function(r) {
27         return r.data.id
28         });
29     
30     
31 };
32
33 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input,  {
34     
35      
36     errorTimeout : 3000,
37      
38     images : false,
39    
40     fileCollection : false,
41     allowBlank : true,
42     
43     getAutoCreate : function()
44     {
45         
46         var cfg =  {
47             cls :'form-group' ,
48             cn : [
49                
50                 {
51                     tag: 'label',
52                    //cls : 'input-group-addon',
53                     html : this.fieldLabel
54
55                 },
56
57                 {
58                     tag: 'input',
59                     type : 'hidden',
60                     value : this.value,
61                     cls : 'd-none  form-control'
62                 },
63                 
64                 {
65                     tag: 'input',
66                     multiple : 'multiple',
67                     type : 'file',
68                     cls : 'd-none  roo-card-upload-selector'
69                 },
70                 
71                 {
72                     cls : 'roo-card-uploader-button-container w-100 mb-2'
73                 },
74                 {
75                     cls : 'card-columns roo-card-uploader-container'
76                 }
77
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 : 'Upload File / Images', // 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 data = [];
313         this.fileCollection.each(function(e) {
314             data.push(e.data);
315         });
316         
317         this.inputEl().dom.value = JSON.stringify(data);
318     }
319     
320     
321 });
322
323
324 Roo.bootstrap.CardUploader.ID = -1;