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