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