86676c67e7624180044e0a1ad8df59feedb1b2df
[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 > this.canvasEl.height ? this.canvasEl.width * 0.05 : this.canvasEl.height * 0.05;
557         var maxPaddingTop = this.canvasEl.width > this.canvasEl.height ? this.canvasEl.width * 0.05 : this.canvasEl.height * 0.05;
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 + maxPaddingTop;
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 + maxPaddingLeft;
565         }
566
567         var textX = Math.ceil(this.thumbEl.getLeft(true) + this.thumbEl.getWidth() - this.canvasEl.width - maxPaddingTop);
568         
569         var minX = Math.ceil(this.thumbEl.getLeft(true));
570         var minY = Math.ceil(this.thumbEl.getTop(true));
571         
572         var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
573         var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
574
575         if(minX > maxX) {
576             var tempX = minX;
577             minX = maxX;
578             maxX = tempX;
579         }
580
581         if(minY > maxY) {
582             var tempY = minY;
583             minY = maxY;
584             maxY = tempY;
585         }
586
587         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
588         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
589         
590         x = x - this.mouseX;
591         y = y - this.mouseY;
592
593         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
594         var bgY = Math.ceil(y + this.previewEl.getTop(true));
595         
596         bgX = (bgX < minX) ? minX : ((bgX > maxX) ? maxX : bgX);
597         bgY = (bgY < minY) ? minY : ((bgY > maxY) ? maxY : bgY);
598         
599         this.previewEl.setLeft(bgX);
600         this.previewEl.setTop(bgY);
601         
602         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
603         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
604     },
605     
606     onMouseUp : function(e)
607     {   
608         e.stopEvent();
609         
610         this.dragable = false;
611     },
612     
613     onMouseWheel : function(e)
614     {   
615         e.stopEvent();
616         
617         this.startScale = this.scale;
618         this.scale = (e.getWheelDelta() > 0) ? (this.scale + 1) : (this.scale - 1);
619         
620         if(!this.zoomable()){
621             this.scale = this.startScale;
622             return;
623         }
624
625         
626         this.draw();
627         
628         return;
629     },
630     
631     zoomable : function()
632     {
633         var minScale = this.thumbEl.getWidth() / this.minWidth;
634         
635         if(this.minWidth < this.minHeight){
636             minScale = this.thumbEl.getHeight() / this.minHeight;
637         }
638         
639         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
640         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
641  
642         var maxWidth = this.imageEl.OriginWidth;
643         var maxHeight = this.imageEl.OriginHeight;
644
645
646         var newCanvasWidth = Math.floor(this.imageEl.OriginWidth * this.getScaleLevel());
647         var newCanvasHeight = Math.floor(this.imageEl.OriginHeight * this.getScaleLevel());
648
649         var oldCenterLeft = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
650         var oldCenterTop = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
651
652         var newCenterLeft = Math.ceil((this.bodyEl.getWidth() - newCanvasWidth) / 2);
653         var newCenterTop = Math.ceil((this.bodyEl.getHeight() - newCanvasHeight) / 2);
654
655         var leftDiff = newCenterLeft - oldCenterLeft;
656         var topDiff = newCenterTop - oldCenterTop;
657
658         var newPreviewLeft = this.previewEl.getLeft(true) + leftDiff;
659         var newPreviewTop = this.previewEl.getTop(true) + topDiff;
660
661         var paddingLeft = newPreviewLeft - this.thumbEl.getLeft(true);
662         var paddingTop = newPreviewTop - this.thumbEl.getTop(true);
663
664         var paddingRight = this.thumbEl.getLeft(true) + this.thumbEl.getWidth() - newCanvasWidth - newPreviewLeft;
665         var paddingBottom = this.thumbEl.getTop(true) + this.thumbEl.getHeight() - newCanvasHeight - newPreviewTop;
666
667         var maxPaddingLeft = newCanvasWidth > newCanvasHeight ? newCanvasWidth * 0.05 : newCanvasHeight * 0.05;
668         var maxPaddingTop = newCanvasWidth > newCanvasHeight ? newCanvasWidth * 0.05 : newCanvasHeight * 0.05;
669
670         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight <= this.minWidth / this.minHeight)) {
671             maxPaddingLeft = (newCanvasHeight * this.minWidth / this.minHeight - newCanvasWidth) / 2 + maxPaddingTop;
672         }
673
674         if ((this.imageEl.OriginWidth / this.imageEl.OriginHeight >= this.minWidth / this.minHeight)) {
675             maxPaddingTop = (newCanvasWidth * this.minHeight / this.minWidth - newCanvasHeight) / 2 + maxPaddingLeft;
676         }
677
678         Roo.log("=================================");
679         Roo.log("old canvas width: " + this.canvasEl.width);
680         Roo.log("old canvas height: " + this.canvasEl.height);
681
682         Roo.log("new canvas width: " + newCanvasWidth);
683         Roo.log("new canvas height: " + newCanvasHeight);
684
685         Roo.log("old preview left: " + this.previewEl.getLeft(true));
686         Roo.log("old preview top: " + this.previewEl.getTop(true));
687
688         Roo.log("new preview left: " + newPreviewLeft);
689         Roo.log("new preview top: " + newPreviewTop);
690
691         Roo.log("padding left: " + paddingLeft);
692         Roo.log("padding top: " + paddingTop);
693
694         Roo.log("padding right: " + paddingRight);
695         Roo.log("padding bottom: " + paddingBottom);
696
697         Roo.log("max padding left: " + maxPaddingLeft);
698         Roo.log("max padding top: " + maxPaddingTop);
699         
700         if(
701                 this.isDocument &&
702                 (this.rotate == 0 || this.rotate == 180) && 
703                 (
704                     width > this.imageEl.OriginWidth || 
705                     height > this.imageEl.OriginHeight ||
706                     (width < this.minWidth && height < this.minHeight)
707                 )
708         ){
709             return false;
710         }
711         
712         if(
713                 this.isDocument &&
714                 (this.rotate == 90 || this.rotate == 270) && 
715                 (
716                     width > this.imageEl.OriginWidth || 
717                     height > this.imageEl.OriginHeight ||
718                     (width < this.minHeight && height < this.minWidth)
719                 )
720         ){
721             return false;
722         }
723         
724         if(
725                 !this.isDocument &&
726                 (this.rotate == 0 || this.rotate == 180) && 
727                 (
728                     // for zoom out
729                     paddingLeft > maxPaddingLeft ||
730                     paddingRight > maxPaddingLeft ||
731                     paddingTop > maxPaddingTop ||
732                     paddingBottom > maxPaddingTop ||
733                     // for zoom in
734                     width > maxWidth ||
735                     height > maxHeight
736                 )
737         ){
738             return false;
739         }
740         
741         if(
742                 !this.isDocument &&
743                 (this.rotate == 90 || this.rotate == 270) && 
744                 (
745                     width < this.minHeight || 
746                     width > this.imageEl.OriginWidth || 
747                     height < this.minWidth || 
748                     height > this.imageEl.OriginHeight
749                 )
750         ){
751             return false;
752         }
753         
754         return true;
755         
756     },
757     
758     onRotateLeft : function(e)
759     {   
760         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
761             
762             var minScale = this.thumbEl.getWidth() / this.minWidth;
763             
764             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
765             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
766             
767             this.startScale = this.scale;
768             
769             while (this.getScaleLevel() < minScale){
770             
771                 this.scale = this.scale + 1;
772                 
773                 if(!this.zoomable()){
774                     break;
775                 }
776                 
777                 if(
778                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
779                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
780                 ){
781                     continue;
782                 }
783                 
784                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
785
786                 this.draw();
787                 
788                 return;
789             }
790             
791             this.scale = this.startScale;
792             
793             this.onRotateFail();
794             
795             return false;
796         }
797         
798         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
799
800         if(this.isDocument){
801             this.setThumbBoxSize();
802             this.setThumbBoxPosition();
803             this.setCanvasPosition();
804         }
805         
806         this.draw();
807         
808         this.fireEvent('rotate', this, 'left');
809         
810     },
811     
812     onRotateRight : function(e)
813     {
814         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
815             
816             var minScale = this.thumbEl.getWidth() / this.minWidth;
817         
818             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
819             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
820             
821             this.startScale = this.scale;
822             
823             while (this.getScaleLevel() < minScale){
824             
825                 this.scale = this.scale + 1;
826                 
827                 if(!this.zoomable()){
828                     break;
829                 }
830                 
831                 if(
832                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
833                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
834                 ){
835                     continue;
836                 }
837                 
838                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
839
840                 this.draw();
841                 
842                 return;
843             }
844             
845             this.scale = this.startScale;
846             
847             this.onRotateFail();
848             
849             return false;
850         }
851         
852         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
853
854         if(this.isDocument){
855             this.setThumbBoxSize();
856             this.setThumbBoxPosition();
857             this.setCanvasPosition();
858         }
859         
860         this.draw();
861         
862         this.fireEvent('rotate', this, 'right');
863     },
864     
865     onRotateFail : function()
866     {
867         this.errorEl.show(true);
868         
869         var _this = this;
870         
871         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
872     },
873     
874     draw : function()
875     {
876         this.previewEl.dom.innerHTML = '';
877         
878         var canvasEl = document.createElement("canvas");
879         
880         var contextEl = canvasEl.getContext("2d");
881         
882         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
883         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
884         var center = this.imageEl.OriginWidth / 2;
885         
886         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
887             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
888             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
889             center = this.imageEl.OriginHeight / 2;
890         }
891         
892         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
893         
894         contextEl.translate(center, center);
895         contextEl.rotate(this.rotate * Math.PI / 180);
896
897         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
898         
899         this.canvasEl = document.createElement("canvas");
900         
901         this.contextEl = this.canvasEl.getContext("2d");
902         
903         switch (this.rotate) {
904             case 0 :
905                 
906                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
907                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
908                 
909                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
910                 
911                 break;
912             case 90 : 
913                 
914                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
915                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
916                 
917                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
918                     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);
919                     break;
920                 }
921                 
922                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
923                 
924                 break;
925             case 180 :
926                 
927                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
928                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
929                 
930                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
931                     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);
932                     break;
933                 }
934                 
935                 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);
936                 
937                 break;
938             case 270 :
939                 
940                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
941                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
942         
943                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
944                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
945                     break;
946                 }
947                 
948                 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);
949                 
950                 break;
951             default : 
952                 break;
953         }
954         
955         this.previewEl.appendChild(this.canvasEl);
956         
957         this.setCanvasPosition(false);
958     },
959     
960     crop : function()
961     {
962         if(!this.canvasLoaded){
963             return;
964         }
965         
966         var imageCanvas = document.createElement("canvas");
967         
968         var imageContext = imageCanvas.getContext("2d");
969         
970         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
971         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
972         
973         var center = imageCanvas.width / 2;
974         
975         imageContext.translate(center, center);
976         
977         imageContext.rotate(this.rotate * Math.PI / 180);
978         
979         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
980         
981         var canvas = document.createElement("canvas");
982         
983         var context = canvas.getContext("2d");
984
985         canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
986         
987         canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
988
989         switch (this.rotate) {
990             case 0 :
991                 
992                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
993                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
994                 
995                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
996                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
997                 
998                 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
999                 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
1000
1001                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1002                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1003
1004                 if(canvas.width > this.outputMaxWidth) {
1005                     var scale = this.outputMaxWidth / canvas.width;
1006                     canvas.width = canvas.width * scale;
1007                     canvas.height = canvas.height * scale;
1008                     context.scale(scale, scale);
1009                 }
1010
1011                 context.fillStyle = 'white';
1012                 context.fillRect(0, 0, this.thumbEl.getWidth() / this.getScaleLevel(), this.thumbEl.getHeight() / this.getScaleLevel());
1013
1014
1015                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1016                 
1017                 break;
1018             case 90 : 
1019                 
1020                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1021                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1022                 
1023                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1024                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1025                 
1026                 var targetWidth = this.minWidth - 2 * x;
1027                 var targetHeight = this.minHeight - 2 * y;
1028                 
1029                 var scale = 1;
1030                 
1031                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1032                     scale = targetWidth / width;
1033                 }
1034                 
1035                 if(x > 0 && y == 0){
1036                     scale = targetHeight / height;
1037                 }
1038                 
1039                 if(x > 0 && y > 0){
1040                     scale = targetWidth / width;
1041                     
1042                     if(width < height){
1043                         scale = targetHeight / height;
1044                     }
1045                 }
1046                 
1047                 context.scale(scale, scale);
1048                 
1049                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1050                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1051
1052                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1053                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1054                 
1055                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1056                 
1057                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1058                 
1059                 break;
1060             case 180 :
1061                 
1062                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
1063                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
1064                 
1065                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1066                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1067                 
1068                 var targetWidth = this.minWidth - 2 * x;
1069                 var targetHeight = this.minHeight - 2 * y;
1070                 
1071                 var scale = 1;
1072                 
1073                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1074                     scale = targetWidth / width;
1075                 }
1076                 
1077                 if(x > 0 && y == 0){
1078                     scale = targetHeight / height;
1079                 }
1080                 
1081                 if(x > 0 && y > 0){
1082                     scale = targetWidth / width;
1083                     
1084                     if(width < height){
1085                         scale = targetHeight / height;
1086                     }
1087                 }
1088                 
1089                 context.scale(scale, scale);
1090                 
1091                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1092                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1093
1094                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1095                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1096
1097                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1098                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1099                 
1100                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1101                 
1102                 break;
1103             case 270 :
1104                 
1105                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1106                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1107                 
1108                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1109                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1110                 
1111                 var targetWidth = this.minWidth - 2 * x;
1112                 var targetHeight = this.minHeight - 2 * y;
1113                 
1114                 var scale = 1;
1115                 
1116                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1117                     scale = targetWidth / width;
1118                 }
1119                 
1120                 if(x > 0 && y == 0){
1121                     scale = targetHeight / height;
1122                 }
1123                 
1124                 if(x > 0 && y > 0){
1125                     scale = targetWidth / width;
1126                     
1127                     if(width < height){
1128                         scale = targetHeight / height;
1129                     }
1130                 }
1131                 
1132                 context.scale(scale, scale);
1133                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1134                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1135
1136                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1137                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1138                 
1139                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1140                 
1141                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1142                 
1143                 break;
1144             default : 
1145                 break;
1146         }
1147         
1148         this.cropData = canvas.toDataURL(this.cropType);
1149         
1150         if(this.fireEvent('crop', this, this.cropData) !== false){
1151             this.process(this.file, this.cropData);
1152         }
1153         
1154         return;
1155         
1156     },
1157     
1158     setThumbBoxSize : function()
1159     {
1160         var width, height;
1161         
1162         if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1163             width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1164             height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1165             
1166             this.minWidth = width;
1167             this.minHeight = height;
1168             
1169             if(this.rotate == 90 || this.rotate == 270){
1170                 this.minWidth = height;
1171                 this.minHeight = width;
1172             }
1173         }
1174         
1175         height = this.windowSize;
1176         width = Math.ceil(this.minWidth * height / this.minHeight);
1177         
1178         if(this.minWidth > this.minHeight){
1179             width = this.windowSize;
1180             height = Math.ceil(this.minHeight * width / this.minWidth);
1181         }
1182         
1183         this.thumbEl.setStyle({
1184             width : width + 'px',
1185             height : height + 'px'
1186         });
1187
1188         return;
1189             
1190     },
1191     
1192     setThumbBoxPosition : function()
1193     {
1194         var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1195         var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1196         
1197         this.thumbEl.setLeft(x);
1198         this.thumbEl.setTop(y);
1199         
1200     },
1201     
1202     baseRotateLevel : function()
1203     {
1204         this.baseRotate = 1;
1205         
1206         if(
1207                 typeof(this.exif) != 'undefined' &&
1208                 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1209                 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1210         ){
1211             this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1212         }
1213         
1214         this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1215         
1216     },
1217     
1218     baseScaleLevel : function()
1219     {
1220         var width, height;
1221         
1222         if(this.isDocument){
1223             
1224             if(this.baseRotate == 6 || this.baseRotate == 8){
1225             
1226                 height = this.thumbEl.getHeight();
1227                 this.baseScale = height / this.imageEl.OriginWidth;
1228
1229                 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1230                     width = this.thumbEl.getWidth();
1231                     this.baseScale = width / this.imageEl.OriginHeight;
1232                 }
1233
1234                 return;
1235             }
1236
1237             height = this.thumbEl.getHeight();
1238             this.baseScale = height / this.imageEl.OriginHeight;
1239
1240             if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1241                 width = this.thumbEl.getWidth();
1242                 this.baseScale = width / this.imageEl.OriginWidth;
1243             }
1244
1245             return;
1246         }
1247         
1248         if(this.baseRotate == 6 || this.baseRotate == 8){
1249             
1250             width = this.thumbEl.getHeight();
1251             this.baseScale = width / this.imageEl.OriginHeight;
1252             
1253             if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1254                 height = this.thumbEl.getWidth();
1255                 this.baseScale = height / this.imageEl.OriginHeight;
1256             }
1257             
1258             if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1259                 height = this.thumbEl.getWidth();
1260                 this.baseScale = height / this.imageEl.OriginHeight;
1261                 
1262                 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1263                     width = this.thumbEl.getHeight();
1264                     this.baseScale = width / this.imageEl.OriginWidth;
1265                 }
1266             }
1267             
1268             return;
1269         }
1270         
1271         width = this.thumbEl.getWidth();
1272         this.baseScale = width / this.imageEl.OriginWidth;
1273         
1274         if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1275             height = this.thumbEl.getHeight();
1276             this.baseScale = height / this.imageEl.OriginHeight;
1277         }
1278         
1279         if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1280             
1281             height = this.thumbEl.getHeight();
1282             this.baseScale = height / this.imageEl.OriginHeight;
1283             
1284             if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1285                 width = this.thumbEl.getWidth();
1286                 this.baseScale = width / this.imageEl.OriginWidth;
1287             }
1288             
1289         }
1290
1291         if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1292             this.baseScale = width / this.minWidth;
1293         }
1294
1295         return;
1296     },
1297     
1298     getScaleLevel : function()
1299     {
1300         return this.baseScale * Math.pow(1.02, this.scale);
1301     },
1302     
1303     onTouchStart : function(e)
1304     {
1305         if(!this.canvasLoaded){
1306             this.beforeSelectFile(e);
1307             return;
1308         }
1309         
1310         var touches = e.browserEvent.touches;
1311         
1312         if(!touches){
1313             return;
1314         }
1315         
1316         if(touches.length == 1){
1317             this.onMouseDown(e);
1318             return;
1319         }
1320         
1321         if(touches.length != 2){
1322             return;
1323         }
1324         
1325         var coords = [];
1326         
1327         for(var i = 0, finger; finger = touches[i]; i++){
1328             coords.push(finger.pageX, finger.pageY);
1329         }
1330         
1331         var x = Math.pow(coords[0] - coords[2], 2);
1332         var y = Math.pow(coords[1] - coords[3], 2);
1333         
1334         this.startDistance = Math.sqrt(x + y);
1335         
1336         this.startScale = this.scale;
1337         
1338         this.pinching = true;
1339         this.dragable = false;
1340         
1341     },
1342     
1343     onTouchMove : function(e)
1344     {
1345         if(!this.pinching && !this.dragable){
1346             return;
1347         }
1348         
1349         var touches = e.browserEvent.touches;
1350         
1351         if(!touches){
1352             return;
1353         }
1354         
1355         if(this.dragable){
1356             this.onMouseMove(e);
1357             return;
1358         }
1359         
1360         var coords = [];
1361         
1362         for(var i = 0, finger; finger = touches[i]; i++){
1363             coords.push(finger.pageX, finger.pageY);
1364         }
1365         
1366         var x = Math.pow(coords[0] - coords[2], 2);
1367         var y = Math.pow(coords[1] - coords[3], 2);
1368         
1369         this.endDistance = Math.sqrt(x + y);
1370         
1371         this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1372         
1373         if(!this.zoomable()){
1374             this.scale = this.startScale;
1375             return;
1376         }
1377         
1378         this.draw();
1379         
1380     },
1381     
1382     onTouchEnd : function(e)
1383     {
1384         this.pinching = false;
1385         this.dragable = false;
1386         
1387     },
1388     
1389     process : function(file, crop)
1390     {
1391         if(this.loadMask){
1392             this.maskEl.mask(this.loadingText);
1393         }
1394         
1395         this.xhr = new XMLHttpRequest();
1396         
1397         file.xhr = this.xhr;
1398
1399         this.xhr.open(this.method, this.url, true);
1400         
1401         var headers = {
1402             "Accept": "application/json",
1403             "Cache-Control": "no-cache",
1404             "X-Requested-With": "XMLHttpRequest"
1405         };
1406         
1407         for (var headerName in headers) {
1408             var headerValue = headers[headerName];
1409             if (headerValue) {
1410                 this.xhr.setRequestHeader(headerName, headerValue);
1411             }
1412         }
1413         
1414         var _this = this;
1415         
1416         this.xhr.onload = function()
1417         {
1418             _this.xhrOnLoad(_this.xhr);
1419         }
1420         
1421         this.xhr.onerror = function()
1422         {
1423             _this.xhrOnError(_this.xhr);
1424         }
1425         
1426         var formData = new FormData();
1427
1428         formData.append('returnHTML', 'NO');
1429
1430         if(crop){
1431             formData.append('crop', crop);
1432             var blobBin = atob(crop.split(',')[1]);
1433             var array = [];
1434             for(var i = 0; i < blobBin.length; i++) {
1435                 array.push(blobBin.charCodeAt(i));
1436             }
1437             var croppedFile =new Blob([new Uint8Array(array)], {type: this.cropType});
1438             formData.append(this.paramName, croppedFile, file.name);
1439         }
1440         
1441         if(typeof(file.filename) != 'undefined'){
1442             formData.append('filename', file.filename);
1443         }
1444         
1445         if(typeof(file.mimetype) != 'undefined'){
1446             formData.append('mimetype', file.mimetype);
1447         }
1448
1449         if(this.fireEvent('arrange', this, formData) != false){
1450             this.xhr.send(formData);
1451         };
1452     },
1453     
1454     xhrOnLoad : function(xhr)
1455     {
1456         if(this.loadMask){
1457             this.maskEl.unmask();
1458         }
1459         
1460         if (xhr.readyState !== 4) {
1461             this.fireEvent('exception', this, xhr);
1462             return;
1463         }
1464
1465         var response = Roo.decode(xhr.responseText);
1466         
1467         if(!response.success){
1468             this.fireEvent('exception', this, xhr);
1469             return;
1470         }
1471         
1472         var response = Roo.decode(xhr.responseText);
1473         
1474         this.fireEvent('upload', this, response);
1475         
1476     },
1477     
1478     xhrOnError : function()
1479     {
1480         if(this.loadMask){
1481             this.maskEl.unmask();
1482         }
1483         
1484         Roo.log('xhr on error');
1485         
1486         var response = Roo.decode(xhr.responseText);
1487           
1488         Roo.log(response);
1489         
1490     },
1491     
1492     prepare : function(file)
1493     {   
1494         if(this.loadMask){
1495             this.maskEl.mask(this.loadingText);
1496         }
1497         
1498         this.file = false;
1499         this.exif = {};
1500         
1501         if(typeof(file) === 'string'){
1502             this.loadCanvas(file);
1503             return;
1504         }
1505         
1506         if(!file || !this.urlAPI){
1507             return;
1508         }
1509         
1510         this.file = file;
1511         if(typeof(file.type) != 'undefined' && file.type.length != 0) {
1512             this.cropType = file.type;
1513         }
1514         
1515         var _this = this;
1516         
1517         if(this.fireEvent('prepare', this, this.file) != false){
1518             
1519             var reader = new FileReader();
1520             
1521             reader.onload = function (e) {
1522                 if (e.target.error) {
1523                     Roo.log(e.target.error);
1524                     return;
1525                 }
1526                 
1527                 var buffer = e.target.result,
1528                     dataView = new DataView(buffer),
1529                     offset = 2,
1530                     maxOffset = dataView.byteLength - 4,
1531                     markerBytes,
1532                     markerLength;
1533                 
1534                 if (dataView.getUint16(0) === 0xffd8) {
1535                     while (offset < maxOffset) {
1536                         markerBytes = dataView.getUint16(offset);
1537                         
1538                         if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1539                             markerLength = dataView.getUint16(offset + 2) + 2;
1540                             if (offset + markerLength > dataView.byteLength) {
1541                                 Roo.log('Invalid meta data: Invalid segment size.');
1542                                 break;
1543                             }
1544                             
1545                             if(markerBytes == 0xffe1){
1546                                 _this.parseExifData(
1547                                     dataView,
1548                                     offset,
1549                                     markerLength
1550                                 );
1551                             }
1552                             
1553                             offset += markerLength;
1554                             
1555                             continue;
1556                         }
1557                         
1558                         break;
1559                     }
1560                     
1561                 }
1562                 
1563                 var url = _this.urlAPI.createObjectURL(_this.file);
1564                 
1565                 _this.loadCanvas(url);
1566                 
1567                 return;
1568             }
1569             
1570             reader.readAsArrayBuffer(this.file);
1571             
1572         }
1573         
1574     },
1575     
1576     parseExifData : function(dataView, offset, length)
1577     {
1578         var tiffOffset = offset + 10,
1579             littleEndian,
1580             dirOffset;
1581     
1582         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1583             // No Exif data, might be XMP data instead
1584             return;
1585         }
1586         
1587         // Check for the ASCII code for "Exif" (0x45786966):
1588         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1589             // No Exif data, might be XMP data instead
1590             return;
1591         }
1592         if (tiffOffset + 8 > dataView.byteLength) {
1593             Roo.log('Invalid Exif data: Invalid segment size.');
1594             return;
1595         }
1596         // Check for the two null bytes:
1597         if (dataView.getUint16(offset + 8) !== 0x0000) {
1598             Roo.log('Invalid Exif data: Missing byte alignment offset.');
1599             return;
1600         }
1601         // Check the byte alignment:
1602         switch (dataView.getUint16(tiffOffset)) {
1603         case 0x4949:
1604             littleEndian = true;
1605             break;
1606         case 0x4D4D:
1607             littleEndian = false;
1608             break;
1609         default:
1610             Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1611             return;
1612         }
1613         // Check for the TIFF tag marker (0x002A):
1614         if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1615             Roo.log('Invalid Exif data: Missing TIFF marker.');
1616             return;
1617         }
1618         // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1619         dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1620         
1621         this.parseExifTags(
1622             dataView,
1623             tiffOffset,
1624             tiffOffset + dirOffset,
1625             littleEndian
1626         );
1627     },
1628     
1629     parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1630     {
1631         var tagsNumber,
1632             dirEndOffset,
1633             i;
1634         if (dirOffset + 6 > dataView.byteLength) {
1635             Roo.log('Invalid Exif data: Invalid directory offset.');
1636             return;
1637         }
1638         tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1639         dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1640         if (dirEndOffset + 4 > dataView.byteLength) {
1641             Roo.log('Invalid Exif data: Invalid directory size.');
1642             return;
1643         }
1644         for (i = 0; i < tagsNumber; i += 1) {
1645             this.parseExifTag(
1646                 dataView,
1647                 tiffOffset,
1648                 dirOffset + 2 + 12 * i, // tag offset
1649                 littleEndian
1650             );
1651         }
1652         // Return the offset to the next directory:
1653         return dataView.getUint32(dirEndOffset, littleEndian);
1654     },
1655     
1656     parseExifTag : function (dataView, tiffOffset, offset, littleEndian) 
1657     {
1658         var tag = dataView.getUint16(offset, littleEndian);
1659         
1660         this.exif[tag] = this.getExifValue(
1661             dataView,
1662             tiffOffset,
1663             offset,
1664             dataView.getUint16(offset + 2, littleEndian), // tag type
1665             dataView.getUint32(offset + 4, littleEndian), // tag length
1666             littleEndian
1667         );
1668     },
1669     
1670     getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1671     {
1672         var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1673             tagSize,
1674             dataOffset,
1675             values,
1676             i,
1677             str,
1678             c;
1679     
1680         if (!tagType) {
1681             Roo.log('Invalid Exif data: Invalid tag type.');
1682             return;
1683         }
1684         
1685         tagSize = tagType.size * length;
1686         // Determine if the value is contained in the dataOffset bytes,
1687         // or if the value at the dataOffset is a pointer to the actual data:
1688         dataOffset = tagSize > 4 ?
1689                 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1690         if (dataOffset + tagSize > dataView.byteLength) {
1691             Roo.log('Invalid Exif data: Invalid data offset.');
1692             return;
1693         }
1694         if (length === 1) {
1695             return tagType.getValue(dataView, dataOffset, littleEndian);
1696         }
1697         values = [];
1698         for (i = 0; i < length; i += 1) {
1699             values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1700         }
1701         
1702         if (tagType.ascii) {
1703             str = '';
1704             // Concatenate the chars:
1705             for (i = 0; i < values.length; i += 1) {
1706                 c = values[i];
1707                 // Ignore the terminating NULL byte(s):
1708                 if (c === '\u0000') {
1709                     break;
1710                 }
1711                 str += c;
1712             }
1713             return str;
1714         }
1715         return values;
1716     }
1717     
1718 });
1719
1720 Roo.apply(Roo.dialog.UploadCropbox, {
1721     tags : {
1722         'Orientation': 0x0112
1723     },
1724     
1725     Orientation: {
1726             1: 0, //'top-left',
1727 //            2: 'top-right',
1728             3: 180, //'bottom-right',
1729 //            4: 'bottom-left',
1730 //            5: 'left-top',
1731             6: 90, //'right-top',
1732 //            7: 'right-bottom',
1733             8: 270 //'left-bottom'
1734     },
1735     
1736     exifTagTypes : {
1737         // byte, 8-bit unsigned int:
1738         1: {
1739             getValue: function (dataView, dataOffset) {
1740                 return dataView.getUint8(dataOffset);
1741             },
1742             size: 1
1743         },
1744         // ascii, 8-bit byte:
1745         2: {
1746             getValue: function (dataView, dataOffset) {
1747                 return String.fromCharCode(dataView.getUint8(dataOffset));
1748             },
1749             size: 1,
1750             ascii: true
1751         },
1752         // short, 16 bit int:
1753         3: {
1754             getValue: function (dataView, dataOffset, littleEndian) {
1755                 return dataView.getUint16(dataOffset, littleEndian);
1756             },
1757             size: 2
1758         },
1759         // long, 32 bit int:
1760         4: {
1761             getValue: function (dataView, dataOffset, littleEndian) {
1762                 return dataView.getUint32(dataOffset, littleEndian);
1763             },
1764             size: 4
1765         },
1766         // rational = two long values, first is numerator, second is denominator:
1767         5: {
1768             getValue: function (dataView, dataOffset, littleEndian) {
1769                 return dataView.getUint32(dataOffset, littleEndian) /
1770                     dataView.getUint32(dataOffset + 4, littleEndian);
1771             },
1772             size: 8
1773         },
1774         // slong, 32 bit signed int:
1775         9: {
1776             getValue: function (dataView, dataOffset, littleEndian) {
1777                 return dataView.getInt32(dataOffset, littleEndian);
1778             },
1779             size: 4
1780         },
1781         // srational, two slongs, first is numerator, second is denominator:
1782         10: {
1783             getValue: function (dataView, dataOffset, littleEndian) {
1784                 return dataView.getInt32(dataOffset, littleEndian) /
1785                     dataView.getInt32(dataOffset + 4, littleEndian);
1786             },
1787             size: 8
1788         }
1789     },
1790     
1791     footer : {
1792         STANDARD : [
1793             {
1794                 tag : 'div',
1795                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1796                 action : 'rotate-left',
1797                 cn : [
1798                     {
1799                         tag : 'button',
1800                         cls : 'btn btn-default',
1801                         html : '<i class="fa fa-undo"></i>'
1802                     }
1803                 ]
1804             },
1805             {
1806                 tag : 'div',
1807                 cls : 'btn-group roo-upload-cropbox-picture',
1808                 action : 'picture',
1809                 cn : [
1810                     {
1811                         tag : 'button',
1812                         cls : 'btn btn-default',
1813                         html : '<i class="fa fa-picture-o"></i>'
1814                     }
1815                 ]
1816             },
1817             {
1818                 tag : 'div',
1819                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1820                 action : 'rotate-right',
1821                 cn : [
1822                     {
1823                         tag : 'button',
1824                         cls : 'btn btn-default',
1825                         html : '<i class="fa fa-repeat"></i>'
1826                     }
1827                 ]
1828             }
1829         ],
1830         DOCUMENT : [
1831             {
1832                 tag : 'div',
1833                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1834                 action : 'rotate-left',
1835                 cn : [
1836                     {
1837                         tag : 'button',
1838                         cls : 'btn btn-default',
1839                         html : '<i class="fa fa-undo"></i>'
1840                     }
1841                 ]
1842             },
1843             {
1844                 tag : 'div',
1845                 cls : 'btn-group roo-upload-cropbox-download',
1846                 action : 'download',
1847                 cn : [
1848                     {
1849                         tag : 'button',
1850                         cls : 'btn btn-default',
1851                         html : '<i class="fa fa-download"></i>'
1852                     }
1853                 ]
1854             },
1855             {
1856                 tag : 'div',
1857                 cls : 'btn-group roo-upload-cropbox-crop',
1858                 action : 'crop',
1859                 cn : [
1860                     {
1861                         tag : 'button',
1862                         cls : 'btn btn-default',
1863                         html : '<i class="fa fa-crop"></i>'
1864                     }
1865                 ]
1866             },
1867             {
1868                 tag : 'div',
1869                 cls : 'btn-group roo-upload-cropbox-trash',
1870                 action : 'trash',
1871                 cn : [
1872                     {
1873                         tag : 'button',
1874                         cls : 'btn btn-default',
1875                         html : '<i class="fa fa-trash"></i>'
1876                     }
1877                 ]
1878             },
1879             {
1880                 tag : 'div',
1881                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1882                 action : 'rotate-right',
1883                 cn : [
1884                     {
1885                         tag : 'button',
1886                         cls : 'btn btn-default',
1887                         html : '<i class="fa fa-repeat"></i>'
1888                     }
1889                 ]
1890             }
1891         ],
1892         ROTATOR : [
1893             {
1894                 tag : 'div',
1895                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1896                 action : 'rotate-left',
1897                 cn : [
1898                     {
1899                         tag : 'button',
1900                         cls : 'btn btn-default',
1901                         html : '<i class="fa fa-undo"></i>'
1902                     }
1903                 ]
1904             },
1905             {
1906                 tag : 'div',
1907                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1908                 action : 'rotate-right',
1909                 cn : [
1910                     {
1911                         tag : 'button',
1912                         cls : 'btn btn-default',
1913                         html : '<i class="fa fa-repeat"></i>'
1914                     }
1915                 ]
1916             }
1917         ],
1918         CENTER : [
1919             {
1920                 tag : 'div',
1921                 cls : 'btn-group roo-upload-cropbox-center',
1922                 action : 'center',
1923                 cn : [
1924                     {
1925                         tag : 'button',
1926                         cls : 'btn btn-default',
1927                         html : 'CENTER'
1928                     }
1929                 ]
1930             }
1931         ]
1932     }
1933 });