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         this.baseRotateLevel();
397         
398         if(this.isDocument){
399             this.setThumbBoxSize();
400         }
401         
402         this.setThumbBoxPosition();
403         
404         this.baseScaleLevel();
405         
406         this.draw();
407         
408         this.resize();
409         
410         this.canvasLoaded = true;
411         
412     },
413     
414     setCanvasPosition : function()
415     {   
416         if(!this.canvasEl){
417             return;
418         }
419         
420         var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
421         var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
422         
423         this.previewEl.setLeft(pw);
424         this.previewEl.setTop(ph);
425         
426     },
427     
428     onMouseDown : function(e)
429     {   
430         e.stopEvent();
431         
432         this.dragable = true;
433         this.pinching = false;
434         
435         if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
436             this.dragable = false;
437             return;
438         }
439         
440         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
441         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
442         
443     },
444     
445     onMouseMove : function(e)
446     {   
447         e.stopEvent();
448         
449         if(!this.canvasLoaded){
450             return;
451         }
452         
453         if (!this.dragable){
454             return;
455         }
456         
457         var minX = Math.ceil(this.thumbEl.getLeft(true));
458         var minY = Math.ceil(this.thumbEl.getTop(true));
459         
460         var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
461         var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
462         
463         var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
464         var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
465         
466         x = x - this.mouseX;
467         y = y - this.mouseY;
468         
469         var bgX = Math.ceil(x + this.previewEl.getLeft(true));
470         var bgY = Math.ceil(y + this.previewEl.getTop(true));
471         
472         bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
473         bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
474         
475         this.previewEl.setLeft(bgX);
476         this.previewEl.setTop(bgY);
477         
478         this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
479         this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
480     },
481     
482     onMouseUp : function(e)
483     {   
484         e.stopEvent();
485         
486         this.dragable = false;
487     },
488     
489     onMouseWheel : function(e)
490     {   
491         e.stopEvent();
492         
493         this.startScale = this.scale;
494         
495         this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
496         
497         if(!this.zoomable()){
498             this.scale = this.startScale;
499             return;
500         }
501         
502         this.draw();
503         
504         return;
505     },
506     
507     zoomable : function()
508     {
509         var minScale = this.thumbEl.getWidth() / this.minWidth;
510         
511         if(this.minWidth < this.minHeight){
512             minScale = this.thumbEl.getHeight() / this.minHeight;
513         }
514         
515         var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
516         var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
517         
518         if(
519                 this.isDocument &&
520                 (this.rotate == 0 || this.rotate == 180) && 
521                 (
522                     width > this.imageEl.OriginWidth || 
523                     height > this.imageEl.OriginHeight ||
524                     (width < this.minWidth && height < this.minHeight)
525                 )
526         ){
527             return false;
528         }
529         
530         if(
531                 this.isDocument &&
532                 (this.rotate == 90 || this.rotate == 270) && 
533                 (
534                     width > this.imageEl.OriginWidth || 
535                     height > this.imageEl.OriginHeight ||
536                     (width < this.minHeight && height < this.minWidth)
537                 )
538         ){
539             return false;
540         }
541         
542         if(
543                 !this.isDocument &&
544                 (this.rotate == 0 || this.rotate == 180) && 
545                 (
546                     width < this.minWidth || 
547                     width > this.imageEl.OriginWidth || 
548                     height < this.minHeight || 
549                     height > this.imageEl.OriginHeight
550                 )
551         ){
552             return false;
553         }
554         
555         if(
556                 !this.isDocument &&
557                 (this.rotate == 90 || this.rotate == 270) && 
558                 (
559                     width < this.minHeight || 
560                     width > this.imageEl.OriginWidth || 
561                     height < this.minWidth || 
562                     height > this.imageEl.OriginHeight
563                 )
564         ){
565             return false;
566         }
567         
568         return true;
569         
570     },
571     
572     onRotateLeft : function(e)
573     {   
574         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
575             
576             var minScale = this.thumbEl.getWidth() / this.minWidth;
577             
578             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
579             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
580             
581             this.startScale = this.scale;
582             
583             while (this.getScaleLevel() < minScale){
584             
585                 this.scale = this.scale + 1;
586                 
587                 if(!this.zoomable()){
588                     break;
589                 }
590                 
591                 if(
592                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
593                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
594                 ){
595                     continue;
596                 }
597                 
598                 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
599
600                 this.draw();
601                 
602                 return;
603             }
604             
605             this.scale = this.startScale;
606             
607             this.onRotateFail();
608             
609             return false;
610         }
611         
612         this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
613
614         if(this.isDocument){
615             this.setThumbBoxSize();
616             this.setThumbBoxPosition();
617             this.setCanvasPosition();
618         }
619         
620         this.draw();
621         
622         this.fireEvent('rotate', this, 'left');
623         
624     },
625     
626     onRotateRight : function(e)
627     {
628         if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
629             
630             var minScale = this.thumbEl.getWidth() / this.minWidth;
631         
632             var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
633             var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
634             
635             this.startScale = this.scale;
636             
637             while (this.getScaleLevel() < minScale){
638             
639                 this.scale = this.scale + 1;
640                 
641                 if(!this.zoomable()){
642                     break;
643                 }
644                 
645                 if(
646                         Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
647                         Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
648                 ){
649                     continue;
650                 }
651                 
652                 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
653
654                 this.draw();
655                 
656                 return;
657             }
658             
659             this.scale = this.startScale;
660             
661             this.onRotateFail();
662             
663             return false;
664         }
665         
666         this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
667
668         if(this.isDocument){
669             this.setThumbBoxSize();
670             this.setThumbBoxPosition();
671             this.setCanvasPosition();
672         }
673         
674         this.draw();
675         
676         this.fireEvent('rotate', this, 'right');
677     },
678     
679     onRotateFail : function()
680     {
681         this.errorEl.show(true);
682         
683         var _this = this;
684         
685         (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
686     },
687     
688     draw : function()
689     {
690         this.previewEl.dom.innerHTML = '';
691         
692         var canvasEl = document.createElement("canvas");
693         
694         var contextEl = canvasEl.getContext("2d");
695         
696         canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
697         canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
698         var center = this.imageEl.OriginWidth / 2;
699         
700         if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
701             canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
702             canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
703             center = this.imageEl.OriginHeight / 2;
704         }
705         
706         contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
707         
708         contextEl.translate(center, center);
709         contextEl.rotate(this.rotate * Math.PI / 180);
710
711         contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
712         
713         this.canvasEl = document.createElement("canvas");
714         
715         this.contextEl = this.canvasEl.getContext("2d");
716         
717         switch (this.rotate) {
718             case 0 :
719                 
720                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
721                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
722                 
723                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
724                 
725                 break;
726             case 90 : 
727                 
728                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
729                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
730                 
731                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
732                     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);
733                     break;
734                 }
735                 
736                 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
737                 
738                 break;
739             case 180 :
740                 
741                 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
742                 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
743                 
744                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
745                     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);
746                     break;
747                 }
748                 
749                 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);
750                 
751                 break;
752             case 270 :
753                 
754                 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
755                 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
756         
757                 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
758                     this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
759                     break;
760                 }
761                 
762                 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);
763                 
764                 break;
765             default : 
766                 break;
767         }
768         
769         this.previewEl.appendChild(this.canvasEl);
770         
771         this.setCanvasPosition();
772     },
773     
774     crop : function()
775     {
776         if(!this.canvasLoaded){
777             return;
778         }
779         
780         var imageCanvas = document.createElement("canvas");
781         
782         var imageContext = imageCanvas.getContext("2d");
783         
784         imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
785         imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
786         
787         var center = imageCanvas.width / 2;
788         
789         imageContext.translate(center, center);
790         
791         imageContext.rotate(this.rotate * Math.PI / 180);
792         
793         imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
794         
795         var canvas = document.createElement("canvas");
796         
797         var context = canvas.getContext("2d");
798                 
799         canvas.width = this.minWidth;
800         canvas.height = this.minHeight;
801
802         switch (this.rotate) {
803             case 0 :
804                 
805                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
806                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
807                 
808                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
809                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
810                 
811                 var targetWidth = this.minWidth - 2 * x;
812                 var targetHeight = this.minHeight - 2 * y;
813                 
814                 var scale = 1;
815                 
816                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
817                     scale = targetWidth / width;
818                 }
819                 
820                 if(x > 0 && y == 0){
821                     scale = targetHeight / height;
822                 }
823                 
824                 if(x > 0 && y > 0){
825                     scale = targetWidth / width;
826                     
827                     if(width < height){
828                         scale = targetHeight / height;
829                     }
830                 }
831                 
832                 context.scale(scale, scale);
833                 
834                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
835                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
836
837                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
838                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
839
840                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
841                 
842                 break;
843             case 90 : 
844                 
845                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
846                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
847                 
848                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
849                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
850                 
851                 var targetWidth = this.minWidth - 2 * x;
852                 var targetHeight = this.minHeight - 2 * y;
853                 
854                 var scale = 1;
855                 
856                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
857                     scale = targetWidth / width;
858                 }
859                 
860                 if(x > 0 && y == 0){
861                     scale = targetHeight / height;
862                 }
863                 
864                 if(x > 0 && y > 0){
865                     scale = targetWidth / width;
866                     
867                     if(width < height){
868                         scale = targetHeight / height;
869                     }
870                 }
871                 
872                 context.scale(scale, scale);
873                 
874                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
875                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
876
877                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
878                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
879                 
880                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
881                 
882                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
883                 
884                 break;
885             case 180 :
886                 
887                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
888                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
889                 
890                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
891                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
892                 
893                 var targetWidth = this.minWidth - 2 * x;
894                 var targetHeight = this.minHeight - 2 * y;
895                 
896                 var scale = 1;
897                 
898                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
899                     scale = targetWidth / width;
900                 }
901                 
902                 if(x > 0 && y == 0){
903                     scale = targetHeight / height;
904                 }
905                 
906                 if(x > 0 && y > 0){
907                     scale = targetWidth / width;
908                     
909                     if(width < height){
910                         scale = targetHeight / height;
911                     }
912                 }
913                 
914                 context.scale(scale, scale);
915                 
916                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
917                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
918
919                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
920                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
921
922                 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
923                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
924                 
925                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
926                 
927                 break;
928             case 270 :
929                 
930                 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
931                 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
932                 
933                 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
934                 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
935                 
936                 var targetWidth = this.minWidth - 2 * x;
937                 var targetHeight = this.minHeight - 2 * y;
938                 
939                 var scale = 1;
940                 
941                 if((x == 0 && y == 0) || (x == 0 && y > 0)){
942                     scale = targetWidth / width;
943                 }
944                 
945                 if(x > 0 && y == 0){
946                     scale = targetHeight / height;
947                 }
948                 
949                 if(x > 0 && y > 0){
950                     scale = targetWidth / width;
951                     
952                     if(width < height){
953                         scale = targetHeight / height;
954                     }
955                 }
956                 
957                 context.scale(scale, scale);
958                 
959                 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
960                 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
961
962                 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
963                 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
964                 
965                 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
966                 
967                 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
968                 
969                 break;
970             default : 
971                 break;
972         }
973         
974         this.cropData = canvas.toDataURL(this.cropType);
975         
976         if(this.fireEvent('crop', this, this.cropData) !== false){
977             this.process(this.file, this.cropData);
978         }
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 });