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--,
195 mimetype : file.type,
202 addCard : function (data)
204 // hidden input element?
205 // if the file is not an image...
206 //then we need to use something other that and header_image
212 xtype : 'CardFooter',
223 html : String.format("<small>{0}</small>", data.title),
224 cls : 'col-11 text-left',
230 this.downloadCard(data.id)
238 style: 'max-height: 28px;min-width: 27px;',
245 t.removeCard(data.id)
257 var cn = this.addxtype(
263 header : !data.mimetype.match(/image/) && !data.preview ? "Document": false,
264 header_image : data.mimetype.match(/image/) ? data.src : data.preview,
265 header_image_fit_square: true, // fixme - we probably need to use the 'Img' element to do stuff like this.
270 initEvents : function() {
271 Roo.bootstrap.Card.prototype.initEvents.call(this);
272 this.imgEl = this.el.select('.card-img-top').first();
274 this.imgEl.on('click', function() { t.previewCard( data.id); }, this);
275 this.imgEl.set({ 'pointer' : 'cursor' });
284 // dont' really need ot update items.
285 // this.items.push(cn);
286 this.fileCollection.add(cn);
294 var reader = new FileReader();
295 reader.addEventListener("load", function() {
296 data.srcdata = reader.result;
299 reader.readAsDataURL(data.srcfile);
304 removeCard : function(id)
307 var card = this.fileCollection.get(id);
308 card.data.is_deleted = 1;
309 card.data.src = ''; /// delete the source - so it reduces size of not uploaded images etc.
310 //this.fileCollection.remove(card);
311 //this.items = this.items.filter(function(e) { return e != card });
312 // dont' really need ot update items.
313 card.el.dom.parentNode.removeChild(card.el.dom);
320 this.fileCollection.each(function(card) {
321 if (card.el.dom && card.el.dom.parentNode) {
322 card.el.dom.parentNode.removeChild(card.el.dom);
325 this.fileCollection.clear();
329 updateInput : function()
332 this.fileCollection.each(function(e) {
336 this.inputEl().dom.value = JSON.stringify(data);
346 Roo.bootstrap.CardUploader.ID = -1;