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