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