Roo/dialog/UploadCropbox.js
[roojs1] / Roo / dialog / UploadCropbox.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.dialog.UploadCropbox
8  * @extends Roo.BoxComponent
9  * Dialog UploadCropbox class
10  * @cfg {String} emptyText show when image has been loaded
11  * @cfg {String} rotateNotify show when image too small to rotate
12  * @cfg {Number} errorTimeout default 3000
13  * @cfg {Number} minWidth default 300
14  * @cfg {Number} minHeight default 300
15  * @cfg {Number} outputMaxWidth default 1200
16  * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
17  * @cfg {Boolean} isDocument (true|false) default false
18  * @cfg {String} url action url
19  * @cfg {String} paramName default 'imageUpload'
20  * @cfg {String} method default POST
21  * @cfg {Boolean} loadMask (true|false) default true
22  * @cfg {Boolean} loadingText default 'Loading...'
23  * 
24  * @constructor
25  * Create a new UploadCropbox
26  * @param {Object} config The config object
27  */
28
29  Roo.dialog.UploadCropbox = function(config){
30     Roo.dialog.UploadCropbox.superclass.constructor.call(this, config);
31     
32     this.addEvents({
33         /**
34          * @event beforeselectfile
35          * Fire before select file
36          * @param {Roo.dialog.UploadCropbox} this
37          */
38         "beforeselectfile" : true,
39         /**
40          * @event initial
41          * Fire after initEvent
42          * @param {Roo.dialog.UploadCropbox} this
43          */
44         "initial" : true,
45         /**
46          * @event crop
47          * Fire after initEvent
48          * @param {Roo.dialog.UploadCropbox} this
49          * @param {String} data
50          */
51         "crop" : true,
52         /**
53          * @event prepare
54          * Fire when preparing the file data
55          * @param {Roo.dialog.UploadCropbox} this
56          * @param {Object} file
57          */
58         "prepare" : true,
59         /**
60          * @event exception
61          * Fire when get exception
62          * @param {Roo.dialog.UploadCropbox} this
63          * @param {XMLHttpRequest} xhr
64          */
65         "exception" : true,
66         /**
67          * @event beforeloadcanvas
68          * Fire before load the canvas
69          * @param {Roo.dialog.UploadCropbox} this
70          * @param {String} src
71          */
72         "beforeloadcanvas" : true,
73         /**
74          * @event trash
75          * Fire when trash image
76          * @param {Roo.dialog.UploadCropbox} this
77          */
78         "trash" : true,
79         /**
80          * @event download
81          * Fire when download the image
82          * @param {Roo.dialog.UploadCropbox} this
83          */
84         "download" : true,
85         /**
86          * @event footerbuttonclick
87          * Fire when footerbuttonclick
88          * @param {Roo.dialog.UploadCropbox} this
89          * @param {String} type
90          */
91         "footerbuttonclick" : true,
92         /**
93          * @event resize
94          * Fire when resize
95          * @param {Roo.dialog.UploadCropbox} this
96          */
97         "resize" : true,
98         /**
99          * @event rotate
100          * Fire when rotate the image
101          * @param {Roo.dialog.UploadCropbox} this
102          * @param {String} pos
103          */
104         "rotate" : true,
105         /**
106          * @event inspect
107          * Fire when inspect the file
108          * @param {Roo.dialog.UploadCropbox} this
109          * @param {Object} file
110          */
111         "inspect" : true,
112         /**
113          * @event 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          * @event loadcanvas
128          * Fire after load the canvas
129          * @param {Roo.dialog.UploadCropbox}
130          * @param {Object} imgEl
131          */
132         "loadcanvas" : true
133     });
134     
135     this.buttons = this.buttons || Roo.dialog.UploadCropbox.footer.STANDARD;
136 };
137
138 Roo.extend(Roo.dialog.UploadCropbox, Roo.Component,  {
139     
140     emptyText : 'Click to upload image',
141     rotateNotify : 'Image is too small to rotate',
142     errorTimeout : 3000,
143     scale : 0,
144     baseScale : 1,
145     rotate : 0,
146     dragable : false,
147     pinching : false,
148     mouseX : 0,
149     mouseY : 0,
150     cropData : false,
151     minWidth : 300,
152     minHeight : 300,
153     outputMaxWidth : 1200,
154     file : false,
155     exif : {},
156     baseRotate : 1,
157     cropType : 'image/jpeg',
158     buttons : false,
159     canvasLoaded : false,
160     isDocument : false,
161     method : 'POST',
162     paramName : 'imageUpload',
163     loadMask : true,
164     loadingText : 'Loading...',
165     maskEl : false,
166     
167     getAutoCreate : function()
168     {
169         var cfg = {
170             tag : 'div',
171             cls : 'roo-upload-cropbox',
172             cn : [
173                 {
174                     tag : 'input',
175                     cls : 'roo-upload-cropbox-selector',
176                     type : 'file'
177                 },
178                 {
179                     tag : 'div',
180                     cls : 'roo-upload-cropbox-body',
181                     style : 'cursor:pointer',
182                     cn : [
183                         {
184                             tag : 'div',
185                             cls : 'roo-upload-cropbox-preview'
186                         },
187                         {
188                             tag : 'div',
189                             cls : 'roo-upload-cropbox-thumb'
190                         },
191                         {
192                             tag : 'div',
193                             cls : 'roo-upload-cropbox-empty-notify',
194                             html : this.emptyText
195                         },
196                         {
197                             tag : 'div',
198                             cls : 'roo-upload-cropbox-error-notify alert alert-danger',
199                             html : this.rotateNotify
200                         }
201                     ]
202                 },
203                 {
204                     tag : 'div',
205                     cls : 'roo-upload-cropbox-footer',
206                     cn : {
207                         tag : 'div',
208                         cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
209                         cn : []
210                     }
211                 }
212             ]
213         };
214         
215         return cfg;
216     },
217     
218     onRender : function(ct, position)
219     {
220         Roo.dialog.UploadCropbox.superclass.onRender.call(this, ct, position);
221
222         if(this.el){
223             if (this.el.attr('xtype')) {
224                 this.el.attr('xtypex', this.el.attr('xtype'));
225                 this.el.dom.removeAttribute('xtype');
226                 
227                 this.initEvents();
228             }
229         }
230         else {
231             var cfg = Roo.apply({},  this.getAutoCreate());
232         
233             cfg.id = this.id || Roo.id();
234             
235             if (this.cls) {
236                 cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
237             }
238             
239             if (this.style) { // fixme needs to support more complex style data.
240                 cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
241             }
242             
243             this.el = ct.createChild(cfg, position);
244             
245             this.initEvents();
246         }
247         
248         if (this.buttons.length) {
249             
250             Roo.each(this.buttons, function(bb) {
251                 
252                 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
253                 
254                 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
255                 
256             }, this);
257         }
258         
259         if(this.loadMask){
260             this.maskEl = this.el;
261         }
262     },
263     
264     initEvents : function()
265     {
266         this.urlAPI = (window.createObjectURL && window) || 
267                                 (window.URL && URL.revokeObjectURL && URL) || 
268                                 (window.webkitURL && webkitURL);
269                         
270         this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
271         this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
272         
273         this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
274         this.selectorEl.hide();
275         
276         this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
277         this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
278         
279         this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
280         this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
281         this.thumbEl.hide();
282         
283         this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
284         this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
285         
286         this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
287         this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
288         this.errorEl.hide();
289         
290         this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
291         this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
292         this.footerEl.hide();
293         
294         this.setThumbBoxSize();
295         
296         this.bind();
297         
298         this.resize();
299         
300         this.fireEvent('initial', this);
301     },
302
303     bind : function()
304     {
305         var _this = this;
306         
307         window.addEventListener("resize", function() { _this.resize(); } );
308         
309         this.bodyEl.on('click', this.beforeSelectFile, this);
310         
311         if(Roo.isTouch){
312             this.bodyEl.on('touchstart', this.onTouchStart, this);
313             this.bodyEl.on('touchmove', this.onTouchMove, this);
314             this.bodyEl.on('touchend', this.onTouchEnd, this);
315         }
316         
317         if(!Roo.isTouch){
318             this.bodyEl.on('mousedown', this.onMouseDown, this);
319             this.bodyEl.on('mousemove', this.onMouseMove, this);
320             var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
321             this.bodyEl.on(mousewheel, this.onMouseWheel, this);
322             Roo.get(document).on('mouseup', this.onMouseUp, this);
323         }
324         
325         this.selectorEl.on('change', this.onFileSelected, this);
326     },
327     
328     reset : function()
329     {    
330         this.scale = 0;
331         this.baseScale = 1;
332         this.rotate = 0;
333         this.baseRotate = 1;
334         this.dragable = false;
335         this.pinching = false;
336         this.mouseX = 0;
337         this.mouseY = 0;
338         this.cropData = false;
339         this.notifyEl.dom.innerHTML = this.emptyText;
340         
341         // this.selectorEl.dom.value = '';
342         
343     },
344     
345     resize : function()
346     {
347         if(this.fireEvent('resize', this) != false){
348             this.setThumbBoxPosition();
349             this.setCanvasPosition();
350         }
351     },
352     
353     onFooterButtonClick : function(e, el, o, type)
354     {
355         switch (type) {
356             case 'rotate-left' :
357                 this.onRotateLeft(e);
358                 break;
359             case 'rotate-right' :
360                 this.onRotateRight(e);
361                 break;
362             case 'picture' :
363                 this.beforeSelectFile(e);
364                 break;
365             case 'trash' :
366                 this.trash(e);
367                 break;
368             case 'crop' :
369                 this.crop(e);
370                 break;
371             case 'download' :
372                 this.download(e);
373                 break;
374             default :
375                 break;
376         }
377         
378         this.fireEvent('footerbuttonclick', this, type);
379     },
380     
381     beforeSelectFile : function(e)
382     {
383         e.preventDefault();
384         
385         if(this.fireEvent('beforeselectfile', this) != false){
386             this.selectorEl.dom.click();
387         }
388     },
389     
390     onFileSelected : function(e)
391     {
392         e.preventDefault();
393         
394         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
395             return;
396         }
397         
398         var file = this.selectorEl.dom.files[0];
399         
400         if(this.fireEvent('inspect', this, file) != false){
401             this.prepare(file);
402         }
403         
404     },
405     
406     trash : function(e)
407     {
408         this.fireEvent('trash', this);
409     },
410     
411     download : function(e)
412     {
413         this.fireEvent('download', this);
414     },
415     
416     loadCanvas : function(src)
417     {   
418         if(this.fireEvent('beforeloadcanvas', this, src) != false){
419             
420             this.reset();
421             
422             this.imageEl = document.createElement('img');
423             
424             var _this = this;
425             
426             this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
427             
428             this.imageEl.src = src;
429         }
430     },
431     
432     onLoadCanvas : function()
433     {   
434         this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
435         this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
436
437         if(this.fireEvent('loadcanvas', this, this.imageEl) != false){
438         
439             this.bodyEl.un('click', this.beforeSelectFile, this);
440             
441             this.notifyEl.hide();
442             this.thumbEl.show();
443             this.footerEl.show();
444             
445             this.baseRotateLevel();
446             
447             if(this.isDocument){
448                 this.setThumbBoxSize();
449             }
450             
451             this.setThumbBoxPosition();
452             
453             this.baseScaleLevel();
454             
455             this.draw();
456             
457             this.resize();
458             
459             this.canvasLoaded = true;
460         
461         }
462         
463         if(this.loadMask){
464             this.maskEl.unmask();
465         }
466         
467     },
468     
469     setCanvasPosition : function()
470     {   
471         if(!this.canvasEl){
472             return;
473         }
474         
475         var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
476         var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
477         
478         this.previewEl.setLeft(pw);
479         this.previewEl.setTop(ph);
480         
481     },
482     
483     onMouseDown : function(e)
484     {   
485         e.stopEvent();
486         
487         this.dragable = true;
488         this.pinching = false;
489         
490         if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
491             this.dragable = false;
492             return;
493         }
494         
495         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
496         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
497         
498     },
499     
500     onMouseMove : function(e)
501     {   
502         e.stopEvent();
503         
504         if(!this.canvasLoaded){
505             return;
506         }
507         
508         if (!this.dragable){
509             return;
510         }
511         
512         var minX = Math.ceil(this.thumbEl.getLeft(true));
513         var minY = Math.ceil(this.thumbEl.getTop(true));
514         
515         var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
516         var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
517
518         if(minX > maxX) {
519             var tempX = minX;
520             minX = maxX;
521             maxX = tempX;
522         }
523
524         if(minY > maxY) {
525             var tempY = minY;
526             minY = maxY;
527             maxY = tempY;
528         }
529
530         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
531         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
532         
533         x = x - this.mouseX;
534         y = y - this.mouseY;
535
536         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
537         var bgY = Math.ceil(y + this.previewEl.getTop(true));
538         
539         bgX = (bgX < minX) ? minX : ((bgX > maxX) ? maxX : bgX);
540         bgY = (bgY < minY) ? minY : ((bgY > maxY) ? maxY : bgY);
541         
542         this.previewEl.setLeft(bgX);
543         this.previewEl.setTop(bgY);
544         
545         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
546         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
547     },
548     
549     onMouseUp : function(e)
550     {   
551         e.stopEvent();
552         
553         this.dragable = false;
554     },
555     
556     onMouseWheel : function(e)
557     {   
558         e.stopEvent();
559         
560         this.startScale = this.scale;
561         this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
562         
563         if(!this.zoomable()){
564             this.scale = this.startScale;
565             return;
566         }
567
568         
569         this.draw();
570         
571         return;
572     },
573     
574     zoomable : function()
575     {
576         var minScale = this.thumbEl.getWidth() / this.minWidth;
577         
578         if(this.minWidth < this.minHeight){
579             minScale = this.thumbEl.getHeight() / this.minHeight;
580         }
581         
582         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
583         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
584
585
586         var maxWidth = this.imageEl.OriginWidth;
587         var maxHeight = this.imageEl.OriginHeight;
588         
589         if(
590                 this.isDocument &&
591                 (this.rotate == 0 || this.rotate == 180) && 
592                 (
593                     width > this.imageEl.OriginWidth || 
594                     height > this.imageEl.OriginHeight ||
595                     (width < this.minWidth && height < this.minHeight)
596                 )
597         ){
598             return false;
599         }
600         
601         if(
602                 this.isDocument &&
603                 (this.rotate == 90 || this.rotate == 270) && 
604                 (
605                     width > this.imageEl.OriginWidth || 
606                     height > this.imageEl.OriginHeight ||
607                     (width < this.minHeight && height < this.minWidth)
608                 )
609         ){
610             return false;
611         }
612         
613         if(
614                 !this.isDocument &&
615                 (this.rotate == 0 || this.rotate == 180) && 
616                 (
617                     (this.imageEl.OriginWidth >= this.minWidth) && width < this.minWidth ||
618                     (this.imageEl.OriginHeight >= this.minHeight) && height < this.minHeight ||
619                     width > maxWidth ||
620                     height > maxHeight
621                 )
622         ){
623             return false;
624         }
625         
626         if(
627                 !this.isDocument &&
628                 (this.rotate == 90 || this.rotate == 270) && 
629                 (
630                     width < this.minHeight || 
631                     width > this.imageEl.OriginWidth || 
632                     height < this.minWidth || 
633                     height > this.imageEl.OriginHeight
634                 )
635         ){
636             return false;
637         }
638         
639         return true;
640         
641     },
642     
643     onRotateLeft : function(e)
644     {   
645         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
646             
647             var minScale = this.thumbEl.getWidth() / this.minWidth;
648             
649             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
650             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
651             
652             this.startScale = this.scale;
653             
654             while (this.getScaleLevel() < minScale){
655             
656                 this.scale = this.scale + 1;
657                 
658                 if(!this.zoomable()){
659                     break;
660                 }
661                 
662                 if(
663                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
664                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
665                 ){
666                     continue;
667                 }
668                 
669                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
670
671                 this.draw();
672                 
673                 return;
674             }
675             
676             this.scale = this.startScale;
677             
678             this.onRotateFail();
679             
680             return false;
681         }
682         
683         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
684
685         if(this.isDocument){
686             this.setThumbBoxSize();
687             this.setThumbBoxPosition();
688             this.setCanvasPosition();
689         }
690         
691         this.draw();
692         
693         this.fireEvent('rotate', this, 'left');
694         
695     },
696     
697     onRotateRight : function(e)
698     {
699         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
700             
701             var minScale = this.thumbEl.getWidth() / this.minWidth;
702         
703             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
704             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
705             
706             this.startScale = this.scale;
707             
708             while (this.getScaleLevel() < minScale){
709             
710                 this.scale = this.scale + 1;
711                 
712                 if(!this.zoomable()){
713                     break;
714                 }
715                 
716                 if(
717                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
718                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
719                 ){
720                     continue;
721                 }
722                 
723                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
724
725                 this.draw();
726                 
727                 return;
728             }
729             
730             this.scale = this.startScale;
731             
732             this.onRotateFail();
733             
734             return false;
735         }
736         
737         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
738
739         if(this.isDocument){
740             this.setThumbBoxSize();
741             this.setThumbBoxPosition();
742             this.setCanvasPosition();
743         }
744         
745         this.draw();
746         
747         this.fireEvent('rotate', this, 'right');
748     },
749     
750     onRotateFail : function()
751     {
752         this.errorEl.show(true);
753         
754         var _this = this;
755         
756         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
757     },
758     
759     draw : function()
760     {
761         this.previewEl.dom.innerHTML = '';
762         
763         var canvasEl = document.createElement("canvas");
764         
765         var contextEl = canvasEl.getContext("2d");
766         
767         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
768         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
769         var center = this.imageEl.OriginWidth / 2;
770         
771         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
772             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
773             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
774             center = this.imageEl.OriginHeight / 2;
775         }
776         
777         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
778         
779         contextEl.translate(center, center);
780         contextEl.rotate(this.rotate * Math.PI / 180);
781
782         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
783         
784         this.canvasEl = document.createElement("canvas");
785         
786         this.contextEl = this.canvasEl.getContext("2d");
787         
788         switch (this.rotate) {
789             case 0 :
790                 
791                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
792                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
793                 
794                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
795                 
796                 break;
797             case 90 : 
798                 
799                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
800                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
801                 
802                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
803                     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);
804                     break;
805                 }
806                 
807                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
808                 
809                 break;
810             case 180 :
811                 
812                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
813                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
814                 
815                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
816                     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);
817                     break;
818                 }
819                 
820                 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);
821                 
822                 break;
823             case 270 :
824                 
825                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
826                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
827         
828                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
829                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
830                     break;
831                 }
832                 
833                 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);
834                 
835                 break;
836             default : 
837                 break;
838         }
839         
840         this.previewEl.appendChild(this.canvasEl);
841         
842         this.setCanvasPosition();
843     },
844     
845     crop : function()
846     {
847         if(!this.canvasLoaded){
848             return;
849         }
850         
851         var imageCanvas = document.createElement("canvas");
852         
853         var imageContext = imageCanvas.getContext("2d");
854         
855         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
856         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
857         
858         var center = imageCanvas.width / 2;
859         
860         imageContext.translate(center, center);
861         
862         imageContext.rotate(this.rotate * Math.PI / 180);
863         
864         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
865         
866         var canvas = document.createElement("canvas");
867         
868         var context = canvas.getContext("2d");
869
870         canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
871         
872         canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
873
874         switch (this.rotate) {
875             case 0 :
876                 
877                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
878                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
879                 
880                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
881                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
882                 
883                 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
884                 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
885
886                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
887                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
888
889                 if(canvas.width > this.outputMaxWidth) {
890                     var scale = this.outputMaxWidth / canvas.width;
891                     canvas.width = canvas.width * scale;
892                     canvas.height = canvas.height * scale;
893                     context.scale(scale, scale);
894                 }
895
896                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
897                 
898                 break;
899             case 90 : 
900                 
901                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
902                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
903                 
904                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
905                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
906                 
907                 var targetWidth = this.minWidth - 2 * x;
908                 var targetHeight = this.minHeight - 2 * y;
909                 
910                 var scale = 1;
911                 
912                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
913                     scale = targetWidth / width;
914                 }
915                 
916                 if(x > 0 && y == 0){
917                     scale = targetHeight / height;
918                 }
919                 
920                 if(x > 0 && y > 0){
921                     scale = targetWidth / width;
922                     
923                     if(width < height){
924                         scale = targetHeight / height;
925                     }
926                 }
927                 
928                 context.scale(scale, scale);
929                 
930                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
931                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
932
933                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
934                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
935                 
936                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
937                 
938                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
939                 
940                 break;
941             case 180 :
942                 
943                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
944                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
945                 
946                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
947                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
948                 
949                 var targetWidth = this.minWidth - 2 * x;
950                 var targetHeight = this.minHeight - 2 * y;
951                 
952                 var scale = 1;
953                 
954                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
955                     scale = targetWidth / width;
956                 }
957                 
958                 if(x > 0 && y == 0){
959                     scale = targetHeight / height;
960                 }
961                 
962                 if(x > 0 && y > 0){
963                     scale = targetWidth / width;
964                     
965                     if(width < height){
966                         scale = targetHeight / height;
967                     }
968                 }
969                 
970                 context.scale(scale, scale);
971                 
972                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
973                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
974
975                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
976                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
977
978                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
979                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
980                 
981                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
982                 
983                 break;
984             case 270 :
985                 
986                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
987                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
988                 
989                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
990                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
991                 
992                 var targetWidth = this.minWidth - 2 * x;
993                 var targetHeight = this.minHeight - 2 * y;
994                 
995                 var scale = 1;
996                 
997                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
998                     scale = targetWidth / width;
999                 }
1000                 
1001                 if(x > 0 && y == 0){
1002                     scale = targetHeight / height;
1003                 }
1004                 
1005                 if(x > 0 && y > 0){
1006                     scale = targetWidth / width;
1007                     
1008                     if(width < height){
1009                         scale = targetHeight / height;
1010                     }
1011                 }
1012                 
1013                 context.scale(scale, scale);
1014                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1015                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1016
1017                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1018                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1019                 
1020                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1021                 
1022                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1023                 
1024                 break;
1025             default : 
1026                 break;
1027         }
1028         
1029         this.cropData = canvas.toDataURL(this.cropType);
1030         
1031         if(this.fireEvent('crop', this, this.cropData) !== false){
1032             this.process(this.file, this.cropData);
1033         }
1034         
1035         return;
1036         
1037     },
1038     
1039     setThumbBoxSize : function()
1040     {
1041         var width, height;
1042         
1043         if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1044             width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1045             height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1046             
1047             this.minWidth = width;
1048             this.minHeight = height;
1049             
1050             if(this.rotate == 90 || this.rotate == 270){
1051                 this.minWidth = height;
1052                 this.minHeight = width;
1053             }
1054         }
1055         
1056         height = 300;
1057         width = Math.ceil(this.minWidth * height / this.minHeight);
1058         
1059         if(this.minWidth > this.minHeight){
1060             width = 300;
1061             height = Math.ceil(this.minHeight * width / this.minWidth);
1062         }
1063         
1064         this.thumbEl.setStyle({
1065             width : width + 'px',
1066             height : height + 'px'
1067         });
1068
1069         return;
1070             
1071     },
1072     
1073     setThumbBoxPosition : function()
1074     {
1075         var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1076         var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1077         
1078         this.thumbEl.setLeft(x);
1079         this.thumbEl.setTop(y);
1080         
1081     },
1082     
1083     baseRotateLevel : function()
1084     {
1085         this.baseRotate = 1;
1086         
1087         if(
1088                 typeof(this.exif) != 'undefined' &&
1089                 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1090                 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1091         ){
1092             this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1093         }
1094         
1095         this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1096         
1097     },
1098     
1099     baseScaleLevel : function()
1100     {
1101         var width, height;
1102         
1103         if(this.isDocument){
1104             
1105             if(this.baseRotate == 6 || this.baseRotate == 8){
1106             
1107                 height = this.thumbEl.getHeight();
1108                 this.baseScale = height / this.imageEl.OriginWidth;
1109
1110                 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1111                     width = this.thumbEl.getWidth();
1112                     this.baseScale = width / this.imageEl.OriginHeight;
1113                 }
1114
1115                 return;
1116             }
1117
1118             height = this.thumbEl.getHeight();
1119             this.baseScale = height / this.imageEl.OriginHeight;
1120
1121             if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1122                 width = this.thumbEl.getWidth();
1123                 this.baseScale = width / this.imageEl.OriginWidth;
1124             }
1125
1126             return;
1127         }
1128         
1129         if(this.baseRotate == 6 || this.baseRotate == 8){
1130             
1131             width = this.thumbEl.getHeight();
1132             this.baseScale = width / this.imageEl.OriginHeight;
1133             
1134             if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1135                 height = this.thumbEl.getWidth();
1136                 this.baseScale = height / this.imageEl.OriginHeight;
1137             }
1138             
1139             if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1140                 height = this.thumbEl.getWidth();
1141                 this.baseScale = height / this.imageEl.OriginHeight;
1142                 
1143                 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1144                     width = this.thumbEl.getHeight();
1145                     this.baseScale = width / this.imageEl.OriginWidth;
1146                 }
1147             }
1148             
1149             return;
1150         }
1151         
1152         width = this.thumbEl.getWidth();
1153         this.baseScale = width / this.imageEl.OriginWidth;
1154         
1155         if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1156             height = this.thumbEl.getHeight();
1157             this.baseScale = height / this.imageEl.OriginHeight;
1158         }
1159         
1160         if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1161             
1162             height = this.thumbEl.getHeight();
1163             this.baseScale = height / this.imageEl.OriginHeight;
1164             
1165             if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1166                 width = this.thumbEl.getWidth();
1167                 this.baseScale = width / this.imageEl.OriginWidth;
1168             }
1169             
1170         }
1171
1172         if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1173             this.baseScale = width / this.minWidth;
1174         }
1175
1176         return;
1177     },
1178     
1179     getScaleLevel : function()
1180     {
1181         return this.baseScale * Math.pow(1.02, this.scale);
1182     },
1183     
1184     onTouchStart : function(e)
1185     {
1186         if(!this.canvasLoaded){
1187             this.beforeSelectFile(e);
1188             return;
1189         }
1190         
1191         var touches = e.browserEvent.touches;
1192         
1193         if(!touches){
1194             return;
1195         }
1196         
1197         if(touches.length == 1){
1198             this.onMouseDown(e);
1199             return;
1200         }
1201         
1202         if(touches.length != 2){
1203             return;
1204         }
1205         
1206         var coords = [];
1207         
1208         for(var i = 0, finger; finger = touches[i]; i++){
1209             coords.push(finger.pageX, finger.pageY);
1210         }
1211         
1212         var x = Math.pow(coords[0] - coords[2], 2);
1213         var y = Math.pow(coords[1] - coords[3], 2);
1214         
1215         this.startDistance = Math.sqrt(x + y);
1216         
1217         this.startScale = this.scale;
1218         
1219         this.pinching = true;
1220         this.dragable = false;
1221         
1222     },
1223     
1224     onTouchMove : function(e)
1225     {
1226         if(!this.pinching && !this.dragable){
1227             return;
1228         }
1229         
1230         var touches = e.browserEvent.touches;
1231         
1232         if(!touches){
1233             return;
1234         }
1235         
1236         if(this.dragable){
1237             this.onMouseMove(e);
1238             return;
1239         }
1240         
1241         var coords = [];
1242         
1243         for(var i = 0, finger; finger = touches[i]; i++){
1244             coords.push(finger.pageX, finger.pageY);
1245         }
1246         
1247         var x = Math.pow(coords[0] - coords[2], 2);
1248         var y = Math.pow(coords[1] - coords[3], 2);
1249         
1250         this.endDistance = Math.sqrt(x + y);
1251         
1252         this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1253         
1254         if(!this.zoomable()){
1255             this.scale = this.startScale;
1256             return;
1257         }
1258         
1259         this.draw();
1260         
1261     },
1262     
1263     onTouchEnd : function(e)
1264     {
1265         this.pinching = false;
1266         this.dragable = false;
1267         
1268     },
1269     
1270     process : function(file, crop)
1271     {
1272         if(this.loadMask){
1273             this.maskEl.mask(this.loadingText);
1274         }
1275         
1276         this.xhr = new XMLHttpRequest();
1277         
1278         file.xhr = this.xhr;
1279
1280         this.xhr.open(this.method, this.url, true);
1281         
1282         var headers = {
1283             "Accept": "application/json",
1284             "Cache-Control": "no-cache",
1285             "X-Requested-With": "XMLHttpRequest"
1286         };
1287         
1288         for (var headerName in headers) {
1289             var headerValue = headers[headerName];
1290             if (headerValue) {
1291                 this.xhr.setRequestHeader(headerName, headerValue);
1292             }
1293         }
1294         
1295         var _this = this;
1296         
1297         this.xhr.onload = function()
1298         {
1299             _this.xhrOnLoad(_this.xhr);
1300         }
1301         
1302         this.xhr.onerror = function()
1303         {
1304             _this.xhrOnError(_this.xhr);
1305         }
1306         
1307         var formData = new FormData();
1308
1309         formData.append('returnHTML', 'NO');
1310
1311         console.log("cropType: " + this.cropType);
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') {
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 });