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 * Fire when xhr load exception
103 * @param {Roo.bootstrap.UploadCropbox} this
104 * @param {XMLHttpRequest} xhr
109 * Fire when xhr upload the file
110 * @param {Roo.bootstrap.UploadCropbox} this
111 * @param {Object} data
116 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
119 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
121 emptyText : 'Click to upload image',
122 rotateNotify : 'Image is too small to rotate',
137 cropType : 'image/jpeg',
139 canvasLoaded : false,
142 getAutoCreate : function()
146 cls : 'roo-upload-cropbox',
150 cls : 'roo-upload-cropbox-body',
151 style : 'cursor:pointer',
155 cls : 'roo-upload-cropbox-selector',
160 cls : 'roo-upload-cropbox-preview'
164 cls : 'roo-upload-cropbox-thumb'
168 cls : 'roo-upload-cropbox-empty-notify',
169 html : this.emptyText
173 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
174 html : this.rotateNotify
180 cls : 'roo-upload-cropbox-footer',
183 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
193 onRender : function(ct, position)
195 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
197 if (this.buttons.length) {
199 Roo.each(this.buttons, function(bb) {
201 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
203 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
209 initEvents : function()
211 this.urlAPI = (window.createObjectURL && window) ||
212 (window.URL && URL.revokeObjectURL && URL) ||
213 (window.webkitURL && webkitURL);
215 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
216 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
218 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
219 this.selectorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
220 this.selectorEl.hide();
222 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
223 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
225 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
226 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
229 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
230 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
232 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
233 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
236 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
237 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
238 this.footerEl.hide();
240 this.setThumbBoxSize();
246 this.fireEvent('initial', this);
253 window.addEventListener("resize", function() { _this.resize(); } );
255 this.bodyEl.on('click', this.beforeSelectFile, this);
258 this.bodyEl.on('touchstart', this.onTouchStart, this);
259 this.bodyEl.on('touchmove', this.onTouchMove, this);
260 this.bodyEl.on('touchend', this.onTouchEnd, this);
264 this.bodyEl.on('mousedown', this.onMouseDown, this);
265 this.bodyEl.on('mousemove', this.onMouseMove, this);
266 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
267 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
268 Roo.get(document).on('mouseup', this.onMouseUp, this);
278 this.dragable = false;
279 this.pinching = false;
282 this.cropData = false;
283 this.notifyEl.dom.innerHTML = this.emptyText;
289 if(this.fireEvent('resize', this) != false){
290 this.setThumbBoxPosition();
291 this.setCanvasPosition();
295 onFooterButtonClick : function(e, el, o, type)
299 this.onRotateLeft(e);
301 case 'rotate-right' :
302 this.onRotateRight(e);
305 this.beforeSelectFile(e);
320 this.fireEvent('footerbuttonclick', this, type);
323 beforeSelectFile : function(e)
327 if(this.fireEvent('beforeselectfile', this) != false){
328 this.selectorEl.dom.click();
334 this.fireEvent('trash', this);
337 download : function(e)
339 this.fireEvent('download', this);
342 loadCanvas : function(src)
344 if(this.fireEvent('beforeloadcanvas', this, src) != false){
348 this.imageEl = document.createElement('img');
352 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
354 this.imageEl.src = src;
358 onLoadCanvas : function()
360 this.bodyEl.un('click', this.beforeSelectFile, this);
362 this.notifyEl.hide();
364 this.footerEl.show();
366 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
367 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
369 this.baseRotateLevel();
372 this.setThumbBoxSize();
375 this.setThumbBoxPosition();
377 this.baseScaleLevel();
383 this.canvasLoaded = true;
387 setCanvasPosition : function()
393 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
394 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
396 this.previewEl.setLeft(pw);
397 this.previewEl.setTop(ph);
401 onMouseDown : function(e)
405 this.dragable = true;
406 this.pinching = false;
408 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
409 this.dragable = false;
413 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
414 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
418 onMouseMove : function(e)
422 if(!this.canvasLoaded){
430 var minX = Math.ceil(this.thumbEl.getLeft(true));
431 var minY = Math.ceil(this.thumbEl.getTop(true));
433 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
434 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
436 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
437 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
442 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
443 var bgY = Math.ceil(y + this.previewEl.getTop(true));
445 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
446 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
448 this.previewEl.setLeft(bgX);
449 this.previewEl.setTop(bgY);
451 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
452 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
455 onMouseUp : function(e)
459 this.dragable = false;
462 onMouseWheel : function(e)
466 this.startScale = this.scale;
468 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
470 if(!this.zoomable()){
471 this.scale = this.startScale;
480 zoomable : function()
482 var minScale = this.thumbEl.getWidth() / this.minWidth;
484 if(this.minWidth < this.minHeight){
485 minScale = this.thumbEl.getHeight() / this.minHeight;
488 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
489 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
493 (this.rotate == 0 || this.rotate == 180) &&
495 width > this.imageEl.OriginWidth ||
496 height > this.imageEl.OriginHeight ||
497 (width < this.minWidth && height < this.minHeight)
505 (this.rotate == 90 || this.rotate == 270) &&
507 width > this.imageEl.OriginWidth ||
508 height > this.imageEl.OriginHeight ||
509 (width < this.minHeight && height < this.minWidth)
517 (this.rotate == 0 || this.rotate == 180) &&
519 width < this.minWidth ||
520 width > this.imageEl.OriginWidth ||
521 height < this.minHeight ||
522 height > this.imageEl.OriginHeight
530 (this.rotate == 90 || this.rotate == 270) &&
532 width < this.minHeight ||
533 width > this.imageEl.OriginWidth ||
534 height < this.minWidth ||
535 height > this.imageEl.OriginHeight
545 onRotateLeft : function(e)
547 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
549 var minScale = this.thumbEl.getWidth() / this.minWidth;
551 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
552 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
554 this.startScale = this.scale;
556 while (this.getScaleLevel() < minScale){
558 this.scale = this.scale + 1;
560 if(!this.zoomable()){
565 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
566 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
571 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
578 this.scale = this.startScale;
585 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
588 this.setThumbBoxSize();
589 this.setThumbBoxPosition();
590 this.setCanvasPosition();
595 this.fireEvent('rotate', this, 'left');
599 onRotateRight : function(e)
601 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
603 var minScale = this.thumbEl.getWidth() / this.minWidth;
605 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
606 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
608 this.startScale = this.scale;
610 while (this.getScaleLevel() < minScale){
612 this.scale = this.scale + 1;
614 if(!this.zoomable()){
619 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
620 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
625 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
632 this.scale = this.startScale;
639 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
642 this.setThumbBoxSize();
643 this.setThumbBoxPosition();
644 this.setCanvasPosition();
649 this.fireEvent('rotate', this, 'right');
652 onRotateFail : function()
654 this.errorEl.show(true);
658 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
663 this.previewEl.dom.innerHTML = '';
665 var canvasEl = document.createElement("canvas");
667 var contextEl = canvasEl.getContext("2d");
669 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
670 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
671 var center = this.imageEl.OriginWidth / 2;
673 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
674 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
675 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
676 center = this.imageEl.OriginHeight / 2;
679 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
681 contextEl.translate(center, center);
682 contextEl.rotate(this.rotate * Math.PI / 180);
684 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
686 this.canvasEl = document.createElement("canvas");
688 this.contextEl = this.canvasEl.getContext("2d");
690 switch (this.rotate) {
693 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
694 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
696 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
701 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
702 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
704 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
705 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);
709 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
714 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
715 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
717 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
718 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);
722 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);
727 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
728 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
730 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
731 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
735 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);
742 this.previewEl.appendChild(this.canvasEl);
744 this.setCanvasPosition();
749 if(!this.canvasLoaded){
753 var imageCanvas = document.createElement("canvas");
755 var imageContext = imageCanvas.getContext("2d");
757 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
758 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
760 var center = imageCanvas.width / 2;
762 imageContext.translate(center, center);
764 imageContext.rotate(this.rotate * Math.PI / 180);
766 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
768 var canvas = document.createElement("canvas");
770 var context = canvas.getContext("2d");
772 canvas.width = this.minWidth;
773 canvas.height = this.minHeight;
775 switch (this.rotate) {
778 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
779 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
781 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
782 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
784 var targetWidth = this.minWidth - 2 * x;
785 var targetHeight = this.minHeight - 2 * y;
789 if((x == 0 && y == 0) || (x == 0 && y > 0)){
790 scale = targetWidth / width;
794 scale = targetHeight / height;
798 scale = targetWidth / width;
801 scale = targetHeight / height;
805 context.scale(scale, scale);
807 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
808 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
810 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
811 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
813 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
818 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
819 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
821 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
822 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
824 var targetWidth = this.minWidth - 2 * x;
825 var targetHeight = this.minHeight - 2 * y;
829 if((x == 0 && y == 0) || (x == 0 && y > 0)){
830 scale = targetWidth / width;
834 scale = targetHeight / height;
838 scale = targetWidth / width;
841 scale = targetHeight / height;
845 context.scale(scale, scale);
847 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
848 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
850 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
851 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
853 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
855 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
860 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
861 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
863 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
864 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
866 var targetWidth = this.minWidth - 2 * x;
867 var targetHeight = this.minHeight - 2 * y;
871 if((x == 0 && y == 0) || (x == 0 && y > 0)){
872 scale = targetWidth / width;
876 scale = targetHeight / height;
880 scale = targetWidth / width;
883 scale = targetHeight / height;
887 context.scale(scale, scale);
889 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
890 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
892 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
893 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
895 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
896 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
898 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
903 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
904 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
906 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
907 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
909 var targetWidth = this.minWidth - 2 * x;
910 var targetHeight = this.minHeight - 2 * y;
914 if((x == 0 && y == 0) || (x == 0 && y > 0)){
915 scale = targetWidth / width;
919 scale = targetHeight / height;
923 scale = targetWidth / width;
926 scale = targetHeight / height;
930 context.scale(scale, scale);
932 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
933 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
935 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
936 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
938 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
940 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
947 this.cropData = canvas.toDataURL(this.cropType);
949 this.fireEvent('crop', this, this.cropData);
953 setThumbBoxSize : function()
957 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
958 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
959 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
961 this.minWidth = width;
962 this.minHeight = height;
964 if(this.rotate == 90 || this.rotate == 270){
965 this.minWidth = height;
966 this.minHeight = width;
971 width = Math.ceil(this.minWidth * height / this.minHeight);
973 if(this.minWidth > this.minHeight){
975 height = Math.ceil(this.minHeight * width / this.minWidth);
978 this.thumbEl.setStyle({
979 width : width + 'px',
980 height : height + 'px'
987 setThumbBoxPosition : function()
989 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
990 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
992 this.thumbEl.setLeft(x);
993 this.thumbEl.setTop(y);
997 baseRotateLevel : function()
1002 typeof(this.exif) != 'undefined' &&
1003 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1004 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1006 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1009 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1013 baseScaleLevel : function()
1017 if(this.isDocument){
1019 if(this.baseRotate == 6 || this.baseRotate == 8){
1021 height = this.thumbEl.getHeight();
1022 this.baseScale = height / this.imageEl.OriginWidth;
1024 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1025 width = this.thumbEl.getWidth();
1026 this.baseScale = width / this.imageEl.OriginHeight;
1032 height = this.thumbEl.getHeight();
1033 this.baseScale = height / this.imageEl.OriginHeight;
1035 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1036 width = this.thumbEl.getWidth();
1037 this.baseScale = width / this.imageEl.OriginWidth;
1043 if(this.baseRotate == 6 || this.baseRotate == 8){
1045 width = this.thumbEl.getHeight();
1046 this.baseScale = width / this.imageEl.OriginHeight;
1048 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1049 height = this.thumbEl.getWidth();
1050 this.baseScale = height / this.imageEl.OriginHeight;
1053 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1054 height = this.thumbEl.getWidth();
1055 this.baseScale = height / this.imageEl.OriginHeight;
1057 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1058 width = this.thumbEl.getHeight();
1059 this.baseScale = width / this.imageEl.OriginWidth;
1066 width = this.thumbEl.getWidth();
1067 this.baseScale = width / this.imageEl.OriginWidth;
1069 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1070 height = this.thumbEl.getHeight();
1071 this.baseScale = height / this.imageEl.OriginHeight;
1074 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1076 height = this.thumbEl.getHeight();
1077 this.baseScale = height / this.imageEl.OriginHeight;
1079 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1080 width = this.thumbEl.getWidth();
1081 this.baseScale = width / this.imageEl.OriginWidth;
1089 getScaleLevel : function()
1091 return this.baseScale * Math.pow(1.1, this.scale);
1094 onTouchStart : function(e)
1096 if(!this.canvasLoaded){
1097 this.beforeSelectFile(e);
1101 var touches = e.browserEvent.touches;
1107 if(touches.length == 1){
1108 this.onMouseDown(e);
1112 if(touches.length != 2){
1118 for(var i = 0, finger; finger = touches[i]; i++){
1119 coords.push(finger.pageX, finger.pageY);
1122 var x = Math.pow(coords[0] - coords[2], 2);
1123 var y = Math.pow(coords[1] - coords[3], 2);
1125 this.startDistance = Math.sqrt(x + y);
1127 this.startScale = this.scale;
1129 this.pinching = true;
1130 this.dragable = false;
1134 onTouchMove : function(e)
1136 if(!this.pinching && !this.dragable){
1140 var touches = e.browserEvent.touches;
1147 this.onMouseMove(e);
1153 for(var i = 0, finger; finger = touches[i]; i++){
1154 coords.push(finger.pageX, finger.pageY);
1157 var x = Math.pow(coords[0] - coords[2], 2);
1158 var y = Math.pow(coords[1] - coords[3], 2);
1160 this.endDistance = Math.sqrt(x + y);
1162 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1164 if(!this.zoomable()){
1165 this.scale = this.startScale;
1173 onTouchEnd : function(e)
1175 this.pinching = false;
1176 this.dragable = false;
1180 process : function(file, crop)
1182 this.xhr = new XMLHttpRequest();
1184 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
1188 file.xhr = this.xhr;
1190 this.xhr.open(this.method, this.url, true);
1193 "Accept": "application/json",
1194 "Cache-Control": "no-cache",
1195 "X-Requested-With": "XMLHttpRequest"
1198 for (var headerName in headers) {
1199 var headerValue = headers[headerName];
1201 this.xhr.setRequestHeader(headerName, headerValue);
1207 this.xhr.onload = function()
1209 _this.xhrOnLoad(_this.xhr);
1212 this.xhr.onerror = function()
1214 _this.xhrOnError(_this.xhr);
1217 var formData = new FormData();
1219 formData.append('returnHTML', 'NO');
1222 formData.append('crop', crop);
1225 formData.append(this.paramName, file, file.name);
1227 if(this.fireEvent('prepare', this, formData) != false){
1228 this.xhr.send(formData);
1232 xhrOnLoad : function(xhr)
1234 if (xhr.readyState !== 4) {
1235 this.fireEvent('exception', this, xhr);
1239 var response = Roo.decode(xhr.responseText);
1241 if(!response.success){
1242 this.fireEvent('exception', this, xhr);
1246 var response = Roo.decode(xhr.responseText);
1248 this.fireEvent('upload', this, response);
1252 xhrOnError : function()
1254 Roo.log('xhr on error');
1256 var response = Roo.decode(xhr.responseText);
1262 prepare : function(file)
1267 if(typeof(file) === 'string'){
1268 this.loadCanvas(file);
1272 if(!file || !this.urlAPI){
1277 this.cropType = file.type;
1281 if(this.fireEvent('prepare', this, this.file) != false){
1283 var reader = new FileReader();
1285 reader.onload = function (e) {
1286 if (e.target.error) {
1287 Roo.log(e.target.error);
1291 var buffer = e.target.result,
1292 dataView = new DataView(buffer),
1294 maxOffset = dataView.byteLength - 4,
1298 if (dataView.getUint16(0) === 0xffd8) {
1299 while (offset < maxOffset) {
1300 markerBytes = dataView.getUint16(offset);
1302 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1303 markerLength = dataView.getUint16(offset + 2) + 2;
1304 if (offset + markerLength > dataView.byteLength) {
1305 Roo.log('Invalid meta data: Invalid segment size.');
1309 if(markerBytes == 0xffe1){
1310 _this.parseExifData(
1317 offset += markerLength;
1327 var url = _this.urlAPI.createObjectURL(_this.file);
1329 _this.loadCanvas(url);
1334 reader.readAsArrayBuffer(this.file);
1340 parseExifData : function(dataView, offset, length)
1342 var tiffOffset = offset + 10,
1346 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1347 // No Exif data, might be XMP data instead
1351 // Check for the ASCII code for "Exif" (0x45786966):
1352 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1353 // No Exif data, might be XMP data instead
1356 if (tiffOffset + 8 > dataView.byteLength) {
1357 Roo.log('Invalid Exif data: Invalid segment size.');
1360 // Check for the two null bytes:
1361 if (dataView.getUint16(offset + 8) !== 0x0000) {
1362 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1365 // Check the byte alignment:
1366 switch (dataView.getUint16(tiffOffset)) {
1368 littleEndian = true;
1371 littleEndian = false;
1374 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1377 // Check for the TIFF tag marker (0x002A):
1378 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1379 Roo.log('Invalid Exif data: Missing TIFF marker.');
1382 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1383 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1388 tiffOffset + dirOffset,
1393 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1398 if (dirOffset + 6 > dataView.byteLength) {
1399 Roo.log('Invalid Exif data: Invalid directory offset.');
1402 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1403 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1404 if (dirEndOffset + 4 > dataView.byteLength) {
1405 Roo.log('Invalid Exif data: Invalid directory size.');
1408 for (i = 0; i < tagsNumber; i += 1) {
1412 dirOffset + 2 + 12 * i, // tag offset
1416 // Return the offset to the next directory:
1417 return dataView.getUint32(dirEndOffset, littleEndian);
1420 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1422 var tag = dataView.getUint16(offset, littleEndian);
1424 this.exif[tag] = this.getExifValue(
1428 dataView.getUint16(offset + 2, littleEndian), // tag type
1429 dataView.getUint32(offset + 4, littleEndian), // tag length
1434 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1436 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1445 Roo.log('Invalid Exif data: Invalid tag type.');
1449 tagSize = tagType.size * length;
1450 // Determine if the value is contained in the dataOffset bytes,
1451 // or if the value at the dataOffset is a pointer to the actual data:
1452 dataOffset = tagSize > 4 ?
1453 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1454 if (dataOffset + tagSize > dataView.byteLength) {
1455 Roo.log('Invalid Exif data: Invalid data offset.');
1459 return tagType.getValue(dataView, dataOffset, littleEndian);
1462 for (i = 0; i < length; i += 1) {
1463 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1466 if (tagType.ascii) {
1468 // Concatenate the chars:
1469 for (i = 0; i < values.length; i += 1) {
1471 // Ignore the terminating NULL byte(s):
1472 if (c === '\u0000') {
1484 Roo.apply(Roo.bootstrap.UploadCropbox, {
1486 'Orientation': 0x0112
1492 3: 180, //'bottom-right',
1493 // 4: 'bottom-left',
1495 6: 90, //'right-top',
1496 // 7: 'right-bottom',
1497 8: 270 //'left-bottom'
1501 // byte, 8-bit unsigned int:
1503 getValue: function (dataView, dataOffset) {
1504 return dataView.getUint8(dataOffset);
1508 // ascii, 8-bit byte:
1510 getValue: function (dataView, dataOffset) {
1511 return String.fromCharCode(dataView.getUint8(dataOffset));
1516 // short, 16 bit int:
1518 getValue: function (dataView, dataOffset, littleEndian) {
1519 return dataView.getUint16(dataOffset, littleEndian);
1523 // long, 32 bit int:
1525 getValue: function (dataView, dataOffset, littleEndian) {
1526 return dataView.getUint32(dataOffset, littleEndian);
1530 // rational = two long values, first is numerator, second is denominator:
1532 getValue: function (dataView, dataOffset, littleEndian) {
1533 return dataView.getUint32(dataOffset, littleEndian) /
1534 dataView.getUint32(dataOffset + 4, littleEndian);
1538 // slong, 32 bit signed int:
1540 getValue: function (dataView, dataOffset, littleEndian) {
1541 return dataView.getInt32(dataOffset, littleEndian);
1545 // srational, two slongs, first is numerator, second is denominator:
1547 getValue: function (dataView, dataOffset, littleEndian) {
1548 return dataView.getInt32(dataOffset, littleEndian) /
1549 dataView.getInt32(dataOffset + 4, littleEndian);
1559 cls : 'btn-group roo-upload-cropbox-rotate-left',
1560 action : 'rotate-left',
1564 cls : 'btn btn-default',
1565 html : '<i class="fa fa-undo"></i>'
1571 cls : 'btn-group roo-upload-cropbox-picture',
1576 cls : 'btn btn-default',
1577 html : '<i class="fa fa-picture-o"></i>'
1583 cls : 'btn-group roo-upload-cropbox-rotate-right',
1584 action : 'rotate-right',
1588 cls : 'btn btn-default',
1589 html : '<i class="fa fa-repeat"></i>'
1597 cls : 'btn-group roo-upload-cropbox-rotate-left',
1598 action : 'rotate-left',
1602 cls : 'btn btn-default',
1603 html : '<i class="fa fa-undo"></i>'
1609 cls : 'btn-group roo-upload-cropbox-download',
1610 action : 'download',
1614 cls : 'btn btn-default',
1615 html : '<i class="fa fa-download"></i>'
1621 cls : 'btn-group roo-upload-cropbox-crop',
1626 cls : 'btn btn-default',
1627 html : '<i class="fa fa-crop"></i>'
1633 cls : 'btn-group roo-upload-cropbox-trash',
1638 cls : 'btn btn-default',
1639 html : '<i class="fa fa-trash"></i>'
1645 cls : 'btn-group roo-upload-cropbox-rotate-right',
1646 action : 'rotate-right',
1650 cls : 'btn btn-default',
1651 html : '<i class="fa fa-repeat"></i>'
1659 cls : 'btn-group roo-upload-cropbox-rotate-left',
1660 action : 'rotate-left',
1664 cls : 'btn btn-default',
1665 html : '<i class="fa fa-undo"></i>'
1671 cls : 'btn-group roo-upload-cropbox-rotate-right',
1672 action : 'rotate-right',
1676 cls : 'btn btn-default',
1677 html : '<i class="fa fa-repeat"></i>'