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 {String} loadingText text when loading the image default 'Loading...'
23 * Create a new UploadCropbox
24 * @param {Object} config The config object
27 Roo.bootstrap.UploadCropbox = function(config){
28 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
32 * @event beforeselectfile
33 * Fire before select file
34 * @param {Roo.bootstrap.UploadCropbox} this
36 "beforeselectfile" : true,
39 * Fire after initEvent
40 * @param {Roo.bootstrap.UploadCropbox} this
45 * Fire after initEvent
46 * @param {Roo.bootstrap.UploadCropbox} this
47 * @param {String} data
52 * Fire when preparing the file data
53 * @param {Roo.bootstrap.UploadCropbox} this
54 * @param {Object} file
59 * Fire when get exception
60 * @param {Roo.bootstrap.UploadCropbox} this
61 * @param {XMLHttpRequest} xhr
65 * @event beforeloadcanvas
66 * Fire before load the canvas
67 * @param {Roo.bootstrap.UploadCropbox} this
70 "beforeloadcanvas" : true,
73 * Fire when trash image
74 * @param {Roo.bootstrap.UploadCropbox} this
79 * Fire when download the image
80 * @param {Roo.bootstrap.UploadCropbox} this
84 * @event footerbuttonclick
85 * Fire when footerbuttonclick
86 * @param {Roo.bootstrap.UploadCropbox} this
87 * @param {String} type
89 "footerbuttonclick" : true,
93 * @param {Roo.bootstrap.UploadCropbox} this
98 * Fire when rotate the image
99 * @param {Roo.bootstrap.UploadCropbox} this
100 * @param {String} pos
105 * Fire when inspect the file
106 * @param {Roo.bootstrap.UploadCropbox} this
107 * @param {Object} file
112 * Fire when xhr upload the file
113 * @param {Roo.bootstrap.UploadCropbox} this
114 * @param {Object} data
119 * Fire when arrange the file data
120 * @param {Roo.bootstrap.UploadCropbox} this
121 * @param {Object} formData
126 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.STANDARD;
129 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
131 emptyText : 'Click to upload image',
132 rotateNotify : 'Image is too small to rotate',
133 loadingText : 'Loading...',
148 cropType : 'image/jpeg',
150 canvasLoaded : false,
153 paramName : 'imageUpload',
155 getAutoCreate : function()
159 cls : 'roo-upload-cropbox',
163 cls : 'roo-upload-cropbox-selector',
168 cls : 'roo-upload-cropbox-body',
169 style : 'cursor:pointer',
173 cls : 'roo-upload-cropbox-preview'
177 cls : 'roo-upload-cropbox-thumb'
181 cls : 'roo-upload-cropbox-empty-notify',
182 html : this.emptyText
186 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
187 html : this.rotateNotify
193 cls : 'roo-upload-cropbox-footer',
196 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
206 onRender : function(ct, position)
208 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
210 if (this.buttons.length) {
212 Roo.each(this.buttons, function(bb) {
214 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
216 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
222 initEvents : function()
224 this.urlAPI = (window.createObjectURL && window) ||
225 (window.URL && URL.revokeObjectURL && URL) ||
226 (window.webkitURL && webkitURL);
228 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
229 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
231 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
232 this.selectorEl.hide();
234 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
235 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
237 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
238 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
241 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
242 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
244 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
245 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
248 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
249 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
250 this.footerEl.hide();
252 this.setThumbBoxSize();
258 this.fireEvent('initial', this);
265 window.addEventListener("resize", function() { _this.resize(); } );
267 this.bodyEl.on('click', this.beforeSelectFile, this);
270 this.bodyEl.on('touchstart', this.onTouchStart, this);
271 this.bodyEl.on('touchmove', this.onTouchMove, this);
272 this.bodyEl.on('touchend', this.onTouchEnd, this);
276 this.bodyEl.on('mousedown', this.onMouseDown, this);
277 this.bodyEl.on('mousemove', this.onMouseMove, this);
278 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
279 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
280 Roo.get(document).on('mouseup', this.onMouseUp, this);
283 this.selectorEl.on('change', this.onFileSelected, this);
292 this.dragable = false;
293 this.pinching = false;
296 this.cropData = false;
297 this.notifyEl.dom.innerHTML = this.emptyText;
299 this.selectorEl.dom.value = '';
305 if(this.fireEvent('resize', this) != false){
306 this.setThumbBoxPosition();
307 this.setCanvasPosition();
311 onFooterButtonClick : function(e, el, o, type)
315 this.onRotateLeft(e);
317 case 'rotate-right' :
318 this.onRotateRight(e);
321 this.beforeSelectFile(e);
336 this.fireEvent('footerbuttonclick', this, type);
339 beforeSelectFile : function(e)
343 if(this.fireEvent('beforeselectfile', this) != false){
344 this.selectorEl.dom.click();
348 onFileSelected : function(e)
352 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
356 var file = this.selectorEl.dom.files[0];
358 if(this.fireEvent('inspect', this, file) != false){
366 this.fireEvent('trash', this);
369 download : function(e)
371 this.fireEvent('download', this);
374 loadCanvas : function(src)
376 if(this.fireEvent('beforeloadcanvas', this, src) != false){
380 this.imageEl = document.createElement('img');
384 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
386 this.imageEl.src = src;
390 onLoadCanvas : function()
392 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
393 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
395 this.bodyEl.un('click', this.beforeSelectFile, this);
397 this.notifyEl.hide();
399 this.footerEl.show();
401 this.baseRotateLevel();
404 this.setThumbBoxSize();
407 this.setThumbBoxPosition();
409 this.baseScaleLevel();
415 this.canvasLoaded = true;
419 setCanvasPosition : function()
425 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
426 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
428 this.previewEl.setLeft(pw);
429 this.previewEl.setTop(ph);
433 onMouseDown : function(e)
437 this.dragable = true;
438 this.pinching = false;
440 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
441 this.dragable = false;
445 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
446 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
450 onMouseMove : function(e)
454 if(!this.canvasLoaded){
462 var minX = Math.ceil(this.thumbEl.getLeft(true));
463 var minY = Math.ceil(this.thumbEl.getTop(true));
465 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
466 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
468 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
469 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
474 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
475 var bgY = Math.ceil(y + this.previewEl.getTop(true));
477 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
478 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
480 this.previewEl.setLeft(bgX);
481 this.previewEl.setTop(bgY);
483 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
484 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
487 onMouseUp : function(e)
491 this.dragable = false;
494 onMouseWheel : function(e)
498 this.startScale = this.scale;
500 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
502 if(!this.zoomable()){
503 this.scale = this.startScale;
512 zoomable : function()
514 var minScale = this.thumbEl.getWidth() / this.minWidth;
516 if(this.minWidth < this.minHeight){
517 minScale = this.thumbEl.getHeight() / this.minHeight;
520 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
521 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
525 (this.rotate == 0 || this.rotate == 180) &&
527 width > this.imageEl.OriginWidth ||
528 height > this.imageEl.OriginHeight ||
529 (width < this.minWidth && height < this.minHeight)
537 (this.rotate == 90 || this.rotate == 270) &&
539 width > this.imageEl.OriginWidth ||
540 height > this.imageEl.OriginHeight ||
541 (width < this.minHeight && height < this.minWidth)
549 (this.rotate == 0 || this.rotate == 180) &&
551 width < this.minWidth ||
552 width > this.imageEl.OriginWidth ||
553 height < this.minHeight ||
554 height > this.imageEl.OriginHeight
562 (this.rotate == 90 || this.rotate == 270) &&
564 width < this.minHeight ||
565 width > this.imageEl.OriginWidth ||
566 height < this.minWidth ||
567 height > this.imageEl.OriginHeight
577 onRotateLeft : function(e)
579 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
581 var minScale = this.thumbEl.getWidth() / this.minWidth;
583 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
584 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
586 this.startScale = this.scale;
588 while (this.getScaleLevel() < minScale){
590 this.scale = this.scale + 1;
592 if(!this.zoomable()){
597 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
598 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
603 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
610 this.scale = this.startScale;
617 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
620 this.setThumbBoxSize();
621 this.setThumbBoxPosition();
622 this.setCanvasPosition();
627 this.fireEvent('rotate', this, 'left');
631 onRotateRight : function(e)
633 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
635 var minScale = this.thumbEl.getWidth() / this.minWidth;
637 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
638 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
640 this.startScale = this.scale;
642 while (this.getScaleLevel() < minScale){
644 this.scale = this.scale + 1;
646 if(!this.zoomable()){
651 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
652 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
657 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
664 this.scale = this.startScale;
671 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
674 this.setThumbBoxSize();
675 this.setThumbBoxPosition();
676 this.setCanvasPosition();
681 this.fireEvent('rotate', this, 'right');
684 onRotateFail : function()
686 this.errorEl.show(true);
690 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
695 this.previewEl.dom.innerHTML = '';
697 var canvasEl = document.createElement("canvas");
699 var contextEl = canvasEl.getContext("2d");
701 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
702 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
703 var center = this.imageEl.OriginWidth / 2;
705 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
706 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
707 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
708 center = this.imageEl.OriginHeight / 2;
711 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
713 contextEl.translate(center, center);
714 contextEl.rotate(this.rotate * Math.PI / 180);
716 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
718 this.canvasEl = document.createElement("canvas");
720 this.contextEl = this.canvasEl.getContext("2d");
722 switch (this.rotate) {
725 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
726 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
728 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
733 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
734 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
736 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
737 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);
741 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
746 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
747 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
749 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
750 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);
754 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);
759 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
760 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
762 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
763 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
767 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);
774 this.previewEl.appendChild(this.canvasEl);
776 this.setCanvasPosition();
781 if(!this.canvasLoaded){
785 var imageCanvas = document.createElement("canvas");
787 var imageContext = imageCanvas.getContext("2d");
789 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
790 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
792 var center = imageCanvas.width / 2;
794 imageContext.translate(center, center);
796 imageContext.rotate(this.rotate * Math.PI / 180);
798 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
800 var canvas = document.createElement("canvas");
802 var context = canvas.getContext("2d");
804 canvas.width = this.minWidth;
805 canvas.height = this.minHeight;
807 switch (this.rotate) {
810 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
811 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
813 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
814 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
816 var targetWidth = this.minWidth - 2 * x;
817 var targetHeight = this.minHeight - 2 * y;
821 if((x == 0 && y == 0) || (x == 0 && y > 0)){
822 scale = targetWidth / width;
826 scale = targetHeight / height;
830 scale = targetWidth / width;
833 scale = targetHeight / height;
837 context.scale(scale, scale);
839 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
840 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
842 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
843 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
845 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
850 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
851 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
853 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
854 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
856 var targetWidth = this.minWidth - 2 * x;
857 var targetHeight = this.minHeight - 2 * y;
861 if((x == 0 && y == 0) || (x == 0 && y > 0)){
862 scale = targetWidth / width;
866 scale = targetHeight / height;
870 scale = targetWidth / width;
873 scale = targetHeight / height;
877 context.scale(scale, scale);
879 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
880 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
882 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
883 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
885 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
887 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
892 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
893 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
895 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
896 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
898 var targetWidth = this.minWidth - 2 * x;
899 var targetHeight = this.minHeight - 2 * y;
903 if((x == 0 && y == 0) || (x == 0 && y > 0)){
904 scale = targetWidth / width;
908 scale = targetHeight / height;
912 scale = targetWidth / width;
915 scale = targetHeight / height;
919 context.scale(scale, scale);
921 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
922 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
924 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
925 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
927 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
928 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
930 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
935 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
936 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
938 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
939 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
941 var targetWidth = this.minWidth - 2 * x;
942 var targetHeight = this.minHeight - 2 * y;
946 if((x == 0 && y == 0) || (x == 0 && y > 0)){
947 scale = targetWidth / width;
951 scale = targetHeight / height;
955 scale = targetWidth / width;
958 scale = targetHeight / height;
962 context.scale(scale, scale);
964 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
965 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
967 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
968 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
970 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
972 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
979 this.cropData = canvas.toDataURL(this.cropType);
981 if(this.fireEvent('crop', this, this.cropData) !== false){
982 this.process(this.file, this.cropData);
989 setThumbBoxSize : function()
993 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
994 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
995 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
997 this.minWidth = width;
998 this.minHeight = height;
1000 if(this.rotate == 90 || this.rotate == 270){
1001 this.minWidth = height;
1002 this.minHeight = width;
1007 width = Math.ceil(this.minWidth * height / this.minHeight);
1009 if(this.minWidth > this.minHeight){
1011 height = Math.ceil(this.minHeight * width / this.minWidth);
1014 this.thumbEl.setStyle({
1015 width : width + 'px',
1016 height : height + 'px'
1023 setThumbBoxPosition : function()
1025 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1026 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1028 this.thumbEl.setLeft(x);
1029 this.thumbEl.setTop(y);
1033 baseRotateLevel : function()
1035 this.baseRotate = 1;
1038 typeof(this.exif) != 'undefined' &&
1039 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1040 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1042 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1045 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1049 baseScaleLevel : function()
1053 if(this.isDocument){
1055 if(this.baseRotate == 6 || this.baseRotate == 8){
1057 height = this.thumbEl.getHeight();
1058 this.baseScale = height / this.imageEl.OriginWidth;
1060 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1061 width = this.thumbEl.getWidth();
1062 this.baseScale = width / this.imageEl.OriginHeight;
1068 height = this.thumbEl.getHeight();
1069 this.baseScale = height / this.imageEl.OriginHeight;
1071 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1072 width = this.thumbEl.getWidth();
1073 this.baseScale = width / this.imageEl.OriginWidth;
1079 if(this.baseRotate == 6 || this.baseRotate == 8){
1081 width = this.thumbEl.getHeight();
1082 this.baseScale = width / this.imageEl.OriginHeight;
1084 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1085 height = this.thumbEl.getWidth();
1086 this.baseScale = height / this.imageEl.OriginHeight;
1089 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1090 height = this.thumbEl.getWidth();
1091 this.baseScale = height / this.imageEl.OriginHeight;
1093 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1094 width = this.thumbEl.getHeight();
1095 this.baseScale = width / this.imageEl.OriginWidth;
1102 width = this.thumbEl.getWidth();
1103 this.baseScale = width / this.imageEl.OriginWidth;
1105 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1106 height = this.thumbEl.getHeight();
1107 this.baseScale = height / this.imageEl.OriginHeight;
1110 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1112 height = this.thumbEl.getHeight();
1113 this.baseScale = height / this.imageEl.OriginHeight;
1115 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1116 width = this.thumbEl.getWidth();
1117 this.baseScale = width / this.imageEl.OriginWidth;
1125 getScaleLevel : function()
1127 return this.baseScale * Math.pow(1.1, this.scale);
1130 onTouchStart : function(e)
1132 if(!this.canvasLoaded){
1133 this.beforeSelectFile(e);
1137 var touches = e.browserEvent.touches;
1143 if(touches.length == 1){
1144 this.onMouseDown(e);
1148 if(touches.length != 2){
1154 for(var i = 0, finger; finger = touches[i]; i++){
1155 coords.push(finger.pageX, finger.pageY);
1158 var x = Math.pow(coords[0] - coords[2], 2);
1159 var y = Math.pow(coords[1] - coords[3], 2);
1161 this.startDistance = Math.sqrt(x + y);
1163 this.startScale = this.scale;
1165 this.pinching = true;
1166 this.dragable = false;
1170 onTouchMove : function(e)
1172 if(!this.pinching && !this.dragable){
1176 var touches = e.browserEvent.touches;
1183 this.onMouseMove(e);
1189 for(var i = 0, finger; finger = touches[i]; i++){
1190 coords.push(finger.pageX, finger.pageY);
1193 var x = Math.pow(coords[0] - coords[2], 2);
1194 var y = Math.pow(coords[1] - coords[3], 2);
1196 this.endDistance = Math.sqrt(x + y);
1198 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1200 if(!this.zoomable()){
1201 this.scale = this.startScale;
1209 onTouchEnd : function(e)
1211 this.pinching = false;
1212 this.dragable = false;
1216 process : function(file, crop)
1218 this.xhr = new XMLHttpRequest();
1220 file.xhr = this.xhr;
1222 this.xhr.open(this.method, this.url, true);
1225 "Accept": "application/json",
1226 "Cache-Control": "no-cache",
1227 "X-Requested-With": "XMLHttpRequest"
1230 for (var headerName in headers) {
1231 var headerValue = headers[headerName];
1233 this.xhr.setRequestHeader(headerName, headerValue);
1239 this.xhr.onload = function()
1241 _this.xhrOnLoad(_this.xhr);
1244 this.xhr.onerror = function()
1246 _this.xhrOnError(_this.xhr);
1249 var formData = new FormData();
1251 formData.append('returnHTML', 'NO');
1254 formData.append('crop', crop);
1257 if(typeof(file) != 'undefined' && (typeof(file.id) == 'undefined' || file.id * 1 < 1)){
1258 formData.append(this.paramName, file, file.name);
1261 if(typeof(file.filename) != 'undefined'){
1262 formData.append('filename', file.filename);
1265 if(typeof(file.mimetype) != 'undefined'){
1266 formData.append('mimetype', file.mimetype);
1269 if(this.fireEvent('arrange', this, formData) != false){
1270 this.xhr.send(formData);
1274 xhrOnLoad : function(xhr)
1276 if (xhr.readyState !== 4) {
1277 this.fireEvent('exception', this, xhr);
1281 var response = Roo.decode(xhr.responseText);
1283 if(!response.success){
1284 this.fireEvent('exception', this, xhr);
1288 var response = Roo.decode(xhr.responseText);
1290 this.fireEvent('upload', this, response);
1294 xhrOnError : function()
1296 Roo.log('xhr on error');
1298 var response = Roo.decode(xhr.responseText);
1304 prepare : function(file)
1309 if(typeof(file) === 'string'){
1310 this.loadCanvas(file);
1314 if(!file || !this.urlAPI){
1319 this.cropType = file.type;
1323 if(this.fireEvent('prepare', this, this.file) != false){
1325 var reader = new FileReader();
1327 reader.onload = function (e) {
1328 if (e.target.error) {
1329 Roo.log(e.target.error);
1333 var buffer = e.target.result,
1334 dataView = new DataView(buffer),
1336 maxOffset = dataView.byteLength - 4,
1340 if (dataView.getUint16(0) === 0xffd8) {
1341 while (offset < maxOffset) {
1342 markerBytes = dataView.getUint16(offset);
1344 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1345 markerLength = dataView.getUint16(offset + 2) + 2;
1346 if (offset + markerLength > dataView.byteLength) {
1347 Roo.log('Invalid meta data: Invalid segment size.');
1351 if(markerBytes == 0xffe1){
1352 _this.parseExifData(
1359 offset += markerLength;
1369 var url = _this.urlAPI.createObjectURL(_this.file);
1371 _this.loadCanvas(url);
1376 reader.readAsArrayBuffer(this.file);
1382 parseExifData : function(dataView, offset, length)
1384 var tiffOffset = offset + 10,
1388 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1389 // No Exif data, might be XMP data instead
1393 // Check for the ASCII code for "Exif" (0x45786966):
1394 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1395 // No Exif data, might be XMP data instead
1398 if (tiffOffset + 8 > dataView.byteLength) {
1399 Roo.log('Invalid Exif data: Invalid segment size.');
1402 // Check for the two null bytes:
1403 if (dataView.getUint16(offset + 8) !== 0x0000) {
1404 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1407 // Check the byte alignment:
1408 switch (dataView.getUint16(tiffOffset)) {
1410 littleEndian = true;
1413 littleEndian = false;
1416 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1419 // Check for the TIFF tag marker (0x002A):
1420 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1421 Roo.log('Invalid Exif data: Missing TIFF marker.');
1424 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1425 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1430 tiffOffset + dirOffset,
1435 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1440 if (dirOffset + 6 > dataView.byteLength) {
1441 Roo.log('Invalid Exif data: Invalid directory offset.');
1444 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1445 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1446 if (dirEndOffset + 4 > dataView.byteLength) {
1447 Roo.log('Invalid Exif data: Invalid directory size.');
1450 for (i = 0; i < tagsNumber; i += 1) {
1454 dirOffset + 2 + 12 * i, // tag offset
1458 // Return the offset to the next directory:
1459 return dataView.getUint32(dirEndOffset, littleEndian);
1462 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1464 var tag = dataView.getUint16(offset, littleEndian);
1466 this.exif[tag] = this.getExifValue(
1470 dataView.getUint16(offset + 2, littleEndian), // tag type
1471 dataView.getUint32(offset + 4, littleEndian), // tag length
1476 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1478 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1487 Roo.log('Invalid Exif data: Invalid tag type.');
1491 tagSize = tagType.size * length;
1492 // Determine if the value is contained in the dataOffset bytes,
1493 // or if the value at the dataOffset is a pointer to the actual data:
1494 dataOffset = tagSize > 4 ?
1495 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1496 if (dataOffset + tagSize > dataView.byteLength) {
1497 Roo.log('Invalid Exif data: Invalid data offset.');
1501 return tagType.getValue(dataView, dataOffset, littleEndian);
1504 for (i = 0; i < length; i += 1) {
1505 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1508 if (tagType.ascii) {
1510 // Concatenate the chars:
1511 for (i = 0; i < values.length; i += 1) {
1513 // Ignore the terminating NULL byte(s):
1514 if (c === '\u0000') {
1526 Roo.apply(Roo.bootstrap.UploadCropbox, {
1528 'Orientation': 0x0112
1534 3: 180, //'bottom-right',
1535 // 4: 'bottom-left',
1537 6: 90, //'right-top',
1538 // 7: 'right-bottom',
1539 8: 270 //'left-bottom'
1543 // byte, 8-bit unsigned int:
1545 getValue: function (dataView, dataOffset) {
1546 return dataView.getUint8(dataOffset);
1550 // ascii, 8-bit byte:
1552 getValue: function (dataView, dataOffset) {
1553 return String.fromCharCode(dataView.getUint8(dataOffset));
1558 // short, 16 bit int:
1560 getValue: function (dataView, dataOffset, littleEndian) {
1561 return dataView.getUint16(dataOffset, littleEndian);
1565 // long, 32 bit int:
1567 getValue: function (dataView, dataOffset, littleEndian) {
1568 return dataView.getUint32(dataOffset, littleEndian);
1572 // rational = two long values, first is numerator, second is denominator:
1574 getValue: function (dataView, dataOffset, littleEndian) {
1575 return dataView.getUint32(dataOffset, littleEndian) /
1576 dataView.getUint32(dataOffset + 4, littleEndian);
1580 // slong, 32 bit signed int:
1582 getValue: function (dataView, dataOffset, littleEndian) {
1583 return dataView.getInt32(dataOffset, littleEndian);
1587 // srational, two slongs, first is numerator, second is denominator:
1589 getValue: function (dataView, dataOffset, littleEndian) {
1590 return dataView.getInt32(dataOffset, littleEndian) /
1591 dataView.getInt32(dataOffset + 4, littleEndian);
1601 cls : 'btn-group roo-upload-cropbox-rotate-left',
1602 action : 'rotate-left',
1606 cls : 'btn btn-default',
1607 html : '<i class="fa fa-undo"></i>'
1613 cls : 'btn-group roo-upload-cropbox-picture',
1618 cls : 'btn btn-default',
1619 html : '<i class="fa fa-picture-o"></i>'
1625 cls : 'btn-group roo-upload-cropbox-rotate-right',
1626 action : 'rotate-right',
1630 cls : 'btn btn-default',
1631 html : '<i class="fa fa-repeat"></i>'
1639 cls : 'btn-group roo-upload-cropbox-rotate-left',
1640 action : 'rotate-left',
1644 cls : 'btn btn-default',
1645 html : '<i class="fa fa-undo"></i>'
1651 cls : 'btn-group roo-upload-cropbox-download',
1652 action : 'download',
1656 cls : 'btn btn-default',
1657 html : '<i class="fa fa-download"></i>'
1663 cls : 'btn-group roo-upload-cropbox-crop',
1668 cls : 'btn btn-default',
1669 html : '<i class="fa fa-crop"></i>'
1675 cls : 'btn-group roo-upload-cropbox-trash',
1680 cls : 'btn btn-default',
1681 html : '<i class="fa fa-trash"></i>'
1687 cls : 'btn-group roo-upload-cropbox-rotate-right',
1688 action : 'rotate-right',
1692 cls : 'btn btn-default',
1693 html : '<i class="fa fa-repeat"></i>'
1701 cls : 'btn-group roo-upload-cropbox-rotate-left',
1702 action : 'rotate-left',
1706 cls : 'btn btn-default',
1707 html : '<i class="fa fa-undo"></i>'
1713 cls : 'btn-group roo-upload-cropbox-rotate-right',
1714 action : 'rotate-right',
1718 cls : 'btn btn-default',
1719 html : '<i class="fa fa-repeat"></i>'