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