7 * @class Roo.bootstrap.DocumentManager
8 * @extends Roo.bootstrap.Component
9 * Bootstrap DocumentManager class
10 * @cfg {String} paramName default 'imageUpload'
11 * @cfg {String} toolTipName default 'filename'
12 * @cfg {String} method default POST
13 * @cfg {String} url action url
14 * @cfg {Number} boxes number of boxes, 0 is no limit.. default 0
15 * @cfg {Boolean} multiple multiple upload default true
16 * @cfg {Number} thumbSize default 300
17 * @cfg {String} fieldLabel
18 * @cfg {Number} labelWidth default 4
19 * @cfg {String} labelAlign (left|top) default left
20 * @cfg {Boolean} editable (true|false) allow edit when upload a image default true
21 * @cfg {Number} labellg set the width of label (1-12)
22 * @cfg {Number} labelmd set the width of label (1-12)
23 * @cfg {Number} labelsm set the width of label (1-12)
24 * @cfg {Number} labelxs set the width of label (1-12)
27 * Create a new DocumentManager
28 * @param {Object} config The config object
31 Roo.bootstrap.DocumentManager = function(config){
32 Roo.bootstrap.DocumentManager.superclass.constructor.call(this, config);
40 * Fire when initial the DocumentManager
41 * @param {Roo.bootstrap.DocumentManager} this
46 * inspect selected file
47 * @param {Roo.bootstrap.DocumentManager} this
53 * Fire when xhr load exception
54 * @param {Roo.bootstrap.DocumentManager} this
55 * @param {XMLHttpRequest} xhr
60 * Fire when xhr load exception
61 * @param {Roo.bootstrap.DocumentManager} this
62 * @param {XMLHttpRequest} xhr
67 * prepare the form data
68 * @param {Roo.bootstrap.DocumentManager} this
69 * @param {Object} formData
74 * Fire when remove the file
75 * @param {Roo.bootstrap.DocumentManager} this
76 * @param {Object} file
81 * Fire after refresh the file
82 * @param {Roo.bootstrap.DocumentManager} this
87 * Fire after click the image
88 * @param {Roo.bootstrap.DocumentManager} this
89 * @param {Object} file
94 * Fire when upload a image and editable set to true
95 * @param {Roo.bootstrap.DocumentManager} this
96 * @param {Object} file
100 * @event beforeselectfile
101 * Fire before select file
102 * @param {Roo.bootstrap.DocumentManager} this
104 "beforeselectfile" : true,
107 * Fire before process file
108 * @param {Roo.bootstrap.DocumentManager} this
109 * @param {Object} file
113 * @event previewrendered
114 * Fire when preview rendered
115 * @param {Roo.bootstrap.DocumentManager} this
116 * @param {Object} file
118 "previewrendered" : true,
121 "previewResize" : true
126 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
135 paramName : 'imageUpload',
136 toolTipName : 'filename',
149 getAutoCreate : function()
151 var managerWidget = {
153 cls : 'roo-document-manager',
157 cls : 'roo-document-manager-selector',
162 cls : 'roo-document-manager-uploader',
166 cls : 'roo-document-manager-upload-btn',
167 html : '<i class="fa fa-plus"></i>'
178 cls : 'column col-md-12',
183 if(this.fieldLabel.length){
188 cls : 'column col-md-12',
189 html : this.fieldLabel
193 cls : 'column col-md-12',
198 if(this.labelAlign == 'left'){
203 html : this.fieldLabel
212 if(this.labelWidth > 12){
213 content[0].style = "width: " + this.labelWidth + 'px';
216 if(this.labelWidth < 13 && this.labelmd == 0){
217 this.labelmd = this.labelWidth;
220 if(this.labellg > 0){
221 content[0].cls += ' col-lg-' + this.labellg;
222 content[1].cls += ' col-lg-' + (12 - this.labellg);
225 if(this.labelmd > 0){
226 content[0].cls += ' col-md-' + this.labelmd;
227 content[1].cls += ' col-md-' + (12 - this.labelmd);
230 if(this.labelsm > 0){
231 content[0].cls += ' col-sm-' + this.labelsm;
232 content[1].cls += ' col-sm-' + (12 - this.labelsm);
235 if(this.labelxs > 0){
236 content[0].cls += ' col-xs-' + this.labelxs;
237 content[1].cls += ' col-xs-' + (12 - this.labelxs);
245 cls : 'row clearfix',
253 initEvents : function()
255 this.managerEl = this.el.select('.roo-document-manager', true).first();
256 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
258 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
259 this.selectorEl.hide();
262 this.selectorEl.attr('multiple', 'multiple');
265 this.selectorEl.on('change', this.onFileSelected, this);
267 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
268 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
270 this.uploader.on('click', this.onUploaderClick, this);
272 this.renderProgressDialog();
276 window.addEventListener("resize", function() { _this.refresh(); } );
278 this.fireEvent('initial', this);
281 renderProgressDialog : function()
285 this.progressDialog = new Roo.bootstrap.Modal({
286 cls : 'roo-document-manager-progress-dialog',
297 btnclick : function() {
298 _this.uploadCancel();
304 this.progressDialog.render(Roo.get(document.body));
306 this.progress = new Roo.bootstrap.Progress({
307 cls : 'roo-document-manager-progress',
312 this.progress.render(this.progressDialog.getChildContainer());
314 this.progressBar = new Roo.bootstrap.ProgressBar({
315 cls : 'roo-document-manager-progress-bar',
322 this.progressBar.render(this.progress.getChildContainer());
325 onUploaderClick : function(e)
329 if(this.fireEvent('beforeselectfile', this) != false){
330 this.selectorEl.dom.click();
335 onFileSelected : function(e)
339 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
343 Roo.each(this.selectorEl.dom.files, function(file){
344 if(this.fireEvent('inspect', this, file) != false){
345 this.files.push(file);
355 this.selectorEl.dom.value = '';
357 if(!this.files || !this.files.length){
361 if(this.boxes > 0 && this.files.length > this.boxes){
362 this.files = this.files.slice(0, this.boxes);
365 this.uploader.show();
367 if(this.boxes > 0 && this.files.length > this.boxes - 1){
368 this.uploader.hide();
377 Roo.each(this.files, function(file){
379 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
380 var f = this.renderPreview(file);
385 if(file.type.indexOf('image') != -1){
389 }).createDelegate(this)
398 }).createDelegate(this)
405 this.delegates = this.delegates.concat(docs);
407 if(!this.delegates.length){
412 this.progressBar.aria_valuemax = this.delegates.length;
421 if(!this.delegates.length){
422 this.progressDialog.hide();
427 var delegate = this.delegates.shift();
429 this.progressDialog.show();
431 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
433 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
440 this.uploader.show();
442 if(this.boxes > 0 && this.files.length > this.boxes - 1){
443 this.uploader.hide();
446 Roo.isTouch ? this.closable(false) : this.closable(true);
448 this.fireEvent('refresh', this);
451 onRemove : function(e, el, o)
455 this.fireEvent('remove', this, o);
463 Roo.each(this.files, function(file){
464 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
480 Roo.each(this.files, function(file){
485 file.target.remove();
494 onClick : function(e, el, o)
498 this.fireEvent('click', this, o);
502 closable : function(closable)
504 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
506 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
518 xhrOnLoad : function(xhr)
520 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
524 if (xhr.readyState !== 4) {
526 this.fireEvent('exception', this, xhr);
530 var response = Roo.decode(xhr.responseText);
532 if(!response.success){
534 this.fireEvent('exception', this, xhr);
538 var file = this.renderPreview(response.data);
540 this.files.push(file);
544 this.fireEvent('afterupload', this, xhr);
548 xhrOnError : function(xhr)
550 Roo.log('xhr on error');
552 var response = Roo.decode(xhr.responseText);
559 process : function(file)
561 if(this.fireEvent('process', this, file) !== false){
562 if(this.editable && file.type.indexOf('image') != -1){
563 this.fireEvent('edit', this, file);
567 this.uploadStart(file, false);
574 uploadStart : function(file, crop)
576 this.xhr = new XMLHttpRequest();
578 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
585 this.managerEl.createChild({
587 cls : 'roo-document-manager-loading',
592 cls : 'roo-document-manager-thumb',
593 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
599 this.xhr.open(this.method, this.url, true);
602 "Accept": "application/json",
603 "Cache-Control": "no-cache",
604 "X-Requested-With": "XMLHttpRequest"
607 for (var headerName in headers) {
608 var headerValue = headers[headerName];
610 this.xhr.setRequestHeader(headerName, headerValue);
616 this.xhr.onload = function()
618 _this.xhrOnLoad(_this.xhr);
621 this.xhr.onerror = function()
623 _this.xhrOnError(_this.xhr);
626 var formData = new FormData();
628 formData.append('returnHTML', 'NO');
631 formData.append('crop', crop);
634 formData.append(this.paramName, file, file.name);
641 if(this.fireEvent('prepare', this, formData, options) != false){
643 if(options.manually){
647 this.xhr.send(formData);
654 uploadCancel : function()
662 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
669 renderPreview : function(file)
671 if(typeof(file.target) != 'undefined' && file.target){
675 var img_src = encodeURI(baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename);
677 var previewEl = this.managerEl.createChild({
679 cls : 'roo-document-manager-preview',
683 tooltip : file[this.toolTipName],
684 cls : 'roo-document-manager-thumb',
685 html : '<img tooltip="' + file[this.toolTipName] + '" src="' + img_src + '">'
690 html : '<i class="fa fa-times-circle"></i>'
695 var close = previewEl.select('button.close', true).first();
697 close.on('click', this.onRemove, this, file);
699 file.target = previewEl;
701 var image = previewEl.select('img', true).first();
705 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
707 image.on('click', this.onClick, this, file);
709 this.fireEvent('previewrendered', this, file);
715 onPreviewLoad : function(file, image)
717 if(typeof(file.target) == 'undefined' || !file.target){
721 var width = image.dom.naturalWidth || image.dom.width;
722 var height = image.dom.naturalHeight || image.dom.height;
724 if(!this.previewResize) {
729 file.target.addClass('wide');
733 file.target.addClass('tall');
738 uploadFromSource : function(file, crop)
740 this.xhr = new XMLHttpRequest();
742 this.managerEl.createChild({
744 cls : 'roo-document-manager-loading',
749 cls : 'roo-document-manager-thumb',
750 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
756 this.xhr.open(this.method, this.url, true);
759 "Accept": "application/json",
760 "Cache-Control": "no-cache",
761 "X-Requested-With": "XMLHttpRequest"
764 for (var headerName in headers) {
765 var headerValue = headers[headerName];
767 this.xhr.setRequestHeader(headerName, headerValue);
773 this.xhr.onload = function()
775 _this.xhrOnLoad(_this.xhr);
778 this.xhr.onerror = function()
780 _this.xhrOnError(_this.xhr);
783 var formData = new FormData();
785 formData.append('returnHTML', 'NO');
787 formData.append('crop', crop);
789 if(typeof(file.filename) != 'undefined'){
790 formData.append('filename', file.filename);
793 if(typeof(file.mimetype) != 'undefined'){
794 formData.append('mimetype', file.mimetype);
799 if(this.fireEvent('prepare', this, formData) != false){
800 this.xhr.send(formData);