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,
123 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
132 paramName : 'imageUpload',
133 toolTipName : 'filename',
146 getAutoCreate : function()
148 var managerWidget = {
150 cls : 'roo-document-manager',
154 cls : 'roo-document-manager-selector',
159 cls : 'roo-document-manager-uploader',
163 cls : 'roo-document-manager-upload-btn',
164 html : '<i class="fa fa-plus"></i>'
175 cls : 'column col-md-12',
180 if(this.fieldLabel.length){
185 cls : 'column col-md-12',
186 html : this.fieldLabel
190 cls : 'column col-md-12',
195 if(this.labelAlign == 'left'){
200 html : this.fieldLabel
209 if(this.labelWidth > 12){
210 content[0].style = "width: " + this.labelWidth + 'px';
213 if(this.labelWidth < 13 && this.labelmd == 0){
214 this.labelmd = this.labelWidth;
217 if(this.labellg > 0){
218 content[0].cls += ' col-lg-' + this.labellg;
219 content[1].cls += ' col-lg-' + (12 - this.labellg);
222 if(this.labelmd > 0){
223 content[0].cls += ' col-md-' + this.labelmd;
224 content[1].cls += ' col-md-' + (12 - this.labelmd);
227 if(this.labelsm > 0){
228 content[0].cls += ' col-sm-' + this.labelsm;
229 content[1].cls += ' col-sm-' + (12 - this.labelsm);
232 if(this.labelxs > 0){
233 content[0].cls += ' col-xs-' + this.labelxs;
234 content[1].cls += ' col-xs-' + (12 - this.labelxs);
242 cls : 'row clearfix',
250 initEvents : function()
252 this.managerEl = this.el.select('.roo-document-manager', true).first();
253 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
255 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
256 this.selectorEl.hide();
259 this.selectorEl.attr('multiple', 'multiple');
262 this.selectorEl.on('change', this.onFileSelected, this);
264 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
265 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
267 this.uploader.on('click', this.onUploaderClick, this);
269 this.renderProgressDialog();
273 window.addEventListener("resize", function() { _this.refresh(); } );
275 this.fireEvent('initial', this);
278 renderProgressDialog : function()
282 this.progressDialog = new Roo.bootstrap.Modal({
283 cls : 'roo-document-manager-progress-dialog',
294 btnclick : function() {
295 _this.uploadCancel();
301 this.progressDialog.render(Roo.get(document.body));
303 this.progress = new Roo.bootstrap.Progress({
304 cls : 'roo-document-manager-progress',
309 this.progress.render(this.progressDialog.getChildContainer());
311 this.progressBar = new Roo.bootstrap.ProgressBar({
312 cls : 'roo-document-manager-progress-bar',
319 this.progressBar.render(this.progress.getChildContainer());
322 onUploaderClick : function(e)
326 if(this.fireEvent('beforeselectfile', this) != false){
327 this.selectorEl.dom.click();
332 onFileSelected : function(e)
336 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
340 Roo.each(this.selectorEl.dom.files, function(file){
341 if(this.fireEvent('inspect', this, file) != false){
342 this.files.push(file);
352 this.selectorEl.dom.value = '';
354 if(!this.files || !this.files.length){
358 if(this.boxes > 0 && this.files.length > this.boxes){
359 this.files = this.files.slice(0, this.boxes);
362 this.uploader.show();
364 if(this.boxes > 0 && this.files.length > this.boxes - 1){
365 this.uploader.hide();
374 Roo.each(this.files, function(file){
376 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
377 var f = this.renderPreview(file);
382 if(file.type.indexOf('image') != -1){
386 }).createDelegate(this)
395 }).createDelegate(this)
402 this.delegates = this.delegates.concat(docs);
404 if(!this.delegates.length){
409 this.progressBar.aria_valuemax = this.delegates.length;
418 if(!this.delegates.length){
419 this.progressDialog.hide();
424 var delegate = this.delegates.shift();
426 this.progressDialog.show();
428 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
430 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
437 this.uploader.show();
439 if(this.boxes > 0 && this.files.length > this.boxes - 1){
440 this.uploader.hide();
443 Roo.isTouch ? this.closable(false) : this.closable(true);
445 this.fireEvent('refresh', this);
448 onRemove : function(e, el, o)
452 this.fireEvent('remove', this, o);
460 Roo.each(this.files, function(file){
461 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
477 Roo.each(this.files, function(file){
482 file.target.remove();
491 onClick : function(e, el, o)
495 this.fireEvent('click', this, o);
499 closable : function(closable)
501 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
503 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
515 xhrOnLoad : function(xhr)
517 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
521 if (xhr.readyState !== 4) {
523 this.fireEvent('exception', this, xhr);
527 var response = Roo.decode(xhr.responseText);
529 if(!response.success){
531 this.fireEvent('exception', this, xhr);
535 var file = this.renderPreview(response.data);
537 this.files.push(file);
541 this.fireEvent('afterupload', this, xhr);
545 xhrOnError : function(xhr)
547 Roo.log('xhr on error');
549 var response = Roo.decode(xhr.responseText);
556 process : function(file)
558 if(this.fireEvent('process', this, file) !== false){
559 if(this.editable && file.type.indexOf('image') != -1){
560 this.fireEvent('edit', this, file);
564 this.uploadStart(file, false);
571 uploadStart : function(file, crop)
573 this.xhr = new XMLHttpRequest();
575 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
582 this.managerEl.createChild({
584 cls : 'roo-document-manager-loading',
589 cls : 'roo-document-manager-thumb',
590 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
596 this.xhr.open(this.method, this.url, true);
599 "Accept": "application/json",
600 "Cache-Control": "no-cache",
601 "X-Requested-With": "XMLHttpRequest"
604 for (var headerName in headers) {
605 var headerValue = headers[headerName];
607 this.xhr.setRequestHeader(headerName, headerValue);
613 this.xhr.onload = function()
615 _this.xhrOnLoad(_this.xhr);
618 this.xhr.onerror = function()
620 _this.xhrOnError(_this.xhr);
623 var formData = new FormData();
625 formData.append('returnHTML', 'NO');
628 formData.append('crop', crop);
631 formData.append(this.paramName, file, file.name);
638 if(this.fireEvent('prepare', this, formData, options) != false){
640 if(options.manually){
644 this.xhr.send(formData);
651 uploadCancel : function()
659 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
666 renderPreview : function(file)
668 if(typeof(file.target) != 'undefined' && file.target){
672 var img_src = encodeURI(baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename);
674 var previewEl = this.managerEl.createChild({
676 cls : 'roo-document-manager-preview',
680 tooltip : file[this.toolTipName],
681 cls : 'roo-document-manager-thumb',
682 html : '<img tooltip="' + file[this.toolTipName] + '" src="' + img_src + '">'
687 html : '<i class="fa fa-times-circle"></i>'
692 var close = previewEl.select('button.close', true).first();
694 close.on('click', this.onRemove, this, file);
696 file.target = previewEl;
698 var image = previewEl.select('img', true).first();
702 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
704 image.on('click', this.onClick, this, file);
706 this.fireEvent('previewrendered', this, file);
712 onPreviewLoad : function(file, image)
714 if(typeof(file.target) == 'undefined' || !file.target){
718 var width = image.dom.naturalWidth || image.dom.width;
719 var height = image.dom.naturalHeight || image.dom.height;
722 file.target.addClass('wide');
726 file.target.addClass('tall');
731 uploadFromSource : function(file, crop)
733 this.xhr = new XMLHttpRequest();
735 this.managerEl.createChild({
737 cls : 'roo-document-manager-loading',
742 cls : 'roo-document-manager-thumb',
743 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
749 this.xhr.open(this.method, this.url, true);
752 "Accept": "application/json",
753 "Cache-Control": "no-cache",
754 "X-Requested-With": "XMLHttpRequest"
757 for (var headerName in headers) {
758 var headerValue = headers[headerName];
760 this.xhr.setRequestHeader(headerName, headerValue);
766 this.xhr.onload = function()
768 _this.xhrOnLoad(_this.xhr);
771 this.xhr.onerror = function()
773 _this.xhrOnError(_this.xhr);
776 var formData = new FormData();
778 formData.append('returnHTML', 'NO');
780 formData.append('crop', crop);
782 if(typeof(file.filename) != 'undefined'){
783 formData.append('filename', file.filename);
786 if(typeof(file.mimetype) != 'undefined'){
787 formData.append('mimetype', file.mimetype);
792 if(this.fireEvent('prepare', this, formData) != false){
793 this.xhr.send(formData);