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()
217 this.progressDialog = new Roo.bootstrap.Modal({
218 cls : 'roo-document-manager-progress-dialog',
229 btnclick : function() {
230 _this.uploadCancel();
236 this.progressDialog.render(Roo.get(document.body));
238 this.progress = new Roo.bootstrap.Progress({
239 cls : 'roo-document-manager-progress',
244 this.progress.render(this.progressDialog.getChildContainer());
246 this.progressBar = new Roo.bootstrap.ProgressBar({
247 cls : 'roo-document-manager-progress-bar',
254 this.progressBar.render(this.progress.getChildContainer());
257 onUploaderClick : function(e)
260 this.selectorEl.dom.click();
263 onFileSelected : function(e)
267 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
271 Roo.each(this.selectorEl.dom.files, function(file){
272 if(this.fireEvent('inspect', this, file) != false){
273 this.files.push(file);
283 this.selectorEl.dom.value = '';
285 if(!this.files.length){
289 if(this.files.length > this.boxes){
290 this.files = this.files.slice(0, this.boxes);
293 this.uploader.show();
295 if(this.files.length > this.boxes - 1){
296 this.uploader.hide();
303 Roo.each(this.files, function(file){
305 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
306 var f = this.renderPreview(file);
314 }).createDelegate(this)
321 if(!this.delegates.length){
326 this.progressBar.aria_valuemax = this.delegates.length;
335 if(!this.delegates.length){
336 this.progressDialog.hide();
341 var delegate = this.delegates.shift();
343 this.progressDialog.show();
345 this.progressDialog.setTitle((this.progressBar.aria_valuemax - this.delegates.length) + ' / ' + this.progressBar.aria_valuemax);
347 this.progressBar.update(this.progressBar.aria_valuemax - this.delegates.length);
354 this.uploader.show();
356 if(this.files.length > this.boxes - 1){
357 this.uploader.hide();
360 Roo.isTouch ? this.closable(false) : this.closable(true);
362 this.fireEvent('refresh', this);
365 onRemove : function(e, el, o)
369 this.fireEvent('remove', this, o);
377 Roo.each(this.files, function(file){
378 if(typeof(file.id) == 'undefined' || file.id * 1 < 1 || file.id != o.id){
392 onClick : function(e, el, o)
396 this.fireEvent('click', this, o);
400 closable : function(closable)
402 Roo.each(this.managerEl.select('.roo-document-manager-preview > button.close', true).elements, function(el){
404 el.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
416 xhrOnLoad : function(xhr)
418 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
422 if (xhr.readyState !== 4) {
424 this.fireEvent('exception', this, xhr);
428 var response = Roo.decode(xhr.responseText);
430 if(!response.success){
432 this.fireEvent('exception', this, xhr);
436 var file = this.renderPreview(response.data);
438 this.files.push(file);
444 xhrOnError : function()
446 Roo.log('xhr on error');
448 var response = Roo.decode(xhr.responseText);
455 process : function(file)
457 if(this.editable && file.type.indexOf('image') != -1){
458 this.fireEvent('edit', this, file);
462 this.uploadStart(file, false);
467 uploadStart : function(file, crop)
469 this.xhr = new XMLHttpRequest();
471 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
478 this.managerEl.createChild({
480 cls : 'roo-document-manager-loading',
485 cls : 'roo-document-manager-thumb',
486 html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
492 this.xhr.open(this.method, this.url, true);
495 "Accept": "application/json",
496 "Cache-Control": "no-cache",
497 "X-Requested-With": "XMLHttpRequest"
500 for (var headerName in headers) {
501 var headerValue = headers[headerName];
503 this.xhr.setRequestHeader(headerName, headerValue);
509 this.xhr.onload = function()
511 _this.xhrOnLoad(_this.xhr);
514 this.xhr.onerror = function()
516 _this.xhrOnError(_this.xhr);
519 var formData = new FormData();
521 formData.append('returnHTML', 'NO');
524 formData.append('crop', crop);
527 formData.append(this.paramName, file, file.name);
529 if(this.fireEvent('prepare', this, formData) != false){
530 this.xhr.send(formData);
534 uploadCancel : function()
540 Roo.each(this.managerEl.select('.roo-document-manager-loading', true).elements, function(el){
547 renderPreview : function(file)
549 if(typeof(file.target) != 'undefined' && file.target){
553 var previewEl = this.managerEl.createChild({
555 cls : 'roo-document-manager-preview',
559 tooltip : file.filename,
560 cls : 'roo-document-manager-thumb',
561 html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
566 html : '<i class="fa fa-times-circle"></i>'
571 var close = previewEl.select('button.close', true).first();
573 close.on('click', this.onRemove, this, file);
575 file.target = previewEl;
577 var image = previewEl.select('img', true).first();
581 image.dom.addEventListener("load", function(){ _this.onPreviewLoad(file, image); });
583 image.on('click', this.onClick, this, file);
589 onPreviewLoad : function(file, image)
591 if(typeof(file.target) == 'undefined' || !file.target){
595 var width = image.dom.naturalWidth || image.dom.width;
596 var height = image.dom.naturalHeight || image.dom.height;
599 file.target.addClass('wide');
603 file.target.addClass('tall');