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
20 * Create a new UploadCropbox
21 * @param {Object} config The config object
24 Roo.bootstrap.UploadCropbox = function(config){
25 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
29 * @event beforeselectfile
30 * Fire before select file
31 * @param {Roo.bootstrap.UploadCropbox} this
33 "beforeselectfile" : true,
36 * Fire after initEvent
37 * @param {Roo.bootstrap.UploadCropbox} this
42 * Fire after initEvent
43 * @param {Roo.bootstrap.UploadCropbox} this
44 * @param {String} data
49 * Fire when preparing the file data
50 * @param {Roo.bootstrap.UploadCropbox} this
51 * @param {Object} file
56 * Fire when get exception
57 * @param {Roo.bootstrap.UploadCropbox} this
58 * @param {Object} options
62 * @event beforeloadcanvas
63 * Fire before load the canvas
64 * @param {Roo.bootstrap.UploadCropbox} this
67 "beforeloadcanvas" : true,
70 * Fire when trash image
71 * @param {Roo.bootstrap.UploadCropbox} this
76 * Fire when download the image
77 * @param {Roo.bootstrap.UploadCropbox} this
81 * @event footerbuttonclick
82 * Fire when footerbuttonclick
83 * @param {Roo.bootstrap.UploadCropbox} this
84 * @param {String} type
86 "footerbuttonclick" : true,
90 * @param {Roo.bootstrap.UploadCropbox} this
95 * Fire when rotate the image
96 * @param {Roo.bootstrap.UploadCropbox} this
102 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
105 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
107 emptyText : 'Click to upload image',
108 rotateNotify : 'Image is too small to rotate',
123 cropType : 'image/jpeg',
125 canvasLoaded : false,
128 getAutoCreate : function()
132 cls : 'roo-upload-cropbox',
136 cls : 'roo-upload-cropbox-body',
137 style : 'cursor:pointer',
141 cls : 'roo-upload-cropbox-preview'
145 cls : 'roo-upload-cropbox-thumb'
149 cls : 'roo-upload-cropbox-empty-notify',
150 html : this.emptyText
154 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
155 html : this.rotateNotify
161 cls : 'roo-upload-cropbox-footer',
164 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
174 onRender : function(ct, position)
176 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
178 if (this.buttons.length) {
180 Roo.each(this.buttons, function(bb) {
182 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
184 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
190 initEvents : function()
192 this.urlAPI = (window.createObjectURL && window) ||
193 (window.URL && URL.revokeObjectURL && URL) ||
194 (window.webkitURL && webkitURL);
196 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
197 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
199 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
200 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
202 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
203 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
206 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
207 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
209 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
210 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
213 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
214 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
215 this.footerEl.hide();
217 this.setThumbBoxSize();
223 this.fireEvent('initial', this);
230 window.addEventListener("resize", function() { _this.resize(); } );
232 this.bodyEl.on('click', this.beforeSelectFile, this);
235 this.bodyEl.on('touchstart', this.onTouchStart, this);
236 this.bodyEl.on('touchmove', this.onTouchMove, this);
237 this.bodyEl.on('touchend', this.onTouchEnd, this);
241 this.bodyEl.on('mousedown', this.onMouseDown, this);
242 this.bodyEl.on('mousemove', this.onMouseMove, this);
243 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
244 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
245 Roo.get(document).on('mouseup', this.onMouseUp, this);
255 this.dragable = false;
256 this.pinching = false;
259 this.cropData = false;
260 this.notifyEl.dom.innerHTML = this.emptyText;
266 if(this.fireEvent('resize', this) != false){
267 this.setThumbBoxPosition();
268 this.setCanvasPosition();
272 onFooterButtonClick : function(e, el, o, type)
276 this.onRotateLeft(e);
278 case 'rotate-right' :
279 this.onRotateRight(e);
282 this.beforeSelectFile(e);
297 this.fireEvent('footerbuttonclick', this, type);
300 beforeSelectFile : function(e)
302 this.fireEvent('beforeselectfile', this);
307 this.fireEvent('trash', this);
310 download : function(e)
312 this.fireEvent('download', this);
315 loadCanvas : function(src)
317 if(this.fireEvent('beforeloadcanvas', this, src) != false){
321 this.imageEl = document.createElement('img');
325 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
327 this.imageEl.src = src;
331 onLoadCanvas : function()
333 this.bodyEl.un('click', this.beforeSelectFile, this);
335 this.notifyEl.hide();
337 this.footerEl.show();
339 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
340 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
342 this.baseRotateLevel();
345 this.setThumbBoxSize();
348 this.setThumbBoxPosition();
350 this.baseScaleLevel();
356 this.canvasLoaded = true;
360 setCanvasPosition : function()
366 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
367 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
369 this.previewEl.setLeft(pw);
370 this.previewEl.setTop(ph);
374 onMouseDown : function(e)
378 this.dragable = true;
379 this.pinching = false;
381 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
382 this.dragable = false;
386 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
387 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
391 onMouseMove : function(e)
395 if(!this.canvasLoaded){
403 var minX = Math.ceil(this.thumbEl.getLeft(true));
404 var minY = Math.ceil(this.thumbEl.getTop(true));
406 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
407 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
409 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
410 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
415 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
416 var bgY = Math.ceil(y + this.previewEl.getTop(true));
418 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
419 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
421 this.previewEl.setLeft(bgX);
422 this.previewEl.setTop(bgY);
424 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
425 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
428 onMouseUp : function(e)
432 this.dragable = false;
435 onMouseWheel : function(e)
439 this.startScale = this.scale;
441 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
443 if(!this.zoomable()){
444 this.scale = this.startScale;
453 zoomable : function()
455 var minScale = this.thumbEl.getWidth() / this.minWidth;
457 if(this.minWidth < this.minHeight){
458 minScale = this.thumbEl.getHeight() / this.minHeight;
461 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
462 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
466 (this.rotate == 0 || this.rotate == 180) &&
468 width > this.imageEl.OriginWidth ||
469 height > this.imageEl.OriginHeight ||
470 (width < this.minWidth && height < this.minHeight)
478 (this.rotate == 90 || this.rotate == 270) &&
480 width > this.imageEl.OriginWidth ||
481 height > this.imageEl.OriginHeight ||
482 (width < this.minHeight && height < this.minWidth)
490 (this.rotate == 0 || this.rotate == 180) &&
492 width < this.minWidth ||
493 width > this.imageEl.OriginWidth ||
494 height < this.minHeight ||
495 height > this.imageEl.OriginHeight
503 (this.rotate == 90 || this.rotate == 270) &&
505 width < this.minHeight ||
506 width > this.imageEl.OriginWidth ||
507 height < this.minWidth ||
508 height > this.imageEl.OriginHeight
518 onRotateLeft : function(e)
520 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
522 var minScale = this.thumbEl.getWidth() / this.minWidth;
524 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
525 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
527 this.startScale = this.scale;
529 while (this.getScaleLevel() < minScale){
531 this.scale = this.scale + 1;
533 if(!this.zoomable()){
538 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
539 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
544 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
551 this.scale = this.startScale;
558 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
561 this.setThumbBoxSize();
562 this.setThumbBoxPosition();
563 this.setCanvasPosition();
568 this.fireEvent('rotate', this, 'left');
572 onRotateRight : function(e)
574 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
576 var minScale = this.thumbEl.getWidth() / this.minWidth;
578 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
579 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
581 this.startScale = this.scale;
583 while (this.getScaleLevel() < minScale){
585 this.scale = this.scale + 1;
587 if(!this.zoomable()){
592 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
593 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
598 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
605 this.scale = this.startScale;
612 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
615 this.setThumbBoxSize();
616 this.setThumbBoxPosition();
617 this.setCanvasPosition();
622 this.fireEvent('rotate', this, 'right');
625 onRotateFail : function()
627 this.errorEl.show(true);
631 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
636 this.previewEl.dom.innerHTML = '';
638 var canvasEl = document.createElement("canvas");
640 var contextEl = canvasEl.getContext("2d");
642 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
643 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
644 var center = this.imageEl.OriginWidth / 2;
646 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
647 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
648 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
649 center = this.imageEl.OriginHeight / 2;
652 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
654 contextEl.translate(center, center);
655 contextEl.rotate(this.rotate * Math.PI / 180);
657 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
659 this.canvasEl = document.createElement("canvas");
661 this.contextEl = this.canvasEl.getContext("2d");
663 switch (this.rotate) {
666 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
667 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
669 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
674 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
675 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
677 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
678 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);
682 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
687 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
688 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
690 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
691 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);
695 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);
700 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
701 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
703 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
704 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
708 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);
715 this.previewEl.appendChild(this.canvasEl);
717 this.setCanvasPosition();
722 if(!this.canvasLoaded){
726 var imageCanvas = document.createElement("canvas");
728 var imageContext = imageCanvas.getContext("2d");
730 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
731 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
733 var center = imageCanvas.width / 2;
735 imageContext.translate(center, center);
737 imageContext.rotate(this.rotate * Math.PI / 180);
739 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
741 var canvas = document.createElement("canvas");
743 var context = canvas.getContext("2d");
745 canvas.width = this.minWidth;
746 canvas.height = this.minHeight;
748 switch (this.rotate) {
751 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
752 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
754 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
755 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
757 var targetWidth = this.minWidth - 2 * x;
758 var targetHeight = this.minHeight - 2 * y;
762 if((x == 0 && y == 0) || (x == 0 && y > 0)){
763 scale = targetWidth / width;
767 scale = targetHeight / height;
771 scale = targetWidth / width;
774 scale = targetHeight / height;
778 context.scale(scale, scale);
780 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
781 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
783 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
784 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
786 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
791 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
792 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
794 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
795 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
797 var targetWidth = this.minWidth - 2 * x;
798 var targetHeight = this.minHeight - 2 * y;
802 if((x == 0 && y == 0) || (x == 0 && y > 0)){
803 scale = targetWidth / width;
807 scale = targetHeight / height;
811 scale = targetWidth / width;
814 scale = targetHeight / height;
818 context.scale(scale, scale);
820 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
821 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
823 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
824 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
826 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
828 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
833 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
834 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
836 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
837 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
839 var targetWidth = this.minWidth - 2 * x;
840 var targetHeight = this.minHeight - 2 * y;
844 if((x == 0 && y == 0) || (x == 0 && y > 0)){
845 scale = targetWidth / width;
849 scale = targetHeight / height;
853 scale = targetWidth / width;
856 scale = targetHeight / height;
860 context.scale(scale, scale);
862 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
863 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
865 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
866 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
868 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
869 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
871 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
876 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
877 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
879 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
880 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
882 var targetWidth = this.minWidth - 2 * x;
883 var targetHeight = this.minHeight - 2 * y;
887 if((x == 0 && y == 0) || (x == 0 && y > 0)){
888 scale = targetWidth / width;
892 scale = targetHeight / height;
896 scale = targetWidth / width;
899 scale = targetHeight / height;
903 context.scale(scale, scale);
905 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
906 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
908 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
909 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
911 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
913 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
920 this.cropData = canvas.toDataURL(this.cropType);
922 this.fireEvent('crop', this, this.cropData);
926 setThumbBoxSize : function()
930 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
931 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
932 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
934 this.minWidth = width;
935 this.minHeight = height;
937 if(this.rotate == 90 || this.rotate == 270){
938 this.minWidth = height;
939 this.minHeight = width;
944 width = Math.ceil(this.minWidth * height / this.minHeight);
946 if(this.minWidth > this.minHeight){
948 height = Math.ceil(this.minHeight * width / this.minWidth);
951 this.thumbEl.setStyle({
952 width : width + 'px',
953 height : height + 'px'
960 setThumbBoxPosition : function()
962 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
963 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
965 this.thumbEl.setLeft(x);
966 this.thumbEl.setTop(y);
970 baseRotateLevel : function()
975 typeof(this.exif) != 'undefined' &&
976 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
977 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
979 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
982 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
986 baseScaleLevel : function()
992 if(this.baseRotate == 6 || this.baseRotate == 8){
994 height = this.thumbEl.getHeight();
995 this.baseScale = height / this.imageEl.OriginWidth;
997 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
998 width = this.thumbEl.getWidth();
999 this.baseScale = width / this.imageEl.OriginHeight;
1005 height = this.thumbEl.getHeight();
1006 this.baseScale = height / this.imageEl.OriginHeight;
1008 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1009 width = this.thumbEl.getWidth();
1010 this.baseScale = width / this.imageEl.OriginWidth;
1016 if(this.baseRotate == 6 || this.baseRotate == 8){
1018 width = this.thumbEl.getHeight();
1019 this.baseScale = width / this.imageEl.OriginHeight;
1021 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1022 height = this.thumbEl.getWidth();
1023 this.baseScale = height / this.imageEl.OriginHeight;
1026 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1027 height = this.thumbEl.getWidth();
1028 this.baseScale = height / this.imageEl.OriginHeight;
1030 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1031 width = this.thumbEl.getHeight();
1032 this.baseScale = width / this.imageEl.OriginWidth;
1039 width = this.thumbEl.getWidth();
1040 this.baseScale = width / this.imageEl.OriginWidth;
1042 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1043 height = this.thumbEl.getHeight();
1044 this.baseScale = height / this.imageEl.OriginHeight;
1047 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1049 height = this.thumbEl.getHeight();
1050 this.baseScale = height / this.imageEl.OriginHeight;
1052 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1053 width = this.thumbEl.getWidth();
1054 this.baseScale = width / this.imageEl.OriginWidth;
1062 getScaleLevel : function()
1064 return this.baseScale * Math.pow(1.1, this.scale);
1067 onTouchStart : function(e)
1069 if(!this.canvasLoaded){
1070 this.beforeSelectFile(e);
1074 var touches = e.browserEvent.touches;
1080 if(touches.length == 1){
1081 this.onMouseDown(e);
1085 if(touches.length != 2){
1091 for(var i = 0, finger; finger = touches[i]; i++){
1092 coords.push(finger.pageX, finger.pageY);
1095 var x = Math.pow(coords[0] - coords[2], 2);
1096 var y = Math.pow(coords[1] - coords[3], 2);
1098 this.startDistance = Math.sqrt(x + y);
1100 this.startScale = this.scale;
1102 this.pinching = true;
1103 this.dragable = false;
1107 onTouchMove : function(e)
1109 if(!this.pinching && !this.dragable){
1113 var touches = e.browserEvent.touches;
1120 this.onMouseMove(e);
1126 for(var i = 0, finger; finger = touches[i]; i++){
1127 coords.push(finger.pageX, finger.pageY);
1130 var x = Math.pow(coords[0] - coords[2], 2);
1131 var y = Math.pow(coords[1] - coords[3], 2);
1133 this.endDistance = Math.sqrt(x + y);
1135 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1137 if(!this.zoomable()){
1138 this.scale = this.startScale;
1146 onTouchEnd : function(e)
1148 this.pinching = false;
1149 this.dragable = false;
1153 uploadStart : function(file, crop)
1155 this.xhr = new XMLHttpRequest();
1157 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
1161 file.xhr = this.xhr;
1163 this.xhr.open(this.method, this.url, true);
1166 "Accept": "application/json",
1167 "Cache-Control": "no-cache",
1168 "X-Requested-With": "XMLHttpRequest"
1171 for (var headerName in headers) {
1172 var headerValue = headers[headerName];
1174 this.xhr.setRequestHeader(headerName, headerValue);
1180 this.xhr.onload = function()
1182 _this.xhrOnLoad(_this.xhr);
1185 this.xhr.onerror = function()
1187 _this.xhrOnError(_this.xhr);
1190 var formData = new FormData();
1192 formData.append('returnHTML', 'NO');
1195 formData.append('crop', crop);
1198 formData.append(this.paramName, file, file.name);
1200 if(this.fireEvent('prepare', this, formData) != false){
1201 this.xhr.send(formData);
1205 xhrOnLoad : function(xhr)
1207 if (xhr.readyState !== 4) {
1208 this.fireEvent('exception', this, xhr);
1212 var response = Roo.decode(xhr.responseText);
1214 if(!response.success){
1215 this.fireEvent('exception', this, xhr);
1219 this.fireEvent('upload', this, xhr);
1223 xhrOnError : function()
1225 Roo.log('xhr on error');
1227 var response = Roo.decode(xhr.responseText);
1233 prepare : function(file)
1238 if(typeof(file) === 'string'){
1239 this.loadCanvas(file);
1243 if(!file || !this.urlAPI){
1248 this.cropType = file.type;
1252 if(this.fireEvent('prepare', this, this.file) != false){
1254 var reader = new FileReader();
1256 reader.onload = function (e) {
1257 if (e.target.error) {
1258 Roo.log(e.target.error);
1262 var buffer = e.target.result,
1263 dataView = new DataView(buffer),
1265 maxOffset = dataView.byteLength - 4,
1269 if (dataView.getUint16(0) === 0xffd8) {
1270 while (offset < maxOffset) {
1271 markerBytes = dataView.getUint16(offset);
1273 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1274 markerLength = dataView.getUint16(offset + 2) + 2;
1275 if (offset + markerLength > dataView.byteLength) {
1276 Roo.log('Invalid meta data: Invalid segment size.');
1280 if(markerBytes == 0xffe1){
1281 _this.parseExifData(
1288 offset += markerLength;
1298 var url = _this.urlAPI.createObjectURL(_this.file);
1300 _this.loadCanvas(url);
1305 reader.readAsArrayBuffer(this.file);
1311 parseExifData : function(dataView, offset, length)
1313 var tiffOffset = offset + 10,
1317 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1318 // No Exif data, might be XMP data instead
1322 // Check for the ASCII code for "Exif" (0x45786966):
1323 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1324 // No Exif data, might be XMP data instead
1327 if (tiffOffset + 8 > dataView.byteLength) {
1328 Roo.log('Invalid Exif data: Invalid segment size.');
1331 // Check for the two null bytes:
1332 if (dataView.getUint16(offset + 8) !== 0x0000) {
1333 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1336 // Check the byte alignment:
1337 switch (dataView.getUint16(tiffOffset)) {
1339 littleEndian = true;
1342 littleEndian = false;
1345 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1348 // Check for the TIFF tag marker (0x002A):
1349 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1350 Roo.log('Invalid Exif data: Missing TIFF marker.');
1353 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1354 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1359 tiffOffset + dirOffset,
1364 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1369 if (dirOffset + 6 > dataView.byteLength) {
1370 Roo.log('Invalid Exif data: Invalid directory offset.');
1373 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1374 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1375 if (dirEndOffset + 4 > dataView.byteLength) {
1376 Roo.log('Invalid Exif data: Invalid directory size.');
1379 for (i = 0; i < tagsNumber; i += 1) {
1383 dirOffset + 2 + 12 * i, // tag offset
1387 // Return the offset to the next directory:
1388 return dataView.getUint32(dirEndOffset, littleEndian);
1391 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1393 var tag = dataView.getUint16(offset, littleEndian);
1395 this.exif[tag] = this.getExifValue(
1399 dataView.getUint16(offset + 2, littleEndian), // tag type
1400 dataView.getUint32(offset + 4, littleEndian), // tag length
1405 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1407 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1416 Roo.log('Invalid Exif data: Invalid tag type.');
1420 tagSize = tagType.size * length;
1421 // Determine if the value is contained in the dataOffset bytes,
1422 // or if the value at the dataOffset is a pointer to the actual data:
1423 dataOffset = tagSize > 4 ?
1424 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1425 if (dataOffset + tagSize > dataView.byteLength) {
1426 Roo.log('Invalid Exif data: Invalid data offset.');
1430 return tagType.getValue(dataView, dataOffset, littleEndian);
1433 for (i = 0; i < length; i += 1) {
1434 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1437 if (tagType.ascii) {
1439 // Concatenate the chars:
1440 for (i = 0; i < values.length; i += 1) {
1442 // Ignore the terminating NULL byte(s):
1443 if (c === '\u0000') {
1455 Roo.apply(Roo.bootstrap.UploadCropbox, {
1457 'Orientation': 0x0112
1463 3: 180, //'bottom-right',
1464 // 4: 'bottom-left',
1466 6: 90, //'right-top',
1467 // 7: 'right-bottom',
1468 8: 270 //'left-bottom'
1472 // byte, 8-bit unsigned int:
1474 getValue: function (dataView, dataOffset) {
1475 return dataView.getUint8(dataOffset);
1479 // ascii, 8-bit byte:
1481 getValue: function (dataView, dataOffset) {
1482 return String.fromCharCode(dataView.getUint8(dataOffset));
1487 // short, 16 bit int:
1489 getValue: function (dataView, dataOffset, littleEndian) {
1490 return dataView.getUint16(dataOffset, littleEndian);
1494 // long, 32 bit int:
1496 getValue: function (dataView, dataOffset, littleEndian) {
1497 return dataView.getUint32(dataOffset, littleEndian);
1501 // rational = two long values, first is numerator, second is denominator:
1503 getValue: function (dataView, dataOffset, littleEndian) {
1504 return dataView.getUint32(dataOffset, littleEndian) /
1505 dataView.getUint32(dataOffset + 4, littleEndian);
1509 // slong, 32 bit signed int:
1511 getValue: function (dataView, dataOffset, littleEndian) {
1512 return dataView.getInt32(dataOffset, littleEndian);
1516 // srational, two slongs, first is numerator, second is denominator:
1518 getValue: function (dataView, dataOffset, littleEndian) {
1519 return dataView.getInt32(dataOffset, littleEndian) /
1520 dataView.getInt32(dataOffset + 4, littleEndian);
1530 cls : 'btn-group roo-upload-cropbox-rotate-left',
1531 action : 'rotate-left',
1535 cls : 'btn btn-default',
1536 html : '<i class="fa fa-undo"></i>'
1542 cls : 'btn-group roo-upload-cropbox-picture',
1547 cls : 'btn btn-default',
1548 html : '<i class="fa fa-picture-o"></i>'
1554 cls : 'btn-group roo-upload-cropbox-rotate-right',
1555 action : 'rotate-right',
1559 cls : 'btn btn-default',
1560 html : '<i class="fa fa-repeat"></i>'
1568 cls : 'btn-group roo-upload-cropbox-rotate-left',
1569 action : 'rotate-left',
1573 cls : 'btn btn-default',
1574 html : '<i class="fa fa-undo"></i>'
1580 cls : 'btn-group roo-upload-cropbox-download',
1581 action : 'download',
1585 cls : 'btn btn-default',
1586 html : '<i class="fa fa-download"></i>'
1592 cls : 'btn-group roo-upload-cropbox-crop',
1597 cls : 'btn btn-default',
1598 html : '<i class="fa fa-crop"></i>'
1604 cls : 'btn-group roo-upload-cropbox-trash',
1609 cls : 'btn btn-default',
1610 html : '<i class="fa fa-trash"></i>'
1616 cls : 'btn-group roo-upload-cropbox-rotate-right',
1617 action : 'rotate-right',
1621 cls : 'btn btn-default',
1622 html : '<i class="fa fa-repeat"></i>'
1630 cls : 'btn-group roo-upload-cropbox-rotate-left',
1631 action : 'rotate-left',
1635 cls : 'btn btn-default',
1636 html : '<i class="fa fa-undo"></i>'
1642 cls : 'btn-group roo-upload-cropbox-rotate-right',
1643 action : 'rotate-right',
1647 cls : 'btn btn-default',
1648 html : '<i class="fa fa-repeat"></i>'