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',
134 getAutoCreate : function()
136 var managerWidget = {
138 cls : 'roo-document-manager',
142 cls : 'roo-document-manager-selector',
147 cls : 'roo-document-manager-uploader',
151 cls : 'roo-document-manager-upload-btn',
152 html : '<i class="fa fa-plus"></i>'
163 cls : 'column col-md-12',
168 if(this.fieldLabel.length){
173 cls : 'column col-md-12',
174 html : this.fieldLabel
178 cls : 'column col-md-12',
183 if(this.labelAlign == 'left'){
187 cls : 'column col-md-' + this.labelWidth,
188 html : this.fieldLabel
192 cls : 'column col-md-' + (12 - this.labelWidth),
202 cls : 'row clearfix',
210 initEvents : function()
212 this.managerEl = this.el.select('.roo-document-manager', true).first();
213 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
215 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
216 this.selectorEl.hide();
219 this.selectorEl.attr('multiple', 'multiple');
222 this.selectorEl.on('change', this.onFileSelected, this);
224 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
225 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
227 this.uploader.on('click', this.onUploaderClick, this);
229 this.renderProgressDialog();
233 window.addEventListener("resize", function() { _this.refresh(); } );
235 this.fireEvent('initial', this);
238 renderProgressDialog : function()
242 this.progressDialog = new Roo.bootstrap.Modal({
243 cls : 'roo-document-manager-progress-dialog',
254 btnclick : function() {
255 _this.uploadCancel();
261 this.progressDialog.render(Roo.get(document.body));
263 this.progress = new Roo.bootstrap.Progress({
264 cls : 'roo-document-manager-progress',
269 this.progress.render(this.progressDialog.getChildContainer());
271 this.progressBar = new Roo.bootstrap.ProgressBar({
272 cls : 'roo-document-manager-progress-bar',
279 this.progressBar.render(this.progress.getChildContainer());
282 onUploaderClick : function(e)
286 if(this.fireEvent('beforeselectfile', this) != false){
287 this.selectorEl.dom.click();
292 onFileSelected : function(e)
296 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
300 Roo.each(this.selectorEl.dom.files, function(file){
301 if(this.fireEvent('inspect', this, file) != false){
302 this.files.push(file);
312 this.selectorEl.dom.value = '';
314 if(!this.files.length){
318 if(this.boxes > 0 && this.files.length > this.boxes){
319 this.files = this.files.slice(0, this.boxes);
322 this.uploader.show();
324 if(this.boxes > 0 && this.files.length > this.boxes - 1){
325 this.uploader.hide();
334 Roo.each(this.files, function(file){
336 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
337 var f = this.renderPreview(file);
342 if(file.type.indexOf('image') != -1){
346 }).createDelegate(this)
355 }).createDelegate(this)
362 this.delegates = this.delegates.concat(docs);
364 if(!this.delegates.length){
369 this.progressBar.aria_valuemax = this.delegates.length;
378 if(!this.delegates.length){
379 this.progressDialog.hide();
384 var delegate = this.delegates.shift();
386 this.progressDialog.show();
388 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
390 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
397 this.uploader.show();
399 if(this.boxes > 0 && this.files.length > this.boxes - 1){
400 this.uploader.hide();
403 Roo.isTouch ? this.closable(false) : this.closable(true);
405 this.fireEvent('refresh', this);
408 onRemove : function(e, el, o)
412 this.fireEvent('remove', this, o);
420 Roo.each(this.files, function(file){
421 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
437 Roo.each(this.files, function(file){
442 file.target.remove();
451 onClick : function(e, el, o)
455 this.fireEvent('click', this, o);
459 closable : function(closable)
461 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
463 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
475 xhrOnLoad : function(xhr)
477 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
481 if (xhr.readyState !== 4) {
483 this.fireEvent('exception', this, xhr);
487 var response = Roo.decode(xhr.responseText);
489 if(!response.success){
491 this.fireEvent('exception', this, xhr);
495 var file = this.renderPreview(response.data);
497 this.files.push(file);
501 this.fireEvent('afterupload', this, xhr);
505 xhrOnError : function(xhr)
507 Roo.log('xhr on error');
509 var response = Roo.decode(xhr.responseText);
516 process : function(file)
518 if(this.fireEvent('process', this, file) !== false){
519 if(this.editable && file.type.indexOf('image') != -1){
520 this.fireEvent('edit', this, file);
524 this.uploadStart(file, false);
531 uploadStart : function(file, crop)
533 this.xhr = new XMLHttpRequest();
535 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
542 this.managerEl.createChild({
544 cls : 'roo-document-manager-loading',
549 cls : 'roo-document-manager-thumb',
550 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
556 this.xhr.open(this.method, this.url, true);
559 "Accept": "application/json",
560 "Cache-Control": "no-cache",
561 "X-Requested-With": "XMLHttpRequest"
564 for (var headerName in headers) {
565 var headerValue = headers[headerName];
567 this.xhr.setRequestHeader(headerName, headerValue);
573 this.xhr.onload = function()
575 _this.xhrOnLoad(_this.xhr);
578 this.xhr.onerror = function()
580 _this.xhrOnError(_this.xhr);
583 var formData = new FormData();
585 formData.append('returnHTML', 'NO');
588 formData.append('crop', crop);
591 formData.append(this.paramName, file, file.name);
598 if(this.fireEvent('prepare', this, formData, options) != false){
600 if(options.manually){
604 this.xhr.send(formData);
611 uploadCancel : function()
619 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
626 renderPreview : function(file)
628 if(typeof(file.target) != 'undefined' && file.target){
632 var previewEl = this.managerEl.createChild({
634 cls : 'roo-document-manager-preview',
638 tooltip : file[this.toolTipName],
639 cls : 'roo-document-manager-thumb',
640 html : '<img tooltip="' + file[this.toolTipName] + '" src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
645 html : '<i class="fa fa-times-circle"></i>'
650 var close = previewEl.select('button.close', true).first();
652 close.on('click', this.onRemove, this, file);
654 file.target = previewEl;
656 var image = previewEl.select('img', true).first();
660 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
662 image.on('click', this.onClick, this, file);
668 onPreviewLoad : function(file, image)
670 if(typeof(file.target) == 'undefined' || !file.target){
674 var width = image.dom.naturalWidth || image.dom.width;
675 var height = image.dom.naturalHeight || image.dom.height;
678 file.target.addClass('wide');
682 file.target.addClass('tall');
687 uploadFromSource : function(file, crop)
689 this.xhr = new XMLHttpRequest();
691 this.managerEl.createChild({
693 cls : 'roo-document-manager-loading',
698 cls : 'roo-document-manager-thumb',
699 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
705 this.xhr.open(this.method, this.url, true);
708 "Accept": "application/json",
709 "Cache-Control": "no-cache",
710 "X-Requested-With": "XMLHttpRequest"
713 for (var headerName in headers) {
714 var headerValue = headers[headerName];
716 this.xhr.setRequestHeader(headerName, headerValue);
722 this.xhr.onload = function()
724 _this.xhrOnLoad(_this.xhr);
727 this.xhr.onerror = function()
729 _this.xhrOnError(_this.xhr);
732 var formData = new FormData();
734 formData.append('returnHTML', 'NO');
736 formData.append('crop', crop);
738 if(typeof(file.filename) != 'undefined'){
739 formData.append('filename', file.filename);
742 if(typeof(file.mimetype) != 'undefined'){
743 formData.append('mimetype', file.mimetype);
746 if(this.fireEvent('prepare', this, formData) != false){
747 this.xhr.send(formData);