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