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.
16 * Create a new CardUploader
17 * @param {Object} config The config object
20 Roo.bootstrap.CardUploader = function(config){
24 Roo.bootstrap.CardUploader.superclass.constructor.call(this, config);
27 this.fileCollection = new Roo.util.MixedCollection(false,function(r) {
34 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input, {
41 fileCollection : false,
44 getAutoCreate : function()
53 //cls : 'input-group-addon',
54 html : this.fieldLabel
63 cls : 'd-none form-control'
68 multiple : 'multiple',
70 cls : 'd-none roo-card-upload-selector'
74 cls : 'roo-card-uploader-button-container w-100 mb-2'
77 cls : 'card-columns roo-card-uploader-container'
87 getChildContainer : function() /// what children are added to.
89 return this.containerEl;
92 getButtonContainer : function() /// what children are added to.
94 return this.el.select(".roo-card-uploader-button-container").first();
97 initEvents : function()
100 Roo.bootstrap.Input.prototype.initEvents.call(this);
107 container_method : 'getButtonContainer' ,
108 html : this.html, // fix changable?
111 'click' : function(btn, e) {
120 this.urlAPI = (window.createObjectURL && window) ||
121 (window.URL && URL.revokeObjectURL && URL) ||
122 (window.webkitURL && webkitURL);
127 this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
129 this.selectorEl.on('change', this.onFileSelected, this);
132 this.images.forEach(function(img) {
137 this.containerEl = this.el.select('.roo-card-uploader-container', true).first();
143 onClick : function(e)
147 this.selectorEl.dom.click();
151 onFileSelected : function(e)
155 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
159 Roo.each(this.selectorEl.dom.files, function(file){
169 addFile : function(file)
172 if(typeof(file) === 'string'){
173 throw "Add file by name?"; // should not happen
177 if(!file || !this.urlAPI){
187 var url = _this.urlAPI.createObjectURL( file);
190 id : Roo.bootstrap.CardUploader.ID--,
194 mimetype : file.type,
201 addCard : function (data)
203 // hidden input element?
204 // if the file is not an image...
205 //then we need to use something other that and header_image
211 xtype : 'CardFooter',
222 html : String.format("<small>{0}</small>", data.title),
223 cls : 'col-11 text-left',
229 this.downloadCard(data.id)
244 t.removeCard(data.id)
256 var cn = this.addxtype(
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.
269 initEvents : function() {
270 Roo.bootstrap.Card.prototype.initEvents.call(this);
271 this.imgEl = this.el.select('.card-img-top').first();
273 this.imgEl.on('click', function() { t.previewCard( data.id); }, this);
274 this.imgEl.set({ 'pointer' : 'cursor' });
283 // dont' really need ot update items.
284 // this.items.push(cn);
285 this.fileCollection.add(cn);
289 removeCard : function(id)
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);
303 this.fileCollection.each(function(card) {
304 card.el.dom.parentNode.removeChild(card.el.dom);
306 this.fileCollection.clear();
310 updateInput : function()
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);
321 var reader = new FileReader();
322 reader.onloadend = function(evt) {
324 var ee = Roo.apply({}, fc[i]);
325 ee.src = evt.target.result;
329 reader.readAsDataURL(fc[i].src);
341 Roo.bootstrap.CardUploader.ID = -1;