7 * @class Roo.bootstrap.DocumentManager
8 * @extends Roo.bootstrap.Component
9 * Bootstrap DocumentManager class
10 * @cfg {String} paramName default 'imageUpload'
11 * @cfg {String} method default POST
12 * @cfg {String} url action url
13 * @cfg {Number} boxes number of boxes default 12
14 * @cfg {Boolean} multiple multiple upload default true
15 * @cfg {Number} minWidth default 300
16 * @cfg {Number} minHeight default 300
17 * @cfg {Number} thumbSize default 300
18 * @cfg {String} fieldLabel
19 * @cfg {Number} labelWidth default 4
20 * @cfg {String} labelAlign (left|top) default left
21 * @cfg {Boolean} editable (true|false) allow edit when upload a image default true
24 * Create a new DocumentManager
25 * @param {Object} config The config object
28 Roo.bootstrap.DocumentManager = function(config){
29 Roo.bootstrap.DocumentManager.superclass.constructor.call(this, config);
34 * Fire when initial the DocumentManager
35 * @param {Roo.bootstrap.DocumentManager} this
40 * inspect selected file
41 * @param {Roo.bootstrap.DocumentManager} this
47 * Fire when xhr load exception
48 * @param {Roo.bootstrap.DocumentManager} this
49 * @param {XMLHttpRequest} xhr
54 * prepare the form data
55 * @param {Roo.bootstrap.DocumentManager} this
56 * @param {Object} formData
61 * Fire when remove the file
62 * @param {Roo.bootstrap.DocumentManager} this
63 * @param {Object} file
68 * Fire after refresh the file
69 * @param {Roo.bootstrap.DocumentManager} this
74 * Fire after click the image
75 * @param {Roo.bootstrap.DocumentManager} this
76 * @param {Object} file
81 * Fire when upload a image and editable set to true
82 * @param {Roo.bootstrap.DocumentManager} this
83 * @param {Object} file
90 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
101 paramName : 'imageUpload',
108 getAutoCreate : function()
110 var managerWidget = {
112 cls : 'roo-document-manager',
116 cls : 'roo-document-manager-selector',
121 cls : 'roo-document-manager-uploader',
125 cls : 'roo-document-manager-upload-btn',
126 html : '<i class="fa fa-plus"></i>'
137 cls : 'column col-md-12',
142 if(this.fieldLabel.length){
147 cls : 'column col-md-12',
148 html : this.fieldLabel
152 cls : 'column col-md-12',
157 if(this.labelAlign == 'left'){
161 cls : 'column col-md-' + this.labelWidth,
162 html : this.fieldLabel
166 cls : 'column col-md-' + (12 - this.labelWidth),
176 cls : 'row clearfix',
184 initEvents : function()
186 this.managerEl = this.el.select('.roo-document-manager', true).first();
187 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
189 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
190 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
191 this.selectorEl.hide();
194 this.selectorEl.attr('multiple', 'multiple');
197 this.selectorEl.on('change', this.onFileSelected, this);
199 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
200 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
202 this.uploader.on('click', this.onUploaderClick, this);
204 this.renderProgressDialog();
208 window.addEventListener("resize", function() { _this.refresh(); } );
210 this.fireEvent('initial', this);
213 renderProgressDialog : function()
217 this.progressDialog = new Roo.bootstrap.Modal({
218 cls : 'roo-document-manager-progress-dialog',
229 btnclick : function() {
230 _this.uploadCancel();
236 this.progressDialog.render(Roo.get(document.body));
238 this.progress = new Roo.bootstrap.Progress({
239 cls : 'roo-document-manager-progress',
244 this.progress.render(this.progressDialog.getChildContainer());
246 this.progressBar = new Roo.bootstrap.ProgressBar({
247 cls : 'roo-document-manager-progress-bar',
254 this.progressBar.render(this.progress.getChildContainer());
257 onUploaderClick : function(e)
261 if(this.fireEvent('onbeforeupload', this) != false){
262 this.selectorEl.dom.click();
267 onFileSelected : function(e)
271 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
275 Roo.each(this.selectorEl.dom.files, function(file){
276 if(this.fireEvent('inspect', this, file) != false){
277 this.files.push(file);
287 this.selectorEl.dom.value = '';
289 if(!this.files.length){
293 if(this.files.length > this.boxes){
294 this.files = this.files.slice(0, this.boxes);
297 this.uploader.show();
299 if(this.files.length > this.boxes - 1){
300 this.uploader.hide();
307 Roo.each(this.files, function(file){
309 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
310 var f = this.renderPreview(file);
318 }).createDelegate(this)
325 if(!this.delegates.length){
330 this.progressBar.aria_valuemax = this.delegates.length;
339 if(!this.delegates.length){
340 this.progressDialog.hide();
345 var delegate = this.delegates.shift();
347 this.progressDialog.show();
349 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
351 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
358 this.uploader.show();
360 if(this.files.length > this.boxes - 1){
361 this.uploader.hide();
364 Roo.isTouch ? this.closable(false) : this.closable(true);
366 this.fireEvent('refresh', this);
369 onRemove : function(e, el, o)
373 this.fireEvent('remove', this, o);
381 Roo.each(this.files, function(file){
382 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
396 onClick : function(e, el, o)
400 this.fireEvent('click', this, o);
404 closable : function(closable)
406 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
408 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
420 xhrOnLoad : function(xhr)
422 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
426 if (xhr.readyState !== 4) {
428 this.fireEvent('exception', this, xhr);
432 var response = Roo.decode(xhr.responseText);
434 if(!response.success){
436 this.fireEvent('exception', this, xhr);
440 var file = this.renderPreview(response.data);
442 this.files.push(file);
448 xhrOnError : function()
450 Roo.log('xhr on error');
452 var response = Roo.decode(xhr.responseText);
459 process : function(file)
461 if(this.editable && file.type.indexOf('image') != -1){
462 this.fireEvent('edit', this, file);
466 this.uploadStart(file, false);
471 uploadStart : function(file, crop)
473 this.xhr = new XMLHttpRequest();
475 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
482 this.managerEl.createChild({
484 cls : 'roo-document-manager-loading',
489 cls : 'roo-document-manager-thumb',
490 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
496 this.xhr.open(this.method, this.url, true);
499 "Accept": "application/json",
500 "Cache-Control": "no-cache",
501 "X-Requested-With": "XMLHttpRequest"
504 for (var headerName in headers) {
505 var headerValue = headers[headerName];
507 this.xhr.setRequestHeader(headerName, headerValue);
513 this.xhr.onload = function()
515 _this.xhrOnLoad(_this.xhr);
518 this.xhr.onerror = function()
520 _this.xhrOnError(_this.xhr);
523 var formData = new FormData();
525 formData.append('returnHTML', 'NO');
528 formData.append('crop', crop);
531 formData.append(this.paramName, file, file.name);
533 if(this.fireEvent('prepare', this, formData) != false){
534 this.xhr.send(formData);
538 uploadCancel : function()
544 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
551 renderPreview : function(file)
553 if(typeof(file.target) != 'undefined' && file.target){
557 var previewEl = this.managerEl.createChild({
559 cls : 'roo-document-manager-preview',
563 tooltip : file.filename,
564 cls : 'roo-document-manager-thumb',
565 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
570 html : '<i class="fa fa-times-circle"></i>'
575 var close = previewEl.select('button.close', true).first();
577 close.on('click', this.onRemove, this, file);
579 file.target = previewEl;
581 var image = previewEl.select('img', true).first();
585 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
587 image.on('click', this.onClick, this, file);
593 onPreviewLoad : function(file, image)
595 if(typeof(file.target) == 'undefined' || !file.target){
599 var width = image.dom.naturalWidth || image.dom.width;
600 var height = image.dom.naturalHeight || image.dom.height;
603 file.target.addClass('wide');
607 file.target.addClass('tall');