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 * Fire when xhr load exception
56 * @param {Roo.bootstrap.DocumentManager} this
57 * @param {XMLHttpRequest} xhr
62 * prepare the form data
63 * @param {Roo.bootstrap.DocumentManager} this
64 * @param {Object} formData
69 * Fire when remove the file
70 * @param {Roo.bootstrap.DocumentManager} this
71 * @param {Object} file
76 * Fire after refresh the file
77 * @param {Roo.bootstrap.DocumentManager} this
82 * Fire after click the image
83 * @param {Roo.bootstrap.DocumentManager} this
84 * @param {Object} file
89 * Fire when upload a image and editable set to true
90 * @param {Roo.bootstrap.DocumentManager} this
91 * @param {Object} file
95 * @event beforeselectfile
96 * Fire before select file
97 * @param {Roo.bootstrap.DocumentManager} this
99 "beforeselectfile" : true,
102 * Fire before process file
103 * @param {Roo.bootstrap.DocumentManager} this
104 * @param {Object} file
111 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
120 paramName : 'imageUpload',
130 getAutoCreate : function()
132 var managerWidget = {
134 cls : 'roo-document-manager',
138 cls : 'roo-document-manager-selector',
143 cls : 'roo-document-manager-uploader',
147 cls : 'roo-document-manager-upload-btn',
148 html : '<i class="fa fa-plus"></i>'
159 cls : 'column col-md-12',
164 if(this.fieldLabel.length){
169 cls : 'column col-md-12',
170 html : this.fieldLabel
174 cls : 'column col-md-12',
179 if(this.labelAlign == 'left'){
183 cls : 'column col-md-' + this.labelWidth,
184 html : this.fieldLabel
188 cls : 'column col-md-' + (12 - this.labelWidth),
198 cls : 'row clearfix',
206 initEvents : function()
208 this.managerEl = this.el.select('.roo-document-manager', true).first();
209 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
211 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
212 this.selectorEl.hide();
215 this.selectorEl.attr('multiple', 'multiple');
218 this.selectorEl.on('change', this.onFileSelected, this);
220 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
221 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
223 this.uploader.on('click', this.onUploaderClick, this);
225 this.renderProgressDialog();
229 window.addEventListener("resize", function() { _this.refresh(); } );
231 this.fireEvent('initial', this);
234 renderProgressDialog : function()
238 this.progressDialog = new Roo.bootstrap.Modal({
239 cls : 'roo-document-manager-progress-dialog',
250 btnclick : function() {
251 _this.uploadCancel();
257 this.progressDialog.render(Roo.get(document.body));
259 this.progress = new Roo.bootstrap.Progress({
260 cls : 'roo-document-manager-progress',
265 this.progress.render(this.progressDialog.getChildContainer());
267 this.progressBar = new Roo.bootstrap.ProgressBar({
268 cls : 'roo-document-manager-progress-bar',
275 this.progressBar.render(this.progress.getChildContainer());
278 onUploaderClick : function(e)
282 if(this.fireEvent('beforeselectfile', this) != false){
283 this.selectorEl.dom.click();
288 onFileSelected : function(e)
292 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
296 Roo.each(this.selectorEl.dom.files, function(file){
297 if(this.fireEvent('inspect', this, file) != false){
298 this.files.push(file);
308 this.selectorEl.dom.value = '';
310 if(!this.files.length){
314 if(this.boxes > 0 && this.files.length > this.boxes){
315 this.files = this.files.slice(0, this.boxes);
318 this.uploader.show();
320 if(this.boxes > 0 && this.files.length > this.boxes - 1){
321 this.uploader.hide();
330 Roo.each(this.files, function(file){
332 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
333 var f = this.renderPreview(file);
338 if(file.type.indexOf('image') != -1){
342 }).createDelegate(this)
351 }).createDelegate(this)
358 this.delegates = this.delegates.concat(docs);
360 if(!this.delegates.length){
365 this.progressBar.aria_valuemax = this.delegates.length;
374 if(!this.delegates.length){
375 this.progressDialog.hide();
380 var delegate = this.delegates.shift();
382 this.progressDialog.show();
384 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
386 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
393 this.uploader.show();
395 if(this.boxes > 0 && this.files.length > this.boxes - 1){
396 this.uploader.hide();
399 Roo.isTouch ? this.closable(false) : this.closable(true);
401 this.fireEvent('refresh', this);
404 onRemove : function(e, el, o)
408 this.fireEvent('remove', this, o);
416 Roo.each(this.files, function(file){
417 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
433 Roo.each(this.files, function(file){
438 file.target.remove();
447 onClick : function(e, el, o)
451 this.fireEvent('click', this, o);
455 closable : function(closable)
457 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
459 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
471 xhrOnLoad : function(xhr)
473 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
477 if (xhr.readyState !== 4) {
479 this.fireEvent('exception', this, xhr);
483 var response = Roo.decode(xhr.responseText);
485 if(!response.success){
487 this.fireEvent('exception', this, xhr);
491 var file = this.renderPreview(response.data);
493 this.files.push(file);
497 this.fireEvent('afterupload', this, xhr);
501 xhrOnError : function(xhr)
503 Roo.log('xhr on error');
505 var response = Roo.decode(xhr.responseText);
512 process : function(file)
514 if(this.fireEvent('process', this, file) !== false){
515 if(this.editable && file.type.indexOf('image') != -1){
516 this.fireEvent('edit', this, file);
520 this.uploadStart(file, false);
527 uploadStart : function(file, crop)
529 this.xhr = new XMLHttpRequest();
531 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
538 this.managerEl.createChild({
540 cls : 'roo-document-manager-loading',
545 cls : 'roo-document-manager-thumb',
546 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
552 this.xhr.open(this.method, this.url, true);
555 "Accept": "application/json",
556 "Cache-Control": "no-cache",
557 "X-Requested-With": "XMLHttpRequest"
560 for (var headerName in headers) {
561 var headerValue = headers[headerName];
563 this.xhr.setRequestHeader(headerName, headerValue);
569 this.xhr.onload = function()
571 _this.xhrOnLoad(_this.xhr);
574 this.xhr.onerror = function()
576 _this.xhrOnError(_this.xhr);
579 var formData = new FormData();
581 formData.append('returnHTML', 'NO');
584 formData.append('crop', crop);
587 formData.append(this.paramName, file, file.name);
589 if(this.fireEvent('prepare', this, formData, file) != false){
590 this.xhr.send(formData);
597 uploadCancel : function()
605 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
612 renderPreview : function(file)
614 if(typeof(file.target) != 'undefined' && file.target){
618 var previewEl = this.managerEl.createChild({
620 cls : 'roo-document-manager-preview',
624 tooltip : file.filename,
625 cls : 'roo-document-manager-thumb',
626 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
631 html : '<i class="fa fa-times-circle"></i>'
636 var close = previewEl.select('button.close', true).first();
638 close.on('click', this.onRemove, this, file);
640 file.target = previewEl;
642 var image = previewEl.select('img', true).first();
646 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
648 image.on('click', this.onClick, this, file);
654 onPreviewLoad : function(file, image)
656 if(typeof(file.target) == 'undefined' || !file.target){
660 var width = image.dom.naturalWidth || image.dom.width;
661 var height = image.dom.naturalHeight || image.dom.height;
664 file.target.addClass('wide');
668 file.target.addClass('tall');
673 uploadFromSource : function(file, crop)
675 this.xhr = new XMLHttpRequest();
677 this.managerEl.createChild({
679 cls : 'roo-document-manager-loading',
684 cls : 'roo-document-manager-thumb',
685 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
691 this.xhr.open(this.method, this.url, true);
694 "Accept": "application/json",
695 "Cache-Control": "no-cache",
696 "X-Requested-With": "XMLHttpRequest"
699 for (var headerName in headers) {
700 var headerValue = headers[headerName];
702 this.xhr.setRequestHeader(headerName, headerValue);
708 this.xhr.onload = function()
710 _this.xhrOnLoad(_this.xhr);
713 this.xhr.onerror = function()
715 _this.xhrOnError(_this.xhr);
718 var formData = new FormData();
720 formData.append('returnHTML', 'NO');
722 formData.append('crop', crop);
724 if(typeof(file.filename) != 'undefined'){
725 formData.append('filename', file.filename);
728 if(typeof(file.mimetype) != 'undefined'){
729 formData.append('mimetype', file.mimetype);
732 if(this.fireEvent('prepare', this, formData) != false){
733 this.xhr.send(formData);