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
52 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Input, {
59 fileCollection : false,
62 getAutoCreate : function()
71 //cls : 'input-group-addon',
72 html : this.fieldLabel
81 cls : 'd-none form-control'
86 multiple : 'multiple',
88 cls : 'd-none roo-card-upload-selector'
92 cls : 'roo-card-uploader-button-container w-100 mb-2'
95 cls : 'card-columns roo-card-uploader-container'
105 getChildContainer : function() /// what children are added to.
107 return this.containerEl;
110 getButtonContainer : function() /// what children are added to.
112 return this.el.select(".roo-card-uploader-button-container").first();
115 initEvents : function()
118 Roo.bootstrap.Input.prototype.initEvents.call(this);
125 container_method : 'getButtonContainer' ,
126 html : this.html, // fix changable?
129 'click' : function(btn, e) {
138 this.urlAPI = (window.createObjectURL && window) ||
139 (window.URL && URL.revokeObjectURL && URL) ||
140 (window.webkitURL && webkitURL);
145 this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
147 this.selectorEl.on('change', this.onFileSelected, this);
150 this.images.forEach(function(img) {
155 this.containerEl = this.el.select('.roo-card-uploader-container', true).first();
161 onClick : function(e)
165 this.selectorEl.dom.click();
169 onFileSelected : function(e)
173 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
177 Roo.each(this.selectorEl.dom.files, function(file){
187 addFile : function(file)
190 if(typeof(file) === 'string'){
191 throw "Add file by name?"; // should not happen
195 if(!file || !this.urlAPI){
205 var url = _this.urlAPI.createObjectURL( file);
208 id : Roo.bootstrap.CardUploader.ID--,
213 mimetype : file.type,
221 * addCard - add an Attachment to the uploader
222 * @param data - the data about the image to upload
226 title : "Title of file",
228 src : "http://.....",
229 srcfile : { the File upload object },
230 mimetype : file.type,
239 addCard : function (data)
241 // hidden input element?
242 // if the file is not an image...
243 //then we need to use something other that and header_image
249 xtype : 'CardFooter',
260 html : String.format("<small>{0}</small>", data.title),
261 cls : 'col-10 text-left',
268 t.fireEvent( "download", t, data );
276 style: 'max-height: 28px; ',
283 t.removeCard(data.id)
295 var cn = this.addxtype(
301 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
302 header_image : data.mimetype.match(/image/) ? data.src : data.preview,
303 header_image_fit_square: true, // fixme - we probably need to use the 'Img' element to do stuff like this.
308 initEvents : function() {
309 Roo.bootstrap.Card.prototype.initEvents.call(this);
311 this.imgEl = this.el.select('.card-img-top').first();
313 this.imgEl.on('click', function() { t.fireEvent( "preview", t, data ); }, this);
314 this.imgEl.set({ 'pointer' : 'cursor' });
317 this.getCardFooter().addClass('p-1');
324 // dont' really need ot update items.
325 // this.items.push(cn);
326 this.fileCollection.add(cn);
334 var reader = new FileReader();
335 reader.addEventListener("load", function() {
336 data.srcdata = reader.result;
339 reader.readAsDataURL(data.srcfile);
344 removeCard : function(id)
347 var card = this.fileCollection.get(id);
348 card.data.is_deleted = 1;
349 card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
350 //this.fileCollection.remove(card);
351 //this.items = this.items.filter(function(e) { return e != card });
352 // dont' really need ot update items.
353 card.el.dom.parentNode.removeChild(card.el.dom);
360 this.fileCollection.each(function(card) {
361 if (card.el.dom && card.el.dom.parentNode) {
362 card.el.dom.parentNode.removeChild(card.el.dom);
365 this.fileCollection.clear();
369 updateInput : function()
372 this.fileCollection.each(function(e) {
376 this.inputEl().dom.value = JSON.stringify(data);
386 Roo.bootstrap.CardUploader.ID = -1;