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