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
116 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
125 paramName : 'imageUpload',
126 toolTipName : 'filename',
139 getAutoCreate : function()
141 var managerWidget = {
143 cls : 'roo-document-manager',
147 cls : 'roo-document-manager-selector',
152 cls : 'roo-document-manager-uploader',
156 cls : 'roo-document-manager-upload-btn',
157 html : '<i class="fa fa-plus"></i>'
168 cls : 'column col-md-12',
173 if(this.fieldLabel.length){
178 cls : 'column col-md-12',
179 html : this.fieldLabel
183 cls : 'column col-md-12',
188 if(this.labelAlign == 'left'){
193 html : this.fieldLabel
202 if(this.labelWidth > 12){
203 content[0].style = "width: " + this.labelWidth + 'px';
206 if(this.labelWidth < 13 && this.labelmd == 0){
207 this.labelmd = this.labelWidth;
210 if(this.labellg > 0){
211 content[0].cls += ' col-lg-' + this.labellg;
212 content[1].cls += ' col-lg-' + (12 - this.labellg);
215 if(this.labelmd > 0){
216 content[0].cls += ' col-md-' + this.labelmd;
217 content[1].cls += ' col-md-' + (12 - this.labelmd);
220 if(this.labelsm > 0){
221 content[0].cls += ' col-sm-' + this.labelsm;
222 content[1].cls += ' col-sm-' + (12 - this.labelsm);
225 if(this.labelxs > 0){
226 content[0].cls += ' col-xs-' + this.labelxs;
227 content[1].cls += ' col-xs-' + (12 - this.labelxs);
235 cls : 'row clearfix',
243 initEvents : function()
245 this.managerEl = this.el.select('.roo-document-manager', true).first();
246 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
248 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
249 this.selectorEl.hide();
252 this.selectorEl.attr('multiple', 'multiple');
255 this.selectorEl.on('change', this.onFileSelected, this);
257 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
258 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
260 this.uploader.on('click', this.onUploaderClick, this);
262 this.renderProgressDialog();
266 window.addEventListener("resize", function() { _this.refresh(); } );
268 this.fireEvent('initial', this);
271 renderProgressDialog : function()
275 this.progressDialog = new Roo.bootstrap.Modal({
276 cls : 'roo-document-manager-progress-dialog',
287 btnclick : function() {
288 _this.uploadCancel();
294 this.progressDialog.render(Roo.get(document.body));
296 this.progress = new Roo.bootstrap.Progress({
297 cls : 'roo-document-manager-progress',
302 this.progress.render(this.progressDialog.getChildContainer());
304 this.progressBar = new Roo.bootstrap.ProgressBar({
305 cls : 'roo-document-manager-progress-bar',
312 this.progressBar.render(this.progress.getChildContainer());
315 onUploaderClick : function(e)
319 if(this.fireEvent('beforeselectfile', this) != false){
320 this.selectorEl.dom.click();
325 onFileSelected : function(e)
329 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
333 Roo.each(this.selectorEl.dom.files, function(file){
334 if(this.fireEvent('inspect', this, file) != false){
335 this.files.push(file);
345 this.selectorEl.dom.value = '';
347 if(!this.files || !this.files.length){
351 if(this.boxes > 0 && this.files.length > this.boxes){
352 this.files = this.files.slice(0, this.boxes);
355 this.uploader.show();
357 if(this.boxes > 0 && this.files.length > this.boxes - 1){
358 this.uploader.hide();
367 Roo.each(this.files, function(file){
369 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
370 var f = this.renderPreview(file);
375 if(file.type.indexOf('image') != -1){
379 }).createDelegate(this)
388 }).createDelegate(this)
395 this.delegates = this.delegates.concat(docs);
397 if(!this.delegates.length){
402 this.progressBar.aria_valuemax = this.delegates.length;
411 if(!this.delegates.length){
412 this.progressDialog.hide();
417 var delegate = this.delegates.shift();
419 this.progressDialog.show();
421 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
423 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
430 this.uploader.show();
432 if(this.boxes > 0 && this.files.length > this.boxes - 1){
433 this.uploader.hide();
436 Roo.isTouch ? this.closable(false) : this.closable(true);
438 this.fireEvent('refresh', this);
441 onRemove : function(e, el, o)
445 this.fireEvent('remove', this, o);
453 Roo.each(this.files, function(file){
454 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
470 Roo.each(this.files, function(file){
475 file.target.remove();
484 onClick : function(e, el, o)
488 this.fireEvent('click', this, o);
492 closable : function(closable)
494 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
496 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
508 xhrOnLoad : function(xhr)
510 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
514 if (xhr.readyState !== 4) {
516 this.fireEvent('exception', this, xhr);
520 var response = Roo.decode(xhr.responseText);
522 if(!response.success){
524 this.fireEvent('exception', this, xhr);
528 var file = this.renderPreview(response.data);
530 this.files.push(file);
534 this.fireEvent('afterupload', this, xhr);
538 xhrOnError : function(xhr)
540 Roo.log('xhr on error');
542 var response = Roo.decode(xhr.responseText);
549 process : function(file)
551 if(this.fireEvent('process', this, file) !== false){
552 if(this.editable && file.type.indexOf('image') != -1){
553 this.fireEvent('edit', this, file);
557 this.uploadStart(file, false);
564 uploadStart : function(file, crop)
566 this.xhr = new XMLHttpRequest();
568 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
575 this.managerEl.createChild({
577 cls : 'roo-document-manager-loading',
582 cls : 'roo-document-manager-thumb',
583 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
589 this.xhr.open(this.method, this.url, true);
592 "Accept": "application/json",
593 "Cache-Control": "no-cache",
594 "X-Requested-With": "XMLHttpRequest"
597 for (var headerName in headers) {
598 var headerValue = headers[headerName];
600 this.xhr.setRequestHeader(headerName, headerValue);
606 this.xhr.onload = function()
608 _this.xhrOnLoad(_this.xhr);
611 this.xhr.onerror = function()
613 _this.xhrOnError(_this.xhr);
616 var formData = new FormData();
618 formData.append('returnHTML', 'NO');
621 formData.append('crop', crop);
624 formData.append(this.paramName, file, file.name);
631 if(this.fireEvent('prepare', this, formData, options) != false){
633 if(options.manually){
637 this.xhr.send(formData);
644 uploadCancel : function()
652 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
659 renderPreview : function(file)
661 if(typeof(file.target) != 'undefined' && file.target){
665 var img_src = encodeURI(baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename);
667 var previewEl = this.managerEl.createChild({
669 cls : 'roo-document-manager-preview',
673 tooltip : file[this.toolTipName],
674 cls : 'roo-document-manager-thumb',
675 html : '<img tooltip="' + file[this.toolTipName] + '" src="' + img_src + '">'
680 html : '<i class="fa fa-times-circle"></i>'
685 var close = previewEl.select('button.close', true).first();
687 close.on('click', this.onRemove, this, file);
689 file.target = previewEl;
691 var image = previewEl.select('img', true).first();
695 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
697 image.on('click', this.onClick, this, file);
699 this.fireEvent('previewrendered', this, file);
705 onPreviewLoad : function(file, image)
707 if(typeof(file.target) == 'undefined' || !file.target){
711 var width = image.dom.naturalWidth || image.dom.width;
712 var height = image.dom.naturalHeight || image.dom.height;
715 file.target.addClass('wide');
719 file.target.addClass('tall');
724 uploadFromSource : function(file, crop)
726 this.xhr = new XMLHttpRequest();
728 this.managerEl.createChild({
730 cls : 'roo-document-manager-loading',
735 cls : 'roo-document-manager-thumb',
736 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
742 this.xhr.open(this.method, this.url, true);
745 "Accept": "application/json",
746 "Cache-Control": "no-cache",
747 "X-Requested-With": "XMLHttpRequest"
750 for (var headerName in headers) {
751 var headerValue = headers[headerName];
753 this.xhr.setRequestHeader(headerName, headerValue);
759 this.xhr.onload = function()
761 _this.xhrOnLoad(_this.xhr);
764 this.xhr.onerror = function()
766 _this.xhrOnError(_this.xhr);
769 var formData = new FormData();
771 formData.append('returnHTML', 'NO');
773 formData.append('crop', crop);
775 if(typeof(file.filename) != 'undefined'){
776 formData.append('filename', file.filename);
779 if(typeof(file.mimetype) != 'undefined'){
780 formData.append('mimetype', file.mimetype);
785 if(this.fireEvent('prepare', this, formData) != false){
786 this.xhr.send(formData);