Roo/bootstrap/UploadCropbox.js
[roojs1] / Roo / bootstrap / UploadCropbox.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.bootstrap.UploadCropbox
8  * @extends Roo.bootstrap.Component
9  * Bootstrap UploadCropbox class
10  * @cfg {String} emptyText show when image has been loaded
11  * @cfg {String} rotateNotify show when image too small to rotate
12  * @cfg {Number} errorTimeout default 3000
13  * @cfg {Number} minWidth default 300
14  * @cfg {Number} minHeight default 300
15  * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
16  * @cfg {Boolean} isDocument (true|false) default false
17  * @cfg {String} url action url
18  * @cfg {String} paramName default 'imageUpload'
19  * @cfg {String} method default POST
20  * @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(this.loadMask){
360             this.maskEl.mask(this.loadingText);
361         }
362         
363         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
364             return;
365         }
366         
367         var file = this.selectorEl.dom.files[0];
368         
369         if(this.fireEvent('inspect', this, file) != false){
370             this.prepare(file);
371         }
372         
373     },
374     
375     trash : function(e)
376     {
377         this.fireEvent('trash', this);
378     },
379     
380     download : function(e)
381     {
382         this.fireEvent('download', this);
383     },
384     
385     loadCanvas : function(src)
386     {   
387         if(this.fireEvent('beforeloadcanvas', this, src) != false){
388             
389             this.reset();
390             
391             this.imageEl = document.createElement('img');
392             
393             var _this = this;
394             
395             this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
396             
397             this.imageEl.src = src;
398         }
399     },
400     
401     onLoadCanvas : function()
402     {   
403         this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
404         this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
405         
406         this.bodyEl.un('click', this.beforeSelectFile, this);
407         
408         this.notifyEl.hide();
409         this.thumbEl.show();
410         this.footerEl.show();
411         
412         this.baseRotateLevel();
413         
414         if(this.isDocument){
415             this.setThumbBoxSize();
416         }
417         
418         this.setThumbBoxPosition();
419         
420         this.baseScaleLevel();
421         
422         this.draw();
423         
424         this.resize();
425         
426         this.canvasLoaded = true;
427         
428         if(this.loadMask){
429             this.maskEl.unmask();
430         }
431         
432     },
433     
434     setCanvasPosition : function()
435     {   
436         if(!this.canvasEl){
437             return;
438         }
439         
440         var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
441         var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
442         
443         this.previewEl.setLeft(pw);
444         this.previewEl.setTop(ph);
445         
446     },
447     
448     onMouseDown : function(e)
449     {   
450         e.stopEvent();
451         
452         this.dragable = true;
453         this.pinching = false;
454         
455         if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
456             this.dragable = false;
457             return;
458         }
459         
460         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
461         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
462         
463     },
464     
465     onMouseMove : function(e)
466     {   
467         e.stopEvent();
468         
469         if(!this.canvasLoaded){
470             return;
471         }
472         
473         if (!this.dragable){
474             return;
475         }
476         
477         var minX = Math.ceil(this.thumbEl.getLeft(true));
478         var minY = Math.ceil(this.thumbEl.getTop(true));
479         
480         var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
481         var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
482         
483         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
484         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
485         
486         x = x - this.mouseX;
487         y = y - this.mouseY;
488         
489         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
490         var bgY = Math.ceil(y + this.previewEl.getTop(true));
491         
492         bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
493         bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
494         
495         this.previewEl.setLeft(bgX);
496         this.previewEl.setTop(bgY);
497         
498         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
499         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
500     },
501     
502     onMouseUp : function(e)
503     {   
504         e.stopEvent();
505         
506         this.dragable = false;
507     },
508     
509     onMouseWheel : function(e)
510     {   
511         e.stopEvent();
512         
513         this.startScale = this.scale;
514         
515         this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
516         
517         if(!this.zoomable()){
518             this.scale = this.startScale;
519             return;
520         }
521         
522         this.draw();
523         
524         return;
525     },
526     
527     zoomable : function()
528     {
529         var minScale = this.thumbEl.getWidth() / this.minWidth;
530         
531         if(this.minWidth < this.minHeight){
532             minScale = this.thumbEl.getHeight() / this.minHeight;
533         }
534         
535         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
536         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
537         
538         if(
539                 this.isDocument &&
540                 (this.rotate == 0 || this.rotate == 180) && 
541                 (
542                     width > this.imageEl.OriginWidth || 
543                     height > this.imageEl.OriginHeight ||
544                     (width < this.minWidth && height < this.minHeight)
545                 )
546         ){
547             return false;
548         }
549         
550         if(
551                 this.isDocument &&
552                 (this.rotate == 90 || this.rotate == 270) && 
553                 (
554                     width > this.imageEl.OriginWidth || 
555                     height > this.imageEl.OriginHeight ||
556                     (width < this.minHeight && height < this.minWidth)
557                 )
558         ){
559             return false;
560         }
561         
562         if(
563                 !this.isDocument &&
564                 (this.rotate == 0 || this.rotate == 180) && 
565                 (
566                     width < this.minWidth || 
567                     width > this.imageEl.OriginWidth || 
568                     height < this.minHeight || 
569                     height > this.imageEl.OriginHeight
570                 )
571         ){
572             return false;
573         }
574         
575         if(
576                 !this.isDocument &&
577                 (this.rotate == 90 || this.rotate == 270) && 
578                 (
579                     width < this.minHeight || 
580                     width > this.imageEl.OriginWidth || 
581                     height < this.minWidth || 
582                     height > this.imageEl.OriginHeight
583                 )
584         ){
585             return false;
586         }
587         
588         return true;
589         
590     },
591     
592     onRotateLeft : function(e)
593     {   
594         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
595             
596             var minScale = this.thumbEl.getWidth() / this.minWidth;
597             
598             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
599             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
600             
601             this.startScale = this.scale;
602             
603             while (this.getScaleLevel() < minScale){
604             
605                 this.scale = this.scale + 1;
606                 
607                 if(!this.zoomable()){
608                     break;
609                 }
610                 
611                 if(
612                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
613                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
614                 ){
615                     continue;
616                 }
617                 
618                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
619
620                 this.draw();
621                 
622                 return;
623             }
624             
625             this.scale = this.startScale;
626             
627             this.onRotateFail();
628             
629             return false;
630         }
631         
632         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
633
634         if(this.isDocument){
635             this.setThumbBoxSize();
636             this.setThumbBoxPosition();
637             this.setCanvasPosition();
638         }
639         
640         this.draw();
641         
642         this.fireEvent('rotate', this, 'left');
643         
644     },
645     
646     onRotateRight : function(e)
647     {
648         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
649             
650             var minScale = this.thumbEl.getWidth() / this.minWidth;
651         
652             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
653             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
654             
655             this.startScale = this.scale;
656             
657             while (this.getScaleLevel() < minScale){
658             
659                 this.scale = this.scale + 1;
660                 
661                 if(!this.zoomable()){
662                     break;
663                 }
664                 
665                 if(
666                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
667                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
668                 ){
669                     continue;
670                 }
671                 
672                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
673
674                 this.draw();
675                 
676                 return;
677             }
678             
679             this.scale = this.startScale;
680             
681             this.onRotateFail();
682             
683             return false;
684         }
685         
686         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
687
688         if(this.isDocument){
689             this.setThumbBoxSize();
690             this.setThumbBoxPosition();
691             this.setCanvasPosition();
692         }
693         
694         this.draw();
695         
696         this.fireEvent('rotate', this, 'right');
697     },
698     
699     onRotateFail : function()
700     {
701         this.errorEl.show(true);
702         
703         var _this = this;
704         
705         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
706     },
707     
708     draw : function()
709     {
710         this.previewEl.dom.innerHTML = '';
711         
712         var canvasEl = document.createElement("canvas");
713         
714         var contextEl = canvasEl.getContext("2d");
715         
716         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
717         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
718         var center = this.imageEl.OriginWidth / 2;
719         
720         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
721             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
722             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
723             center = this.imageEl.OriginHeight / 2;
724         }
725         
726         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
727         
728         contextEl.translate(center, center);
729         contextEl.rotate(this.rotate * Math.PI / 180);
730
731         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
732         
733         this.canvasEl = document.createElement("canvas");
734         
735         this.contextEl = this.canvasEl.getContext("2d");
736         
737         switch (this.rotate) {
738             case 0 :
739                 
740                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
741                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
742                 
743                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
744                 
745                 break;
746             case 90 : 
747                 
748                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
749                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
750                 
751                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
752                     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);
753                     break;
754                 }
755                 
756                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
757                 
758                 break;
759             case 180 :
760                 
761                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
762                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
763                 
764                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
765                     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);
766                     break;
767                 }
768                 
769                 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);
770                 
771                 break;
772             case 270 :
773                 
774                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
775                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
776         
777                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
778                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
779                     break;
780                 }
781                 
782                 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);
783                 
784                 break;
785             default : 
786                 break;
787         }
788         
789         this.previewEl.appendChild(this.canvasEl);
790         
791         this.setCanvasPosition();
792     },
793     
794     crop : function()
795     {
796         if(!this.canvasLoaded){
797             return;
798         }
799         
800         if(this.loadMask){
801             this.maskEl.mask(this.loadingText);
802         }
803         
804         var imageCanvas = document.createElement("canvas");
805         
806         var imageContext = imageCanvas.getContext("2d");
807         
808         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
809         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
810         
811         var center = imageCanvas.width / 2;
812         
813         imageContext.translate(center, center);
814         
815         imageContext.rotate(this.rotate * Math.PI / 180);
816         
817         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
818         
819         var canvas = document.createElement("canvas");
820         
821         var context = canvas.getContext("2d");
822                 
823         canvas.width = this.minWidth;
824         canvas.height = this.minHeight;
825
826         switch (this.rotate) {
827             case 0 :
828                 
829                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
830                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
831                 
832                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
833                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
834                 
835                 var targetWidth = this.minWidth - 2 * x;
836                 var targetHeight = this.minHeight - 2 * y;
837                 
838                 var scale = 1;
839                 
840                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
841                     scale = targetWidth / width;
842                 }
843                 
844                 if(x > 0 && y == 0){
845                     scale = targetHeight / height;
846                 }
847                 
848                 if(x > 0 && y > 0){
849                     scale = targetWidth / width;
850                     
851                     if(width < height){
852                         scale = targetHeight / height;
853                     }
854                 }
855                 
856                 context.scale(scale, scale);
857                 
858                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
859                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
860
861                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
862                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
863
864                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
865                 
866                 break;
867             case 90 : 
868                 
869                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
870                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
871                 
872                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
873                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
874                 
875                 var targetWidth = this.minWidth - 2 * x;
876                 var targetHeight = this.minHeight - 2 * y;
877                 
878                 var scale = 1;
879                 
880                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
881                     scale = targetWidth / width;
882                 }
883                 
884                 if(x > 0 && y == 0){
885                     scale = targetHeight / height;
886                 }
887                 
888                 if(x > 0 && y > 0){
889                     scale = targetWidth / width;
890                     
891                     if(width < height){
892                         scale = targetHeight / height;
893                     }
894                 }
895                 
896                 context.scale(scale, scale);
897                 
898                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
899                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
900
901                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
902                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
903                 
904                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
905                 
906                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
907                 
908                 break;
909             case 180 :
910                 
911                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
912                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
913                 
914                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
915                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
916                 
917                 var targetWidth = this.minWidth - 2 * x;
918                 var targetHeight = this.minHeight - 2 * y;
919                 
920                 var scale = 1;
921                 
922                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
923                     scale = targetWidth / width;
924                 }
925                 
926                 if(x > 0 && y == 0){
927                     scale = targetHeight / height;
928                 }
929                 
930                 if(x > 0 && y > 0){
931                     scale = targetWidth / width;
932                     
933                     if(width < height){
934                         scale = targetHeight / height;
935                     }
936                 }
937                 
938                 context.scale(scale, scale);
939                 
940                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
941                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
942
943                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
944                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
945
946                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
947                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
948                 
949                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
950                 
951                 break;
952             case 270 :
953                 
954                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
955                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
956                 
957                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
958                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
959                 
960                 var targetWidth = this.minWidth - 2 * x;
961                 var targetHeight = this.minHeight - 2 * y;
962                 
963                 var scale = 1;
964                 
965                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
966                     scale = targetWidth / width;
967                 }
968                 
969                 if(x > 0 && y == 0){
970                     scale = targetHeight / height;
971                 }
972                 
973                 if(x > 0 && y > 0){
974                     scale = targetWidth / width;
975                     
976                     if(width < height){
977                         scale = targetHeight / height;
978                     }
979                 }
980                 
981                 context.scale(scale, scale);
982                 
983                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
984                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
985
986                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
987                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
988                 
989                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
990                 
991                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
992                 
993                 break;
994             default : 
995                 break;
996         }
997         
998         this.cropData = canvas.toDataURL(this.cropType);
999         
1000         if(this.loadMask){
1001             this.maskEl.unmask();
1002         }
1003         
1004         if(this.fireEvent('crop', this, this.cropData) !== false){
1005             this.process(this.file, this.cropData);
1006         }
1007         
1008         return;
1009         
1010     },
1011     
1012     setThumbBoxSize : function()
1013     {
1014         var width, height;
1015         
1016         if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1017             width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1018             height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1019             
1020             this.minWidth = width;
1021             this.minHeight = height;
1022             
1023             if(this.rotate == 90 || this.rotate == 270){
1024                 this.minWidth = height;
1025                 this.minHeight = width;
1026             }
1027         }
1028         
1029         height = 300;
1030         width = Math.ceil(this.minWidth * height / this.minHeight);
1031         
1032         if(this.minWidth > this.minHeight){
1033             width = 300;
1034             height = Math.ceil(this.minHeight * width / this.minWidth);
1035         }
1036         
1037         this.thumbEl.setStyle({
1038             width : width + 'px',
1039             height : height + 'px'
1040         });
1041
1042         return;
1043             
1044     },
1045     
1046     setThumbBoxPosition : function()
1047     {
1048         var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1049         var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1050         
1051         this.thumbEl.setLeft(x);
1052         this.thumbEl.setTop(y);
1053         
1054     },
1055     
1056     baseRotateLevel : function()
1057     {
1058         this.baseRotate = 1;
1059         
1060         if(
1061                 typeof(this.exif) != 'undefined' &&
1062                 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1063                 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1064         ){
1065             this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1066         }
1067         
1068         this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1069         
1070     },
1071     
1072     baseScaleLevel : function()
1073     {
1074         var width, height;
1075         
1076         if(this.isDocument){
1077             
1078             if(this.baseRotate == 6 || this.baseRotate == 8){
1079             
1080                 height = this.thumbEl.getHeight();
1081                 this.baseScale = height / this.imageEl.OriginWidth;
1082
1083                 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1084                     width = this.thumbEl.getWidth();
1085                     this.baseScale = width / this.imageEl.OriginHeight;
1086                 }
1087
1088                 return;
1089             }
1090
1091             height = this.thumbEl.getHeight();
1092             this.baseScale = height / this.imageEl.OriginHeight;
1093
1094             if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1095                 width = this.thumbEl.getWidth();
1096                 this.baseScale = width / this.imageEl.OriginWidth;
1097             }
1098
1099             return;
1100         }
1101         
1102         if(this.baseRotate == 6 || this.baseRotate == 8){
1103             
1104             width = this.thumbEl.getHeight();
1105             this.baseScale = width / this.imageEl.OriginHeight;
1106             
1107             if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1108                 height = this.thumbEl.getWidth();
1109                 this.baseScale = height / this.imageEl.OriginHeight;
1110             }
1111             
1112             if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1113                 height = this.thumbEl.getWidth();
1114                 this.baseScale = height / this.imageEl.OriginHeight;
1115                 
1116                 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1117                     width = this.thumbEl.getHeight();
1118                     this.baseScale = width / this.imageEl.OriginWidth;
1119                 }
1120             }
1121             
1122             return;
1123         }
1124         
1125         width = this.thumbEl.getWidth();
1126         this.baseScale = width / this.imageEl.OriginWidth;
1127         
1128         if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1129             height = this.thumbEl.getHeight();
1130             this.baseScale = height / this.imageEl.OriginHeight;
1131         }
1132         
1133         if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1134             
1135             height = this.thumbEl.getHeight();
1136             this.baseScale = height / this.imageEl.OriginHeight;
1137             
1138             if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1139                 width = this.thumbEl.getWidth();
1140                 this.baseScale = width / this.imageEl.OriginWidth;
1141             }
1142             
1143         }
1144         
1145         return;
1146     },
1147     
1148     getScaleLevel : function()
1149     {
1150         return this.baseScale * Math.pow(1.1, this.scale);
1151     },
1152     
1153     onTouchStart : function(e)
1154     {
1155         if(!this.canvasLoaded){
1156             this.beforeSelectFile(e);
1157             return;
1158         }
1159         
1160         var touches = e.browserEvent.touches;
1161         
1162         if(!touches){
1163             return;
1164         }
1165         
1166         if(touches.length == 1){
1167             this.onMouseDown(e);
1168             return;
1169         }
1170         
1171         if(touches.length != 2){
1172             return;
1173         }
1174         
1175         var coords = [];
1176         
1177         for(var i = 0, finger; finger = touches[i]; i++){
1178             coords.push(finger.pageX, finger.pageY);
1179         }
1180         
1181         var x = Math.pow(coords[0] - coords[2], 2);
1182         var y = Math.pow(coords[1] - coords[3], 2);
1183         
1184         this.startDistance = Math.sqrt(x + y);
1185         
1186         this.startScale = this.scale;
1187         
1188         this.pinching = true;
1189         this.dragable = false;
1190         
1191     },
1192     
1193     onTouchMove : function(e)
1194     {
1195         if(!this.pinching && !this.dragable){
1196             return;
1197         }
1198         
1199         var touches = e.browserEvent.touches;
1200         
1201         if(!touches){
1202             return;
1203         }
1204         
1205         if(this.dragable){
1206             this.onMouseMove(e);
1207             return;
1208         }
1209         
1210         var coords = [];
1211         
1212         for(var i = 0, finger; finger = touches[i]; i++){
1213             coords.push(finger.pageX, finger.pageY);
1214         }
1215         
1216         var x = Math.pow(coords[0] - coords[2], 2);
1217         var y = Math.pow(coords[1] - coords[3], 2);
1218         
1219         this.endDistance = Math.sqrt(x + y);
1220         
1221         this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1222         
1223         if(!this.zoomable()){
1224             this.scale = this.startScale;
1225             return;
1226         }
1227         
1228         this.draw();
1229         
1230     },
1231     
1232     onTouchEnd : function(e)
1233     {
1234         this.pinching = false;
1235         this.dragable = false;
1236         
1237     },
1238     
1239     process : function(file, crop)
1240     {
1241         this.xhr = new XMLHttpRequest();
1242         
1243         file.xhr = this.xhr;
1244
1245         this.xhr.open(this.method, this.url, true);
1246         
1247         var headers = {
1248             "Accept": "application/json",
1249             "Cache-Control": "no-cache",
1250             "X-Requested-With": "XMLHttpRequest"
1251         };
1252         
1253         for (var headerName in headers) {
1254             var headerValue = headers[headerName];
1255             if (headerValue) {
1256                 this.xhr.setRequestHeader(headerName, headerValue);
1257             }
1258         }
1259         
1260         var _this = this;
1261         
1262         this.xhr.onload = function()
1263         {
1264             _this.xhrOnLoad(_this.xhr);
1265         }
1266         
1267         this.xhr.onerror = function()
1268         {
1269             _this.xhrOnError(_this.xhr);
1270         }
1271         
1272         var formData = new FormData();
1273
1274         formData.append('returnHTML', 'NO');
1275         
1276         if(crop){
1277             formData.append('crop', crop);
1278         }
1279         
1280         if(typeof(file) != 'undefined' && (typeof(file.id) == 'undefined' || file.id * 1 < 1)){
1281             formData.append(this.paramName, file, file.name);
1282         }
1283         
1284         if(typeof(file.filename) != 'undefined'){
1285             formData.append('filename', file.filename);
1286         }
1287         
1288         if(typeof(file.mimetype) != 'undefined'){
1289             formData.append('mimetype', file.mimetype);
1290         }
1291         
1292         if(this.fireEvent('arrange', this, formData) != false){
1293             this.xhr.send(formData);
1294         };
1295     },
1296     
1297     xhrOnLoad : function(xhr)
1298     {
1299         if (xhr.readyState !== 4) {
1300             this.fireEvent('exception', this, xhr);
1301             return;
1302         }
1303
1304         var response = Roo.decode(xhr.responseText);
1305         
1306         if(!response.success){
1307             this.fireEvent('exception', this, xhr);
1308             return;
1309         }
1310         
1311         var response = Roo.decode(xhr.responseText);
1312         
1313         this.fireEvent('upload', this, response);
1314         
1315     },
1316     
1317     xhrOnError : function()
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         this.file = false;
1330         this.exif = {};
1331         
1332         if(typeof(file) === 'string'){
1333             this.loadCanvas(file);
1334             return;
1335         }
1336         
1337         if(!file || !this.urlAPI){
1338             return;
1339         }
1340         
1341         this.file = file;
1342         this.cropType = file.type;
1343         
1344         var _this = this;
1345         
1346         if(this.fireEvent('prepare', this, this.file) != false){
1347             
1348             var reader = new FileReader();
1349             
1350             reader.onload = function (e) {
1351                 if (e.target.error) {
1352                     Roo.log(e.target.error);
1353                     return;
1354                 }
1355                 
1356                 var buffer = e.target.result,
1357                     dataView = new DataView(buffer),
1358                     offset = 2,
1359                     maxOffset = dataView.byteLength - 4,
1360                     markerBytes,
1361                     markerLength;
1362                 
1363                 if (dataView.getUint16(0) === 0xffd8) {
1364                     while (offset < maxOffset) {
1365                         markerBytes = dataView.getUint16(offset);
1366                         
1367                         if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1368                             markerLength = dataView.getUint16(offset + 2) + 2;
1369                             if (offset + markerLength > dataView.byteLength) {
1370                                 Roo.log('Invalid meta data: Invalid segment size.');
1371                                 break;
1372                             }
1373                             
1374                             if(markerBytes == 0xffe1){
1375                                 _this.parseExifData(
1376                                     dataView,
1377                                     offset,
1378                                     markerLength
1379                                 );
1380                             }
1381                             
1382                             offset += markerLength;
1383                             
1384                             continue;
1385                         }
1386                         
1387                         break;
1388                     }
1389                     
1390                 }
1391                 
1392                 var url = _this.urlAPI.createObjectURL(_this.file);
1393                 
1394                 _this.loadCanvas(url);
1395                 
1396                 return;
1397             }
1398             
1399             reader.readAsArrayBuffer(this.file);
1400             
1401         }
1402         
1403     },
1404     
1405     parseExifData : function(dataView, offset, length)
1406     {
1407         var tiffOffset = offset + 10,
1408             littleEndian,
1409             dirOffset;
1410     
1411         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1412             // No Exif data, might be XMP data instead
1413             return;
1414         }
1415         
1416         // Check for the ASCII code for "Exif" (0x45786966):
1417         if (dataView.getUint32(offset + 4) !== 0x45786966) {
1418             // No Exif data, might be XMP data instead
1419             return;
1420         }
1421         if (tiffOffset + 8 > dataView.byteLength) {
1422             Roo.log('Invalid Exif data: Invalid segment size.');
1423             return;
1424         }
1425         // Check for the two null bytes:
1426         if (dataView.getUint16(offset + 8) !== 0x0000) {
1427             Roo.log('Invalid Exif data: Missing byte alignment offset.');
1428             return;
1429         }
1430         // Check the byte alignment:
1431         switch (dataView.getUint16(tiffOffset)) {
1432         case 0x4949:
1433             littleEndian = true;
1434             break;
1435         case 0x4D4D:
1436             littleEndian = false;
1437             break;
1438         default:
1439             Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1440             return;
1441         }
1442         // Check for the TIFF tag marker (0x002A):
1443         if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1444             Roo.log('Invalid Exif data: Missing TIFF marker.');
1445             return;
1446         }
1447         // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1448         dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1449         
1450         this.parseExifTags(
1451             dataView,
1452             tiffOffset,
1453             tiffOffset + dirOffset,
1454             littleEndian
1455         );
1456     },
1457     
1458     parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1459     {
1460         var tagsNumber,
1461             dirEndOffset,
1462             i;
1463         if (dirOffset + 6 > dataView.byteLength) {
1464             Roo.log('Invalid Exif data: Invalid directory offset.');
1465             return;
1466         }
1467         tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1468         dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1469         if (dirEndOffset + 4 > dataView.byteLength) {
1470             Roo.log('Invalid Exif data: Invalid directory size.');
1471             return;
1472         }
1473         for (i = 0; i < tagsNumber; i += 1) {
1474             this.parseExifTag(
1475                 dataView,
1476                 tiffOffset,
1477                 dirOffset + 2 + 12 * i, // tag offset
1478                 littleEndian
1479             );
1480         }
1481         // Return the offset to the next directory:
1482         return dataView.getUint32(dirEndOffset, littleEndian);
1483     },
1484     
1485     parseExifTag : function (dataView, tiffOffset, offset, littleEndian) 
1486     {
1487         var tag = dataView.getUint16(offset, littleEndian);
1488         
1489         this.exif[tag] = this.getExifValue(
1490             dataView,
1491             tiffOffset,
1492             offset,
1493             dataView.getUint16(offset + 2, littleEndian), // tag type
1494             dataView.getUint32(offset + 4, littleEndian), // tag length
1495             littleEndian
1496         );
1497     },
1498     
1499     getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1500     {
1501         var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1502             tagSize,
1503             dataOffset,
1504             values,
1505             i,
1506             str,
1507             c;
1508     
1509         if (!tagType) {
1510             Roo.log('Invalid Exif data: Invalid tag type.');
1511             return;
1512         }
1513         
1514         tagSize = tagType.size * length;
1515         // Determine if the value is contained in the dataOffset bytes,
1516         // or if the value at the dataOffset is a pointer to the actual data:
1517         dataOffset = tagSize > 4 ?
1518                 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1519         if (dataOffset + tagSize > dataView.byteLength) {
1520             Roo.log('Invalid Exif data: Invalid data offset.');
1521             return;
1522         }
1523         if (length === 1) {
1524             return tagType.getValue(dataView, dataOffset, littleEndian);
1525         }
1526         values = [];
1527         for (i = 0; i < length; i += 1) {
1528             values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1529         }
1530         
1531         if (tagType.ascii) {
1532             str = '';
1533             // Concatenate the chars:
1534             for (i = 0; i < values.length; i += 1) {
1535                 c = values[i];
1536                 // Ignore the terminating NULL byte(s):
1537                 if (c === '\u0000') {
1538                     break;
1539                 }
1540                 str += c;
1541             }
1542             return str;
1543         }
1544         return values;
1545     }
1546     
1547 });
1548
1549 Roo.apply(Roo.bootstrap.UploadCropbox, {
1550     tags : {
1551         'Orientation': 0x0112
1552     },
1553     
1554     Orientation: {
1555             1: 0, //'top-left',
1556 //            2: 'top-right',
1557             3: 180, //'bottom-right',
1558 //            4: 'bottom-left',
1559 //            5: 'left-top',
1560             6: 90, //'right-top',
1561 //            7: 'right-bottom',
1562             8: 270 //'left-bottom'
1563     },
1564     
1565     exifTagTypes : {
1566         // byte, 8-bit unsigned int:
1567         1: {
1568             getValue: function (dataView, dataOffset) {
1569                 return dataView.getUint8(dataOffset);
1570             },
1571             size: 1
1572         },
1573         // ascii, 8-bit byte:
1574         2: {
1575             getValue: function (dataView, dataOffset) {
1576                 return String.fromCharCode(dataView.getUint8(dataOffset));
1577             },
1578             size: 1,
1579             ascii: true
1580         },
1581         // short, 16 bit int:
1582         3: {
1583             getValue: function (dataView, dataOffset, littleEndian) {
1584                 return dataView.getUint16(dataOffset, littleEndian);
1585             },
1586             size: 2
1587         },
1588         // long, 32 bit int:
1589         4: {
1590             getValue: function (dataView, dataOffset, littleEndian) {
1591                 return dataView.getUint32(dataOffset, littleEndian);
1592             },
1593             size: 4
1594         },
1595         // rational = two long values, first is numerator, second is denominator:
1596         5: {
1597             getValue: function (dataView, dataOffset, littleEndian) {
1598                 return dataView.getUint32(dataOffset, littleEndian) /
1599                     dataView.getUint32(dataOffset + 4, littleEndian);
1600             },
1601             size: 8
1602         },
1603         // slong, 32 bit signed int:
1604         9: {
1605             getValue: function (dataView, dataOffset, littleEndian) {
1606                 return dataView.getInt32(dataOffset, littleEndian);
1607             },
1608             size: 4
1609         },
1610         // srational, two slongs, first is numerator, second is denominator:
1611         10: {
1612             getValue: function (dataView, dataOffset, littleEndian) {
1613                 return dataView.getInt32(dataOffset, littleEndian) /
1614                     dataView.getInt32(dataOffset + 4, littleEndian);
1615             },
1616             size: 8
1617         }
1618     },
1619     
1620     footer : {
1621         STANDARD : [
1622             {
1623                 tag : 'div',
1624                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1625                 action : 'rotate-left',
1626                 cn : [
1627                     {
1628                         tag : 'button',
1629                         cls : 'btn btn-default',
1630                         html : '<i class="fa fa-undo"></i>'
1631                     }
1632                 ]
1633             },
1634             {
1635                 tag : 'div',
1636                 cls : 'btn-group roo-upload-cropbox-picture',
1637                 action : 'picture',
1638                 cn : [
1639                     {
1640                         tag : 'button',
1641                         cls : 'btn btn-default',
1642                         html : '<i class="fa fa-picture-o"></i>'
1643                     }
1644                 ]
1645             },
1646             {
1647                 tag : 'div',
1648                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1649                 action : 'rotate-right',
1650                 cn : [
1651                     {
1652                         tag : 'button',
1653                         cls : 'btn btn-default',
1654                         html : '<i class="fa fa-repeat"></i>'
1655                     }
1656                 ]
1657             }
1658         ],
1659         DOCUMENT : [
1660             {
1661                 tag : 'div',
1662                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1663                 action : 'rotate-left',
1664                 cn : [
1665                     {
1666                         tag : 'button',
1667                         cls : 'btn btn-default',
1668                         html : '<i class="fa fa-undo"></i>'
1669                     }
1670                 ]
1671             },
1672             {
1673                 tag : 'div',
1674                 cls : 'btn-group roo-upload-cropbox-download',
1675                 action : 'download',
1676                 cn : [
1677                     {
1678                         tag : 'button',
1679                         cls : 'btn btn-default',
1680                         html : '<i class="fa fa-download"></i>'
1681                     }
1682                 ]
1683             },
1684             {
1685                 tag : 'div',
1686                 cls : 'btn-group roo-upload-cropbox-crop',
1687                 action : 'crop',
1688                 cn : [
1689                     {
1690                         tag : 'button',
1691                         cls : 'btn btn-default',
1692                         html : '<i class="fa fa-crop"></i>'
1693                     }
1694                 ]
1695             },
1696             {
1697                 tag : 'div',
1698                 cls : 'btn-group roo-upload-cropbox-trash',
1699                 action : 'trash',
1700                 cn : [
1701                     {
1702                         tag : 'button',
1703                         cls : 'btn btn-default',
1704                         html : '<i class="fa fa-trash"></i>'
1705                     }
1706                 ]
1707             },
1708             {
1709                 tag : 'div',
1710                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1711                 action : 'rotate-right',
1712                 cn : [
1713                     {
1714                         tag : 'button',
1715                         cls : 'btn btn-default',
1716                         html : '<i class="fa fa-repeat"></i>'
1717                     }
1718                 ]
1719             }
1720         ],
1721         ROTATOR : [
1722             {
1723                 tag : 'div',
1724                 cls : 'btn-group roo-upload-cropbox-rotate-left',
1725                 action : 'rotate-left',
1726                 cn : [
1727                     {
1728                         tag : 'button',
1729                         cls : 'btn btn-default',
1730                         html : '<i class="fa fa-undo"></i>'
1731                     }
1732                 ]
1733             },
1734             {
1735                 tag : 'div',
1736                 cls : 'btn-group roo-upload-cropbox-rotate-right',
1737                 action : 'rotate-right',
1738                 cn : [
1739                     {
1740                         tag : 'button',
1741                         cls : 'btn btn-default',
1742                         html : '<i class="fa fa-repeat"></i>'
1743                     }
1744                 ]
1745             }
1746         ]
1747     }
1748 });