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...'
24 * Create a new UploadCropbox
25 * @param {Object} config The config object
28 Roo.bootstrap.UploadCropbox = function(config){
29 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
33 * @event beforeselectfile
34 * Fire before select file
35 * @param {Roo.bootstrap.UploadCropbox} this
37 "beforeselectfile" : true,
40 * Fire after initEvent
41 * @param {Roo.bootstrap.UploadCropbox} this
46 * Fire after initEvent
47 * @param {Roo.bootstrap.UploadCropbox} this
48 * @param {String} data
53 * Fire when preparing the file data
54 * @param {Roo.bootstrap.UploadCropbox} this
55 * @param {Object} file
60 * Fire when get exception
61 * @param {Roo.bootstrap.UploadCropbox} this
62 * @param {XMLHttpRequest} xhr
66 * @event beforeloadcanvas
67 * Fire before load the canvas
68 * @param {Roo.bootstrap.UploadCropbox} this
71 "beforeloadcanvas" : true,
74 * Fire when trash image
75 * @param {Roo.bootstrap.UploadCropbox} this
80 * Fire when download the image
81 * @param {Roo.bootstrap.UploadCropbox} this
85 * @event footerbuttonclick
86 * Fire when footerbuttonclick
87 * @param {Roo.bootstrap.UploadCropbox} this
88 * @param {String} type
90 "footerbuttonclick" : true,
94 * @param {Roo.bootstrap.UploadCropbox} this
99 * Fire when rotate the image
100 * @param {Roo.bootstrap.UploadCropbox} this
101 * @param {String} pos
106 * Fire when inspect the file
107 * @param {Roo.bootstrap.UploadCropbox} this
108 * @param {Object} file
113 * Fire when xhr upload the file
114 * @param {Roo.bootstrap.UploadCropbox} this
115 * @param {Object} data
120 * Fire when arrange the file data
121 * @param {Roo.bootstrap.UploadCropbox} this
122 * @param {Object} formData
127 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
130 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
132 emptyText : 'Click to upload image',
133 rotateNotify : 'Image is too small to rotate',
148 cropType : 'image/jpeg',
150 canvasLoaded : false,
153 paramName : 'imageUpload',
155 loadingText : 'Loading...',
158 getAutoCreate : function()
162 cls : 'roo-upload-cropbox',
166 cls : 'roo-upload-cropbox-selector',
171 cls : 'roo-upload-cropbox-body',
172 style : 'cursor:pointer',
176 cls : 'roo-upload-cropbox-preview'
180 cls : 'roo-upload-cropbox-thumb'
184 cls : 'roo-upload-cropbox-empty-notify',
185 html : this.emptyText
189 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
190 html : this.rotateNotify
196 cls : 'roo-upload-cropbox-footer',
199 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
209 onRender : function(ct, position)
211 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
213 if (this.buttons.length) {
215 Roo.each(this.buttons, function(bb) {
217 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
219 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
225 this.maskEl = this.el;
229 initEvents : function()
231 this.urlAPI = (window.createObjectURL && window) ||
232 (window.URL && URL.revokeObjectURL && URL) ||
233 (window.webkitURL && webkitURL);
235 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
236 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
238 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
239 this.selectorEl.hide();
241 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
242 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
244 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
245 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
248 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
249 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
251 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
252 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
255 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
256 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
257 this.footerEl.hide();
259 this.setThumbBoxSize();
265 this.fireEvent('initial', this);
272 window.addEventListener("resize", function() { _this.resize(); } );
274 this.bodyEl.on('click', this.beforeSelectFile, this);
277 this.bodyEl.on('touchstart', this.onTouchStart, this);
278 this.bodyEl.on('touchmove', this.onTouchMove, this);
279 this.bodyEl.on('touchend', this.onTouchEnd, this);
283 this.bodyEl.on('mousedown', this.onMouseDown, this);
284 this.bodyEl.on('mousemove', this.onMouseMove, this);
285 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
286 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
287 Roo.get(document).on('mouseup', this.onMouseUp, this);
290 this.selectorEl.on('change', this.onFileSelected, this);
299 this.dragable = false;
300 this.pinching = false;
303 this.cropData = false;
304 this.notifyEl.dom.innerHTML = this.emptyText;
306 this.selectorEl.dom.value = '';
312 if(this.fireEvent('resize', this) != false){
313 this.setThumbBoxPosition();
314 this.setCanvasPosition();
318 onFooterButtonClick : function(e, el, o, type)
322 this.onRotateLeft(e);
324 case 'rotate-right' :
325 this.onRotateRight(e);
328 this.beforeSelectFile(e);
343 this.fireEvent('footerbuttonclick', this, type);
346 beforeSelectFile : function(e)
350 if(this.fireEvent('beforeselectfile', this) != false){
351 this.selectorEl.dom.click();
355 onFileSelected : function(e)
360 this.maskEl.mask(this.loadingText);
363 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
367 var file = this.selectorEl.dom.files[0];
369 if(this.fireEvent('inspect', this, file) != false){
377 this.fireEvent('trash', this);
380 download : function(e)
382 this.fireEvent('download', this);
385 loadCanvas : function(src)
387 if(this.fireEvent('beforeloadcanvas', this, src) != false){
391 this.imageEl = document.createElement('img');
395 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
397 this.imageEl.src = src;
401 onLoadCanvas : function()
403 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
404 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
406 this.bodyEl.un('click', this.beforeSelectFile, this);
408 this.notifyEl.hide();
410 this.footerEl.show();
412 this.baseRotateLevel();
415 this.setThumbBoxSize();
418 this.setThumbBoxPosition();
420 this.baseScaleLevel();
426 this.canvasLoaded = true;
429 this.maskEl.unmask();
434 setCanvasPosition : function()
440 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
441 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
443 this.previewEl.setLeft(pw);
444 this.previewEl.setTop(ph);
448 onMouseDown : function(e)
452 this.dragable = true;
453 this.pinching = false;
455 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
456 this.dragable = false;
460 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
461 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
465 onMouseMove : function(e)
469 if(!this.canvasLoaded){
477 var minX = Math.ceil(this.thumbEl.getLeft(true));
478 var minY = Math.ceil(this.thumbEl.getTop(true));
480 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
481 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
483 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
484 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
489 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
490 var bgY = Math.ceil(y + this.previewEl.getTop(true));
492 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
493 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
495 this.previewEl.setLeft(bgX);
496 this.previewEl.setTop(bgY);
498 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
499 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
502 onMouseUp : function(e)
506 this.dragable = false;
509 onMouseWheel : function(e)
513 this.startScale = this.scale;
515 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
517 if(!this.zoomable()){
518 this.scale = this.startScale;
527 zoomable : function()
529 var minScale = this.thumbEl.getWidth() / this.minWidth;
531 if(this.minWidth < this.minHeight){
532 minScale = this.thumbEl.getHeight() / this.minHeight;
535 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
536 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
540 (this.rotate == 0 || this.rotate == 180) &&
542 width > this.imageEl.OriginWidth ||
543 height > this.imageEl.OriginHeight ||
544 (width < this.minWidth && height < this.minHeight)
552 (this.rotate == 90 || this.rotate == 270) &&
554 width > this.imageEl.OriginWidth ||
555 height > this.imageEl.OriginHeight ||
556 (width < this.minHeight && height < this.minWidth)
564 (this.rotate == 0 || this.rotate == 180) &&
566 width < this.minWidth ||
567 width > this.imageEl.OriginWidth ||
568 height < this.minHeight ||
569 height > this.imageEl.OriginHeight
577 (this.rotate == 90 || this.rotate == 270) &&
579 width < this.minHeight ||
580 width > this.imageEl.OriginWidth ||
581 height < this.minWidth ||
582 height > this.imageEl.OriginHeight
592 onRotateLeft : function(e)
594 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
596 var minScale = this.thumbEl.getWidth() / this.minWidth;
598 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
599 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
601 this.startScale = this.scale;
603 while (this.getScaleLevel() < minScale){
605 this.scale = this.scale + 1;
607 if(!this.zoomable()){
612 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
613 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
618 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
625 this.scale = this.startScale;
632 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
635 this.setThumbBoxSize();
636 this.setThumbBoxPosition();
637 this.setCanvasPosition();
642 this.fireEvent('rotate', this, 'left');
646 onRotateRight : function(e)
648 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
650 var minScale = this.thumbEl.getWidth() / this.minWidth;
652 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
653 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
655 this.startScale = this.scale;
657 while (this.getScaleLevel() < minScale){
659 this.scale = this.scale + 1;
661 if(!this.zoomable()){
666 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
667 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
672 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
679 this.scale = this.startScale;
686 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
689 this.setThumbBoxSize();
690 this.setThumbBoxPosition();
691 this.setCanvasPosition();
696 this.fireEvent('rotate', this, 'right');
699 onRotateFail : function()
701 this.errorEl.show(true);
705 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
710 this.previewEl.dom.innerHTML = '';
712 var canvasEl = document.createElement("canvas");
714 var contextEl = canvasEl.getContext("2d");
716 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
717 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
718 var center = this.imageEl.OriginWidth / 2;
720 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
721 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
722 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
723 center = this.imageEl.OriginHeight / 2;
726 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
728 contextEl.translate(center, center);
729 contextEl.rotate(this.rotate * Math.PI / 180);
731 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
733 this.canvasEl = document.createElement("canvas");
735 this.contextEl = this.canvasEl.getContext("2d");
737 switch (this.rotate) {
740 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
741 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
743 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
748 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
749 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
751 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
752 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.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
761 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
762 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
764 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
765 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);
769 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);
774 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
775 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
777 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
778 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
782 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);
789 this.previewEl.appendChild(this.canvasEl);
791 this.setCanvasPosition();
796 if(!this.canvasLoaded){
800 var imageCanvas = document.createElement("canvas");
802 var imageContext = imageCanvas.getContext("2d");
804 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
805 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
807 var center = imageCanvas.width / 2;
809 imageContext.translate(center, center);
811 imageContext.rotate(this.rotate * Math.PI / 180);
813 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
815 var canvas = document.createElement("canvas");
817 var context = canvas.getContext("2d");
819 canvas.width = this.minWidth;
820 canvas.height = this.minHeight;
822 switch (this.rotate) {
825 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
826 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
828 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
829 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
831 var targetWidth = this.minWidth - 2 * x;
832 var targetHeight = this.minHeight - 2 * y;
836 if((x == 0 && y == 0) || (x == 0 && y > 0)){
837 scale = targetWidth / width;
841 scale = targetHeight / height;
845 scale = targetWidth / width;
848 scale = targetHeight / height;
852 context.scale(scale, scale);
854 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
855 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
857 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
858 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
860 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
865 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
866 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
868 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
869 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
871 var targetWidth = this.minWidth - 2 * x;
872 var targetHeight = this.minHeight - 2 * y;
876 if((x == 0 && y == 0) || (x == 0 && y > 0)){
877 scale = targetWidth / width;
881 scale = targetHeight / height;
885 scale = targetWidth / width;
888 scale = targetHeight / height;
892 context.scale(scale, scale);
894 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
895 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
897 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
898 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
900 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
902 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
907 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
908 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
910 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
911 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
913 var targetWidth = this.minWidth - 2 * x;
914 var targetHeight = this.minHeight - 2 * y;
918 if((x == 0 && y == 0) || (x == 0 && y > 0)){
919 scale = targetWidth / width;
923 scale = targetHeight / height;
927 scale = targetWidth / width;
930 scale = targetHeight / height;
934 context.scale(scale, scale);
936 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
937 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
939 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
940 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
942 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
943 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
945 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
950 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
951 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
953 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
954 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
956 var targetWidth = this.minWidth - 2 * x;
957 var targetHeight = this.minHeight - 2 * y;
961 if((x == 0 && y == 0) || (x == 0 && y > 0)){
962 scale = targetWidth / width;
966 scale = targetHeight / height;
970 scale = targetWidth / width;
973 scale = targetHeight / height;
977 context.scale(scale, scale);
979 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
980 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
982 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
983 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
985 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
987 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
994 this.cropData = canvas.toDataURL(this.cropType);
996 if(this.fireEvent('crop', this, this.cropData) !== false){
997 this.process(this.file, this.cropData);
1004 setThumbBoxSize : function()
1008 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1009 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1010 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1012 this.minWidth = width;
1013 this.minHeight = height;
1015 if(this.rotate == 90 || this.rotate == 270){
1016 this.minWidth = height;
1017 this.minHeight = width;
1022 width = Math.ceil(this.minWidth * height / this.minHeight);
1024 if(this.minWidth > this.minHeight){
1026 height = Math.ceil(this.minHeight * width / this.minWidth);
1029 this.thumbEl.setStyle({
1030 width : width + 'px',
1031 height : height + 'px'
1038 setThumbBoxPosition : function()
1040 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1041 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1043 this.thumbEl.setLeft(x);
1044 this.thumbEl.setTop(y);
1048 baseRotateLevel : function()
1050 this.baseRotate = 1;
1053 typeof(this.exif) != 'undefined' &&
1054 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1055 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1057 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1060 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1064 baseScaleLevel : function()
1068 if(this.isDocument){
1070 if(this.baseRotate == 6 || this.baseRotate == 8){
1072 height = this.thumbEl.getHeight();
1073 this.baseScale = height / this.imageEl.OriginWidth;
1075 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1076 width = this.thumbEl.getWidth();
1077 this.baseScale = width / this.imageEl.OriginHeight;
1083 height = this.thumbEl.getHeight();
1084 this.baseScale = height / this.imageEl.OriginHeight;
1086 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1087 width = this.thumbEl.getWidth();
1088 this.baseScale = width / this.imageEl.OriginWidth;
1094 if(this.baseRotate == 6 || this.baseRotate == 8){
1096 width = this.thumbEl.getHeight();
1097 this.baseScale = width / this.imageEl.OriginHeight;
1099 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1100 height = this.thumbEl.getWidth();
1101 this.baseScale = height / this.imageEl.OriginHeight;
1104 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1105 height = this.thumbEl.getWidth();
1106 this.baseScale = height / this.imageEl.OriginHeight;
1108 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1109 width = this.thumbEl.getHeight();
1110 this.baseScale = width / this.imageEl.OriginWidth;
1117 width = this.thumbEl.getWidth();
1118 this.baseScale = width / this.imageEl.OriginWidth;
1120 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1121 height = this.thumbEl.getHeight();
1122 this.baseScale = height / this.imageEl.OriginHeight;
1125 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1127 height = this.thumbEl.getHeight();
1128 this.baseScale = height / this.imageEl.OriginHeight;
1130 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1131 width = this.thumbEl.getWidth();
1132 this.baseScale = width / this.imageEl.OriginWidth;
1140 getScaleLevel : function()
1142 return this.baseScale * Math.pow(1.1, this.scale);
1145 onTouchStart : function(e)
1147 if(!this.canvasLoaded){
1148 this.beforeSelectFile(e);
1152 var touches = e.browserEvent.touches;
1158 if(touches.length == 1){
1159 this.onMouseDown(e);
1163 if(touches.length != 2){
1169 for(var i = 0, finger; finger = touches[i]; i++){
1170 coords.push(finger.pageX, finger.pageY);
1173 var x = Math.pow(coords[0] - coords[2], 2);
1174 var y = Math.pow(coords[1] - coords[3], 2);
1176 this.startDistance = Math.sqrt(x + y);
1178 this.startScale = this.scale;
1180 this.pinching = true;
1181 this.dragable = false;
1185 onTouchMove : function(e)
1187 if(!this.pinching && !this.dragable){
1191 var touches = e.browserEvent.touches;
1198 this.onMouseMove(e);
1204 for(var i = 0, finger; finger = touches[i]; i++){
1205 coords.push(finger.pageX, finger.pageY);
1208 var x = Math.pow(coords[0] - coords[2], 2);
1209 var y = Math.pow(coords[1] - coords[3], 2);
1211 this.endDistance = Math.sqrt(x + y);
1213 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1215 if(!this.zoomable()){
1216 this.scale = this.startScale;
1224 onTouchEnd : function(e)
1226 this.pinching = false;
1227 this.dragable = false;
1231 process : function(file, crop)
1233 this.xhr = new XMLHttpRequest();
1235 file.xhr = this.xhr;
1237 this.xhr.open(this.method, this.url, true);
1240 "Accept": "application/json",
1241 "Cache-Control": "no-cache",
1242 "X-Requested-With": "XMLHttpRequest"
1245 for (var headerName in headers) {
1246 var headerValue = headers[headerName];
1248 this.xhr.setRequestHeader(headerName, headerValue);
1254 this.xhr.onload = function()
1256 _this.xhrOnLoad(_this.xhr);
1259 this.xhr.onerror = function()
1261 _this.xhrOnError(_this.xhr);
1264 var formData = new FormData();
1266 formData.append('returnHTML', 'NO');
1269 formData.append('crop', crop);
1272 if(typeof(file) != 'undefined' && (typeof(file.id) == 'undefined' || file.id * 1 < 1)){
1273 formData.append(this.paramName, file, file.name);
1276 if(typeof(file.filename) != 'undefined'){
1277 formData.append('filename', file.filename);
1280 if(typeof(file.mimetype) != 'undefined'){
1281 formData.append('mimetype', file.mimetype);
1284 if(this.fireEvent('arrange', this, formData) != false){
1285 this.xhr.send(formData);
1289 xhrOnLoad : function(xhr)
1291 if (xhr.readyState !== 4) {
1292 this.fireEvent('exception', this, xhr);
1296 var response = Roo.decode(xhr.responseText);
1298 if(!response.success){
1299 this.fireEvent('exception', this, xhr);
1303 var response = Roo.decode(xhr.responseText);
1305 this.fireEvent('upload', this, response);
1309 xhrOnError : function()
1311 Roo.log('xhr on error');
1313 var response = Roo.decode(xhr.responseText);
1319 prepare : function(file)
1324 if(typeof(file) === 'string'){
1325 this.loadCanvas(file);
1329 if(!file || !this.urlAPI){
1334 this.cropType = file.type;
1338 if(this.fireEvent('prepare', this, this.file) != false){
1340 var reader = new FileReader();
1342 reader.onload = function (e) {
1343 if (e.target.error) {
1344 Roo.log(e.target.error);
1348 var buffer = e.target.result,
1349 dataView = new DataView(buffer),
1351 maxOffset = dataView.byteLength - 4,
1355 if (dataView.getUint16(0) === 0xffd8) {
1356 while (offset < maxOffset) {
1357 markerBytes = dataView.getUint16(offset);
1359 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1360 markerLength = dataView.getUint16(offset + 2) + 2;
1361 if (offset + markerLength > dataView.byteLength) {
1362 Roo.log('Invalid meta data: Invalid segment size.');
1366 if(markerBytes == 0xffe1){
1367 _this.parseExifData(
1374 offset += markerLength;
1384 var url = _this.urlAPI.createObjectURL(_this.file);
1386 _this.loadCanvas(url);
1391 reader.readAsArrayBuffer(this.file);
1397 parseExifData : function(dataView, offset, length)
1399 var tiffOffset = offset + 10,
1403 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1404 // No Exif data, might be XMP data instead
1408 // Check for the ASCII code for "Exif" (0x45786966):
1409 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1410 // No Exif data, might be XMP data instead
1413 if (tiffOffset + 8 > dataView.byteLength) {
1414 Roo.log('Invalid Exif data: Invalid segment size.');
1417 // Check for the two null bytes:
1418 if (dataView.getUint16(offset + 8) !== 0x0000) {
1419 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1422 // Check the byte alignment:
1423 switch (dataView.getUint16(tiffOffset)) {
1425 littleEndian = true;
1428 littleEndian = false;
1431 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1434 // Check for the TIFF tag marker (0x002A):
1435 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1436 Roo.log('Invalid Exif data: Missing TIFF marker.');
1439 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1440 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1445 tiffOffset + dirOffset,
1450 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1455 if (dirOffset + 6 > dataView.byteLength) {
1456 Roo.log('Invalid Exif data: Invalid directory offset.');
1459 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1460 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1461 if (dirEndOffset + 4 > dataView.byteLength) {
1462 Roo.log('Invalid Exif data: Invalid directory size.');
1465 for (i = 0; i < tagsNumber; i += 1) {
1469 dirOffset + 2 + 12 * i, // tag offset
1473 // Return the offset to the next directory:
1474 return dataView.getUint32(dirEndOffset, littleEndian);
1477 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1479 var tag = dataView.getUint16(offset, littleEndian);
1481 this.exif[tag] = this.getExifValue(
1485 dataView.getUint16(offset + 2, littleEndian), // tag type
1486 dataView.getUint32(offset + 4, littleEndian), // tag length
1491 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1493 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1502 Roo.log('Invalid Exif data: Invalid tag type.');
1506 tagSize = tagType.size * length;
1507 // Determine if the value is contained in the dataOffset bytes,
1508 // or if the value at the dataOffset is a pointer to the actual data:
1509 dataOffset = tagSize > 4 ?
1510 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1511 if (dataOffset + tagSize > dataView.byteLength) {
1512 Roo.log('Invalid Exif data: Invalid data offset.');
1516 return tagType.getValue(dataView, dataOffset, littleEndian);
1519 for (i = 0; i < length; i += 1) {
1520 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1523 if (tagType.ascii) {
1525 // Concatenate the chars:
1526 for (i = 0; i < values.length; i += 1) {
1528 // Ignore the terminating NULL byte(s):
1529 if (c === '\u0000') {
1541 Roo.apply(Roo.bootstrap.UploadCropbox, {
1543 'Orientation': 0x0112
1549 3: 180, //'bottom-right',
1550 // 4: 'bottom-left',
1552 6: 90, //'right-top',
1553 // 7: 'right-bottom',
1554 8: 270 //'left-bottom'
1558 // byte, 8-bit unsigned int:
1560 getValue: function (dataView, dataOffset) {
1561 return dataView.getUint8(dataOffset);
1565 // ascii, 8-bit byte:
1567 getValue: function (dataView, dataOffset) {
1568 return String.fromCharCode(dataView.getUint8(dataOffset));
1573 // short, 16 bit int:
1575 getValue: function (dataView, dataOffset, littleEndian) {
1576 return dataView.getUint16(dataOffset, littleEndian);
1580 // long, 32 bit int:
1582 getValue: function (dataView, dataOffset, littleEndian) {
1583 return dataView.getUint32(dataOffset, littleEndian);
1587 // rational = two long values, first is numerator, second is denominator:
1589 getValue: function (dataView, dataOffset, littleEndian) {
1590 return dataView.getUint32(dataOffset, littleEndian) /
1591 dataView.getUint32(dataOffset + 4, littleEndian);
1595 // slong, 32 bit signed int:
1597 getValue: function (dataView, dataOffset, littleEndian) {
1598 return dataView.getInt32(dataOffset, littleEndian);
1602 // srational, two slongs, first is numerator, second is denominator:
1604 getValue: function (dataView, dataOffset, littleEndian) {
1605 return dataView.getInt32(dataOffset, littleEndian) /
1606 dataView.getInt32(dataOffset + 4, littleEndian);
1616 cls : 'btn-group roo-upload-cropbox-rotate-left',
1617 action : 'rotate-left',
1621 cls : 'btn btn-default',
1622 html : '<i class="fa fa-undo"></i>'
1628 cls : 'btn-group roo-upload-cropbox-picture',
1633 cls : 'btn btn-default',
1634 html : '<i class="fa fa-picture-o"></i>'
1640 cls : 'btn-group roo-upload-cropbox-rotate-right',
1641 action : 'rotate-right',
1645 cls : 'btn btn-default',
1646 html : '<i class="fa fa-repeat"></i>'
1654 cls : 'btn-group roo-upload-cropbox-rotate-left',
1655 action : 'rotate-left',
1659 cls : 'btn btn-default',
1660 html : '<i class="fa fa-undo"></i>'
1666 cls : 'btn-group roo-upload-cropbox-download',
1667 action : 'download',
1671 cls : 'btn btn-default',
1672 html : '<i class="fa fa-download"></i>'
1678 cls : 'btn-group roo-upload-cropbox-crop',
1683 cls : 'btn btn-default',
1684 html : '<i class="fa fa-crop"></i>'
1690 cls : 'btn-group roo-upload-cropbox-trash',
1695 cls : 'btn btn-default',
1696 html : '<i class="fa fa-trash"></i>'
1702 cls : 'btn-group roo-upload-cropbox-rotate-right',
1703 action : 'rotate-right',
1707 cls : 'btn btn-default',
1708 html : '<i class="fa fa-repeat"></i>'
1716 cls : 'btn-group roo-upload-cropbox-rotate-left',
1717 action : 'rotate-left',
1721 cls : 'btn btn-default',
1722 html : '<i class="fa fa-undo"></i>'
1728 cls : 'btn-group roo-upload-cropbox-rotate-right',
1729 action : 'rotate-right',
1733 cls : 'btn btn-default',
1734 html : '<i class="fa fa-repeat"></i>'