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