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