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 default 12
14 * @cfg {Boolean} multiple multiple upload default true
15 * @cfg {Number} minWidth default 300
16 * @cfg {Number} minHeight default 300
17 * @cfg {Number} thumbSize default 300
18 * @cfg {String} fieldLabel
19 * @cfg {Number} labelWidth default 4
20 * @cfg {String} labelAlign (left|top) default left
21 * @cfg {Boolean} editable (true|false) allow edit when upload a image default true
24 * Create a new DocumentManager
25 * @param {Object} config The config object
28 Roo.bootstrap.DocumentManager = function(config){
29 Roo.bootstrap.DocumentManager.superclass.constructor.call(this, config);
34 * Fire when initial the DocumentManager
35 * @param {Roo.bootstrap.DocumentManager} this
40 * inspect selected file
41 * @param {Roo.bootstrap.DocumentManager} this
47 * Fire when xhr load exception
48 * @param {Roo.bootstrap.DocumentManager} this
49 * @param {XMLHttpRequest} xhr
54 * prepare the form data
55 * @param {Roo.bootstrap.DocumentManager} this
56 * @param {Object} formData
61 * Fire when remove the file
62 * @param {Roo.bootstrap.DocumentManager} this
63 * @param {Object} file
68 * Fire after refresh the file
69 * @param {Roo.bootstrap.DocumentManager} this
74 * Fire after click the image
75 * @param {Roo.bootstrap.DocumentManager} this
76 * @param {Object} file
81 * Fire when upload a image and editable set to true
82 * @param {Roo.bootstrap.DocumentManager} this
83 * @param {Object} file
90 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
101 paramName : 'imageUpload',
108 getAutoCreate : function()
110 var managerWidget = {
112 cls : 'roo-document-manager',
116 cls : 'roo-document-manager-selector',
121 cls : 'roo-document-manager-uploader',
125 cls : 'roo-document-manager-upload-btn',
126 html : '<i class="fa fa-plus"></i>'
137 cls : 'column col-md-12',
142 if(this.fieldLabel.length){
147 cls : 'column col-md-12',
148 html : this.fieldLabel
152 cls : 'column col-md-12',
157 if(this.labelAlign == 'left'){
161 cls : 'column col-md-' + this.labelWidth,
162 html : this.fieldLabel
166 cls : 'column col-md-' + (12 - this.labelWidth),
176 cls : 'row clearfix',
184 initEvents : function()
186 this.managerEl = this.el.select('.roo-document-manager', true).first();
187 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
189 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
190 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
191 this.selectorEl.hide();
194 this.selectorEl.attr('multiple', 'multiple');
197 this.selectorEl.on('change', this.onFileSelected, this);
199 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
200 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
202 this.uploader.on('click', this.onUploaderClick, this);
204 this.renderProgressDialog();
208 // window.addEventListener("resize", function() { _this.refresh(); } );
210 this.fireEvent('initial', this);
213 renderProgressDialog : function()
215 this.progressDialog = new Roo.bootstrap.Modal({
216 cls : 'roo-document-manager-progress-dialog',
219 buttons : Roo.bootstrap.Modal.OK,
221 btnclick : function() {
227 this.progressDialog.render(Roo.get(document.body));
229 this.progress = new Roo.bootstrap.Progress({
230 cls : 'roo-document-manager-progress',
235 this.progress.render(this.progressDialog.getChildContainer());
237 this.progressBar = new Roo.bootstrap.ProgressBar({
238 cls : 'roo-document-manager-progress-bar',
245 this.progressBar.render(this.progress.getChildContainer());
248 updateProgressDialog : function()
253 onUploaderClick : function(e)
256 this.selectorEl.dom.click();
259 onFileSelected : function(e)
263 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
267 Roo.each(this.selectorEl.dom.files, function(file){
268 if(this.fireEvent('inspect', this, file) != false){
269 this.files.push(file);
279 this.selectorEl.dom.value = '';
281 if(!this.files.length){
285 if(this.files.length > this.boxes){
286 this.files = this.files.slice(0, this.boxes);
293 Roo.each(this.files, function(file){
295 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
303 }).createDelegate(this)
315 if(!this.delegates.length){
319 this.progressBar.aria_valuemax = this.delegates.length;
321 var delegate = this.delegates.shift();
327 var xhr = new XMLHttpRequest();
329 Roo.each(this.files, function(file, index){
330 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
336 this.managerEl.createChild({
338 cls : 'roo-document-manager-loading',
343 cls : 'roo-document-manager-thumb',
344 html : '<i class="fa fa-spinner fa-pulse"></i>'
352 if(this.files.length > this.boxes - 1 ){
353 this.uploader.hide();
357 "Accept": "application/json",
358 "Cache-Control": "no-cache",
359 "X-Requested-With": "XMLHttpRequest"
362 xhr.open(this.method, this.url, true);
364 for (var headerName in headers) {
365 var headerValue = headers[headerName];
367 xhr.setRequestHeader(headerName, headerValue);
373 xhr.onload = function()
375 _this.xhrOnLoad(xhr);
378 xhr.onerror = function()
380 _this.xhrOnError(xhr);
383 var formData = new FormData();
385 formData.append('returnHTML', 'NO');
387 Roo.each(this.files, function(file, index){
389 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
393 formData.append(this.getParamName(index), file, file.name);
397 if(this.fireEvent('prepare', this, formData) != false){
405 if(!this.delegates.length){
409 Roo.log('arrange...');
411 Roo.log(this.delegates.length);
413 // this.progressBar.aria_valuemax = this.delegates.length;
415 var delegate = this.delegates.shift();
422 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
429 Roo.each(this.files, function(file){
431 if(typeof(file.id) == 'undefined' || file.id * 1 < 1){
440 var previewEl = this.managerEl.createChild({
442 cls : 'roo-document-manager-preview',
446 tooltip : file.filename,
447 cls : 'roo-document-manager-thumb',
448 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
458 var close = previewEl.select('button.close', true).first();
460 close.on('click', this.onRemove, this, file);
462 file.target = previewEl;
464 var image = previewEl.select('img', true).first();
466 image.on('click', this.onClick, this, file);
476 this.uploader.show();
478 if(this.files.length > this.boxes - 1){
479 this.uploader.hide();
482 Roo.isTouch ? this.closable(false) : this.closable(true);
484 this.fireEvent('refresh', this);
487 onRemove : function(e, el, o)
491 this.fireEvent('remove', this, o);
499 Roo.each(this.files, function(file){
500 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
514 onClick : function(e, el, o)
518 this.fireEvent('click', this, o);
522 closable : function(closable)
524 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
526 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
538 xhrOnLoad : function(xhr)
540 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
544 if (xhr.readyState !== 4) {
546 this.fireEvent('exception', this, xhr);
550 var response = Roo.decode(xhr.responseText);
552 if(!response.success){
554 this.fireEvent('exception', this, xhr);
558 var previewEl = this.managerEl.createChild({
560 cls : 'roo-document-manager-preview',
564 tooltip : response.data.filename,
565 cls : 'roo-document-manager-thumb',
566 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + response.data.id + '/' + response.data.filename + '">'
576 var close = previewEl.select('button.close', true).first();
578 close.on('click', this.onRemove, this, file);
580 response.data.target = previewEl;
582 var image = previewEl.select('img', true).first();
584 image.on('click', this.onClick, this, file);
586 this.files.push(response.data);
592 xhrOnError : function()
594 Roo.log('xhr on error');
596 var response = Roo.decode(xhr.responseText);
603 process : function(file)
605 Roo.log('process...');
608 if(this.editable && file.type.indexOf('image') != -1){
609 this.fireEvent('edit', this, file);
618 uploadStart : function(file, crop)
620 var xhr = new XMLHttpRequest();
622 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
629 this.managerEl.createChild({
631 cls : 'roo-document-manager-loading',
636 cls : 'roo-document-manager-thumb',
637 html : '<i class="fa fa-spinner fa-pulse"></i>'
643 xhr.open(this.method, this.url, true);
646 "Accept": "application/json",
647 "Cache-Control": "no-cache",
648 "X-Requested-With": "XMLHttpRequest"
651 for (var headerName in headers) {
652 var headerValue = headers[headerName];
654 xhr.setRequestHeader(headerName, headerValue);
660 xhr.onload = function()
662 _this.xhrOnLoad(xhr);
665 xhr.onerror = function()
667 _this.xhrOnError(xhr);
670 var formData = new FormData();
672 formData.append('returnHTML', 'NO');
675 formData.append('crop', crop);
678 formData.append(this.paramName, file, file.name);
680 if(this.fireEvent('prepare', this, formData) != false){