Roo/dialog/UploadCropbox.js
[roojs1] / Roo / dialog / UploadCropbox.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.dialog.UploadCropbox
8  * @extends Roo.BoxComponent
9  * Dialog UploadCropbox class
10  * @cfg {String} emptyText show when image has been loaded
11  * @cfg {String} rotateNotify show when image too small to rotate
12  * @cfg {Number} errorTimeout default 3000
13  * @cfg {Number} minWidth default 300
14  * @cfg {Number} minHeight default 300
15  * @cfg {Number} outputMaxWidth default 1200
16  * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
17  * @cfg {Boolean} isDocument (true|false) default false
18  * @cfg {String} url action url
19  * @cfg {String} paramName default 'imageUpload'
20  * @cfg {String} method default POST
21  * @cfg {Boolean} loadMask (true|false) default true
22  * @cfg {Boolean} loadingText default 'Loading...'
23  * 
24  * @constructor
25  * Create a new UploadCropbox
26  * @param {Object} config The config object
27  */
28
29  Roo.dialog.UploadCropbox = function(config){
30     Roo.dialog.UploadCropbox.superclass.constructor.call(this, config);
31     
32     this.addEvents({
33         /**
34          * @event beforeselectfile
35          * Fire before select file
36          * @param {Roo.dialog.UploadCropbox} this
37          */
38         "beforeselectfile" : true,
39         /**
40          * @event initial
41          * Fire after initEvent
42          * @param {Roo.dialog.UploadCropbox} this
43          */
44         "initial" : true,
45         /**
46          * @event crop
47          * Fire after initEvent
48          * @param {Roo.dialog.UploadCropbox} this
49          * @param {String} data
50          */
51         "crop" : true,
52         /**
53          * @event prepare
54          * Fire when preparing the file data
55          * @param {Roo.dialog.UploadCropbox} this
56          * @param {Object} file
57          */
58         "prepare" : true,
59         /**
60          * @event exception
61          * Fire when get exception
62          * @param {Roo.dialog.UploadCropbox} this
63          * @param {XMLHttpRequest} xhr
64          */
65         "exception" : true,
66         /**
67          * @event beforeloadcanvas
68          * Fire before load the canvas
69          * @param {Roo.dialog.UploadCropbox} this
70          * @param {String} src
71          */
72         "beforeloadcanvas" : true,
73         /**
74          * @event trash
75          * Fire when trash image
76          * @param {Roo.dialog.UploadCropbox} this
77          */
78         "trash" : true,
79         /**
80          * @event download
81          * Fire when download the image
82          * @param {Roo.dialog.UploadCropbox} this
83          */
84         "download" : true,
85         /**
86          * @event footerbuttonclick
87          * Fire when footerbuttonclick
88          * @param {Roo.dialog.UploadCropbox} this
89          * @param {String} type
90          */
91         "footerbuttonclick" : true,
92         /**
93          * @event resize
94          * Fire when resize
95          * @param {Roo.dialog.UploadCropbox} this
96          */
97         "resize" : true,
98         /**
99          * @event rotate
100          * Fire when rotate the image
101          * @param {Roo.dialog.UploadCropbox} this
102          * @param {String} pos
103          */
104         "rotate" : true,
105         /**
106          * @event inspect
107          * Fire when inspect the file
108          * @param {Roo.dialog.UploadCropbox} this
109          * @param {Object} file
110          */
111         "inspect" : true,
112         /**
113          * @event beforeupload
114          * Fire before xhr upload the file
115          * @param {Roo.dialog.UploadCropbox} this
116          * @param {Object} formData
117          */
118          "beforeupload" : true,
119         /**
120          * @event upload
121          * Fire when xhr upload the file
122          * @param {Roo.dialog.UploadCropbox} this
123          * @param {Object} data
124          */
125         "upload" : true,
126         /**
127          * @event arrange
128          * Fire when arrange the file data
129          * @param {Roo.dialog.UploadCropbox} this
130          * @param {Object} formData
131          */
132         "arrange" : true,
133         /**
134          * @event loadcanvas
135          * Fire after load the canvas
136          * @param {Roo.dialog.UploadCropbox}
137          * @param {Object} imgEl
138          */
139         "loadcanvas" : true
140     });
141     
142     this.buttons = this.buttons || Roo.dialog.UploadCropbox.footer.STANDARD;
143 };
144
145 Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
146     
147     emptyText : 'Click to upload image',
148     rotateNotify : 'Image is too small to rotate',
149     errorTimeout : 3000,
150     scale : 0,
151     baseScale : 1,
152     rotate : 0,
153     dragable : false,
154     pinching : false,
155     mouseX : 0,
156     mouseY : 0,
157     cropData : false,
158     minWidth : 300,
159     minHeight : 300,
160     outputMaxWidth : 1200,
161     file : false,
162     exif : {},
163     baseRotate : 1,
164     cropType : 'image/jpeg',
165     buttons : false,
166     canvasLoaded : false,
167     isDocument : false,
168     method : 'POST',
169     paramName : 'imageUpload',
170     loadMask : true,
171     loadingText : 'Loading...',
172     maskEl : false,
173     
174     getAutoCreate : function()
175     {
176         var cfg = {
177             tag : 'div',
178             cls : 'roo-upload-cropbox',
179             cn : [
180                 {
181                     tag : 'input',
182                     cls : 'roo-upload-cropbox-selector',
183                     type : 'file'
184                 },
185                 {
186                     tag : 'div',
187                     cls : 'roo-upload-cropbox-body',
188                     style : 'cursor:pointer',
189                     cn : [
190                         {
191                             tag : 'div',
192                             cls : 'roo-upload-cropbox-preview'
193                         },
194                         {
195                             tag : 'div',
196                             cls : 'roo-upload-cropbox-thumb'
197                         },
198                         {
199                             tag : 'div',
200                             cls : 'roo-upload-cropbox-empty-notify',
201                             html : this.emptyText
202                         },
203                         {
204                             tag : 'div',
205                             cls : 'roo-upload-cropbox-error-notify alert alert-danger',
206                             html : this.rotateNotify
207                         }
208                     ]
209                 },
210                 {
211                     tag : 'div',
212                     cls : 'roo-upload-cropbox-footer',
213                     cn : {
214                         tag : 'div',
215                         cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
216                         cn : []
217                     }
218                 }
219             ]
220         };
221         
222         return cfg;
223     },
224     
225     onRender : function(ct, position)
226     {
227         Roo.dialog.UploadCropbox.superclass.onRender.call(this, ct, position);
228
229         if(this.el){
230             if (this.el.attr('xtype')) {
231                 this.el.attr('xtypex', this.el.attr('xtype'));
232                 this.el.dom.removeAttribute('xtype');
233                 
234                 this.initEvents();
235             }
236         }
237         else {
238             var cfg = Roo.apply({},  this.getAutoCreate());
239         
240             cfg.id = this.id || Roo.id();
241             
242             if (this.cls) {
243                 cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
244             }
245             
246             if (this.style) { // fixme needs to support more complex style data.
247                 cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
248             }
249             
250             this.el = ct.createChild(cfg, position);
251             
252             this.initEvents();
253         }
254         
255         if (this.buttons.length) {
256             
257             Roo.each(this.buttons, function(bb) {
258                 
259                 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
260                 
261                 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
262                 
263             }, this);
264         }
265         
266         if(this.loadMask){
267             this.maskEl = this.el;
268         }
269     },
270     
271     initEvents : function()
272     {
273         this.urlAPI = (window.createObjectURL && window) || 
274                                 (window.URL && URL.revokeObjectURL && URL) || 
275                                 (window.webkitURL && webkitURL);
276                         
277         this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
278         this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
279         
280         this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
281         this.selectorEl.hide();
282         
283         this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
284         this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
285         
286         this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
287         this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
288         this.thumbEl.hide();
289         
290         this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
291         this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
292         
293         this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
294         this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
295         this.errorEl.hide();
296         
297         this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
298         this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
299         this.footerEl.hide();
300         
301         this.setThumbBoxSize();
302         
303         this.bind();
304         
305         this.resize();
306         
307         this.fireEvent('initial', this);
308     },
309
310     bind : function()
311     {
312         var _this = this;
313         
314         window.addEventListener("resize", function() { _this.resize(); } );
315         
316         this.bodyEl.on('click', this.beforeSelectFile, this);
317         
318         if(Roo.isTouch){
319             this.bodyEl.on('touchstart', this.onTouchStart, this);
320             this.bodyEl.on('touchmove', this.onTouchMove, this);
321             this.bodyEl.on('touchend', this.onTouchEnd, this);
322         }
323         
324         if(!Roo.isTouch){
325             this.bodyEl.on('mousedown', this.onMouseDown, this);
326             this.bodyEl.on('mousemove', this.onMouseMove, this);
327             var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
328             this.bodyEl.on(mousewheel, this.onMouseWheel, this);
329             Roo.get(document).on('mouseup', this.onMouseUp, this);
330         }
331         
332         this.selectorEl.on('change', this.onFileSelected, this);
333     },
334     
335     reset : function()
336     {    
337         this.scale = 0;
338         this.baseScale = 1;
339         this.rotate = 0;
340         this.baseRotate = 1;
341         this.dragable = false;
342         this.pinching = false;
343         this.mouseX = 0;
344         this.mouseY = 0;
345         this.cropData = false;
346         this.notifyEl.dom.innerHTML = this.emptyText;
347         
348         // this.selectorEl.dom.value = '';
349         
350     },
351     
352     resize : function()
353     {
354         if(this.fireEvent('resize', this) != false){
355             this.setThumbBoxPosition();
356             this.setCanvasPosition();
357         }
358     },
359     
360     onFooterButtonClick : function(e, el, o, type)
361     {
362         switch (type) {
363             case 'rotate-left' :
364                 this.onRotateLeft(e);
365                 break;
366             case 'rotate-right' :
367                 this.onRotateRight(e);
368                 break;
369             case 'picture' :
370                 this.beforeSelectFile(e);
371                 break;
372             case 'trash' :
373                 this.trash(e);
374                 break;
375             case 'crop' :
376                 this.crop(e);
377                 break;
378             case 'download' :
379                 this.download(e);
380                 break;
381             default :
382                 break;
383         }
384         
385         this.fireEvent('footerbuttonclick', this, type);
386     },
387     
388     beforeSelectFile : function(e)
389     {
390         e.preventDefault();
391         
392         if(this.fireEvent('beforeselectfile', this) != false){
393             this.selectorEl.dom.click();
394         }
395     },
396     
397     onFileSelected : function(e)
398     {
399         e.preventDefault();
400         
401         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
402             return;
403         }
404         
405         var file = this.selectorEl.dom.files[0];
406         
407         if(this.fireEvent('inspect', this, file) != false){
408             this.prepare(file);
409         }
410         
411     },
412     
413     trash : function(e)
414     {
415         this.fireEvent('trash', this);
416     },
417     
418     download : function(e)
419     {
420         this.fireEvent('download', this);
421     },
422     
423     loadCanvas : function(src)
424     {   
425         if(this.fireEvent('beforeloadcanvas', this, src) != false){
426             
427             this.reset();
428             
429             this.imageEl = document.createElement('img');
430             
431             var _this = this;
432             
433             this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
434             
435             this.imageEl.src = src;
436         }
437     },
438     
439     onLoadCanvas : function()
440     {   
441         this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
442         this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
443
444         if(this.fireEvent('loadcanvas', this, this.imageEl) != false){
445         
446             this.bodyEl.un('click', this.beforeSelectFile, this);
447             
448             this.notifyEl.hide();
449             this.thumbEl.show();
450             this.footerEl.show();
451             
452             this.baseRotateLevel();
453             
454             if(this.isDocument){
455                 this.setThumbBoxSize();
456             }
457             
458             this.setThumbBoxPosition();
459             
460             this.baseScaleLevel();
461             
462             this.draw();
463             
464             this.resize();
465             
466             this.canvasLoaded = true;
467         
468         }
469         
470         if(this.loadMask){
471             this.maskEl.unmask();
472         }
473         
474     },
475     
476     setCanvasPosition : function()
477     {   
478         if(!this.canvasEl){
479             return;
480         }
481         
482         var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
483         var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
484         
485         this.previewEl.setLeft(pw);
486         this.previewEl.setTop(ph);
487         
488     },
489     
490     onMouseDown : function(e)
491     {   
492         e.stopEvent();
493         
494         this.dragable = true;
495         this.pinching = false;
496         
497         if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
498             this.dragable = false;
499             return;
500         }
501         
502         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
503         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
504         
505     },
506     
507     onMouseMove : function(e)
508     {   
509         e.stopEvent();
510         
511         if(!this.canvasLoaded){
512             return;
513         }
514         
515         if (!this.dragable){
516             return;
517         }
518         
519         var minX = Math.ceil(this.thumbEl.getLeft(true));
520         var minY = Math.ceil(this.thumbEl.getTop(true));
521         
522         var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
523         var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
524
525         if(minX > maxX) {
526             var tempX = minX;
527             minX = maxX;
528             maxX = tempX;
529         }
530
531         if(minY > maxY) {
532             var tempY = minY;
533             minY = maxY;
534             maxY = tempY;
535         }
536
537         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
538         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
539         
540         x = x - this.mouseX;
541         y = y - this.mouseY;
542
543         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
544         var bgY = Math.ceil(y + this.previewEl.getTop(true));
545         
546         bgX = (bgX < minX) ? minX : ((bgX > maxX) ? maxX : bgX);
547         bgY = (bgY < minY) ? minY : ((bgY > maxY) ? maxY : bgY);
548         
549         this.previewEl.setLeft(bgX);
550         this.previewEl.setTop(bgY);
551         
552         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
553         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
554     },
555     
556     onMouseUp : function(e)
557     {   
558         e.stopEvent();
559         
560         this.dragable = false;
561     },
562     
563     onMouseWheel : function(e)
564     {   
565         e.stopEvent();
566         
567         this.startScale = this.scale;
568         this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
569         
570         if(!this.zoomable()){
571             this.scale = this.startScale;
572             return;
573         }
574
575         
576         this.draw();
577         
578         return;
579     },
580     
581     zoomable : function()
582     {
583         var minScale = this.thumbEl.getWidth() / this.minWidth;
584         
585         if(this.minWidth < this.minHeight){
586             minScale = this.thumbEl.getHeight() / this.minHeight;
587         }
588         
589         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
590         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
591
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 = 300;
1064         width = Math.ceil(this.minWidth * height / this.minHeight);
1065         
1066         if(this.minWidth > this.minHeight){
1067             width = 300;
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("onid", "43024");
1317         formData.append("ontable", "pressrelease_boilerplate");
1318
1319         formData.append('returnHTML', 'NO');
1320         
1321         if(crop){
1322             formData.append('crop', crop);
1323             var blobBin = atob(crop.split(',')[1]);
1324             var array = [];
1325             for(var i = 0; i < blobBin.length; i++) {
1326                 array.push(blobBin.charCodeAt(i));
1327             }
1328             var croppedFile =new Blob([new Uint8Array(array)], {type: 'image/png'});
1329             formData.append(this.paramName, croppedFile, file.name);
1330         }
1331         
1332         if(typeof(file.filename) != 'undefined'){
1333             formData.append('filename', file.filename);
1334         }
1335         
1336         if(typeof(file.mimetype) != 'undefined'){
1337             formData.append('mimetype', file.mimetype);
1338         }
1339
1340         if(this.fireEvent('arrange', this, formData) != false){
1341             this.xhr.send(formData);
1342         };
1343     },
1344     
1345     xhrOnLoad : function(xhr)
1346     {
1347         if(this.loadMask){
1348             this.maskEl.unmask();
1349         }
1350         
1351         if (xhr.readyState !== 4) {
1352             this.fireEvent('exception', this, xhr);
1353             return;
1354         }
1355
1356         var response = Roo.decode(xhr.responseText);
1357         
1358         if(!response.success){
1359             this.fireEvent('exception', this, xhr);
1360             return;
1361         }
1362         
1363         var response = Roo.decode(xhr.responseText);
1364         
1365         this.fireEvent('upload', this, response);
1366         
1367     },
1368     
1369     xhrOnError : function()
1370     {
1371         if(this.loadMask){
1372             this.maskEl.unmask();
1373         }
1374         
1375         Roo.log('xhr on error');
1376         
1377         var response = Roo.decode(xhr.responseText);
1378           
1379         Roo.log(response);
1380         
1381     },
1382     
1383     prepare : function(file)
1384     {   
1385         if(this.loadMask){
1386             this.maskEl.mask(this.loadingText);
1387         }
1388         
1389         this.file = false;
1390         this.exif = {};
1391         
1392         if(typeof(file) === 'string'){
1393             this.loadCanvas(file);
1394             return;
1395         }
1396         
1397         if(!file || !this.urlAPI){
1398             return;
1399         }
1400         
1401         this.file = file;
1402         this.cropType = file.type;
1403         
1404         var _this = this;
1405         
1406         if(this.fireEvent('prepare', this, this.file) != false){
1407             
1408             var reader = new FileReader();
1409             
1410             reader.onload = function (e) {
1411                 if (e.target.error) {
1412                     Roo.log(e.target.error);
1413                     return;
1414                 }
1415                 
1416                 var buffer = e.target.result,
1417                     dataView = new DataView(buffer),
1418                     offset = 2,
1419                     maxOffset = dataView.byteLength - 4,
1420                     markerBytes,
1421                     markerLength;
1422                 
1423                 if (dataView.getUint16(0) === 0xffd8) {
1424                     while (offset < maxOffset) {
1425                         markerBytes = dataView.getUint16(offset);
1426                         
1427                         if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1428                             markerLength = dataView.getUint16(offset + 2) + 2;
1429                             if (offset + markerLength > dataView.byteLength) {
1430                                 Roo.log('Invalid meta data: Invalid segment size.');
1431                                 break;
1432                             }
1433                             
1434                             if(markerBytes == 0xffe1){
1435                                 _this.parseExifData(
1436                                     dataView,
1437                                     offset,
1438                                     markerLength
1439                                 );
1440                             }
1441                             
1442                             offset += markerLength;
1443                             
1444                             continue;
1445                         }
1446                         
1447                         break;
1448                     }
1449                     
1450                 }
1451                 
1452                 var url = _this.urlAPI.createObjectURL(_this.file);
1453                 
1454                 _this.loadCanvas(url);
1455                 
1456                 return;
1457             }
1458             
1459             reader.readAsArrayBuffer(this.file);
1460             
1461         }
1462         
1463     },
1464     
1465     parseExifData : function(dataView, offset, length)
1466     {
1467         var tiffOffset = offset + 10,
1468             littleEndian,
1469             dirOffset;
1470     
1471         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1472             // No Exif data, might be XMP data instead
1473             return;
1474         }
1475         
1476         // Check for the ASCII code for "Exif" (0x45786966):
1477         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1478             // No Exif data, might be XMP data instead
1479             return;
1480         }
1481         if (tiffOffset + 8 > dataView.byteLength) {
1482             Roo.log('Invalid Exif data: Invalid segment size.');
1483             return;
1484         }
1485         // Check for the two null bytes:
1486         if (dataView.getUint16(offset + 8) !== 0x0000) {
1487             Roo.log('Invalid Exif data: Missing byte alignment offset.');
1488             return;
1489         }
1490         // Check the byte alignment:
1491         switch (dataView.getUint16(tiffOffset)) {
1492         case 0x4949:
1493             littleEndian = true;
1494             break;
1495         case 0x4D4D:
1496             littleEndian = false;
1497             break;
1498         default:
1499             Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1500             return;
1501         }
1502         // Check for the TIFF tag marker (0x002A):
1503         if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1504             Roo.log('Invalid Exif data: Missing TIFF marker.');
1505             return;
1506         }
1507         // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1508         dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1509         
1510         this.parseExifTags(
1511             dataView,
1512             tiffOffset,
1513             tiffOffset + dirOffset,
1514             littleEndian
1515         );
1516     },
1517     
1518     parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1519     {
1520         var tagsNumber,
1521             dirEndOffset,
1522             i;
1523         if (dirOffset + 6 > dataView.byteLength) {
1524             Roo.log('Invalid Exif data: Invalid directory offset.');
1525             return;
1526         }
1527         tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1528         dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1529         if (dirEndOffset + 4 > dataView.byteLength) {
1530             Roo.log('Invalid Exif data: Invalid directory size.');
1531             return;
1532         }
1533         for (i = 0; i < tagsNumber; i += 1) {
1534             this.parseExifTag(
1535                 dataView,
1536                 tiffOffset,
1537                 dirOffset + 2 + 12 * i, // tag offset
1538                 littleEndian
1539             );
1540         }
1541         // Return the offset to the next directory:
1542         return dataView.getUint32(dirEndOffset, littleEndian);
1543     },
1544     
1545     parseExifTag : function (dataView, tiffOffset, offset, littleEndian) 
1546     {
1547         var tag = dataView.getUint16(offset, littleEndian);
1548         
1549         this.exif[tag] = this.getExifValue(
1550             dataView,
1551             tiffOffset,
1552             offset,
1553             dataView.getUint16(offset + 2, littleEndian), // tag type
1554             dataView.getUint32(offset + 4, littleEndian), // tag length
1555             littleEndian
1556         );
1557     },
1558     
1559     getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1560     {
1561         var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1562             tagSize,
1563             dataOffset,
1564             values,
1565             i,
1566             str,
1567             c;
1568     
1569         if (!tagType) {
1570             Roo.log('Invalid Exif data: Invalid tag type.');
1571             return;
1572         }
1573         
1574         tagSize = tagType.size * length;
1575         // Determine if the value is contained in the dataOffset bytes,
1576         // or if the value at the dataOffset is a pointer to the actual data:
1577         dataOffset = tagSize > 4 ?
1578                 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1579         if (dataOffset + tagSize > dataView.byteLength) {
1580             Roo.log('Invalid Exif data: Invalid data offset.');
1581             return;
1582         }
1583         if (length === 1) {
1584             return tagType.getValue(dataView, dataOffset, littleEndian);
1585         }
1586         values = [];
1587         for (i = 0; i < length; i += 1) {
1588             values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1589         }
1590         
1591         if (tagType.ascii) {
1592             str = '';
1593             // Concatenate the chars:
1594             for (i = 0; i < values.length; i += 1) {
1595                 c = values[i];
1596                 // Ignore the terminating NULL byte(s):
1597                 if (c === '\u0000') {
1598                     break;
1599                 }
1600                 str += c;
1601             }
1602             return str;
1603         }
1604         return values;
1605     }
1606     
1607 });
1608
1609 Roo.apply(Roo.dialog.UploadCropbox, {
1610     tags : {
1611         'Orientation': 0x0112
1612     },
1613     
1614     Orientation: {
1615             1: 0, //'top-left',
1616 //            2: 'top-right',
1617             3: 180, //'bottom-right',
1618 //            4: 'bottom-left',
1619 //            5: 'left-top',
1620             6: 90, //'right-top',
1621 //            7: 'right-bottom',
1622             8: 270 //'left-bottom'
1623     },
1624     
1625     exifTagTypes : {
1626         // byte, 8-bit unsigned int:
1627         1: {
1628             getValue: function (dataView, dataOffset) {
1629                 return dataView.getUint8(dataOffset);
1630             },
1631             size: 1
1632         },
1633         // ascii, 8-bit byte:
1634         2: {
1635             getValue: function (dataView, dataOffset) {
1636                 return String.fromCharCode(dataView.getUint8(dataOffset));
1637             },
1638             size: 1,
1639             ascii: true
1640         },
1641         // short, 16 bit int:
1642         3: {
1643             getValue: function (dataView, dataOffset, littleEndian) {
1644                 return dataView.getUint16(dataOffset, littleEndian);
1645             },
1646             size: 2
1647         },
1648         // long, 32 bit int:
1649         4: {
1650             getValue: function (dataView, dataOffset, littleEndian) {
1651                 return dataView.getUint32(dataOffset, littleEndian);
1652             },
1653             size: 4
1654         },
1655         // rational = two long values, first is numerator, second is denominator:
1656         5: {
1657             getValue: function (dataView, dataOffset, littleEndian) {
1658                 return dataView.getUint32(dataOffset, littleEndian) /
1659                     dataView.getUint32(dataOffset + 4, littleEndian);
1660             },
1661             size: 8
1662         },
1663         // slong, 32 bit signed int:
1664         9: {
1665             getValue: function (dataView, dataOffset, littleEndian) {
1666                 return dataView.getInt32(dataOffset, littleEndian);
1667             },
1668             size: 4
1669         },
1670         // srational, two slongs, first is numerator, second is denominator:
1671         10: {
1672             getValue: function (dataView, dataOffset, littleEndian) {
1673                 return dataView.getInt32(dataOffset, littleEndian) /
1674                     dataView.getInt32(dataOffset + 4, littleEndian);
1675             },
1676             size: 8
1677         }
1678     },
1679     
1680     footer : {
1681         STANDARD : [
1682             {
1683                 tag : 'div',
1684                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1685                 action : 'rotate-left',
1686                 cn : [
1687                     {
1688                         tag : 'button',
1689                         cls : 'btn btn-default',
1690                         html : '<i class="fa fa-undo"></i>'
1691                     }
1692                 ]
1693             },
1694             {
1695                 tag : 'div',
1696                 cls : 'btn-group roo-upload-cropbox-picture',
1697                 action : 'picture',
1698                 cn : [
1699                     {
1700                         tag : 'button',
1701                         cls : 'btn btn-default',
1702                         html : '<i class="fa fa-picture-o"></i>'
1703                     }
1704                 ]
1705             },
1706             {
1707                 tag : 'div',
1708                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1709                 action : 'rotate-right',
1710                 cn : [
1711                     {
1712                         tag : 'button',
1713                         cls : 'btn btn-default',
1714                         html : '<i class="fa fa-repeat"></i>'
1715                     }
1716                 ]
1717             }
1718         ],
1719         DOCUMENT : [
1720             {
1721                 tag : 'div',
1722                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1723                 action : 'rotate-left',
1724                 cn : [
1725                     {
1726                         tag : 'button',
1727                         cls : 'btn btn-default',
1728                         html : '<i class="fa fa-undo"></i>'
1729                     }
1730                 ]
1731             },
1732             {
1733                 tag : 'div',
1734                 cls : 'btn-group roo-upload-cropbox-download',
1735                 action : 'download',
1736                 cn : [
1737                     {
1738                         tag : 'button',
1739                         cls : 'btn btn-default',
1740                         html : '<i class="fa fa-download"></i>'
1741                     }
1742                 ]
1743             },
1744             {
1745                 tag : 'div',
1746                 cls : 'btn-group roo-upload-cropbox-crop',
1747                 action : 'crop',
1748                 cn : [
1749                     {
1750                         tag : 'button',
1751                         cls : 'btn btn-default',
1752                         html : '<i class="fa fa-crop"></i>'
1753                     }
1754                 ]
1755             },
1756             {
1757                 tag : 'div',
1758                 cls : 'btn-group roo-upload-cropbox-trash',
1759                 action : 'trash',
1760                 cn : [
1761                     {
1762                         tag : 'button',
1763                         cls : 'btn btn-default',
1764                         html : '<i class="fa fa-trash"></i>'
1765                     }
1766                 ]
1767             },
1768             {
1769                 tag : 'div',
1770                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1771                 action : 'rotate-right',
1772                 cn : [
1773                     {
1774                         tag : 'button',
1775                         cls : 'btn btn-default',
1776                         html : '<i class="fa fa-repeat"></i>'
1777                     }
1778                 ]
1779             }
1780         ],
1781         ROTATOR : [
1782             {
1783                 tag : 'div',
1784                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1785                 action : 'rotate-left',
1786                 cn : [
1787                     {
1788                         tag : 'button',
1789                         cls : 'btn btn-default',
1790                         html : '<i class="fa fa-undo"></i>'
1791                     }
1792                 ]
1793             },
1794             {
1795                 tag : 'div',
1796                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1797                 action : 'rotate-right',
1798                 cn : [
1799                     {
1800                         tag : 'button',
1801                         cls : 'btn btn-default',
1802                         html : '<i class="fa fa-repeat"></i>'
1803                     }
1804                 ]
1805             }
1806         ]
1807     }
1808 });