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