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