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