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