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 {Object} The image information data
43 * When a the download link is clicked
44 * @param {Roo.bootstrap.Card} this
45 * @param {Object} The image information data contains
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,
222 * addCard - add an Attachment to the uploader
223 * @param data - the data about the image to upload
227 title : "Title of file",
229 src : "http://.....",
230 srcfile : { the File upload object },
231 mimetype : file.type,
240 addCard : function (data)
242 // hidden input element?
243 // if the file is not an image...
244 //then we need to use something other that and header_image
250 xtype : 'CardFooter',
261 html : String.format("<small>{0}</small>", data.title),
262 cls : 'col-10 text-left',
269 t.fireEvent( "download", t, data );
277 style: 'max-height: 28px; ',
284 t.removeCard(data.id)
296 var cn = this.addxtype(
302 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
303 header_image : data.mimetype.match(/image/) ? data.src : data.preview,
304 header_image_fit_square: true, // fixme - we probably need to use the 'Img' element to do stuff like this.
309 initEvents : function() {
310 Roo.bootstrap.Card.prototype.initEvents.call(this);
312 this.imgEl = this.el.select('.card-img-top').first();
314 this.imgEl.on('click', function() { t.fireEvent( "preview", t, data ); }, this);
315 this.imgEl.set({ 'pointer' : 'cursor' });
318 this.getCardFooter().addClass('p-1');
325 // dont' really need ot update items.
326 // this.items.push(cn);
327 this.fileCollection.add(cn);
335 var reader = new FileReader();
336 reader.addEventListener("load", function() {
337 data.srcdata = reader.result;
340 reader.readAsDataURL(data.srcfile);
345 removeCard : function(id)
348 var card = this.fileCollection.get(id);
349 card.data.is_deleted = 1;
350 card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
351 //this.fileCollection.remove(card);
352 //this.items = this.items.filter(function(e) { return e != card });
353 // dont' really need ot update items.
354 card.el.dom.parentNode.removeChild(card.el.dom);
361 this.fileCollection.each(function(card) {
362 if (card.el.dom && card.el.dom.parentNode) {
363 card.el.dom.parentNode.removeChild(card.el.dom);
366 this.fileCollection.clear();
370 updateInput : function()
373 this.fileCollection.each(function(e) {
377 this.inputEl().dom.value = JSON.stringify(data);
387 Roo.bootstrap.CardUploader.ID = -1;