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