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