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) {
35 * When a image is clicked on - and needs to display a slideshow or similar..
36 * @param {Roo.bootstrap.Card} this
37 * @param {Roo.bootstrap.Card} The card containing the image data (.data is a property with image info.)
43 * When a the download link is clicked
44 * @param {Roo.bootstrap.Card} this
45 * @param {Roo.bootstrap.Card} The card containing the image data (.data is a property with image info.)
53 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input, {
60 fileCollection : false,
63 getAutoCreate : function()
72 //cls : 'input-group-addon',
73 html : this.fieldLabel
82 cls : 'd-none form-control'
87 multiple : 'multiple',
89 cls : 'd-none roo-card-upload-selector'
93 cls : 'roo-card-uploader-button-container w-100 mb-2'
96 cls : 'card-columns roo-card-uploader-container'
106 getChildContainer : function() /// what children are added to.
108 return this.containerEl;
111 getButtonContainer : function() /// what children are added to.
113 return this.el.select(".roo-card-uploader-button-container").first();
116 initEvents : function()
119 Roo.bootstrap.Input.prototype.initEvents.call(this);
126 container_method : 'getButtonContainer' ,
127 html : this.html, // fix changable?
130 'click' : function(btn, e) {
139 this.urlAPI = (window.createObjectURL && window) ||
140 (window.URL && URL.revokeObjectURL && URL) ||
141 (window.webkitURL && webkitURL);
146 this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
148 this.selectorEl.on('change', this.onFileSelected, this);
151 this.images.forEach(function(img) {
156 this.containerEl = this.el.select('.roo-card-uploader-container', true).first();
162 onClick : function(e)
166 this.selectorEl.dom.click();
170 onFileSelected : function(e)
174 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
178 Roo.each(this.selectorEl.dom.files, function(file){
188 addFile : function(file)
191 if(typeof(file) === 'string'){
192 throw "Add file by name?"; // should not happen
196 if(!file || !this.urlAPI){
206 var url = _this.urlAPI.createObjectURL( file);
209 id : Roo.bootstrap.CardUploader.ID--,
214 mimetype : file.type,
221 addCard : function (data)
223 // hidden input element?
224 // if the file is not an image...
225 //then we need to use something other that and header_image
231 xtype : 'CardFooter',
242 html : String.format("<small>{0}</small>", data.title),
243 cls : 'col-10 text-left',
249 this.downloadCard(data.id)
257 style: 'max-height: 28px; ',
264 t.removeCard(data.id)
276 var cn = this.addxtype(
282 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
283 header_image : data.mimetype.match(/image/) ? data.src : data.preview,
284 header_image_fit_square: true, // fixme - we probably need to use the 'Img' element to do stuff like this.
289 initEvents : function() {
290 Roo.bootstrap.Card.prototype.initEvents.call(this);
292 this.imgEl = this.el.select('.card-img-top').first();
294 this.imgEl.on('click', function() { t.fireEvent( "preview", t, card ); }, this);
295 this.imgEl.set({ 'pointer' : 'cursor' });
298 this.getCardFooter().addClass('p-1');
305 // dont' really need ot update items.
306 // this.items.push(cn);
307 this.fileCollection.add(cn);
315 var reader = new FileReader();
316 reader.addEventListener("load", function() {
317 data.srcdata = reader.result;
320 reader.readAsDataURL(data.srcfile);
325 removeCard : function(id)
328 var card = this.fileCollection.get(id);
329 card.data.is_deleted = 1;
330 card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
331 //this.fileCollection.remove(card);
332 //this.items = this.items.filter(function(e) { return e != card });
333 // dont' really need ot update items.
334 card.el.dom.parentNode.removeChild(card.el.dom);
341 this.fileCollection.each(function(card) {
342 if (card.el.dom && card.el.dom.parentNode) {
343 card.el.dom.parentNode.removeChild(card.el.dom);
346 this.fileCollection.clear();
350 updateInput : function()
353 this.fileCollection.each(function(e) {
357 this.inputEl().dom.value = JSON.stringify(data);
367 Roo.bootstrap.CardUploader.ID = -1;