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
94 Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component, {
103 paramName : 'imageUpload',
110 getAutoCreate : function()
112 var managerWidget = {
114 cls : 'roo-document-manager',
118 cls : 'roo-document-manager-selector',
123 cls : 'roo-document-manager-uploader',
127 cls : 'roo-document-manager-upload-btn',
128 html : '<i class="fa fa-plus"></i>'
139 cls : 'column col-md-12',
144 if(this.fieldLabel.length){
149 cls : 'column col-md-12',
150 html : this.fieldLabel
154 cls : 'column col-md-12',
159 if(this.labelAlign == 'left'){
163 cls : 'column col-md-' + this.labelWidth,
164 html : this.fieldLabel
168 cls : 'column col-md-' + (12 - this.labelWidth),
178 cls : 'row clearfix',
186 initEvents : function()
188 this.managerEl = this.el.select('.roo-document-manager', true).first();
189 this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
191 this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
192 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
193 this.selectorEl.hide();
196 this.selectorEl.attr('multiple', 'multiple');
199 this.selectorEl.on('change', this.onFileSelected, this);
201 this.uploader = this.el.select('.roo-document-manager-uploader', true).first();
202 this.uploader.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
204 this.uploader.on('click', this.onUploaderClick, this);
206 this.renderProgressDialog();
210 window.addEventListener("resize", function() { _this.refresh(); } );
212 this.fireEvent('initial', this);
215 renderProgressDialog : function()
219 this.progressDialog = new Roo.bootstrap.Modal({
220 cls : 'roo-document-manager-progress-dialog',
231 btnclick : function() {
232 _this.uploadCancel();
238 this.progressDialog.render(Roo.get(document.body));
240 this.progress = new Roo.bootstrap.Progress({
241 cls : 'roo-document-manager-progress',
246 this.progress.render(this.progressDialog.getChildContainer());
248 this.progressBar = new Roo.bootstrap.ProgressBar({
249 cls : 'roo-document-manager-progress-bar',
256 this.progressBar.render(this.progress.getChildContainer());
259 onUploaderClick : function(e)
263 if(this.fireEvent('beforeselectfile', this) != false){
264 this.selectorEl.dom.click();
269 onFileSelected : function(e)
273 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
277 Roo.each(this.selectorEl.dom.files, function(file){
278 if(this.fireEvent('inspect', this, file) != false){
279 this.files.push(file);
289 this.selectorEl.dom.value = '';
291 if(!this.files.length){
295 if(this.boxes > 0 && this.files.length > this.boxes){
296 this.files = this.files.slice(0, this.boxes);
299 this.uploader.show();
301 if(this.boxes > 0 && this.files.length > this.boxes - 1){
302 this.uploader.hide();
311 Roo.each(this.files, function(file){
313 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
314 var f = this.renderPreview(file);
319 if(file.type.indexOf('image') != -1){
323 }).createDelegate(this)
332 }).createDelegate(this)
339 this.delegates = this.delegates.concat(docs);
341 if(!this.delegates.length){
346 this.progressBar.aria_valuemax = this.delegates.length;
355 if(!this.delegates.length){
356 this.progressDialog.hide();
361 var delegate = this.delegates.shift();
363 this.progressDialog.show();
365 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
367 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
374 this.uploader.show();
376 if(this.boxes > 0 && this.files.length > this.boxes - 1){
377 this.uploader.hide();
380 Roo.isTouch ? this.closable(false) : this.closable(true);
382 this.fireEvent('refresh', this);
385 onRemove : function(e, el, o)
389 this.fireEvent('remove', this, o);
397 Roo.each(this.files, function(file){
398 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
414 Roo.each(this.files, function(file){
419 file.target.remove();
428 onClick : function(e, el, o)
432 this.fireEvent('click', this, o);
436 closable : function(closable)
438 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
440 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
452 xhrOnLoad : function(xhr)
454 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
458 if (xhr.readyState !== 4) {
460 this.fireEvent('exception', this, xhr);
464 var response = Roo.decode(xhr.responseText);
466 if(!response.success){
468 this.fireEvent('exception', this, xhr);
472 var file = this.renderPreview(response.data);
474 this.files.push(file);
480 xhrOnError : function()
482 Roo.log('xhr on error');
484 var response = Roo.decode(xhr.responseText);
491 process : function(file)
493 if(this.fireEvent('beforeprocess', this, file) !== false){
495 if(this.editable && file.type.indexOf('image') != -1){
496 this.fireEvent('edit', this, file);
500 this.uploadStart(file, false);
507 uploadStart : function(file, crop)
509 this.xhr = new XMLHttpRequest();
511 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
518 this.managerEl.createChild({
520 cls : 'roo-document-manager-loading',
525 cls : 'roo-document-manager-thumb',
526 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
532 this.xhr.open(this.method, this.url, true);
535 "Accept": "application/json",
536 "Cache-Control": "no-cache",
537 "X-Requested-With": "XMLHttpRequest"
540 for (var headerName in headers) {
541 var headerValue = headers[headerName];
543 this.xhr.setRequestHeader(headerName, headerValue);
549 this.xhr.onload = function()
551 _this.xhrOnLoad(_this.xhr);
554 this.xhr.onerror = function()
556 _this.xhrOnError(_this.xhr);
559 var formData = new FormData();
561 formData.append('returnHTML', 'NO');
564 formData.append('crop', crop);
567 formData.append(this.paramName, file, file.name);
569 if(this.fireEvent('prepare', this, formData) != false){
570 this.xhr.send(formData);
574 uploadCancel : function()
580 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
587 renderPreview : function(file)
589 if(typeof(file.target) != 'undefined' && file.target){
593 var previewEl = this.managerEl.createChild({
595 cls : 'roo-document-manager-preview',
599 tooltip : file.filename,
600 cls : 'roo-document-manager-thumb',
601 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
606 html : '<i class="fa fa-times-circle"></i>'
611 var close = previewEl.select('button.close', true).first();
613 close.on('click', this.onRemove, this, file);
615 file.target = previewEl;
617 var image = previewEl.select('img', true).first();
621 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
623 image.on('click', this.onClick, this, file);
629 onPreviewLoad : function(file, image)
631 if(typeof(file.target) == 'undefined' || !file.target){
635 var width = image.dom.naturalWidth || image.dom.width;
636 var height = image.dom.naturalHeight || image.dom.height;
639 file.target.addClass('wide');
643 file.target.addClass('tall');
648 uploadFromSource : function(file, crop)
650 this.xhr = new XMLHttpRequest();
652 this.managerEl.createChild({
654 cls : 'roo-document-manager-loading',
659 cls : 'roo-document-manager-thumb',
660 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
666 this.xhr.open(this.method, this.url, true);
669 "Accept": "application/json",
670 "Cache-Control": "no-cache",
671 "X-Requested-With": "XMLHttpRequest"
674 for (var headerName in headers) {
675 var headerValue = headers[headerName];
677 this.xhr.setRequestHeader(headerName, headerValue);
683 this.xhr.onload = function()
685 _this.xhrOnLoad(_this.xhr);
688 this.xhr.onerror = function()
690 _this.xhrOnError(_this.xhr);
693 var formData = new FormData();
695 formData.append('returnHTML', 'NO');
697 formData.append('crop', crop);
699 if(typeof(file.filename) != 'undefined'){
700 formData.append('filename', file.filename);
703 if(typeof(file.mimetype) != 'undefined'){
704 formData.append('mimetype', file.mimetype);
707 if(this.fireEvent('prepare', this, formData) != false){
708 this.xhr.send(formData);