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