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 {XMLHttpRequest} xhr
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 * Fire when inspect the file
103 * @param {Roo.bootstrap.UploadCropbox} this
104 * @param {Object} file
109 * Fire when xhr upload the file
110 * @param {Roo.bootstrap.UploadCropbox} this
111 * @param {Object} data
116 * Fire when arrange the file data
117 * @param {Roo.bootstrap.UploadCropbox} this
118 * @param {Object} formData
123 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
126 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
128 emptyText : 'Click to upload image',
129 rotateNotify : 'Image is too small to rotate',
144 cropType : 'image/jpeg',
146 canvasLoaded : false,
149 getAutoCreate : function()
153 cls : 'roo-upload-cropbox',
157 cls : 'roo-upload-cropbox-body',
158 style : 'cursor:pointer',
162 cls : 'roo-upload-cropbox-selector',
167 cls : 'roo-upload-cropbox-preview'
171 cls : 'roo-upload-cropbox-thumb'
175 cls : 'roo-upload-cropbox-empty-notify',
176 html : this.emptyText
180 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
181 html : this.rotateNotify
187 cls : 'roo-upload-cropbox-footer',
190 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
200 onRender : function(ct, position)
202 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
204 if (this.buttons.length) {
206 Roo.each(this.buttons, function(bb) {
208 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
210 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
216 initEvents : function()
218 this.urlAPI = (window.createObjectURL && window) ||
219 (window.URL && URL.revokeObjectURL && URL) ||
220 (window.webkitURL && webkitURL);
222 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
223 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
225 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
226 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
227 this.selectorEl.hide();
229 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
230 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
232 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
233 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
236 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
237 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
239 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
240 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
243 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
244 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
245 this.footerEl.hide();
247 this.setThumbBoxSize();
253 this.fireEvent('initial', this);
260 window.addEventListener("resize", function() { _this.resize(); } );
262 this.bodyEl.on('click', this.beforeSelectFile, this);
265 this.bodyEl.on('touchstart', this.onTouchStart, this);
266 this.bodyEl.on('touchmove', this.onTouchMove, this);
267 this.bodyEl.on('touchend', this.onTouchEnd, this);
271 this.bodyEl.on('mousedown', this.onMouseDown, this);
272 this.bodyEl.on('mousemove', this.onMouseMove, this);
273 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
274 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
275 Roo.get(document).on('mouseup', this.onMouseUp, this);
278 this.selectorEl.on('change', this.onFileSelected, this);
287 this.dragable = false;
288 this.pinching = false;
291 this.cropData = false;
292 this.notifyEl.dom.innerHTML = this.emptyText;
294 this.selectorEl.dom.value = '';
300 if(this.fireEvent('resize', this) != false){
301 this.setThumbBoxPosition();
302 this.setCanvasPosition();
306 onFooterButtonClick : function(e, el, o, type)
310 this.onRotateLeft(e);
312 case 'rotate-right' :
313 this.onRotateRight(e);
316 this.beforeSelectFile(e);
331 this.fireEvent('footerbuttonclick', this, type);
334 beforeSelectFile : function(e)
338 if(this.fireEvent('beforeselectfile', this) != false){
339 this.selectorEl.dom.click();
343 onFileSelected : function(e)
347 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
351 var file = this.selectorEl.dom.files[0];
353 if(this.fireEvent('inspect', this, file) != false){
361 this.fireEvent('trash', this);
364 download : function(e)
366 this.fireEvent('download', this);
369 loadCanvas : function(src)
371 if(this.fireEvent('beforeloadcanvas', this, src) != false){
375 this.imageEl = document.createElement('img');
379 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
381 this.imageEl.src = src;
385 onLoadCanvas : function()
387 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
388 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
390 this.bodyEl.un('click', this.beforeSelectFile, this);
392 this.notifyEl.hide();
394 this.footerEl.show();
398 this.baseRotateLevel();
401 this.setThumbBoxSize();
404 this.setThumbBoxPosition();
406 this.baseScaleLevel();
412 this.canvasLoaded = true;
416 setCanvasPosition : function()
422 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
423 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
425 this.previewEl.setLeft(pw);
426 this.previewEl.setTop(ph);
430 onMouseDown : function(e)
434 this.dragable = true;
435 this.pinching = false;
437 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
438 this.dragable = false;
442 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
443 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
447 onMouseMove : function(e)
451 if(!this.canvasLoaded){
459 var minX = Math.ceil(this.thumbEl.getLeft(true));
460 var minY = Math.ceil(this.thumbEl.getTop(true));
462 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
463 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
465 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
466 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
471 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
472 var bgY = Math.ceil(y + this.previewEl.getTop(true));
474 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
475 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
477 this.previewEl.setLeft(bgX);
478 this.previewEl.setTop(bgY);
480 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
481 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
484 onMouseUp : function(e)
488 this.dragable = false;
491 onMouseWheel : function(e)
495 this.startScale = this.scale;
497 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
499 if(!this.zoomable()){
500 this.scale = this.startScale;
509 zoomable : function()
511 var minScale = this.thumbEl.getWidth() / this.minWidth;
513 if(this.minWidth < this.minHeight){
514 minScale = this.thumbEl.getHeight() / this.minHeight;
517 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
518 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
522 (this.rotate == 0 || this.rotate == 180) &&
524 width > this.imageEl.OriginWidth ||
525 height > this.imageEl.OriginHeight ||
526 (width < this.minWidth && height < this.minHeight)
534 (this.rotate == 90 || this.rotate == 270) &&
536 width > this.imageEl.OriginWidth ||
537 height > this.imageEl.OriginHeight ||
538 (width < this.minHeight && height < this.minWidth)
546 (this.rotate == 0 || this.rotate == 180) &&
548 width < this.minWidth ||
549 width > this.imageEl.OriginWidth ||
550 height < this.minHeight ||
551 height > this.imageEl.OriginHeight
559 (this.rotate == 90 || this.rotate == 270) &&
561 width < this.minHeight ||
562 width > this.imageEl.OriginWidth ||
563 height < this.minWidth ||
564 height > this.imageEl.OriginHeight
574 onRotateLeft : function(e)
576 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
578 var minScale = this.thumbEl.getWidth() / this.minWidth;
580 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
581 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
583 this.startScale = this.scale;
585 while (this.getScaleLevel() < minScale){
587 this.scale = this.scale + 1;
589 if(!this.zoomable()){
594 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
595 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
600 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
607 this.scale = this.startScale;
614 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
617 this.setThumbBoxSize();
618 this.setThumbBoxPosition();
619 this.setCanvasPosition();
624 this.fireEvent('rotate', this, 'left');
628 onRotateRight : function(e)
630 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
632 var minScale = this.thumbEl.getWidth() / this.minWidth;
634 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
635 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
637 this.startScale = this.scale;
639 while (this.getScaleLevel() < minScale){
641 this.scale = this.scale + 1;
643 if(!this.zoomable()){
648 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
649 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
654 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
661 this.scale = this.startScale;
668 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
671 this.setThumbBoxSize();
672 this.setThumbBoxPosition();
673 this.setCanvasPosition();
678 this.fireEvent('rotate', this, 'right');
681 onRotateFail : function()
683 this.errorEl.show(true);
687 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
692 this.previewEl.dom.innerHTML = '';
694 var canvasEl = document.createElement("canvas");
696 var contextEl = canvasEl.getContext("2d");
698 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
699 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
700 var center = this.imageEl.OriginWidth / 2;
702 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
703 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
704 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
705 center = this.imageEl.OriginHeight / 2;
708 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
710 contextEl.translate(center, center);
711 contextEl.rotate(this.rotate * Math.PI / 180);
713 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
715 this.canvasEl = document.createElement("canvas");
717 this.contextEl = this.canvasEl.getContext("2d");
719 switch (this.rotate) {
722 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
723 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
725 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
730 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
731 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
733 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
734 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
738 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
743 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
744 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
746 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
747 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
751 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
756 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
757 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
759 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
760 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
764 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
771 this.previewEl.appendChild(this.canvasEl);
773 this.setCanvasPosition();
778 if(!this.canvasLoaded){
782 var imageCanvas = document.createElement("canvas");
784 var imageContext = imageCanvas.getContext("2d");
786 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
787 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
789 var center = imageCanvas.width / 2;
791 imageContext.translate(center, center);
793 imageContext.rotate(this.rotate * Math.PI / 180);
795 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
797 var canvas = document.createElement("canvas");
799 var context = canvas.getContext("2d");
801 canvas.width = this.minWidth;
802 canvas.height = this.minHeight;
804 switch (this.rotate) {
807 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
808 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
810 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
811 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
813 var targetWidth = this.minWidth - 2 * x;
814 var targetHeight = this.minHeight - 2 * y;
818 if((x == 0 && y == 0) || (x == 0 && y > 0)){
819 scale = targetWidth / width;
823 scale = targetHeight / height;
827 scale = targetWidth / width;
830 scale = targetHeight / height;
834 context.scale(scale, scale);
836 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
837 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
839 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
840 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
842 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
847 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
848 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
850 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
851 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
853 var targetWidth = this.minWidth - 2 * x;
854 var targetHeight = this.minHeight - 2 * y;
858 if((x == 0 && y == 0) || (x == 0 && y > 0)){
859 scale = targetWidth / width;
863 scale = targetHeight / height;
867 scale = targetWidth / width;
870 scale = targetHeight / height;
874 context.scale(scale, scale);
876 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
877 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
879 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
880 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
882 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
884 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
889 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
890 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
892 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
893 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
895 var targetWidth = this.minWidth - 2 * x;
896 var targetHeight = this.minHeight - 2 * y;
900 if((x == 0 && y == 0) || (x == 0 && y > 0)){
901 scale = targetWidth / width;
905 scale = targetHeight / height;
909 scale = targetWidth / width;
912 scale = targetHeight / height;
916 context.scale(scale, scale);
918 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
919 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
921 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
922 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
924 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
925 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
927 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
932 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
933 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
935 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
936 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
938 var targetWidth = this.minWidth - 2 * x;
939 var targetHeight = this.minHeight - 2 * y;
943 if((x == 0 && y == 0) || (x == 0 && y > 0)){
944 scale = targetWidth / width;
948 scale = targetHeight / height;
952 scale = targetWidth / width;
955 scale = targetHeight / height;
959 context.scale(scale, scale);
961 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
962 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
964 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
965 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
967 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
969 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
976 this.cropData = canvas.toDataURL(this.cropType);
978 this.fireEvent('crop', this, this.cropData);
984 setThumbBoxSize : function()
988 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
989 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
990 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
992 this.minWidth = width;
993 this.minHeight = height;
995 if(this.rotate == 90 || this.rotate == 270){
996 this.minWidth = height;
997 this.minHeight = width;
1002 width = Math.ceil(this.minWidth * height / this.minHeight);
1004 if(this.minWidth > this.minHeight){
1006 height = Math.ceil(this.minHeight * width / this.minWidth);
1009 this.thumbEl.setStyle({
1010 width : width + 'px',
1011 height : height + 'px'
1018 setThumbBoxPosition : function()
1020 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1021 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1023 this.thumbEl.setLeft(x);
1024 this.thumbEl.setTop(y);
1028 baseRotateLevel : function()
1030 this.baseRotate = 1;
1033 typeof(this.exif) != 'undefined' &&
1034 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1035 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1037 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1040 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1044 baseScaleLevel : function()
1048 if(this.isDocument){
1050 if(this.baseRotate == 6 || this.baseRotate == 8){
1052 height = this.thumbEl.getHeight();
1053 this.baseScale = height / this.imageEl.OriginWidth;
1055 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1056 width = this.thumbEl.getWidth();
1057 this.baseScale = width / this.imageEl.OriginHeight;
1063 height = this.thumbEl.getHeight();
1064 this.baseScale = height / this.imageEl.OriginHeight;
1066 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1067 width = this.thumbEl.getWidth();
1068 this.baseScale = width / this.imageEl.OriginWidth;
1074 if(this.baseRotate == 6 || this.baseRotate == 8){
1076 width = this.thumbEl.getHeight();
1077 this.baseScale = width / this.imageEl.OriginHeight;
1079 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1080 height = this.thumbEl.getWidth();
1081 this.baseScale = height / this.imageEl.OriginHeight;
1084 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1085 height = this.thumbEl.getWidth();
1086 this.baseScale = height / this.imageEl.OriginHeight;
1088 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1089 width = this.thumbEl.getHeight();
1090 this.baseScale = width / this.imageEl.OriginWidth;
1097 width = this.thumbEl.getWidth();
1098 this.baseScale = width / this.imageEl.OriginWidth;
1100 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1101 height = this.thumbEl.getHeight();
1102 this.baseScale = height / this.imageEl.OriginHeight;
1105 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1107 height = this.thumbEl.getHeight();
1108 this.baseScale = height / this.imageEl.OriginHeight;
1110 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1111 width = this.thumbEl.getWidth();
1112 this.baseScale = width / this.imageEl.OriginWidth;
1120 getScaleLevel : function()
1122 return this.baseScale * Math.pow(1.1, this.scale);
1125 onTouchStart : function(e)
1127 if(!this.canvasLoaded){
1128 this.beforeSelectFile(e);
1132 var touches = e.browserEvent.touches;
1138 if(touches.length == 1){
1139 this.onMouseDown(e);
1143 if(touches.length != 2){
1149 for(var i = 0, finger; finger = touches[i]; i++){
1150 coords.push(finger.pageX, finger.pageY);
1153 var x = Math.pow(coords[0] - coords[2], 2);
1154 var y = Math.pow(coords[1] - coords[3], 2);
1156 this.startDistance = Math.sqrt(x + y);
1158 this.startScale = this.scale;
1160 this.pinching = true;
1161 this.dragable = false;
1165 onTouchMove : function(e)
1167 if(!this.pinching && !this.dragable){
1171 var touches = e.browserEvent.touches;
1178 this.onMouseMove(e);
1184 for(var i = 0, finger; finger = touches[i]; i++){
1185 coords.push(finger.pageX, finger.pageY);
1188 var x = Math.pow(coords[0] - coords[2], 2);
1189 var y = Math.pow(coords[1] - coords[3], 2);
1191 this.endDistance = Math.sqrt(x + y);
1193 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1195 if(!this.zoomable()){
1196 this.scale = this.startScale;
1204 onTouchEnd : function(e)
1206 this.pinching = false;
1207 this.dragable = false;
1211 process : function(file, crop)
1213 this.xhr = new XMLHttpRequest();
1215 if(typeof(this.file.id) != 'undefined' && this.file.id * 1 > 0){
1219 file.xhr = this.xhr;
1221 this.xhr.open(this.method, this.url, true);
1224 "Accept": "application/json",
1225 "Cache-Control": "no-cache",
1226 "X-Requested-With": "XMLHttpRequest"
1229 for (var headerName in headers) {
1230 var headerValue = headers[headerName];
1232 this.xhr.setRequestHeader(headerName, headerValue);
1238 this.xhr.onload = function()
1240 _this.xhrOnLoad(_this.xhr);
1243 this.xhr.onerror = function()
1245 _this.xhrOnError(_this.xhr);
1248 var formData = new FormData();
1250 formData.append('returnHTML', 'NO');
1253 formData.append('crop', crop);
1256 formData.append(this.paramName, file, file.name);
1258 if(this.fireEvent('arrange', this, formData) != false){
1259 this.xhr.send(formData);
1263 xhrOnLoad : function(xhr)
1265 if (xhr.readyState !== 4) {
1266 this.fireEvent('exception', this, xhr);
1270 var response = Roo.decode(xhr.responseText);
1272 if(!response.success){
1273 this.fireEvent('exception', this, xhr);
1277 var response = Roo.decode(xhr.responseText);
1279 this.fireEvent('upload', this, response);
1283 xhrOnError : function()
1285 Roo.log('xhr on error');
1287 var response = Roo.decode(xhr.responseText);
1293 uploadFromSource : function(file, crop)
1295 this.xhr = new XMLHttpRequest();
1297 this.xhr.open(this.method, this.url, true);
1300 "Accept": "application/json",
1301 "Cache-Control": "no-cache",
1302 "X-Requested-With": "XMLHttpRequest"
1305 for (var headerName in headers) {
1306 var headerValue = headers[headerName];
1308 this.xhr.setRequestHeader(headerName, headerValue);
1314 this.xhr.onload = function()
1316 _this.xhrOnLoad(_this.xhr);
1319 this.xhr.onerror = function()
1321 _this.xhrOnError(_this.xhr);
1324 var formData = new FormData();
1326 formData.append('returnHTML', 'NO');
1328 formData.append('crop', crop);
1330 if(typeof(file.filename) != 'undefined'){
1331 formData.append('filename', file.filename);
1334 if(typeof(file.mimetype) != 'undefined'){
1335 formData.append('mimetype', file.mimetype);
1338 if(this.fireEvent('arrange', this, formData) != false){
1339 this.xhr.send(formData);
1343 prepare : function(file)
1348 if(typeof(file) === 'string'){
1349 this.loadCanvas(file);
1353 if(!file || !this.urlAPI){
1358 this.cropType = file.type;
1362 if(this.fireEvent('prepare', this, this.file) != false){
1364 var reader = new FileReader();
1366 reader.onload = function (e) {
1367 if (e.target.error) {
1368 Roo.log(e.target.error);
1372 var buffer = e.target.result,
1373 dataView = new DataView(buffer),
1375 maxOffset = dataView.byteLength - 4,
1379 if (dataView.getUint16(0) === 0xffd8) {
1380 while (offset < maxOffset) {
1381 markerBytes = dataView.getUint16(offset);
1383 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1384 markerLength = dataView.getUint16(offset + 2) + 2;
1385 if (offset + markerLength > dataView.byteLength) {
1386 Roo.log('Invalid meta data: Invalid segment size.');
1390 if(markerBytes == 0xffe1){
1391 _this.parseExifData(
1398 offset += markerLength;
1408 var url = _this.urlAPI.createObjectURL(_this.file);
1410 _this.loadCanvas(url);
1415 reader.readAsArrayBuffer(this.file);
1421 parseExifData : function(dataView, offset, length)
1423 var tiffOffset = offset + 10,
1427 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1428 // No Exif data, might be XMP data instead
1432 // Check for the ASCII code for "Exif" (0x45786966):
1433 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1434 // No Exif data, might be XMP data instead
1437 if (tiffOffset + 8 > dataView.byteLength) {
1438 Roo.log('Invalid Exif data: Invalid segment size.');
1441 // Check for the two null bytes:
1442 if (dataView.getUint16(offset + 8) !== 0x0000) {
1443 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1446 // Check the byte alignment:
1447 switch (dataView.getUint16(tiffOffset)) {
1449 littleEndian = true;
1452 littleEndian = false;
1455 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1458 // Check for the TIFF tag marker (0x002A):
1459 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1460 Roo.log('Invalid Exif data: Missing TIFF marker.');
1463 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1464 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1469 tiffOffset + dirOffset,
1474 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1479 if (dirOffset + 6 > dataView.byteLength) {
1480 Roo.log('Invalid Exif data: Invalid directory offset.');
1483 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1484 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1485 if (dirEndOffset + 4 > dataView.byteLength) {
1486 Roo.log('Invalid Exif data: Invalid directory size.');
1489 for (i = 0; i < tagsNumber; i += 1) {
1493 dirOffset + 2 + 12 * i, // tag offset
1497 // Return the offset to the next directory:
1498 return dataView.getUint32(dirEndOffset, littleEndian);
1501 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1503 var tag = dataView.getUint16(offset, littleEndian);
1505 this.exif[tag] = this.getExifValue(
1509 dataView.getUint16(offset + 2, littleEndian), // tag type
1510 dataView.getUint32(offset + 4, littleEndian), // tag length
1515 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1517 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1526 Roo.log('Invalid Exif data: Invalid tag type.');
1530 tagSize = tagType.size * length;
1531 // Determine if the value is contained in the dataOffset bytes,
1532 // or if the value at the dataOffset is a pointer to the actual data:
1533 dataOffset = tagSize > 4 ?
1534 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1535 if (dataOffset + tagSize > dataView.byteLength) {
1536 Roo.log('Invalid Exif data: Invalid data offset.');
1540 return tagType.getValue(dataView, dataOffset, littleEndian);
1543 for (i = 0; i < length; i += 1) {
1544 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1547 if (tagType.ascii) {
1549 // Concatenate the chars:
1550 for (i = 0; i < values.length; i += 1) {
1552 // Ignore the terminating NULL byte(s):
1553 if (c === '\u0000') {
1565 Roo.apply(Roo.bootstrap.UploadCropbox, {
1567 'Orientation': 0x0112
1573 3: 180, //'bottom-right',
1574 // 4: 'bottom-left',
1576 6: 90, //'right-top',
1577 // 7: 'right-bottom',
1578 8: 270 //'left-bottom'
1582 // byte, 8-bit unsigned int:
1584 getValue: function (dataView, dataOffset) {
1585 return dataView.getUint8(dataOffset);
1589 // ascii, 8-bit byte:
1591 getValue: function (dataView, dataOffset) {
1592 return String.fromCharCode(dataView.getUint8(dataOffset));
1597 // short, 16 bit int:
1599 getValue: function (dataView, dataOffset, littleEndian) {
1600 return dataView.getUint16(dataOffset, littleEndian);
1604 // long, 32 bit int:
1606 getValue: function (dataView, dataOffset, littleEndian) {
1607 return dataView.getUint32(dataOffset, littleEndian);
1611 // rational = two long values, first is numerator, second is denominator:
1613 getValue: function (dataView, dataOffset, littleEndian) {
1614 return dataView.getUint32(dataOffset, littleEndian) /
1615 dataView.getUint32(dataOffset + 4, littleEndian);
1619 // slong, 32 bit signed int:
1621 getValue: function (dataView, dataOffset, littleEndian) {
1622 return dataView.getInt32(dataOffset, littleEndian);
1626 // srational, two slongs, first is numerator, second is denominator:
1628 getValue: function (dataView, dataOffset, littleEndian) {
1629 return dataView.getInt32(dataOffset, littleEndian) /
1630 dataView.getInt32(dataOffset + 4, littleEndian);
1640 cls : 'btn-group roo-upload-cropbox-rotate-left',
1641 action : 'rotate-left',
1645 cls : 'btn btn-default',
1646 html : '<i class="fa fa-undo"></i>'
1652 cls : 'btn-group roo-upload-cropbox-picture',
1657 cls : 'btn btn-default',
1658 html : '<i class="fa fa-picture-o"></i>'
1664 cls : 'btn-group roo-upload-cropbox-rotate-right',
1665 action : 'rotate-right',
1669 cls : 'btn btn-default',
1670 html : '<i class="fa fa-repeat"></i>'
1678 cls : 'btn-group roo-upload-cropbox-rotate-left',
1679 action : 'rotate-left',
1683 cls : 'btn btn-default',
1684 html : '<i class="fa fa-undo"></i>'
1690 cls : 'btn-group roo-upload-cropbox-download',
1691 action : 'download',
1695 cls : 'btn btn-default',
1696 html : '<i class="fa fa-download"></i>'
1702 cls : 'btn-group roo-upload-cropbox-crop',
1707 cls : 'btn btn-default',
1708 html : '<i class="fa fa-crop"></i>'
1714 cls : 'btn-group roo-upload-cropbox-trash',
1719 cls : 'btn btn-default',
1720 html : '<i class="fa fa-trash"></i>'
1726 cls : 'btn-group roo-upload-cropbox-rotate-right',
1727 action : 'rotate-right',
1731 cls : 'btn btn-default',
1732 html : '<i class="fa fa-repeat"></i>'
1740 cls : 'btn-group roo-upload-cropbox-rotate-left',
1741 action : 'rotate-left',
1745 cls : 'btn btn-default',
1746 html : '<i class="fa fa-undo"></i>'
1752 cls : 'btn-group roo-upload-cropbox-rotate-right',
1753 action : 'rotate-right',
1757 cls : 'btn btn-default',
1758 html : '<i class="fa fa-repeat"></i>'