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, 0 is no limit.. default 0
14 * @cfg {Boolean} multiple multiple upload default true
15 * @cfg {Number} thumbSize default 300
16 * @cfg {String} fieldLabel
17 * @cfg {Number} labelWidth default 4
18 * @cfg {String} labelAlign (left|top) default left
19 * @cfg {Boolean} editable (true|false) allow edit when upload a image default true
22 * Create a new DocumentManager
23 * @param {Object} config The config object
26 Roo.bootstrap.DocumentManager = function(config){
27 Roo.bootstrap.DocumentManager.superclass.constructor.call(this, config);
35 * Fire when initial the DocumentManager
36 * @param {Roo.bootstrap.DocumentManager} this
41 * inspect selected file
42 * @param {Roo.bootstrap.DocumentManager} this
48 * Fire when xhr load exception
49 * @param {Roo.bootstrap.DocumentManager} this
50 * @param {XMLHttpRequest} xhr
55 * prepare the form data
56 * @param {Roo.bootstrap.DocumentManager} this
57 * @param {Object} formData
62 * Fire when remove the file
63 * @param {Roo.bootstrap.DocumentManager} this
64 * @param {Object} file
69 * Fire after refresh the file
70 * @param {Roo.bootstrap.DocumentManager} this
75 * Fire after click the image
76 * @param {Roo.bootstrap.DocumentManager} this
77 * @param {Object} file
82 * Fire when upload a image and editable set to true
83 * @param {Roo.bootstrap.DocumentManager} this
84 * @param {Object} file
88 * @event beforeselectfile
89 * Fire before select file
90 * @param {Roo.bootstrap.DocumentManager} this
92 "beforeselectfile" : true,
95 * Fire before process file
96 * @param {Roo.bootstrap.DocumentManager} this
97 * @param {Object} file
104 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
113 paramName : 'imageUpload',
123 getAutoCreate : function()
125 var managerWidget = {
127 cls : 'roo-document-manager',
131 cls : 'roo-document-manager-selector',
136 cls : 'roo-document-manager-uploader',
140 cls : 'roo-document-manager-upload-btn',
141 html : '<i class="fa fa-plus"></i>'
152 cls : 'column col-md-12',
157 if(this.fieldLabel.length){
162 cls : 'column col-md-12',
163 html : this.fieldLabel
167 cls : 'column col-md-12',
172 if(this.labelAlign == 'left'){
176 cls : 'column col-md-' + this.labelWidth,
177 html : this.fieldLabel
181 cls : 'column col-md-' + (12 - this.labelWidth),
191 cls : 'row clearfix',
199 initEvents : function()
201 this.managerEl = this.el.select('.roo-document-manager', true).first();
202 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
204 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
205 this.selectorEl.hide();
208 this.selectorEl.attr('multiple', 'multiple');
211 this.selectorEl.on('change', this.onFileSelected, this);
213 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
214 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
216 this.uploader.on('click', this.onUploaderClick, this);
218 this.renderProgressDialog();
222 window.addEventListener("resize", function() { _this.refresh(); } );
224 this.fireEvent('initial', this);
227 renderProgressDialog : function()
231 this.progressDialog = new Roo.bootstrap.Modal({
232 cls : 'roo-document-manager-progress-dialog',
243 btnclick : function() {
244 _this.uploadCancel();
250 this.progressDialog.render(Roo.get(document.body));
252 this.progress = new Roo.bootstrap.Progress({
253 cls : 'roo-document-manager-progress',
258 this.progress.render(this.progressDialog.getChildContainer());
260 this.progressBar = new Roo.bootstrap.ProgressBar({
261 cls : 'roo-document-manager-progress-bar',
268 this.progressBar.render(this.progress.getChildContainer());
271 onUploaderClick : function(e)
275 if(this.fireEvent('beforeselectfile', this) != false){
276 this.selectorEl.dom.click();
281 onFileSelected : function(e)
285 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
289 Roo.each(this.selectorEl.dom.files, function(file){
290 if(this.fireEvent('inspect', this, file) != false){
291 this.files.push(file);
301 this.selectorEl.dom.value = '';
303 if(!this.files.length){
307 if(this.boxes > 0 && this.files.length > this.boxes){
308 this.files = this.files.slice(0, this.boxes);
311 this.uploader.show();
313 if(this.boxes > 0 && this.files.length > this.boxes - 1){
314 this.uploader.hide();
323 Roo.each(this.files, function(file){
325 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
326 var f = this.renderPreview(file);
331 if(file.type.indexOf('image') != -1){
335 }).createDelegate(this)
344 }).createDelegate(this)
351 this.delegates = this.delegates.concat(docs);
353 if(!this.delegates.length){
358 this.progressBar.aria_valuemax = this.delegates.length;
367 if(!this.delegates.length){
368 this.progressDialog.hide();
373 var delegate = this.delegates.shift();
375 this.progressDialog.show();
377 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
379 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
386 this.uploader.show();
388 if(this.boxes > 0 && this.files.length > this.boxes - 1){
389 this.uploader.hide();
392 Roo.isTouch ? this.closable(false) : this.closable(true);
394 this.fireEvent('refresh', this);
397 onRemove : function(e, el, o)
401 this.fireEvent('remove', this, o);
409 Roo.each(this.files, function(file){
410 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
426 Roo.each(this.files, function(file){
431 file.target.remove();
440 onClick : function(e, el, o)
444 this.fireEvent('click', this, o);
448 closable : function(closable)
450 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
452 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
464 xhrOnLoad : function(xhr)
466 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
470 if (xhr.readyState !== 4) {
472 this.fireEvent('exception', this, xhr);
476 var response = Roo.decode(xhr.responseText);
478 if(!response.success){
480 this.fireEvent('exception', this, xhr);
484 var file = this.renderPreview(response.data);
486 this.files.push(file);
492 xhrOnError : function(xhr)
494 Roo.log('xhr on error');
496 var response = Roo.decode(xhr.responseText);
503 process : function(file)
505 if(this.fireEvent('process', this, file) !== false){
506 if(this.editable && file.type.indexOf('image') != -1){
507 this.fireEvent('edit', this, file);
511 this.uploadStart(file, false);
518 uploadStart : function(file, crop)
520 this.xhr = new XMLHttpRequest();
522 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
529 this.managerEl.createChild({
531 cls : 'roo-document-manager-loading',
536 cls : 'roo-document-manager-thumb',
537 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
543 this.xhr.open(this.method, this.url, true);
546 "Accept": "application/json",
547 "Cache-Control": "no-cache",
548 "X-Requested-With": "XMLHttpRequest"
551 for (var headerName in headers) {
552 var headerValue = headers[headerName];
554 this.xhr.setRequestHeader(headerName, headerValue);
560 this.xhr.onload = function()
562 _this.xhrOnLoad(_this.xhr);
565 this.xhr.onerror = function()
567 _this.xhrOnError(_this.xhr);
570 var formData = new FormData();
572 formData.append('returnHTML', 'NO');
575 formData.append('crop', crop);
578 formData.append(this.paramName, file, file.name);
580 if(this.fireEvent('prepare', this, formData) != false){
581 this.xhr.send(formData);
585 uploadCancel : function()
594 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
601 renderPreview : function(file)
603 if(typeof(file.target) != 'undefined' && file.target){
607 var previewEl = this.managerEl.createChild({
609 cls : 'roo-document-manager-preview',
613 tooltip : file.filename,
614 cls : 'roo-document-manager-thumb',
615 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
620 html : '<i class="fa fa-times-circle"></i>'
625 var close = previewEl.select('button.close', true).first();
627 close.on('click', this.onRemove, this, file);
629 file.target = previewEl;
631 var image = previewEl.select('img', true).first();
635 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
637 image.on('click', this.onClick, this, file);
643 onPreviewLoad : function(file, image)
645 if(typeof(file.target) == 'undefined' || !file.target){
649 var width = image.dom.naturalWidth || image.dom.width;
650 var height = image.dom.naturalHeight || image.dom.height;
653 file.target.addClass('wide');
657 file.target.addClass('tall');
662 uploadFromSource : function(file, crop)
664 this.xhr = new XMLHttpRequest();
666 this.managerEl.createChild({
668 cls : 'roo-document-manager-loading',
673 cls : 'roo-document-manager-thumb',
674 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
680 this.xhr.open(this.method, this.url, true);
683 "Accept": "application/json",
684 "Cache-Control": "no-cache",
685 "X-Requested-With": "XMLHttpRequest"
688 for (var headerName in headers) {
689 var headerValue = headers[headerName];
691 this.xhr.setRequestHeader(headerName, headerValue);
697 this.xhr.onload = function()
699 _this.xhrOnLoad(_this.xhr);
702 this.xhr.onerror = function()
704 _this.xhrOnError(_this.xhr);
707 var formData = new FormData();
709 formData.append('returnHTML', 'NO');
711 formData.append('crop', crop);
713 if(typeof(file.filename) != 'undefined'){
714 formData.append('filename', file.filename);
717 if(typeof(file.mimetype) != 'undefined'){
718 formData.append('mimetype', file.mimetype);
721 if(this.fireEvent('prepare', this, formData) != false){
722 this.xhr.send(formData);