Roo/dialog/UploadCropbox.js
[roojs1] / Roo / dialog / UploadCropbox.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.dialog.UploadCropbox
8  * @extends Roo.BoxComponent
9  * Dialog UploadCropbox class
10  * @cfg {String} emptyText show when image has been loaded
11  * @cfg {String} rotateNotify show when image too small to rotate
12  * @cfg {Number} errorTimeout default 3000
13  * @cfg {Number} minWidth default 300
14  * @cfg {Number} minHeight default 300
15  * @cfg {Number} outputMaxWidth default 1200
16  * @cfg {Number} windowSize default 300
17  * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
18  * @cfg {Boolean} isDocument (true|false) default false
19  * @cfg {String} url action url
20  * @cfg {String} paramName default 'imageUpload'
21  * @cfg {String} method default POST
22  * @cfg {Boolean} loadMask (true|false) default true
23  * @cfg {Boolean} loadingText default 'Loading...'
24  * 
25  * @constructor
26  * Create a new UploadCropbox
27  * @param {Object} config The config object
28  */
29
30  Roo.dialog.UploadCropbox = function(config){
31     Roo.dialog.UploadCropbox.superclass.constructor.call(this, config);
32     
33     this.addEvents({
34         /**
35          * @event beforeselectfile
36          * Fire before select file
37          * @param {Roo.dialog.UploadCropbox} this
38          */
39         "beforeselectfile" : true,
40         /**
41          * @event initial
42          * Fire after initEvent
43          * @param {Roo.dialog.UploadCropbox} this
44          */
45         "initial" : true,
46         /**
47          * @event crop
48          * Fire after initEvent
49          * @param {Roo.dialog.UploadCropbox} this
50          * @param {String} data
51          */
52         "crop" : true,
53         /**
54          * @event prepare
55          * Fire when preparing the file data
56          * @param {Roo.dialog.UploadCropbox} this
57          * @param {Object} file
58          */
59         "prepare" : true,
60         /**
61          * @event exception
62          * Fire when get exception
63          * @param {Roo.dialog.UploadCropbox} this
64          * @param {XMLHttpRequest} xhr
65          */
66         "exception" : true,
67         /**
68          * @event beforeloadcanvas
69          * Fire before load the canvas
70          * @param {Roo.dialog.UploadCropbox} this
71          * @param {String} src
72          */
73         "beforeloadcanvas" : true,
74         /**
75          * @event trash
76          * Fire when trash image
77          * @param {Roo.dialog.UploadCropbox} this
78          */
79         "trash" : true,
80         /**
81          * @event download
82          * Fire when download the image
83          * @param {Roo.dialog.UploadCropbox} this
84          */
85         "download" : true,
86         /**
87          * @event footerbuttonclick
88          * Fire when footerbuttonclick
89          * @param {Roo.dialog.UploadCropbox} this
90          * @param {String} type
91          */
92         "footerbuttonclick" : true,
93         /**
94          * @event resize
95          * Fire when resize
96          * @param {Roo.dialog.UploadCropbox} this
97          */
98         "resize" : true,
99         /**
100          * @event rotate
101          * Fire when rotate the image
102          * @param {Roo.dialog.UploadCropbox} this
103          * @param {String} pos
104          */
105         "rotate" : true,
106         /**
107          * @event inspect
108          * Fire when inspect the file
109          * @param {Roo.dialog.UploadCropbox} this
110          * @param {Object} file
111          */
112         "inspect" : true,
113         /**
114          * @event upload
115          * Fire when xhr upload the file
116          * @param {Roo.dialog.UploadCropbox} this
117          * @param {Object} data
118          */
119         "upload" : true,
120         /**
121          * @event arrange
122          * Fire when arrange the file data
123          * @param {Roo.dialog.UploadCropbox} this
124          * @param {Object} formData
125          */
126         "arrange" : true,
127         /**
128          * @event loadcanvas
129          * Fire after load the canvas
130          * @param {Roo.dialog.UploadCropbox}
131          * @param {Object} imgEl
132          */
133         "loadcanvas" : true
134     });
135     
136     this.buttons = this.buttons || Roo.dialog.UploadCropbox.footer.STANDARD;
137 };
138
139 Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
140     
141     emptyText : 'Click to upload image',
142     rotateNotify : 'Image is too small to rotate',
143     errorTimeout : 3000,
144     scale : 0,
145     baseScale : 1,
146     rotate : 0,
147     dragable : false,
148     pinching : false,
149     mouseX : 0,
150     mouseY : 0,
151     cropData : false,
152     minWidth : 300,
153     minHeight : 300,
154     outputMaxWidth : 1200,
155     windowSize : 300,
156     file : false,
157     exif : {},
158     baseRotate : 1,
159     cropType : 'image/jpeg',
160     buttons : false,
161     canvasLoaded : false,
162     isDocument : false,
163     method : 'POST',
164     paramName : 'imageUpload',
165     loadMask : true,
166     loadingText : 'Loading...',
167     maskEl : false,
168     
169     getAutoCreate : function()
170     {
171         var cfg = {
172             tag : 'div',
173             cls : 'roo-upload-cropbox',
174             cn : [
175                 {
176                     tag : 'input',
177                     cls : 'roo-upload-cropbox-selector',
178                     type : 'file'
179                 },
180                 {
181                     tag : 'div',
182                     cls : 'roo-upload-cropbox-body',
183                     style : 'cursor:pointer',
184                     cn : [
185                         {
186                             tag : 'div',
187                             cls : 'roo-upload-cropbox-preview'
188                         },
189                         {
190                             tag : 'div',
191                             cls : 'roo-upload-cropbox-thumb'
192                         },
193                         {
194                             tag : 'div',
195                             cls : 'roo-upload-cropbox-empty-notify',
196                             html : this.emptyText
197                         },
198                         {
199                             tag : 'div',
200                             cls : 'roo-upload-cropbox-error-notify alert alert-danger',
201                             html : this.rotateNotify
202                         }
203                     ]
204                 },
205                 {
206                     tag : 'div',
207                     cls : 'roo-upload-cropbox-footer',
208                     cn : {
209                         tag : 'div',
210                         cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
211                         cn : []
212                     }
213                 }
214             ]
215         };
216         
217         return cfg;
218     },
219     
220     onRender : function(ct, position)
221     {
222         Roo.dialog.UploadCropbox.superclass.onRender.call(this, ct, position);
223
224         if(this.el){
225             if (this.el.attr('xtype')) {
226                 this.el.attr('xtypex', this.el.attr('xtype'));
227                 this.el.dom.removeAttribute('xtype');
228                 
229                 this.initEvents();
230             }
231         }
232         else {
233             var cfg = Roo.apply({},  this.getAutoCreate());
234         
235             cfg.id = this.id || Roo.id();
236             
237             if (this.cls) {
238                 cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
239             }
240             
241             if (this.style) { // fixme needs to support more complex style data.
242                 cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
243             }
244             
245             this.el = ct.createChild(cfg, position);
246             
247             this.initEvents();
248         }
249         
250         if (this.buttons.length) {
251             
252             Roo.each(this.buttons, function(bb) {
253                 
254                 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
255                 
256                 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
257                 
258             }, this);
259         }
260         
261         if(this.loadMask){
262             this.maskEl = this.el;
263         }
264     },
265     
266     initEvents : function()
267     {
268         this.urlAPI = (window.createObjectURL && window) || 
269                                 (window.URL && URL.revokeObjectURL && URL) || 
270                                 (window.webkitURL && webkitURL);
271                         
272         this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
273         this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
274         
275         this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
276         this.selectorEl.hide();
277         
278         this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
279         this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
280         
281         this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
282         this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
283         this.thumbEl.hide();
284         
285         this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
286         this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
287         
288         this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
289         this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
290         this.errorEl.hide();
291         
292         this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
293         this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
294         this.footerEl.hide();
295         
296         this.setThumbBoxSize();
297         
298         this.bind();
299         
300         this.resize();
301         
302         this.fireEvent('initial', this);
303     },
304
305     bind : function()
306     {
307         var _this = this;
308         
309         window.addEventListener("resize", function() { _this.resize(); } );
310         
311         this.bodyEl.on('click', this.beforeSelectFile, this);
312         
313         if(Roo.isTouch){
314             this.bodyEl.on('touchstart', this.onTouchStart, this);
315             this.bodyEl.on('touchmove', this.onTouchMove, this);
316             this.bodyEl.on('touchend', this.onTouchEnd, this);
317         }
318         
319         if(!Roo.isTouch){
320             this.bodyEl.on('mousedown', this.onMouseDown, this);
321             this.bodyEl.on('mousemove', this.onMouseMove, this);
322             var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
323             this.bodyEl.on(mousewheel, this.onMouseWheel, this);
324             Roo.get(document).on('mouseup', this.onMouseUp, this);
325         }
326         
327         this.selectorEl.on('change', this.onFileSelected, this);
328     },
329     
330     reset : function()
331     {    
332         this.scale = 0;
333         this.baseScale = 1;
334         this.rotate = 0;
335         this.baseRotate = 1;
336         this.dragable = false;
337         this.pinching = false;
338         this.mouseX = 0;
339         this.mouseY = 0;
340         this.cropData = false;
341         this.notifyEl.dom.innerHTML = this.emptyText;
342         
343         // this.selectorEl.dom.value = '';
344         
345     },
346     
347     resize : function()
348     {
349         if(this.fireEvent('resize', this) != false){
350             this.setThumbBoxPosition();
351             this.setCanvasPosition();
352         }
353     },
354     
355     onFooterButtonClick : function(e, el, o, type)
356     {
357         switch (type) {
358             case 'rotate-left' :
359                 this.onRotateLeft(e);
360                 break;
361             case 'rotate-right' :
362                 this.onRotateRight(e);
363                 break;
364             case 'picture' :
365                 this.beforeSelectFile(e);
366                 break;
367             case 'trash' :
368                 this.trash(e);
369                 break;
370             case 'crop' :
371                 this.crop(e);
372                 break;
373             case 'download' :
374                 this.download(e);
375                 break;
376             case 'center' :
377                 this.center(e);
378                 break;
379             default :
380                 break;
381         }
382         
383         this.fireEvent('footerbuttonclick', this, type);
384     },
385     
386     beforeSelectFile : function(e)
387     {
388         e.preventDefault();
389         
390         if(this.fireEvent('beforeselectfile', this) != false){
391             this.selectorEl.dom.click();
392         }
393     },
394     
395     onFileSelected : function(e)
396     {
397         e.preventDefault();
398         
399         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
400             return;
401         }
402         
403         var file = this.selectorEl.dom.files[0];
404         
405         if(this.fireEvent('inspect', this, file) != false){
406             this.prepare(file);
407         }
408         
409     },
410     
411     trash : function(e)
412     {
413         this.fireEvent('trash', this);
414     },
415     
416     download : function(e)
417     {
418         this.fireEvent('download', this);
419     },
420
421     center : function(e)
422     {
423         this.setCanvasPosition();
424     },
425     
426     loadCanvas : function(src)
427     {   
428         if(this.fireEvent('beforeloadcanvas', this, src) != false){
429             
430             this.reset();
431             
432             this.imageEl = document.createElement('img');
433             
434             var _this = this;
435             
436             this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
437             
438             this.imageEl.src = src;
439         }
440     },
441     
442     onLoadCanvas : function()
443     {   
444         this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
445         this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
446
447         if(this.fireEvent('loadcanvas', this, this.imageEl) != false){
448         
449             this.bodyEl.un('click', this.beforeSelectFile, this);
450             
451             this.notifyEl.hide();
452             this.thumbEl.show();
453             this.footerEl.show();
454             
455             this.baseRotateLevel();
456             
457             if(this.isDocument){
458                 this.setThumbBoxSize();
459             }
460             
461             this.setThumbBoxPosition();
462             
463             this.baseScaleLevel();
464             
465             this.draw();
466             
467             this.resize();
468             
469             this.canvasLoaded = true;
470         
471         }
472         
473         if(this.loadMask){
474             this.maskEl.unmask();
475         }
476         
477     },
478     
479     setCanvasPosition : function(center = true)
480     {   
481         if(!this.canvasEl){
482             return;
483         }
484
485         var newCenterLeft = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
486         var newCenterTop = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
487
488         if(center) {
489             this.previewEl.setLeft(newCenterLeft);
490             this.previewEl.setTop(newCenterTop);
491
492             return;
493         }
494         
495         var oldScaleLevel = this.baseScale * Math.pow(1.02, this.startScale);
496         var oldCanvasWidth = Math.floor(this.imageEl.OriginWidth * oldScaleLevel);
497         var oldCanvasHeight = Math.floor(this.imageEl.OriginHeight * oldScaleLevel);
498
499         var oldCenterLeft = Math.ceil((this.bodyEl.getWidth() - oldCanvasWidth) / 2);
500         var oldCenterTop = Math.ceil((this.bodyEl.getHeight() - oldCanvasHeight) / 2);
501
502         var leftDiff = newCenterLeft - oldCenterLeft;
503         var topDiff = newCenterTop - oldCenterTop;
504
505         var newPreviewLeft = this.previewEl.getLeft(true) + leftDiff;
506         var newPreviewTop = this.previewEl.getTop(true) + topDiff;
507
508         Roo.log("-------------");
509
510         // Roo.log("old canvas width: " + oldCanvasWidth);
511         // Roo.log("old canvas height: " + oldCanvasHeight);
512
513         // Roo.log("new canvas width: " + this.canvasEl.width);
514         // Roo.log("new canvas height: " + this.canvasEl.height); 
515
516         // Roo.log("old preview left: " + this.previewEl.getLeft(true));
517         // Roo.log("old preview top: " + this.previewEl.getTop(true));
518
519         // Roo.log("new preview left: " + newPreviewLeft);
520         // Roo.log("new preview top: " + newPreviewTop); 
521
522         this.previewEl.setLeft(newPreviewLeft);
523         this.previewEl.setTop(newPreviewTop);
524         
525     },
526     
527     onMouseDown : function(e)
528     {   
529         e.stopEvent();
530         
531         this.dragable = true;
532         this.pinching = false;
533         
534         if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
535             this.dragable = false;
536             return;
537         }
538         
539         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
540         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
541         
542     },
543     
544     onMouseMove : function(e)
545     {   
546         e.stopEvent();
547         
548         if(!this.canvasLoaded){
549             return;
550         }
551         
552         if (!this.dragable){
553             return;
554         }
555
556         var maxPaddingLeft = this.canvasEl.width * 0.05;
557         var maxPaddingTop = maxPaddingLeft * this.minHeight / this.minWidth;
558
559         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight)) {
560             maxPaddingLeft = (this.canvasEl.height * this.minWidth / this.minHeight - this.canvasEl.width) / 2 + maxPaddingLeft;
561         }
562
563         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight)) {
564             maxPaddingTop = (this.canvasEl.width * this.minHeight / this.minWidth - this.canvasEl.height) / 2 + maxPaddingTop;
565         }
566         
567         var minX = Math.ceil(this.thumbEl.getLeft(true) + this.thumbEl.getWidth() - this.canvasEl.width - maxPaddingLeft);
568         var minY = Math.ceil(this.thumbEl.getTop(true) + this.thumbEl.getHeight() - this.canvasEl.height - maxPaddingTop);
569         
570         var maxX = Math.ceil(this.thumbEl.getLeft(true) + maxPaddingLeft);
571         var maxY = Math.ceil(this.thumbEl.getTop(true) +  maxPaddingTop);
572
573         Roo.log("++++++++++++++++++");
574         
575         Roo.log("maxPaddingLeft: " + maxPaddingLeft);
576         Roo.log("min X: " + minX);
577         Roo.log("min Y: " + minY);
578         Roo.log("max X: " + maxX);
579         Roo.log("max Y: " + maxY);
580
581
582         if(minX > maxX) {
583             var tempX = minX;
584             minX = maxX;
585             maxX = tempX;
586         }
587
588         if(minY > maxY) {
589             var tempY = minY;
590             minY = maxY;
591             maxY = tempY;
592         }
593
594         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
595         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
596         
597         x = x - this.mouseX;
598         y = y - this.mouseY;
599
600         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
601         var bgY = Math.ceil(y + this.previewEl.getTop(true));
602         
603         bgX = (bgX < minX) ? minX : ((bgX > maxX) ? maxX : bgX);
604         bgY = (bgY < minY) ? minY : ((bgY > maxY) ? maxY : bgY);
605         
606         this.previewEl.setLeft(bgX);
607         this.previewEl.setTop(bgY);
608         
609         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
610         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
611     },
612     
613     onMouseUp : function(e)
614     {   
615         e.stopEvent();
616         
617         this.dragable = false;
618     },
619     
620     onMouseWheel : function(e)
621     {   
622         e.stopEvent();
623         
624         this.startScale = this.scale;
625         this.scale = (e.getWheelDelta() > 0) ? (this.scale + 1) : (this.scale - 1);
626         
627         if(!this.zoomable()){
628             this.scale = this.startScale;
629             return;
630         }
631
632         
633         this.draw();
634         
635         return;
636     },
637     
638     zoomable : function()
639     {
640         var minScale = this.thumbEl.getWidth() / this.minWidth;
641         
642         if(this.minWidth < this.minHeight){
643             minScale = this.thumbEl.getHeight() / this.minHeight;
644         }
645         
646         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
647         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
648  
649         var maxWidth = this.imageEl.OriginWidth;
650         var maxHeight = this.imageEl.OriginHeight;
651
652
653         var newCanvasWidth = Math.floor(this.imageEl.OriginWidth * this.getScaleLevel());
654         var newCanvasHeight = Math.floor(this.imageEl.OriginHeight * this.getScaleLevel());
655
656         var oldCenterLeft = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
657         var oldCenterTop = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
658
659         var newCenterLeft = Math.ceil((this.bodyEl.getWidth() - newCanvasWidth) / 2);
660         var newCenterTop = Math.ceil((this.bodyEl.getHeight() - newCanvasHeight) / 2);
661
662         var leftDiff = newCenterLeft - oldCenterLeft;
663         var topDiff = newCenterTop - oldCenterTop;
664
665         var newPreviewLeft = this.previewEl.getLeft(true) + leftDiff;
666         var newPreviewTop = this.previewEl.getTop(true) + topDiff;
667
668         var paddingLeft = newPreviewLeft - this.thumbEl.getLeft(true);
669         var paddingTop = newPreviewTop - this.thumbEl.getTop(true);
670
671         var paddingRight = this.thumbEl.getLeft(true) + this.thumbEl.getWidth() - newCanvasWidth - newPreviewLeft;
672         var paddingBottom = this.thumbEl.getTop(true) + this.thumbEl.getHeight() - newCanvasHeight - newPreviewTop;
673
674         var maxPaddingLeft = newCanvasWidth * 0.05;
675         var maxPaddingTop = newCanvasWidth * this.minHeight / this.minWidth * 0.05;
676
677         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight)) {
678             maxPaddingLeft = (newCanvasHeight * this.minWidth / this.minHeight - newCanvasWidth) / 2 + maxPaddingLeft;
679         }
680
681         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight)) {
682             maxPaddingTop = (newCanvasWidth * this.minHeight / this.minWidth - newCanvasHeight) / 2 + maxPaddingTop;
683         }
684
685         Roo.log("=================================");
686         Roo.log("old canvas width: " + this.canvasEl.width);
687         Roo.log("old canvas height: " + this.canvasEl.height);
688
689         Roo.log("new canvas width: " + newCanvasWidth);
690         Roo.log("new canvas height: " + newCanvasHeight);
691
692         Roo.log("old preview left: " + this.previewEl.getLeft(true));
693         Roo.log("old preview top: " + this.previewEl.getTop(true));
694
695         Roo.log("new preview left: " + newPreviewLeft);
696         Roo.log("new preview top: " + newPreviewTop);
697
698         Roo.log("padding left: " + paddingLeft);
699         Roo.log("padding top: " + paddingTop);
700
701         Roo.log("padding right: " + paddingRight);
702         Roo.log("padding bottom: " + paddingBottom);
703
704         Roo.log("max padding left: " + maxPaddingLeft);
705         Roo.log("max padding top: " + maxPaddingTop);
706         
707         if(
708                 this.isDocument &&
709                 (this.rotate == 0 || this.rotate == 180) && 
710                 (
711                     width > this.imageEl.OriginWidth || 
712                     height > this.imageEl.OriginHeight ||
713                     (width < this.minWidth && height < this.minHeight)
714                 )
715         ){
716             return false;
717         }
718         
719         if(
720                 this.isDocument &&
721                 (this.rotate == 90 || this.rotate == 270) && 
722                 (
723                     width > this.imageEl.OriginWidth || 
724                     height > this.imageEl.OriginHeight ||
725                     (width < this.minHeight && height < this.minWidth)
726                 )
727         ){
728             return false;
729         }
730         
731         if(
732                 !this.isDocument &&
733                 (this.rotate == 0 || this.rotate == 180) && 
734                 (
735                     // for zoom out
736                     paddingLeft > maxPaddingLeft ||
737                     paddingRight > maxPaddingLeft ||
738                     paddingTop > maxPaddingTop ||
739                     paddingBottom > maxPaddingTop ||
740                     // for zoom in
741                     width > maxWidth ||
742                     height > maxHeight
743                 )
744         ){
745             return false;
746         }
747         
748         if(
749                 !this.isDocument &&
750                 (this.rotate == 90 || this.rotate == 270) && 
751                 (
752                     width < this.minHeight || 
753                     width > this.imageEl.OriginWidth || 
754                     height < this.minWidth || 
755                     height > this.imageEl.OriginHeight
756                 )
757         ){
758             return false;
759         }
760         
761         return true;
762         
763     },
764     
765     onRotateLeft : function(e)
766     {   
767         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
768             
769             var minScale = this.thumbEl.getWidth() / this.minWidth;
770             
771             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
772             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
773             
774             this.startScale = this.scale;
775             
776             while (this.getScaleLevel() < minScale){
777             
778                 this.scale = this.scale + 1;
779                 
780                 if(!this.zoomable()){
781                     break;
782                 }
783                 
784                 if(
785                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
786                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
787                 ){
788                     continue;
789                 }
790                 
791                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
792
793                 this.draw();
794                 
795                 return;
796             }
797             
798             this.scale = this.startScale;
799             
800             this.onRotateFail();
801             
802             return false;
803         }
804         
805         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
806
807         if(this.isDocument){
808             this.setThumbBoxSize();
809             this.setThumbBoxPosition();
810             this.setCanvasPosition();
811         }
812         
813         this.draw();
814         
815         this.fireEvent('rotate', this, 'left');
816         
817     },
818     
819     onRotateRight : function(e)
820     {
821         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
822             
823             var minScale = this.thumbEl.getWidth() / this.minWidth;
824         
825             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
826             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
827             
828             this.startScale = this.scale;
829             
830             while (this.getScaleLevel() < minScale){
831             
832                 this.scale = this.scale + 1;
833                 
834                 if(!this.zoomable()){
835                     break;
836                 }
837                 
838                 if(
839                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
840                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
841                 ){
842                     continue;
843                 }
844                 
845                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
846
847                 this.draw();
848                 
849                 return;
850             }
851             
852             this.scale = this.startScale;
853             
854             this.onRotateFail();
855             
856             return false;
857         }
858         
859         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
860
861         if(this.isDocument){
862             this.setThumbBoxSize();
863             this.setThumbBoxPosition();
864             this.setCanvasPosition();
865         }
866         
867         this.draw();
868         
869         this.fireEvent('rotate', this, 'right');
870     },
871     
872     onRotateFail : function()
873     {
874         this.errorEl.show(true);
875         
876         var _this = this;
877         
878         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
879     },
880     
881     draw : function()
882     {
883         this.previewEl.dom.innerHTML = '';
884         
885         var canvasEl = document.createElement("canvas");
886         
887         var contextEl = canvasEl.getContext("2d");
888         
889         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
890         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
891         var center = this.imageEl.OriginWidth / 2;
892         
893         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
894             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
895             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
896             center = this.imageEl.OriginHeight / 2;
897         }
898         
899         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
900         
901         contextEl.translate(center, center);
902         contextEl.rotate(this.rotate * Math.PI / 180);
903
904         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
905         
906         this.canvasEl = document.createElement("canvas");
907         
908         this.contextEl = this.canvasEl.getContext("2d");
909         
910         switch (this.rotate) {
911             case 0 :
912                 
913                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
914                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
915                 
916                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
917                 
918                 break;
919             case 90 : 
920                 
921                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
922                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
923                 
924                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
925                     this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
926                     break;
927                 }
928                 
929                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
930                 
931                 break;
932             case 180 :
933                 
934                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
935                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
936                 
937                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
938                     this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
939                     break;
940                 }
941                 
942                 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
943                 
944                 break;
945             case 270 :
946                 
947                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
948                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
949         
950                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
951                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
952                     break;
953                 }
954                 
955                 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
956                 
957                 break;
958             default : 
959                 break;
960         }
961         
962         this.previewEl.appendChild(this.canvasEl);
963         
964         this.setCanvasPosition(false);
965     },
966     
967     crop : function()
968     {
969         if(!this.canvasLoaded){
970             return;
971         }
972         
973         var imageCanvas = document.createElement("canvas");
974         
975         var imageContext = imageCanvas.getContext("2d");
976         
977         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
978         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
979         
980         var center = imageCanvas.width / 2;
981         
982         imageContext.translate(center, center);
983         
984         imageContext.rotate(this.rotate * Math.PI / 180);
985         
986         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
987         
988         var canvas = document.createElement("canvas");
989         
990         var context = canvas.getContext("2d");
991
992         canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
993         
994         canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
995
996         switch (this.rotate) {
997             case 0 :
998                 
999                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
1000                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
1001                 
1002                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1003                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1004                 
1005                 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
1006                 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
1007
1008                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1009                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1010
1011                 if(canvas.width > this.outputMaxWidth) {
1012                     var scale = this.outputMaxWidth / canvas.width;
1013                     canvas.width = canvas.width * scale;
1014                     canvas.height = canvas.height * scale;
1015                     context.scale(scale, scale);
1016                 }
1017
1018                 context.fillStyle = 'white';
1019                 context.fillRect(0, 0, this.thumbEl.getWidth() / this.getScaleLevel(), this.thumbEl.getHeight() / this.getScaleLevel());
1020
1021
1022                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1023                 
1024                 break;
1025             case 90 : 
1026                 
1027                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1028                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1029                 
1030                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1031                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1032                 
1033                 var targetWidth = this.minWidth - 2 * x;
1034                 var targetHeight = this.minHeight - 2 * y;
1035                 
1036                 var scale = 1;
1037                 
1038                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1039                     scale = targetWidth / width;
1040                 }
1041                 
1042                 if(x > 0 && y == 0){
1043                     scale = targetHeight / height;
1044                 }
1045                 
1046                 if(x > 0 && y > 0){
1047                     scale = targetWidth / width;
1048                     
1049                     if(width < height){
1050                         scale = targetHeight / height;
1051                     }
1052                 }
1053                 
1054                 context.scale(scale, scale);
1055                 
1056                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1057                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1058
1059                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1060                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1061                 
1062                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1063                 
1064                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1065                 
1066                 break;
1067             case 180 :
1068                 
1069                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
1070                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
1071                 
1072                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1073                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1074                 
1075                 var targetWidth = this.minWidth - 2 * x;
1076                 var targetHeight = this.minHeight - 2 * y;
1077                 
1078                 var scale = 1;
1079                 
1080                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1081                     scale = targetWidth / width;
1082                 }
1083                 
1084                 if(x > 0 && y == 0){
1085                     scale = targetHeight / height;
1086                 }
1087                 
1088                 if(x > 0 && y > 0){
1089                     scale = targetWidth / width;
1090                     
1091                     if(width < height){
1092                         scale = targetHeight / height;
1093                     }
1094                 }
1095                 
1096                 context.scale(scale, scale);
1097                 
1098                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1099                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1100
1101                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1102                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1103
1104                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1105                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1106                 
1107                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1108                 
1109                 break;
1110             case 270 :
1111                 
1112                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1113                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1114                 
1115                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1116                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1117                 
1118                 var targetWidth = this.minWidth - 2 * x;
1119                 var targetHeight = this.minHeight - 2 * y;
1120                 
1121                 var scale = 1;
1122                 
1123                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1124                     scale = targetWidth / width;
1125                 }
1126                 
1127                 if(x > 0 && y == 0){
1128                     scale = targetHeight / height;
1129                 }
1130                 
1131                 if(x > 0 && y > 0){
1132                     scale = targetWidth / width;
1133                     
1134                     if(width < height){
1135                         scale = targetHeight / height;
1136                     }
1137                 }
1138                 
1139                 context.scale(scale, scale);
1140                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1141                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1142
1143                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1144                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1145                 
1146                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1147                 
1148                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1149                 
1150                 break;
1151             default : 
1152                 break;
1153         }
1154         
1155         this.cropData = canvas.toDataURL(this.cropType);
1156         
1157         if(this.fireEvent('crop', this, this.cropData) !== false){
1158             this.process(this.file, this.cropData);
1159         }
1160         
1161         return;
1162         
1163     },
1164     
1165     setThumbBoxSize : function()
1166     {
1167         var width, height;
1168         
1169         if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1170             width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1171             height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1172             
1173             this.minWidth = width;
1174             this.minHeight = height;
1175             
1176             if(this.rotate == 90 || this.rotate == 270){
1177                 this.minWidth = height;
1178                 this.minHeight = width;
1179             }
1180         }
1181         
1182         height = this.windowSize;
1183         width = Math.ceil(this.minWidth * height / this.minHeight);
1184         
1185         if(this.minWidth > this.minHeight){
1186             width = this.windowSize;
1187             height = Math.ceil(this.minHeight * width / this.minWidth);
1188         }
1189         
1190         this.thumbEl.setStyle({
1191             width : width + 'px',
1192             height : height + 'px'
1193         });
1194
1195         return;
1196             
1197     },
1198     
1199     setThumbBoxPosition : function()
1200     {
1201         var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1202         var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1203         
1204         this.thumbEl.setLeft(x);
1205         this.thumbEl.setTop(y);
1206         
1207     },
1208     
1209     baseRotateLevel : function()
1210     {
1211         this.baseRotate = 1;
1212         
1213         if(
1214                 typeof(this.exif) != 'undefined' &&
1215                 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1216                 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1217         ){
1218             this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1219         }
1220         
1221         this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1222         
1223     },
1224     
1225     baseScaleLevel : function()
1226     {
1227         var width, height;
1228         
1229         if(this.isDocument){
1230             
1231             if(this.baseRotate == 6 || this.baseRotate == 8){
1232             
1233                 height = this.thumbEl.getHeight();
1234                 this.baseScale = height / this.imageEl.OriginWidth;
1235
1236                 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1237                     width = this.thumbEl.getWidth();
1238                     this.baseScale = width / this.imageEl.OriginHeight;
1239                 }
1240
1241                 return;
1242             }
1243
1244             height = this.thumbEl.getHeight();
1245             this.baseScale = height / this.imageEl.OriginHeight;
1246
1247             if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1248                 width = this.thumbEl.getWidth();
1249                 this.baseScale = width / this.imageEl.OriginWidth;
1250             }
1251
1252             return;
1253         }
1254         
1255         if(this.baseRotate == 6 || this.baseRotate == 8){
1256             
1257             width = this.thumbEl.getHeight();
1258             this.baseScale = width / this.imageEl.OriginHeight;
1259             
1260             if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1261                 height = this.thumbEl.getWidth();
1262                 this.baseScale = height / this.imageEl.OriginHeight;
1263             }
1264             
1265             if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1266                 height = this.thumbEl.getWidth();
1267                 this.baseScale = height / this.imageEl.OriginHeight;
1268                 
1269                 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1270                     width = this.thumbEl.getHeight();
1271                     this.baseScale = width / this.imageEl.OriginWidth;
1272                 }
1273             }
1274             
1275             return;
1276         }
1277         
1278         width = this.thumbEl.getWidth();
1279         this.baseScale = width / this.imageEl.OriginWidth;
1280         
1281         if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1282             height = this.thumbEl.getHeight();
1283             this.baseScale = height / this.imageEl.OriginHeight;
1284         }
1285         
1286         if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1287             
1288             height = this.thumbEl.getHeight();
1289             this.baseScale = height / this.imageEl.OriginHeight;
1290             
1291             if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1292                 width = this.thumbEl.getWidth();
1293                 this.baseScale = width / this.imageEl.OriginWidth;
1294             }
1295             
1296         }
1297
1298         if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1299             this.baseScale = width / this.minWidth;
1300         }
1301
1302         return;
1303     },
1304     
1305     getScaleLevel : function()
1306     {
1307         return this.baseScale * Math.pow(1.02, this.scale);
1308     },
1309     
1310     onTouchStart : function(e)
1311     {
1312         if(!this.canvasLoaded){
1313             this.beforeSelectFile(e);
1314             return;
1315         }
1316         
1317         var touches = e.browserEvent.touches;
1318         
1319         if(!touches){
1320             return;
1321         }
1322         
1323         if(touches.length == 1){
1324             this.onMouseDown(e);
1325             return;
1326         }
1327         
1328         if(touches.length != 2){
1329             return;
1330         }
1331         
1332         var coords = [];
1333         
1334         for(var i = 0, finger; finger = touches[i]; i++){
1335             coords.push(finger.pageX, finger.pageY);
1336         }
1337         
1338         var x = Math.pow(coords[0] - coords[2], 2);
1339         var y = Math.pow(coords[1] - coords[3], 2);
1340         
1341         this.startDistance = Math.sqrt(x + y);
1342         
1343         this.startScale = this.scale;
1344         
1345         this.pinching = true;
1346         this.dragable = false;
1347         
1348     },
1349     
1350     onTouchMove : function(e)
1351     {
1352         if(!this.pinching && !this.dragable){
1353             return;
1354         }
1355         
1356         var touches = e.browserEvent.touches;
1357         
1358         if(!touches){
1359             return;
1360         }
1361         
1362         if(this.dragable){
1363             this.onMouseMove(e);
1364             return;
1365         }
1366         
1367         var coords = [];
1368         
1369         for(var i = 0, finger; finger = touches[i]; i++){
1370             coords.push(finger.pageX, finger.pageY);
1371         }
1372         
1373         var x = Math.pow(coords[0] - coords[2], 2);
1374         var y = Math.pow(coords[1] - coords[3], 2);
1375         
1376         this.endDistance = Math.sqrt(x + y);
1377         
1378         this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1379         
1380         if(!this.zoomable()){
1381             this.scale = this.startScale;
1382             return;
1383         }
1384         
1385         this.draw();
1386         
1387     },
1388     
1389     onTouchEnd : function(e)
1390     {
1391         this.pinching = false;
1392         this.dragable = false;
1393         
1394     },
1395     
1396     process : function(file, crop)
1397     {
1398         if(this.loadMask){
1399             this.maskEl.mask(this.loadingText);
1400         }
1401         
1402         this.xhr = new XMLHttpRequest();
1403         
1404         file.xhr = this.xhr;
1405
1406         this.xhr.open(this.method, this.url, true);
1407         
1408         var headers = {
1409             "Accept": "application/json",
1410             "Cache-Control": "no-cache",
1411             "X-Requested-With": "XMLHttpRequest"
1412         };
1413         
1414         for (var headerName in headers) {
1415             var headerValue = headers[headerName];
1416             if (headerValue) {
1417                 this.xhr.setRequestHeader(headerName, headerValue);
1418             }
1419         }
1420         
1421         var _this = this;
1422         
1423         this.xhr.onload = function()
1424         {
1425             _this.xhrOnLoad(_this.xhr);
1426         }
1427         
1428         this.xhr.onerror = function()
1429         {
1430             _this.xhrOnError(_this.xhr);
1431         }
1432         
1433         var formData = new FormData();
1434
1435         formData.append('returnHTML', 'NO');
1436
1437         if(crop){
1438             formData.append('crop', crop);
1439             var blobBin = atob(crop.split(',')[1]);
1440             var array = [];
1441             for(var i = 0; i < blobBin.length; i++) {
1442                 array.push(blobBin.charCodeAt(i));
1443             }
1444             var croppedFile =new Blob([new Uint8Array(array)], {type: this.cropType});
1445             formData.append(this.paramName, croppedFile, file.name);
1446         }
1447         
1448         if(typeof(file.filename) != 'undefined'){
1449             formData.append('filename', file.filename);
1450         }
1451         
1452         if(typeof(file.mimetype) != 'undefined'){
1453             formData.append('mimetype', file.mimetype);
1454         }
1455
1456         if(this.fireEvent('arrange', this, formData) != false){
1457             this.xhr.send(formData);
1458         };
1459     },
1460     
1461     xhrOnLoad : function(xhr)
1462     {
1463         if(this.loadMask){
1464             this.maskEl.unmask();
1465         }
1466         
1467         if (xhr.readyState !== 4) {
1468             this.fireEvent('exception', this, xhr);
1469             return;
1470         }
1471
1472         var response = Roo.decode(xhr.responseText);
1473         
1474         if(!response.success){
1475             this.fireEvent('exception', this, xhr);
1476             return;
1477         }
1478         
1479         var response = Roo.decode(xhr.responseText);
1480         
1481         this.fireEvent('upload', this, response);
1482         
1483     },
1484     
1485     xhrOnError : function()
1486     {
1487         if(this.loadMask){
1488             this.maskEl.unmask();
1489         }
1490         
1491         Roo.log('xhr on error');
1492         
1493         var response = Roo.decode(xhr.responseText);
1494           
1495         Roo.log(response);
1496         
1497     },
1498     
1499     prepare : function(file)
1500     {   
1501         if(this.loadMask){
1502             this.maskEl.mask(this.loadingText);
1503         }
1504         
1505         this.file = false;
1506         this.exif = {};
1507         
1508         if(typeof(file) === 'string'){
1509             this.loadCanvas(file);
1510             return;
1511         }
1512         
1513         if(!file || !this.urlAPI){
1514             return;
1515         }
1516         
1517         this.file = file;
1518         if(typeof(file.type) != 'undefined' && file.type.length != 0) {
1519             this.cropType = file.type;
1520         }
1521         
1522         var _this = this;
1523         
1524         if(this.fireEvent('prepare', this, this.file) != false){
1525             
1526             var reader = new FileReader();
1527             
1528             reader.onload = function (e) {
1529                 if (e.target.error) {
1530                     Roo.log(e.target.error);
1531                     return;
1532                 }
1533                 
1534                 var buffer = e.target.result,
1535                     dataView = new DataView(buffer),
1536                     offset = 2,
1537                     maxOffset = dataView.byteLength - 4,
1538                     markerBytes,
1539                     markerLength;
1540                 
1541                 if (dataView.getUint16(0) === 0xffd8) {
1542                     while (offset < maxOffset) {
1543                         markerBytes = dataView.getUint16(offset);
1544                         
1545                         if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1546                             markerLength = dataView.getUint16(offset + 2) + 2;
1547                             if (offset + markerLength > dataView.byteLength) {
1548                                 Roo.log('Invalid meta data: Invalid segment size.');
1549                                 break;
1550                             }
1551                             
1552                             if(markerBytes == 0xffe1){
1553                                 _this.parseExifData(
1554                                     dataView,
1555                                     offset,
1556                                     markerLength
1557                                 );
1558                             }
1559                             
1560                             offset += markerLength;
1561                             
1562                             continue;
1563                         }
1564                         
1565                         break;
1566                     }
1567                     
1568                 }
1569                 
1570                 var url = _this.urlAPI.createObjectURL(_this.file);
1571                 
1572                 _this.loadCanvas(url);
1573                 
1574                 return;
1575             }
1576             
1577             reader.readAsArrayBuffer(this.file);
1578             
1579         }
1580         
1581     },
1582     
1583     parseExifData : function(dataView, offset, length)
1584     {
1585         var tiffOffset = offset + 10,
1586             littleEndian,
1587             dirOffset;
1588     
1589         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1590             // No Exif data, might be XMP data instead
1591             return;
1592         }
1593         
1594         // Check for the ASCII code for "Exif" (0x45786966):
1595         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1596             // No Exif data, might be XMP data instead
1597             return;
1598         }
1599         if (tiffOffset + 8 > dataView.byteLength) {
1600             Roo.log('Invalid Exif data: Invalid segment size.');
1601             return;
1602         }
1603         // Check for the two null bytes:
1604         if (dataView.getUint16(offset + 8) !== 0x0000) {
1605             Roo.log('Invalid Exif data: Missing byte alignment offset.');
1606             return;
1607         }
1608         // Check the byte alignment:
1609         switch (dataView.getUint16(tiffOffset)) {
1610         case 0x4949:
1611             littleEndian = true;
1612             break;
1613         case 0x4D4D:
1614             littleEndian = false;
1615             break;
1616         default:
1617             Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1618             return;
1619         }
1620         // Check for the TIFF tag marker (0x002A):
1621         if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1622             Roo.log('Invalid Exif data: Missing TIFF marker.');
1623             return;
1624         }
1625         // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1626         dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1627         
1628         this.parseExifTags(
1629             dataView,
1630             tiffOffset,
1631             tiffOffset + dirOffset,
1632             littleEndian
1633         );
1634     },
1635     
1636     parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1637     {
1638         var tagsNumber,
1639             dirEndOffset,
1640             i;
1641         if (dirOffset + 6 > dataView.byteLength) {
1642             Roo.log('Invalid Exif data: Invalid directory offset.');
1643             return;
1644         }
1645         tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1646         dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1647         if (dirEndOffset + 4 > dataView.byteLength) {
1648             Roo.log('Invalid Exif data: Invalid directory size.');
1649             return;
1650         }
1651         for (i = 0; i < tagsNumber; i += 1) {
1652             this.parseExifTag(
1653                 dataView,
1654                 tiffOffset,
1655                 dirOffset + 2 + 12 * i, // tag offset
1656                 littleEndian
1657             );
1658         }
1659         // Return the offset to the next directory:
1660         return dataView.getUint32(dirEndOffset, littleEndian);
1661     },
1662     
1663     parseExifTag : function (dataView, tiffOffset, offset, littleEndian) 
1664     {
1665         var tag = dataView.getUint16(offset, littleEndian);
1666         
1667         this.exif[tag] = this.getExifValue(
1668             dataView,
1669             tiffOffset,
1670             offset,
1671             dataView.getUint16(offset + 2, littleEndian), // tag type
1672             dataView.getUint32(offset + 4, littleEndian), // tag length
1673             littleEndian
1674         );
1675     },
1676     
1677     getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1678     {
1679         var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1680             tagSize,
1681             dataOffset,
1682             values,
1683             i,
1684             str,
1685             c;
1686     
1687         if (!tagType) {
1688             Roo.log('Invalid Exif data: Invalid tag type.');
1689             return;
1690         }
1691         
1692         tagSize = tagType.size * length;
1693         // Determine if the value is contained in the dataOffset bytes,
1694         // or if the value at the dataOffset is a pointer to the actual data:
1695         dataOffset = tagSize > 4 ?
1696                 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1697         if (dataOffset + tagSize > dataView.byteLength) {
1698             Roo.log('Invalid Exif data: Invalid data offset.');
1699             return;
1700         }
1701         if (length === 1) {
1702             return tagType.getValue(dataView, dataOffset, littleEndian);
1703         }
1704         values = [];
1705         for (i = 0; i < length; i += 1) {
1706             values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1707         }
1708         
1709         if (tagType.ascii) {
1710             str = '';
1711             // Concatenate the chars:
1712             for (i = 0; i < values.length; i += 1) {
1713                 c = values[i];
1714                 // Ignore the terminating NULL byte(s):
1715                 if (c === '\u0000') {
1716                     break;
1717                 }
1718                 str += c;
1719             }
1720             return str;
1721         }
1722         return values;
1723     }
1724     
1725 });
1726
1727 Roo.apply(Roo.dialog.UploadCropbox, {
1728     tags : {
1729         'Orientation': 0x0112
1730     },
1731     
1732     Orientation: {
1733             1: 0, //'top-left',
1734 //            2: 'top-right',
1735             3: 180, //'bottom-right',
1736 //            4: 'bottom-left',
1737 //            5: 'left-top',
1738             6: 90, //'right-top',
1739 //            7: 'right-bottom',
1740             8: 270 //'left-bottom'
1741     },
1742     
1743     exifTagTypes : {
1744         // byte, 8-bit unsigned int:
1745         1: {
1746             getValue: function (dataView, dataOffset) {
1747                 return dataView.getUint8(dataOffset);
1748             },
1749             size: 1
1750         },
1751         // ascii, 8-bit byte:
1752         2: {
1753             getValue: function (dataView, dataOffset) {
1754                 return String.fromCharCode(dataView.getUint8(dataOffset));
1755             },
1756             size: 1,
1757             ascii: true
1758         },
1759         // short, 16 bit int:
1760         3: {
1761             getValue: function (dataView, dataOffset, littleEndian) {
1762                 return dataView.getUint16(dataOffset, littleEndian);
1763             },
1764             size: 2
1765         },
1766         // long, 32 bit int:
1767         4: {
1768             getValue: function (dataView, dataOffset, littleEndian) {
1769                 return dataView.getUint32(dataOffset, littleEndian);
1770             },
1771             size: 4
1772         },
1773         // rational = two long values, first is numerator, second is denominator:
1774         5: {
1775             getValue: function (dataView, dataOffset, littleEndian) {
1776                 return dataView.getUint32(dataOffset, littleEndian) /
1777                     dataView.getUint32(dataOffset + 4, littleEndian);
1778             },
1779             size: 8
1780         },
1781         // slong, 32 bit signed int:
1782         9: {
1783             getValue: function (dataView, dataOffset, littleEndian) {
1784                 return dataView.getInt32(dataOffset, littleEndian);
1785             },
1786             size: 4
1787         },
1788         // srational, two slongs, first is numerator, second is denominator:
1789         10: {
1790             getValue: function (dataView, dataOffset, littleEndian) {
1791                 return dataView.getInt32(dataOffset, littleEndian) /
1792                     dataView.getInt32(dataOffset + 4, littleEndian);
1793             },
1794             size: 8
1795         }
1796     },
1797     
1798     footer : {
1799         STANDARD : [
1800             {
1801                 tag : 'div',
1802                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1803                 action : 'rotate-left',
1804                 cn : [
1805                     {
1806                         tag : 'button',
1807                         cls : 'btn btn-default',
1808                         html : '<i class="fa fa-undo"></i>'
1809                     }
1810                 ]
1811             },
1812             {
1813                 tag : 'div',
1814                 cls : 'btn-group roo-upload-cropbox-picture',
1815                 action : 'picture',
1816                 cn : [
1817                     {
1818                         tag : 'button',
1819                         cls : 'btn btn-default',
1820                         html : '<i class="fa fa-picture-o"></i>'
1821                     }
1822                 ]
1823             },
1824             {
1825                 tag : 'div',
1826                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1827                 action : 'rotate-right',
1828                 cn : [
1829                     {
1830                         tag : 'button',
1831                         cls : 'btn btn-default',
1832                         html : '<i class="fa fa-repeat"></i>'
1833                     }
1834                 ]
1835             }
1836         ],
1837         DOCUMENT : [
1838             {
1839                 tag : 'div',
1840                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1841                 action : 'rotate-left',
1842                 cn : [
1843                     {
1844                         tag : 'button',
1845                         cls : 'btn btn-default',
1846                         html : '<i class="fa fa-undo"></i>'
1847                     }
1848                 ]
1849             },
1850             {
1851                 tag : 'div',
1852                 cls : 'btn-group roo-upload-cropbox-download',
1853                 action : 'download',
1854                 cn : [
1855                     {
1856                         tag : 'button',
1857                         cls : 'btn btn-default',
1858                         html : '<i class="fa fa-download"></i>'
1859                     }
1860                 ]
1861             },
1862             {
1863                 tag : 'div',
1864                 cls : 'btn-group roo-upload-cropbox-crop',
1865                 action : 'crop',
1866                 cn : [
1867                     {
1868                         tag : 'button',
1869                         cls : 'btn btn-default',
1870                         html : '<i class="fa fa-crop"></i>'
1871                     }
1872                 ]
1873             },
1874             {
1875                 tag : 'div',
1876                 cls : 'btn-group roo-upload-cropbox-trash',
1877                 action : 'trash',
1878                 cn : [
1879                     {
1880                         tag : 'button',
1881                         cls : 'btn btn-default',
1882                         html : '<i class="fa fa-trash"></i>'
1883                     }
1884                 ]
1885             },
1886             {
1887                 tag : 'div',
1888                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1889                 action : 'rotate-right',
1890                 cn : [
1891                     {
1892                         tag : 'button',
1893                         cls : 'btn btn-default',
1894                         html : '<i class="fa fa-repeat"></i>'
1895                     }
1896                 ]
1897             }
1898         ],
1899         ROTATOR : [
1900             {
1901                 tag : 'div',
1902                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1903                 action : 'rotate-left',
1904                 cn : [
1905                     {
1906                         tag : 'button',
1907                         cls : 'btn btn-default',
1908                         html : '<i class="fa fa-undo"></i>'
1909                     }
1910                 ]
1911             },
1912             {
1913                 tag : 'div',
1914                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1915                 action : 'rotate-right',
1916                 cn : [
1917                     {
1918                         tag : 'button',
1919                         cls : 'btn btn-default',
1920                         html : '<i class="fa fa-repeat"></i>'
1921                     }
1922                 ]
1923             }
1924         ],
1925         CENTER : [
1926             {
1927                 tag : 'div',
1928                 cls : 'btn-group roo-upload-cropbox-center',
1929                 action : 'center',
1930                 cn : [
1931                     {
1932                         tag : 'button',
1933                         cls : 'btn btn-default',
1934                         html : 'CENTER'
1935                     }
1936                 ]
1937             }
1938         ]
1939     }
1940 });