7 * @class Roo.bootstrap.ButtonUploader
8 * @extends Roo.bootstrap.Button
9 * Bootstrap Button Uploader class - it's a button which when you add files to it
12 * @cfg {Number} errorTimeout default 3000
13 * @cfg {Array} images an array of ?? Img objects ??? when loading existing files..
14 * @cfg {Array} html The button text.
18 * Create a new CardUploader
19 * @param {Object} config The config object
22 Roo.bootstrap.ButtonUploader = function(config){
26 Roo.bootstrap.CardUploader.superclass.constructor.call(this, config);
29 this.fileCollection = new Roo.util.MixedCollection(false,function(r) {
37 * When a image is clicked on - and needs to display a slideshow or similar..
38 * @param {Roo.bootstrap.Card} this
39 * @param {Object} The image information data
45 * When a the download link is clicked
46 * @param {Roo.bootstrap.Card} this
47 * @param {Object} The image information data contains
54 Roo.extend(Roo.bootstrap.CardUploader, Roo.bootstrap.Button, {
61 fileCollection : false,
64 getAutoCreate : function()
71 Roo.bootstrap.Button.prototype.getAutoCreate(),
74 multiple : 'multiple',
76 cls : 'd-none roo-card-upload-selector'
88 initEvents : function()
91 Roo.bootstrap.Button.prototype.initEvents.call(this);
97 this.urlAPI = (window.createObjectURL && window) ||
98 (window.URL && URL.revokeObjectURL && URL) ||
99 (window.webkitURL && webkitURL);
104 this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
106 this.selectorEl.on('change', this.onFileSelected, this);
113 onClick : function(e)
117 this.selectorEl.dom.click();
121 onFileSelected : function(e)
125 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
129 Roo.each(this.selectorEl.dom.files, function(file){
139 addFile : function(file)
142 if(typeof(file) === 'string'){
143 throw "Add file by name?"; // should not happen
147 if(!file || !this.urlAPI){
157 var url = _this.urlAPI.createObjectURL( file);
160 id : Roo.bootstrap.CardUploader.ID--,
165 mimetype : file.type,
173 * addCard - add an Attachment to the uploader
174 * @param data - the data about the image to upload
178 title : "Title of file",
180 src : "http://.....",
181 srcfile : { the File upload object },
182 mimetype : file.type,
191 addCard : function (data)
193 // hidden input element?
194 // if the file is not an image...
195 //then we need to use something other that and header_image
201 xtype : 'CardFooter',
212 html : String.format("<small>{0}</small>", data.title),
213 cls : 'col-10 text-left',
220 t.fireEvent( "download", t, data );
228 style: 'max-height: 28px; ',
235 t.removeCard(data.id)
247 var cn = this.addxtype(
253 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
254 header_image : data.mimetype.match(/image/) ? data.src : data.preview,
255 header_image_fit_square: true, // fixme - we probably need to use the 'Img' element to do stuff like this.
260 initEvents : function() {
261 Roo.bootstrap.Card.prototype.initEvents.call(this);
263 this.imgEl = this.el.select('.card-img-top').first();
265 this.imgEl.on('click', function() { t.fireEvent( "preview", t, data ); }, this);
266 this.imgEl.set({ 'pointer' : 'cursor' });
269 this.getCardFooter().addClass('p-1');
276 // dont' really need ot update items.
277 // this.items.push(cn);
278 this.fileCollection.add(cn);
286 var reader = new FileReader();
287 reader.addEventListener("load", function() {
288 data.srcdata = reader.result;
291 reader.readAsDataURL(data.srcfile);
296 removeCard : function(id)
299 var card = this.fileCollection.get(id);
300 card.data.is_deleted = 1;
301 card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
302 //this.fileCollection.remove(card);
303 //this.items = this.items.filter(function(e) { return e != card });
304 // dont' really need ot update items.
305 card.el.dom.parentNode.removeChild(card.el.dom);
312 this.fileCollection.each(function(card) {
313 if (card.el.dom && card.el.dom.parentNode) {
314 card.el.dom.parentNode.removeChild(card.el.dom);
317 this.fileCollection.clear();
321 updateInput : function()
324 this.fileCollection.each(function(e) {
328 this.inputEl().dom.value = JSON.stringify(data);
338 Roo.bootstrap.CardUploader.ID = -1;