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