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         console.log("ZOOM");
584         console.log("WIDTH: " + (this.thumbEl.getWidth() / this.getScaleLevel()));
585         console.log("HEIGHT: " + (this.thumbEl.getHeight() / this.getScaleLevel()));
586
587
588         var maxWidth = this.imageEl.OriginWidth;
589         var maxHeight = this.imageEl.OriginHeight;
590         
591         if(
592                 this.isDocument &&
593                 (this.rotate == 0 || this.rotate == 180) && 
594                 (
595                     width > this.imageEl.OriginWidth || 
596                     height > this.imageEl.OriginHeight ||
597                     (width < this.minWidth && height < this.minHeight)
598                 )
599         ){
600             return false;
601         }
602         
603         if(
604                 this.isDocument &&
605                 (this.rotate == 90 || this.rotate == 270) && 
606                 (
607                     width > this.imageEl.OriginWidth || 
608                     height > this.imageEl.OriginHeight ||
609                     (width < this.minHeight && height < this.minWidth)
610                 )
611         ){
612             return false;
613         }
614         
615         if(
616                 !this.isDocument &&
617                 (this.rotate == 0 || this.rotate == 180) && 
618                 (
619                     (this.imageEl.OriginWidth >= this.minWidth) && width < this.minWidth ||
620                     (this.imageEl.OriginHeight >= this.minHeight) && height < this.minHeight ||
621                     width > maxWidth ||
622                     height > maxHeight
623                 )
624         ){
625             return false;
626         }
627         
628         if(
629                 !this.isDocument &&
630                 (this.rotate == 90 || this.rotate == 270) && 
631                 (
632                     width < this.minHeight || 
633                     width > this.imageEl.OriginWidth || 
634                     height < this.minWidth || 
635                     height > this.imageEl.OriginHeight
636                 )
637         ){
638             return false;
639         }
640         
641         return true;
642         
643     },
644     
645     onRotateLeft : function(e)
646     {   
647         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
648             
649             var minScale = this.thumbEl.getWidth() / this.minWidth;
650             
651             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
652             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
653             
654             this.startScale = this.scale;
655             
656             while (this.getScaleLevel() < minScale){
657             
658                 this.scale = this.scale + 1;
659                 
660                 if(!this.zoomable()){
661                     break;
662                 }
663                 
664                 if(
665                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
666                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
667                 ){
668                     continue;
669                 }
670                 
671                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
672
673                 this.draw();
674                 
675                 return;
676             }
677             
678             this.scale = this.startScale;
679             
680             this.onRotateFail();
681             
682             return false;
683         }
684         
685         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
686
687         if(this.isDocument){
688             this.setThumbBoxSize();
689             this.setThumbBoxPosition();
690             this.setCanvasPosition();
691         }
692         
693         this.draw();
694         
695         this.fireEvent('rotate', this, 'left');
696         
697     },
698     
699     onRotateRight : function(e)
700     {
701         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
702             
703             var minScale = this.thumbEl.getWidth() / this.minWidth;
704         
705             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
706             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
707             
708             this.startScale = this.scale;
709             
710             while (this.getScaleLevel() < minScale){
711             
712                 this.scale = this.scale + 1;
713                 
714                 if(!this.zoomable()){
715                     break;
716                 }
717                 
718                 if(
719                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
720                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
721                 ){
722                     continue;
723                 }
724                 
725                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
726
727                 this.draw();
728                 
729                 return;
730             }
731             
732             this.scale = this.startScale;
733             
734             this.onRotateFail();
735             
736             return false;
737         }
738         
739         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
740
741         if(this.isDocument){
742             this.setThumbBoxSize();
743             this.setThumbBoxPosition();
744             this.setCanvasPosition();
745         }
746         
747         this.draw();
748         
749         this.fireEvent('rotate', this, 'right');
750     },
751     
752     onRotateFail : function()
753     {
754         this.errorEl.show(true);
755         
756         var _this = this;
757         
758         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
759     },
760     
761     draw : function()
762     {
763         this.previewEl.dom.innerHTML = '';
764         
765         var canvasEl = document.createElement("canvas");
766         
767         var contextEl = canvasEl.getContext("2d");
768         
769         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
770         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
771         var center = this.imageEl.OriginWidth / 2;
772         
773         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
774             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
775             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
776             center = this.imageEl.OriginHeight / 2;
777         }
778         
779         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
780         
781         contextEl.translate(center, center);
782         contextEl.rotate(this.rotate * Math.PI / 180);
783
784         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
785         
786         this.canvasEl = document.createElement("canvas");
787         
788         this.contextEl = this.canvasEl.getContext("2d");
789         
790         switch (this.rotate) {
791             case 0 :
792                 
793                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
794                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
795                 
796                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
797                 
798                 break;
799             case 90 : 
800                 
801                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
802                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
803                 
804                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
805                     this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
806                     break;
807                 }
808                 
809                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
810                 
811                 break;
812             case 180 :
813                 
814                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
815                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
816                 
817                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
818                     this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
819                     break;
820                 }
821                 
822                 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
823                 
824                 break;
825             case 270 :
826                 
827                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
828                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
829         
830                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
831                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
832                     break;
833                 }
834                 
835                 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
836                 
837                 break;
838             default : 
839                 break;
840         }
841         
842         this.previewEl.appendChild(this.canvasEl);
843         
844         this.setCanvasPosition();
845     },
846     
847     crop : function()
848     {
849         console.log("CROP");
850         if(!this.canvasLoaded){
851             return;
852         }
853         
854         var imageCanvas = document.createElement("canvas");
855         
856         var imageContext = imageCanvas.getContext("2d");
857         
858         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
859         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
860
861         console.log("IMAGECANVAS WIDTH: " + imageCanvas.width);
862         console.log("IMAGECANVAS HEIGHT: " + imageCanvas.height);
863         
864         var center = imageCanvas.width / 2;
865         
866         imageContext.translate(center, center);
867         
868         imageContext.rotate(this.rotate * Math.PI / 180);
869         
870         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
871         
872         var canvas = document.createElement("canvas");
873         
874         var context = canvas.getContext("2d");
875
876         canvas.width = this.thumbEl.getWidth() / this.getScaleLevel();
877         
878         canvas.height = this.thumbEl.getHeight() / this.getScaleLevel();
879
880         console.log("CANVAS WIDTH: " + canvas.width);
881         console.log("CANVAS HEIGHT: " + canvas.height);
882
883         console.log("SCALELEVLE: " + this.getScaleLevel());
884
885         switch (this.rotate) {
886             case 0 :
887                 
888                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
889                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
890
891                 console.log("WIDTH: " + width);
892                 console.log("HEIGHT: " + height);
893                 
894                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
895                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
896
897                 console.log("X: " + x);
898                 console.log("Y: " + y);
899                 
900                 // var targetWidth = this.minWidth - 2 * x;
901                 // var targetHeight = this.minHeight - 2 * y;
902
903                 // console.log("targetWidth: " + targetWidth);
904                 // console.log("targetHeight: " + targetHeight);
905                 
906                 // var scale = 1;
907                 
908                 // if((x == 0 && y == 0) || (x == 0 && y > 0)){
909                 //     scale = targetWidth / width;
910                 // }
911                 
912                 // if(x > 0 && y == 0){
913                 //     scale = targetHeight / height;
914                 // }
915                 
916                 // if(x > 0 && y > 0){
917                 //     scale = targetWidth / width;
918                     
919                 //     if(width < height){
920                 //         scale = targetHeight / height;
921                 //     }
922                 // }
923
924                 // console.log("scale: " + scale);
925                 
926                 // context.scale(scale, scale);
927                 
928                 var sx = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
929                 var sy = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
930
931                 console.log("sx: " + sx);
932                 console.log("sy: " + sy);
933
934                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
935                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
936
937                 if(canvas.width > 1200) {
938                     var scale = 1200 / canvas.width;
939                     context.scale(scale, scale);
940                     canvas.height = 1200 * this.thumbEl.getHeight() / this.thumbEl.getWidth();
941                     canvas.width = canvas.width * scale;
942                 }
943
944                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
945                 
946                 break;
947             case 90 : 
948                 
949                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
950                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
951                 
952                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
953                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
954                 
955                 var targetWidth = this.minWidth - 2 * x;
956                 var targetHeight = this.minHeight - 2 * y;
957                 
958                 var scale = 1;
959                 
960                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
961                     scale = targetWidth / width;
962                 }
963                 
964                 if(x > 0 && y == 0){
965                     scale = targetHeight / height;
966                 }
967                 
968                 if(x > 0 && y > 0){
969                     scale = targetWidth / width;
970                     
971                     if(width < height){
972                         scale = targetHeight / height;
973                     }
974                 }
975                 
976                 context.scale(scale, scale);
977                 
978                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
979                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
980
981                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
982                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
983                 
984                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
985                 
986                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
987                 
988                 break;
989             case 180 :
990                 
991                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
992                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
993                 
994                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
995                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
996                 
997                 var targetWidth = this.minWidth - 2 * x;
998                 var targetHeight = this.minHeight - 2 * y;
999                 
1000                 var scale = 1;
1001                 
1002                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1003                     scale = targetWidth / width;
1004                 }
1005                 
1006                 if(x > 0 && y == 0){
1007                     scale = targetHeight / height;
1008                 }
1009                 
1010                 if(x > 0 && y > 0){
1011                     scale = targetWidth / width;
1012                     
1013                     if(width < height){
1014                         scale = targetHeight / height;
1015                     }
1016                 }
1017                 
1018                 context.scale(scale, scale);
1019                 
1020                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1021                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1022
1023                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1024                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1025
1026                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1027                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
1028                 
1029                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1030                 
1031                 break;
1032             case 270 :
1033                 
1034                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
1035                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
1036                 
1037                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
1038                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
1039                 
1040                 var targetWidth = this.minWidth - 2 * x;
1041                 var targetHeight = this.minHeight - 2 * y;
1042                 
1043                 var scale = 1;
1044                 
1045                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
1046                     scale = targetWidth / width;
1047                 }
1048                 
1049                 if(x > 0 && y == 0){
1050                     scale = targetHeight / height;
1051                 }
1052                 
1053                 if(x > 0 && y > 0){
1054                     scale = targetWidth / width;
1055                     
1056                     if(width < height){
1057                         scale = targetHeight / height;
1058                     }
1059                 }
1060                 
1061                 context.scale(scale, scale);
1062                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
1063                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
1064
1065                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
1066                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
1067                 
1068                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
1069                 
1070                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
1071                 
1072                 break;
1073             default : 
1074                 break;
1075         }
1076         
1077         this.cropData = canvas.toDataURL(this.cropType);
1078         
1079         if(this.fireEvent('crop', this, this.cropData) !== false){
1080             this.process(this.file, this.cropData);
1081         }
1082         
1083         return;
1084         
1085     },
1086     
1087     setThumbBoxSize : function()
1088     {
1089         var width, height;
1090         
1091         if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1092             width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1093             height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1094             
1095             this.minWidth = width;
1096             this.minHeight = height;
1097             
1098             if(this.rotate == 90 || this.rotate == 270){
1099                 this.minWidth = height;
1100                 this.minHeight = width;
1101             }
1102         }
1103         
1104         height = 300;
1105         width = Math.ceil(this.minWidth * height / this.minHeight);
1106         
1107         if(this.minWidth > this.minHeight){
1108             width = 300;
1109             height = Math.ceil(this.minHeight * width / this.minWidth);
1110         }
1111         
1112         this.thumbEl.setStyle({
1113             width : width + 'px',
1114             height : height + 'px'
1115         });
1116
1117         return;
1118             
1119     },
1120     
1121     setThumbBoxPosition : function()
1122     {
1123         var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1124         var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1125         
1126         this.thumbEl.setLeft(x);
1127         this.thumbEl.setTop(y);
1128         
1129     },
1130     
1131     baseRotateLevel : function()
1132     {
1133         this.baseRotate = 1;
1134         
1135         if(
1136                 typeof(this.exif) != 'undefined' &&
1137                 typeof(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1138                 [1, 3, 6, 8].indexOf(this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']]) != -1
1139         ){
1140             this.baseRotate = this.exif[Roo.dialog.UploadCropbox['tags']['Orientation']];
1141         }
1142         
1143         this.rotate = Roo.dialog.UploadCropbox['Orientation'][this.baseRotate];
1144         
1145     },
1146     
1147     baseScaleLevel : function()
1148     {
1149         console.log("BASE SCALE LEVEL");
1150         var width, height;
1151         
1152         if(this.isDocument){
1153             
1154             if(this.baseRotate == 6 || this.baseRotate == 8){
1155             
1156                 height = this.thumbEl.getHeight();
1157                 this.baseScale = height / this.imageEl.OriginWidth;
1158
1159                 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1160                     width = this.thumbEl.getWidth();
1161                     this.baseScale = width / this.imageEl.OriginHeight;
1162                 }
1163
1164                 return;
1165             }
1166
1167             height = this.thumbEl.getHeight();
1168             this.baseScale = height / this.imageEl.OriginHeight;
1169
1170             if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1171                 width = this.thumbEl.getWidth();
1172                 this.baseScale = width / this.imageEl.OriginWidth;
1173             }
1174
1175             return;
1176         }
1177         
1178         if(this.baseRotate == 6 || this.baseRotate == 8){
1179             
1180             width = this.thumbEl.getHeight();
1181             this.baseScale = width / this.imageEl.OriginHeight;
1182             
1183             if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1184                 height = this.thumbEl.getWidth();
1185                 this.baseScale = height / this.imageEl.OriginHeight;
1186             }
1187             
1188             if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1189                 height = this.thumbEl.getWidth();
1190                 this.baseScale = height / this.imageEl.OriginHeight;
1191                 
1192                 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1193                     width = this.thumbEl.getHeight();
1194                     this.baseScale = width / this.imageEl.OriginWidth;
1195                 }
1196             }
1197             
1198             return;
1199         }
1200         
1201         width = this.thumbEl.getWidth();
1202         this.baseScale = width / this.imageEl.OriginWidth;
1203         
1204         if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1205             height = this.thumbEl.getHeight();
1206             this.baseScale = height / this.imageEl.OriginHeight;
1207         }
1208         
1209         if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1210             
1211             height = this.thumbEl.getHeight();
1212             this.baseScale = height / this.imageEl.OriginHeight;
1213             
1214             if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1215                 width = this.thumbEl.getWidth();
1216                 this.baseScale = width / this.imageEl.OriginWidth;
1217             }
1218             
1219         }
1220
1221         if(this.imageEl.OriginWidth < this.minWidth || this.imageEl.OriginHeight < this.minHeight) {
1222             this.baseScale = width / this.minWidth;
1223         }
1224         console.log(this.baseScale);
1225         return;
1226     },
1227     
1228     getScaleLevel : function()
1229     {
1230         return this.baseScale * Math.pow(1.02, this.scale);
1231     },
1232     
1233     onTouchStart : function(e)
1234     {
1235         if(!this.canvasLoaded){
1236             this.beforeSelectFile(e);
1237             return;
1238         }
1239         
1240         var touches = e.browserEvent.touches;
1241         
1242         if(!touches){
1243             return;
1244         }
1245         
1246         if(touches.length == 1){
1247             this.onMouseDown(e);
1248             return;
1249         }
1250         
1251         if(touches.length != 2){
1252             return;
1253         }
1254         
1255         var coords = [];
1256         
1257         for(var i = 0, finger; finger = touches[i]; i++){
1258             coords.push(finger.pageX, finger.pageY);
1259         }
1260         
1261         var x = Math.pow(coords[0] - coords[2], 2);
1262         var y = Math.pow(coords[1] - coords[3], 2);
1263         
1264         this.startDistance = Math.sqrt(x + y);
1265         
1266         this.startScale = this.scale;
1267         
1268         this.pinching = true;
1269         this.dragable = false;
1270         
1271     },
1272     
1273     onTouchMove : function(e)
1274     {
1275         if(!this.pinching && !this.dragable){
1276             return;
1277         }
1278         
1279         var touches = e.browserEvent.touches;
1280         
1281         if(!touches){
1282             return;
1283         }
1284         
1285         if(this.dragable){
1286             this.onMouseMove(e);
1287             return;
1288         }
1289         
1290         var coords = [];
1291         
1292         for(var i = 0, finger; finger = touches[i]; i++){
1293             coords.push(finger.pageX, finger.pageY);
1294         }
1295         
1296         var x = Math.pow(coords[0] - coords[2], 2);
1297         var y = Math.pow(coords[1] - coords[3], 2);
1298         
1299         this.endDistance = Math.sqrt(x + y);
1300         
1301         this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1302         
1303         if(!this.zoomable()){
1304             this.scale = this.startScale;
1305             return;
1306         }
1307         
1308         this.draw();
1309         
1310     },
1311     
1312     onTouchEnd : function(e)
1313     {
1314         this.pinching = false;
1315         this.dragable = false;
1316         
1317     },
1318     
1319     process : function(file, crop)
1320     {
1321         if(this.loadMask){
1322             this.maskEl.mask(this.loadingText);
1323         }
1324         
1325         this.xhr = new XMLHttpRequest();
1326         
1327         file.xhr = this.xhr;
1328
1329         this.xhr.open(this.method, this.url, true);
1330         
1331         var headers = {
1332             "Accept": "application/json",
1333             "Cache-Control": "no-cache",
1334             "X-Requested-With": "XMLHttpRequest"
1335         };
1336         
1337         for (var headerName in headers) {
1338             var headerValue = headers[headerName];
1339             if (headerValue) {
1340                 this.xhr.setRequestHeader(headerName, headerValue);
1341             }
1342         }
1343         
1344         var _this = this;
1345         
1346         this.xhr.onload = function()
1347         {
1348             _this.xhrOnLoad(_this.xhr);
1349         }
1350         
1351         this.xhr.onerror = function()
1352         {
1353             _this.xhrOnError(_this.xhr);
1354         }
1355         
1356         var formData = new FormData();
1357
1358         formData.append("onid", "43024");
1359         formData.append("ontable", "pressrelease_boilerplate");
1360
1361         formData.append('returnHTML', 'NO');
1362         
1363         if(crop){
1364             formData.append('crop', crop);
1365             var blobBin = atob(crop.split(',')[1]);
1366             var array = [];
1367             for(var i = 0; i < blobBin.length; i++) {
1368                 array.push(blobBin.charCodeAt(i));
1369             }
1370             var croppedFile =new Blob([new Uint8Array(array)], {type: 'image/png'});
1371             formData.append(this.paramName, croppedFile, file.name);
1372         }
1373         
1374         if(typeof(file.filename) != 'undefined'){
1375             formData.append('filename', file.filename);
1376         }
1377         
1378         if(typeof(file.mimetype) != 'undefined'){
1379             formData.append('mimetype', file.mimetype);
1380         }
1381
1382         if(this.fireEvent('arrange', this, formData) != false){
1383             this.xhr.send(formData);
1384         };
1385     },
1386     
1387     xhrOnLoad : function(xhr)
1388     {
1389         if(this.loadMask){
1390             this.maskEl.unmask();
1391         }
1392         
1393         if (xhr.readyState !== 4) {
1394             this.fireEvent('exception', this, xhr);
1395             return;
1396         }
1397
1398         var response = Roo.decode(xhr.responseText);
1399         
1400         if(!response.success){
1401             this.fireEvent('exception', this, xhr);
1402             return;
1403         }
1404         
1405         var response = Roo.decode(xhr.responseText);
1406         
1407         this.fireEvent('upload', this, response);
1408         
1409     },
1410     
1411     xhrOnError : function()
1412     {
1413         if(this.loadMask){
1414             this.maskEl.unmask();
1415         }
1416         
1417         Roo.log('xhr on error');
1418         
1419         var response = Roo.decode(xhr.responseText);
1420           
1421         Roo.log(response);
1422         
1423     },
1424     
1425     prepare : function(file)
1426     {   
1427         if(this.loadMask){
1428             this.maskEl.mask(this.loadingText);
1429         }
1430         
1431         this.file = false;
1432         this.exif = {};
1433         
1434         if(typeof(file) === 'string'){
1435             this.loadCanvas(file);
1436             return;
1437         }
1438         
1439         if(!file || !this.urlAPI){
1440             return;
1441         }
1442         
1443         this.file = file;
1444         this.cropType = file.type;
1445         
1446         var _this = this;
1447         
1448         if(this.fireEvent('prepare', this, this.file) != false){
1449             
1450             var reader = new FileReader();
1451             
1452             reader.onload = function (e) {
1453                 if (e.target.error) {
1454                     Roo.log(e.target.error);
1455                     return;
1456                 }
1457                 
1458                 var buffer = e.target.result,
1459                     dataView = new DataView(buffer),
1460                     offset = 2,
1461                     maxOffset = dataView.byteLength - 4,
1462                     markerBytes,
1463                     markerLength;
1464                 
1465                 if (dataView.getUint16(0) === 0xffd8) {
1466                     while (offset < maxOffset) {
1467                         markerBytes = dataView.getUint16(offset);
1468                         
1469                         if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1470                             markerLength = dataView.getUint16(offset + 2) + 2;
1471                             if (offset + markerLength > dataView.byteLength) {
1472                                 Roo.log('Invalid meta data: Invalid segment size.');
1473                                 break;
1474                             }
1475                             
1476                             if(markerBytes == 0xffe1){
1477                                 _this.parseExifData(
1478                                     dataView,
1479                                     offset,
1480                                     markerLength
1481                                 );
1482                             }
1483                             
1484                             offset += markerLength;
1485                             
1486                             continue;
1487                         }
1488                         
1489                         break;
1490                     }
1491                     
1492                 }
1493                 
1494                 var url = _this.urlAPI.createObjectURL(_this.file);
1495                 
1496                 _this.loadCanvas(url);
1497                 
1498                 return;
1499             }
1500             
1501             reader.readAsArrayBuffer(this.file);
1502             
1503         }
1504         
1505     },
1506     
1507     parseExifData : function(dataView, offset, length)
1508     {
1509         var tiffOffset = offset + 10,
1510             littleEndian,
1511             dirOffset;
1512     
1513         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1514             // No Exif data, might be XMP data instead
1515             return;
1516         }
1517         
1518         // Check for the ASCII code for "Exif" (0x45786966):
1519         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1520             // No Exif data, might be XMP data instead
1521             return;
1522         }
1523         if (tiffOffset + 8 > dataView.byteLength) {
1524             Roo.log('Invalid Exif data: Invalid segment size.');
1525             return;
1526         }
1527         // Check for the two null bytes:
1528         if (dataView.getUint16(offset + 8) !== 0x0000) {
1529             Roo.log('Invalid Exif data: Missing byte alignment offset.');
1530             return;
1531         }
1532         // Check the byte alignment:
1533         switch (dataView.getUint16(tiffOffset)) {
1534         case 0x4949:
1535             littleEndian = true;
1536             break;
1537         case 0x4D4D:
1538             littleEndian = false;
1539             break;
1540         default:
1541             Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1542             return;
1543         }
1544         // Check for the TIFF tag marker (0x002A):
1545         if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1546             Roo.log('Invalid Exif data: Missing TIFF marker.');
1547             return;
1548         }
1549         // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1550         dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1551         
1552         this.parseExifTags(
1553             dataView,
1554             tiffOffset,
1555             tiffOffset + dirOffset,
1556             littleEndian
1557         );
1558     },
1559     
1560     parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1561     {
1562         var tagsNumber,
1563             dirEndOffset,
1564             i;
1565         if (dirOffset + 6 > dataView.byteLength) {
1566             Roo.log('Invalid Exif data: Invalid directory offset.');
1567             return;
1568         }
1569         tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1570         dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1571         if (dirEndOffset + 4 > dataView.byteLength) {
1572             Roo.log('Invalid Exif data: Invalid directory size.');
1573             return;
1574         }
1575         for (i = 0; i < tagsNumber; i += 1) {
1576             this.parseExifTag(
1577                 dataView,
1578                 tiffOffset,
1579                 dirOffset + 2 + 12 * i, // tag offset
1580                 littleEndian
1581             );
1582         }
1583         // Return the offset to the next directory:
1584         return dataView.getUint32(dirEndOffset, littleEndian);
1585     },
1586     
1587     parseExifTag : function (dataView, tiffOffset, offset, littleEndian) 
1588     {
1589         var tag = dataView.getUint16(offset, littleEndian);
1590         
1591         this.exif[tag] = this.getExifValue(
1592             dataView,
1593             tiffOffset,
1594             offset,
1595             dataView.getUint16(offset + 2, littleEndian), // tag type
1596             dataView.getUint32(offset + 4, littleEndian), // tag length
1597             littleEndian
1598         );
1599     },
1600     
1601     getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1602     {
1603         var tagType = Roo.dialog.UploadCropbox.exifTagTypes[type],
1604             tagSize,
1605             dataOffset,
1606             values,
1607             i,
1608             str,
1609             c;
1610     
1611         if (!tagType) {
1612             Roo.log('Invalid Exif data: Invalid tag type.');
1613             return;
1614         }
1615         
1616         tagSize = tagType.size * length;
1617         // Determine if the value is contained in the dataOffset bytes,
1618         // or if the value at the dataOffset is a pointer to the actual data:
1619         dataOffset = tagSize > 4 ?
1620                 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1621         if (dataOffset + tagSize > dataView.byteLength) {
1622             Roo.log('Invalid Exif data: Invalid data offset.');
1623             return;
1624         }
1625         if (length === 1) {
1626             return tagType.getValue(dataView, dataOffset, littleEndian);
1627         }
1628         values = [];
1629         for (i = 0; i < length; i += 1) {
1630             values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1631         }
1632         
1633         if (tagType.ascii) {
1634             str = '';
1635             // Concatenate the chars:
1636             for (i = 0; i < values.length; i += 1) {
1637                 c = values[i];
1638                 // Ignore the terminating NULL byte(s):
1639                 if (c === '\u0000') {
1640                     break;
1641                 }
1642                 str += c;
1643             }
1644             return str;
1645         }
1646         return values;
1647     }
1648     
1649 });
1650
1651 Roo.apply(Roo.dialog.UploadCropbox, {
1652     tags : {
1653         'Orientation': 0x0112
1654     },
1655     
1656     Orientation: {
1657             1: 0, //'top-left',
1658 //            2: 'top-right',
1659             3: 180, //'bottom-right',
1660 //            4: 'bottom-left',
1661 //            5: 'left-top',
1662             6: 90, //'right-top',
1663 //            7: 'right-bottom',
1664             8: 270 //'left-bottom'
1665     },
1666     
1667     exifTagTypes : {
1668         // byte, 8-bit unsigned int:
1669         1: {
1670             getValue: function (dataView, dataOffset) {
1671                 return dataView.getUint8(dataOffset);
1672             },
1673             size: 1
1674         },
1675         // ascii, 8-bit byte:
1676         2: {
1677             getValue: function (dataView, dataOffset) {
1678                 return String.fromCharCode(dataView.getUint8(dataOffset));
1679             },
1680             size: 1,
1681             ascii: true
1682         },
1683         // short, 16 bit int:
1684         3: {
1685             getValue: function (dataView, dataOffset, littleEndian) {
1686                 return dataView.getUint16(dataOffset, littleEndian);
1687             },
1688             size: 2
1689         },
1690         // long, 32 bit int:
1691         4: {
1692             getValue: function (dataView, dataOffset, littleEndian) {
1693                 return dataView.getUint32(dataOffset, littleEndian);
1694             },
1695             size: 4
1696         },
1697         // rational = two long values, first is numerator, second is denominator:
1698         5: {
1699             getValue: function (dataView, dataOffset, littleEndian) {
1700                 return dataView.getUint32(dataOffset, littleEndian) /
1701                     dataView.getUint32(dataOffset + 4, littleEndian);
1702             },
1703             size: 8
1704         },
1705         // slong, 32 bit signed int:
1706         9: {
1707             getValue: function (dataView, dataOffset, littleEndian) {
1708                 return dataView.getInt32(dataOffset, littleEndian);
1709             },
1710             size: 4
1711         },
1712         // srational, two slongs, first is numerator, second is denominator:
1713         10: {
1714             getValue: function (dataView, dataOffset, littleEndian) {
1715                 return dataView.getInt32(dataOffset, littleEndian) /
1716                     dataView.getInt32(dataOffset + 4, littleEndian);
1717             },
1718             size: 8
1719         }
1720     },
1721     
1722     footer : {
1723         STANDARD : [
1724             {
1725                 tag : 'div',
1726                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1727                 action : 'rotate-left',
1728                 cn : [
1729                     {
1730                         tag : 'button',
1731                         cls : 'btn btn-default',
1732                         html : '<i class="fa fa-undo"></i>'
1733                     }
1734                 ]
1735             },
1736             {
1737                 tag : 'div',
1738                 cls : 'btn-group roo-upload-cropbox-picture',
1739                 action : 'picture',
1740                 cn : [
1741                     {
1742                         tag : 'button',
1743                         cls : 'btn btn-default',
1744                         html : '<i class="fa fa-picture-o"></i>'
1745                     }
1746                 ]
1747             },
1748             {
1749                 tag : 'div',
1750                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1751                 action : 'rotate-right',
1752                 cn : [
1753                     {
1754                         tag : 'button',
1755                         cls : 'btn btn-default',
1756                         html : '<i class="fa fa-repeat"></i>'
1757                     }
1758                 ]
1759             }
1760         ],
1761         DOCUMENT : [
1762             {
1763                 tag : 'div',
1764                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1765                 action : 'rotate-left',
1766                 cn : [
1767                     {
1768                         tag : 'button',
1769                         cls : 'btn btn-default',
1770                         html : '<i class="fa fa-undo"></i>'
1771                     }
1772                 ]
1773             },
1774             {
1775                 tag : 'div',
1776                 cls : 'btn-group roo-upload-cropbox-download',
1777                 action : 'download',
1778                 cn : [
1779                     {
1780                         tag : 'button',
1781                         cls : 'btn btn-default',
1782                         html : '<i class="fa fa-download"></i>'
1783                     }
1784                 ]
1785             },
1786             {
1787                 tag : 'div',
1788                 cls : 'btn-group roo-upload-cropbox-crop',
1789                 action : 'crop',
1790                 cn : [
1791                     {
1792                         tag : 'button',
1793                         cls : 'btn btn-default',
1794                         html : '<i class="fa fa-crop"></i>'
1795                     }
1796                 ]
1797             },
1798             {
1799                 tag : 'div',
1800                 cls : 'btn-group roo-upload-cropbox-trash',
1801                 action : 'trash',
1802                 cn : [
1803                     {
1804                         tag : 'button',
1805                         cls : 'btn btn-default',
1806                         html : '<i class="fa fa-trash"></i>'
1807                     }
1808                 ]
1809             },
1810             {
1811                 tag : 'div',
1812                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1813                 action : 'rotate-right',
1814                 cn : [
1815                     {
1816                         tag : 'button',
1817                         cls : 'btn btn-default',
1818                         html : '<i class="fa fa-repeat"></i>'
1819                     }
1820                 ]
1821             }
1822         ],
1823         ROTATOR : [
1824             {
1825                 tag : 'div',
1826                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1827                 action : 'rotate-left',
1828                 cn : [
1829                     {
1830                         tag : 'button',
1831                         cls : 'btn btn-default',
1832                         html : '<i class="fa fa-undo"></i>'
1833                     }
1834                 ]
1835             },
1836             {
1837                 tag : 'div',
1838                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1839                 action : 'rotate-right',
1840                 cn : [
1841                     {
1842                         tag : 'button',
1843                         cls : 'btn btn-default',
1844                         html : '<i class="fa fa-repeat"></i>'
1845                     }
1846                 ]
1847             }
1848         ]
1849     }
1850 });