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