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