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