roojs-core.js
[roojs1] / Roo / dialog / UploadCropbox.js
index 92a4ac6..7bc78e3 100644 (file)
@@ -12,6 +12,8 @@
  * @cfg {Number} errorTimeout default 3000
  * @cfg {Number} minWidth default 300
  * @cfg {Number} minHeight default 300
+ * @cfg {Number} outputMaxWidth default 1200
+ * @cfg {Number} windowSize default 300
  * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
  * @cfg {Boolean} isDocument (true|false) default false
  * @cfg {String} url action url
@@ -149,6 +151,8 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
     cropData : false,
     minWidth : 300,
     minHeight : 300,
+    outputMaxWidth : 1200,
+    windowSize : 300,
     file : false,
     exif : {},
     baseRotate : 1,
@@ -556,7 +560,7 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         e.stopEvent();
         
         this.startScale = this.scale;
-        this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
+        this.scale = (e.getWheelDelta() > 0) ? (this.scale + 1) : (this.scale - 1);
         
         if(!this.zoomable()){
             this.scale = this.startScale;
@@ -579,12 +583,7 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         
         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
-
-        console.log("ZOOM");
-        console.log("WIDTH: " + (this.thumbEl.getWidth() / this.getScaleLevel()));
-        console.log("HEIGHT: " + (this.thumbEl.getHeight() / this.getScaleLevel()));
-
-
         var maxWidth = this.imageEl.OriginWidth;
         var maxHeight = this.imageEl.OriginHeight;
         
@@ -616,8 +615,8 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
                 !this.isDocument &&
                 (this.rotate == 0 || this.rotate == 180) && 
                 (
-                    (this.imageEl.OriginWidth >= this.minWidth) && width < this.minWidth ||
-                    (this.imageEl.OriginHeight >= this.minHeight) && height < this.minHeight ||
+                    (this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight) && width < this.minWidth ||
+                    (this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight) && height < this.minHeight ||
                     width > maxWidth ||
                     height > maxHeight
                 )
@@ -846,7 +845,6 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
     
     crop : function()
     {
-        console.log("CROP");
         if(!this.canvasLoaded){
             return;
         }
@@ -857,9 +855,6 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         
         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
-
-        console.log("IMAGECANVAS WIDTH: " + imageCanvas.width);
-        console.log("IMAGECANVAS HEIGHT: " + imageCanvas.height);
         
         var center = imageCanvas.width / 2;
         
@@ -877,68 +872,32 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         
         canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
 
-        console.log("CANVAS WIDTH: " + canvas.width);
-        console.log("CANVAS HEIGHT: " + canvas.height);
-
-        console.log("SCALELEVLE: " + scaleLevel);
-
         switch (this.rotate) {
             case 0 :
                 
                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
-
-                console.log("WIDTH: " + width);
-                console.log("HEIGHT: " + height);
                 
                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
-
-                console.log("X: " + x);
-                console.log("Y: " + y);
-                
-                // var targetWidth = this.minWidth - 2 * x;
-                // var targetHeight = this.minHeight - 2 * y;
-
-                // console.log("targetWidth: " + targetWidth);
-                // console.log("targetHeight: " + targetHeight);
-                
-                // var scale = 1;
-                
-                // if((x == 0 && y == 0) || (x == 0 && y > 0)){
-                //     scale = targetWidth / width;
-                // }
-                
-                // if(x > 0 && y == 0){
-                //     scale = targetHeight / height;
-                // }
-                
-                // if(x > 0 && y > 0){
-                //     scale = targetWidth / width;
-                    
-                //     if(width < height){
-                //         scale = targetHeight / height;
-                //     }
-                // }
-
-                // console.log("scale: " + scale);
-                
-                // context.scale(scale, scale);
                 
                 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
                 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
 
-                console.log("sx: " + sx);
-                console.log("sy: " + sy);
-
                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
 
-                if(canvas.width > 1200) {
-                    canvas.height = 1200 * height / canvas.width
-                    canvas.width = 1200;
+                if(canvas.width > this.outputMaxWidth) {
+                    var scale = this.outputMaxWidth / canvas.width;
+                    canvas.width = canvas.width * scale;
+                    canvas.height = canvas.height * scale;
+                    context.scale(scale, scale);
                 }
 
+                context.fillStyle = 'white';
+                context.fillRect(0, 0, this.thumbEl.getWidth() / this.getScaleLevel(), this.thumbEl.getHeight() / this.getScaleLevel());
+
+
                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
                 
                 break;
@@ -1099,11 +1058,11 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
             }
         }
         
-        height = 300;
+        height = this.windowSize;
         width = Math.ceil(this.minWidth * height / this.minHeight);
         
         if(this.minWidth > this.minHeight){
-            width = 300;
+            width = this.windowSize;
             height = Math.ceil(this.minHeight * width / this.minWidth);
         }
         
@@ -1144,7 +1103,6 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
     
     baseScaleLevel : function()
     {
-        console.log("BASE SCALE LEVEL");
         var width, height;
         
         if(this.isDocument){
@@ -1219,7 +1177,7 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
             this.baseScale = width / this.minWidth;
         }
-        console.log(this.baseScale);
+
         return;
     },
     
@@ -1353,11 +1311,8 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         
         var formData = new FormData();
 
-        formData.append("onid", "43024");
-        formData.append("ontable", "pressrelease_boilerplate");
-
         formData.append('returnHTML', 'NO');
-        
+
         if(crop){
             formData.append('crop', crop);
             var blobBin = atob(crop.split(',')[1]);
@@ -1365,7 +1320,7 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
             for(var i = 0; i < blobBin.length; i++) {
                 array.push(blobBin.charCodeAt(i));
             }
-            var croppedFile =new Blob([new Uint8Array(array)], {type: 'image/png'});
+            var croppedFile =new Blob([new Uint8Array(array)], {type: this.cropType});
             formData.append(this.paramName, croppedFile, file.name);
         }
         
@@ -1439,7 +1394,9 @@ Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
         }
         
         this.file = file;
-        this.cropType = file.type;
+        if(typeof(file.type) != 'undefined' && file.type.length != 0) {
+            this.cropType = file.type;
+        }
         
         var _this = this;