Roo/bootstrap/DocumentManager.js
[roojs1] / Roo / bootstrap / DocumentManager.js
index 4b8e5bf..dce4442 100644 (file)
@@ -8,6 +8,7 @@
  * @extends Roo.bootstrap.Component
  * Bootstrap DocumentManager class
  * @cfg {String} paramName default 'imageUpload'
+ * @cfg {String} toolTipName default 'filename'
  * @cfg {String} method default POST
  * @cfg {String} url action url
  * @cfg {Number} boxes number of boxes, 0 is no limit.. default 0
  * @cfg {Number} labelWidth default 4
  * @cfg {String} labelAlign (left|top) default left
  * @cfg {Boolean} editable (true|false) allow edit when upload a image default true
+* @cfg {Number} labellg set the width of label (1-12)
+ * @cfg {Number} labelmd set the width of label (1-12)
+ * @cfg {Number} labelsm set the width of label (1-12)
+ * @cfg {Number} labelxs set the width of label (1-12)
  * 
  * @constructor
  * Create a new DocumentManager
@@ -26,6 +31,9 @@
 Roo.bootstrap.DocumentManager = function(config){
     Roo.bootstrap.DocumentManager.superclass.constructor.call(this, config);
     
+    this.files = [];
+    this.delegates = [];
+    
     this.addEvents({
         /**
          * @event initial
@@ -47,6 +55,13 @@ Roo.bootstrap.DocumentManager = function(config){
          * @param {XMLHttpRequest} xhr
          */
         "exception" : true,
+        /**
+         * @event afterupload
+         * Fire when xhr load exception
+         * @param {Roo.bootstrap.DocumentManager} this
+         * @param {XMLHttpRequest} xhr
+         */
+        "afterupload" : true,
         /**
          * @event prepare
          * prepare the form data
@@ -86,7 +101,24 @@ Roo.bootstrap.DocumentManager = function(config){
          * Fire before select file
          * @param {Roo.bootstrap.DocumentManager} this
          */
-        "beforeselectfile" : true
+        "beforeselectfile" : true,
+        /**
+         * @event process
+         * Fire before process file
+         * @param {Roo.bootstrap.DocumentManager} this
+         * @param {Object} file
+         */
+        "process" : true,
+        /**
+         * @event previewrendered
+         * Fire when preview rendered
+         * @param {Roo.bootstrap.DocumentManager} this
+         * @param {Object} file
+         */
+        "previewrendered" : true,
+        /**
+         */
+        "previewResize" : true
         
     });
 };
@@ -97,15 +129,22 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
     inputName : '',
     thumbSize : 300,
     multiple : true,
-    files : [],
+    files : false,
     method : 'POST',
     url : '',
     paramName : 'imageUpload',
+    toolTipName : 'filename',
     fieldLabel : '',
     labelWidth : 4,
     labelAlign : 'left',
     editable : true,
-    delegates : [],
+    delegates : false,
+    xhr : false, 
+    
+    labellg : 0,
+    labelmd : 0,
+    labelsm : 0,
+    labelxs : 0,
     
     getAutoCreate : function()
     {   
@@ -160,16 +199,44 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
                 content = [
                     {
                         tag : 'div',
-                        cls : 'column col-md-' + this.labelWidth,
+                        cls : 'column',
                         html : this.fieldLabel
                     },
                     {
                         tag : 'div',
-                        cls : 'column col-md-' + (12 - this.labelWidth),
+                        cls : 'column',
                         cn : managerWidget
                     }
                 ];
                 
+                if(this.labelWidth > 12){
+                    content[0].style = "width: " + this.labelWidth + 'px';
+                }
+
+                if(this.labelWidth < 13 && this.labelmd == 0){
+                    this.labelmd = this.labelWidth;
+                }
+
+                if(this.labellg > 0){
+                    content[0].cls += ' col-lg-' + this.labellg;
+                    content[1].cls += ' col-lg-' + (12 - this.labellg);
+                }
+
+                if(this.labelmd > 0){
+                    content[0].cls += ' col-md-' + this.labelmd;
+                    content[1].cls += ' col-md-' + (12 - this.labelmd);
+                }
+
+                if(this.labelsm > 0){
+                    content[0].cls += ' col-sm-' + this.labelsm;
+                    content[1].cls += ' col-sm-' + (12 - this.labelsm);
+                }
+
+                if(this.labelxs > 0){
+                    content[0].cls += ' col-xs-' + this.labelxs;
+                    content[1].cls += ' col-xs-' + (12 - this.labelxs);
+                }
+                
             }
         }
         
@@ -189,7 +256,6 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         this.managerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
         
         this.selectorEl = this.el.select('.roo-document-manager-selector', true).first();
-        this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
         this.selectorEl.hide();
         
         if(this.multiple){
@@ -219,6 +285,7 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         this.progressDialog = new Roo.bootstrap.Modal({
             cls : 'roo-document-manager-progress-dialog',
             allow_close : false,
+            animate : false,
             title : '',
             buttons : [
                 {
@@ -259,7 +326,7 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
     onUploaderClick : function(e)
     {
         e.preventDefault();
-        
+     
         if(this.fireEvent('beforeselectfile', this) != false){
             this.selectorEl.dom.click();
         }
@@ -288,7 +355,7 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
     {
         this.selectorEl.dom.value = '';
         
-        if(!this.files.length){
+        if(!this.files || !this.files.length){
             return;
         }
         
@@ -336,6 +403,8 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         
         this.files = files;
         
+        this.delegates = this.delegates.concat(docs);
+        
         if(!this.delegates.length){
             this.refresh();
             return;
@@ -473,9 +542,11 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         
         this.arrange();
         
+        this.fireEvent('afterupload', this, xhr);
+        
     },
     
-    xhrOnError : function()
+    xhrOnError : function(xhr)
     {
         Roo.log('xhr on error');
         
@@ -488,14 +559,17 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
     
     process : function(file)
     {
-        if(this.editable && file.type.indexOf('image') != -1){
-            this.fireEvent('edit', this, file);
+        if(this.fireEvent('process', this, file) !== false){
+            if(this.editable && file.type.indexOf('image') != -1){
+                this.fireEvent('edit', this, file);
+                return;
+            }
+
+            this.uploadStart(file, false);
+
             return;
         }
         
-        this.uploadStart(file, false);
-        
-        return;
     },
     
     uploadStart : function(file, crop)
@@ -560,14 +634,29 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         
         formData.append(this.paramName, file, file.name);
         
-        if(this.fireEvent('prepare', this, formData) != false){
+        var options = {
+            file : file, 
+            manually : false
+        };
+        
+        if(this.fireEvent('prepare', this, formData, options) != false){
+            
+            if(options.manually){
+                return;
+            }
+            
             this.xhr.send(formData);
+            return;
         };
+        
+        this.uploadCancel();
     },
     
     uploadCancel : function()
     {
-        this.xhr.abort();
+        if (this.xhr) {
+            this.xhr.abort();
+        }
         
         this.delegates = [];
         
@@ -584,15 +673,17 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
             return file;
         }
         
+        var img_src = encodeURI(baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename);
+        
         var previewEl = this.managerEl.createChild({
             tag : 'div',
             cls : 'roo-document-manager-preview',
             cn : [
                 {
                     tag : 'div',
-                    tooltip : file.filename,
+                    tooltip : file[this.toolTipName],
                     cls : 'roo-document-manager-thumb',
-                    html : '<img src="' + baseURL +'/Images/Thumb/' + this.thumbSize + '/' + file.id + '/' + file.filename + '">'
+                    html : '<img tooltip="' + file[this.toolTipName] + '" src="' + img_src + '">'
                 },
                 {
                     tag : 'button',
@@ -616,6 +707,8 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         
         image.on('click', this.onClick, this, file);
         
+        this.fireEvent('previewrendered', this, file);
+        
         return file;
         
     },
@@ -629,6 +722,10 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         var width = image.dom.naturalWidth || image.dom.width;
         var height = image.dom.naturalHeight || image.dom.height;
         
+        if(!this.previewResize) {
+            return;
+        }
+        
         if(width > height){
             file.target.addClass('wide');
             return;
@@ -637,5 +734,71 @@ Roo.extend(Roo.bootstrap.DocumentManager, Roo.bootstrap.Component,  {
         file.target.addClass('tall');
         return;
         
+    },
+    
+    uploadFromSource : function(file, crop)
+    {
+        this.xhr = new XMLHttpRequest();
+        
+        this.managerEl.createChild({
+            tag : 'div',
+            cls : 'roo-document-manager-loading',
+            cn : [
+                {
+                    tag : 'div',
+                    tooltip : file.name,
+                    cls : 'roo-document-manager-thumb',
+                    html : '<i class="fa fa-circle-o-notch fa-spin"></i>'
+                }
+            ]
+
+        });
+
+        this.xhr.open(this.method, this.url, true);
+        
+        var headers = {
+            "Accept": "application/json",
+            "Cache-Control": "no-cache",
+            "X-Requested-With": "XMLHttpRequest"
+        };
+        
+        for (var headerName in headers) {
+            var headerValue = headers[headerName];
+            if (headerValue) {
+                this.xhr.setRequestHeader(headerName, headerValue);
+            }
+        }
+        
+        var _this = this;
+        
+        this.xhr.onload = function()
+        {
+            _this.xhrOnLoad(_this.xhr);
+        }
+        
+        this.xhr.onerror = function()
+        {
+            _this.xhrOnError(_this.xhr);
+        }
+        
+        var formData = new FormData();
+
+        formData.append('returnHTML', 'NO');
+        
+        formData.append('crop', crop);
+        
+        if(typeof(file.filename) != 'undefined'){
+            formData.append('filename', file.filename);
+        }
+        
+        if(typeof(file.mimetype) != 'undefined'){
+            formData.append('mimetype', file.mimetype);
+        }
+        
+        Roo.log(formData);
+        
+        if(this.fireEvent('prepare', this, formData) != false){
+            this.xhr.send(formData);
+        };
     }
 });