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);
32 * Fire when initial the DocumentManager
33 * @param {Roo.bootstrap.DocumentManager} this
38 * inspect selected file
39 * @param {Roo.bootstrap.DocumentManager} this
45 * Fire when xhr load exception
46 * @param {Roo.bootstrap.DocumentManager} this
47 * @param {XMLHttpRequest} xhr
52 * prepare the form data
53 * @param {Roo.bootstrap.DocumentManager} this
54 * @param {Object} formData
59 * Fire when remove the file
60 * @param {Roo.bootstrap.DocumentManager} this
61 * @param {Object} file
66 * Fire after refresh the file
67 * @param {Roo.bootstrap.DocumentManager} this
72 * Fire after click the image
73 * @param {Roo.bootstrap.DocumentManager} this
74 * @param {Object} file
79 * Fire when upload a image and editable set to true
80 * @param {Roo.bootstrap.DocumentManager} this
81 * @param {Object} file
85 * @event beforeselectfile
86 * Fire before select file
87 * @param {Roo.bootstrap.DocumentManager} this
89 "beforeselectfile" : true,
92 * Fire before process file
93 * @param {Roo.bootstrap.DocumentManager} this
94 * @param {Object} file
101 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
110 paramName : 'imageUpload',
120 getAutoCreate : function()
122 var managerWidget = {
124 cls : 'roo-document-manager',
128 cls : 'roo-document-manager-selector',
133 cls : 'roo-document-manager-uploader',
137 cls : 'roo-document-manager-upload-btn',
138 html : '<i class="fa fa-plus"></i>'
149 cls : 'column col-md-12',
154 if(this.fieldLabel.length){
159 cls : 'column col-md-12',
160 html : this.fieldLabel
164 cls : 'column col-md-12',
169 if(this.labelAlign == 'left'){
173 cls : 'column col-md-' + this.labelWidth,
174 html : this.fieldLabel
178 cls : 'column col-md-' + (12 - this.labelWidth),
188 cls : 'row clearfix',
196 initEvents : function()
198 this.managerEl = this.el.select('.roo-document-manager', true).first();
199 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
201 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
202 this.selectorEl.hide();
205 this.selectorEl.attr('multiple', 'multiple');
208 this.selectorEl.on('change', this.onFileSelected, this);
210 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
211 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
213 this.uploader.on('click', this.onUploaderClick, this);
215 this.renderProgressDialog();
219 window.addEventListener("resize", function() { _this.refresh(); } );
221 this.fireEvent('initial', this);
224 renderProgressDialog : function()
228 this.progressDialog = new Roo.bootstrap.Modal({
229 cls : 'roo-document-manager-progress-dialog',
240 btnclick : function() {
241 _this.uploadCancel();
247 this.progressDialog.render(Roo.get(document.body));
249 this.progress = new Roo.bootstrap.Progress({
250 cls : 'roo-document-manager-progress',
255 this.progress.render(this.progressDialog.getChildContainer());
257 this.progressBar = new Roo.bootstrap.ProgressBar({
258 cls : 'roo-document-manager-progress-bar',
265 this.progressBar.render(this.progress.getChildContainer());
268 onUploaderClick : function(e)
272 if(this.fireEvent('beforeselectfile', this) != false){
273 this.selectorEl.dom.click();
278 onFileSelected : function(e)
282 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
286 Roo.each(this.selectorEl.dom.files, function(file){
287 if(this.fireEvent('inspect', this, file) != false){
288 this.files.push(file);
298 this.selectorEl.dom.value = '';
300 if(!this.files.length){
304 if(this.boxes > 0 && this.files.length > this.boxes){
305 this.files = this.files.slice(0, this.boxes);
308 this.uploader.show();
310 if(this.boxes > 0 && this.files.length > this.boxes - 1){
311 this.uploader.hide();
320 Roo.each(this.files, function(file){
322 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
323 var f = this.renderPreview(file);
328 if(file.type.indexOf('image') != -1){
332 }).createDelegate(this)
341 }).createDelegate(this)
348 this.delegates = this.delegates.concat(docs);
350 if(!this.delegates.length){
355 this.progressBar.aria_valuemax = this.delegates.length;
364 if(!this.delegates.length){
365 this.progressDialog.hide();
370 var delegate = this.delegates.shift();
372 this.progressDialog.show();
374 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
376 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
383 this.uploader.show();
385 if(this.boxes > 0 && this.files.length > this.boxes - 1){
386 this.uploader.hide();
389 Roo.isTouch ? this.closable(false) : this.closable(true);
391 this.fireEvent('refresh', this);
394 onRemove : function(e, el, o)
398 this.fireEvent('remove', this, o);
406 Roo.each(this.files, function(file){
407 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
423 Roo.each(this.files, function(file){
428 file.target.remove();
437 onClick : function(e, el, o)
441 this.fireEvent('click', this, o);
445 closable : function(closable)
447 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
449 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
461 xhrOnLoad : function(xhr)
463 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
467 if (xhr.readyState !== 4) {
469 this.fireEvent('exception', this, xhr);
473 var response = Roo.decode(xhr.responseText);
475 if(!response.success){
477 this.fireEvent('exception', this, xhr);
481 var file = this.renderPreview(response.data);
483 this.files.push(file);
489 xhrOnError : function(xhr)
491 Roo.log('xhr on error');
493 var response = Roo.decode(xhr.responseText);
500 process : function(file)
502 if(this.fireEvent('process', this, file) !== false){
503 if(this.editable && file.type.indexOf('image') != -1){
504 this.fireEvent('edit', this, file);
508 this.uploadStart(file, false);
515 uploadStart : function(file, crop)
517 this.xhr = new XMLHttpRequest();
519 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
526 this.managerEl.createChild({
528 cls : 'roo-document-manager-loading',
533 cls : 'roo-document-manager-thumb',
534 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
540 this.xhr.open(this.method, this.url, true);
543 "Accept": "application/json",
544 "Cache-Control": "no-cache",
545 "X-Requested-With": "XMLHttpRequest"
548 for (var headerName in headers) {
549 var headerValue = headers[headerName];
551 this.xhr.setRequestHeader(headerName, headerValue);
557 this.xhr.onload = function()
559 _this.xhrOnLoad(_this.xhr);
562 this.xhr.onerror = function()
564 _this.xhrOnError(_this.xhr);
567 var formData = new FormData();
569 formData.append('returnHTML', 'NO');
572 formData.append('crop', crop);
575 formData.append(this.paramName, file, file.name);
577 if(this.fireEvent('prepare', this, formData) != false){
578 this.xhr.send(formData);
582 uploadCancel : function()
591 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
598 renderPreview : function(file)
600 if(typeof(file.target) != 'undefined' && file.target){
604 var previewEl = this.managerEl.createChild({
606 cls : 'roo-document-manager-preview',
610 tooltip : file.filename,
611 cls : 'roo-document-manager-thumb',
612 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
617 html : '<i class="fa fa-times-circle"></i>'
622 var close = previewEl.select('button.close', true).first();
624 close.on('click', this.onRemove, this, file);
626 file.target = previewEl;
628 var image = previewEl.select('img', true).first();
632 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
634 image.on('click', this.onClick, this, file);
640 onPreviewLoad : function(file, image)
642 if(typeof(file.target) == 'undefined' || !file.target){
646 var width = image.dom.naturalWidth || image.dom.width;
647 var height = image.dom.naturalHeight || image.dom.height;
650 file.target.addClass('wide');
654 file.target.addClass('tall');
659 uploadFromSource : function(file, crop)
661 this.xhr = new XMLHttpRequest();
663 this.managerEl.createChild({
665 cls : 'roo-document-manager-loading',
670 cls : 'roo-document-manager-thumb',
671 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
677 this.xhr.open(this.method, this.url, true);
680 "Accept": "application/json",
681 "Cache-Control": "no-cache",
682 "X-Requested-With": "XMLHttpRequest"
685 for (var headerName in headers) {
686 var headerValue = headers[headerName];
688 this.xhr.setRequestHeader(headerName, headerValue);
694 this.xhr.onload = function()
696 _this.xhrOnLoad(_this.xhr);
699 this.xhr.onerror = function()
701 _this.xhrOnError(_this.xhr);
704 var formData = new FormData();
706 formData.append('returnHTML', 'NO');
708 formData.append('crop', crop);
710 if(typeof(file.filename) != 'undefined'){
711 formData.append('filename', file.filename);
714 if(typeof(file.mimetype) != 'undefined'){
715 formData.append('mimetype', file.mimetype);
718 if(this.fireEvent('prepare', this, formData) != false){
719 this.xhr.send(formData);