7 * @class Roo.bootstrap.UploadCropbox
8 * @extends Roo.bootstrap.Component
9 * Bootstrap UploadCropbox class
10 * @cfg {String} emptyText show when image has been loaded
11 * @cfg {String} rotateNotify show when image too small to rotate
12 * @cfg {Number} errorTimeout default 3000
13 * @cfg {Number} minWidth default 300
14 * @cfg {Number} minHeight default 300
15 * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
16 * @cfg {Boolean} isDocument (true|false) default false
17 * @cfg {String} url action url
20 * Create a new UploadCropbox
21 * @param {Object} config The config object
24 Roo.bootstrap.UploadCropbox = function(config){
25 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
29 * @event beforeselectfile
30 * Fire before select file
31 * @param {Roo.bootstrap.UploadCropbox} this
33 "beforeselectfile" : true,
36 * Fire after initEvent
37 * @param {Roo.bootstrap.UploadCropbox} this
42 * Fire after initEvent
43 * @param {Roo.bootstrap.UploadCropbox} this
44 * @param {String} data
49 * Fire when preparing the file data
50 * @param {Roo.bootstrap.UploadCropbox} this
51 * @param {Object} file
56 * Fire when get exception
57 * @param {Roo.bootstrap.UploadCropbox} this
58 * @param {XMLHttpRequest} xhr
62 * @event beforeloadcanvas
63 * Fire before load the canvas
64 * @param {Roo.bootstrap.UploadCropbox} this
67 "beforeloadcanvas" : true,
70 * Fire when trash image
71 * @param {Roo.bootstrap.UploadCropbox} this
76 * Fire when download the image
77 * @param {Roo.bootstrap.UploadCropbox} this
81 * @event footerbuttonclick
82 * Fire when footerbuttonclick
83 * @param {Roo.bootstrap.UploadCropbox} this
84 * @param {String} type
86 "footerbuttonclick" : true,
90 * @param {Roo.bootstrap.UploadCropbox} this
95 * Fire when rotate the image
96 * @param {Roo.bootstrap.UploadCropbox} this
102 * Fire when inspect the file
103 * @param {Roo.bootstrap.UploadCropbox} this
104 * @param {Object} file
109 * Fire when xhr upload the file
110 * @param {Roo.bootstrap.UploadCropbox} this
111 * @param {Object} data
116 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);
271 this.selectorEl.on('change', this.onFileSelected, this);
280 this.dragable = false;
281 this.pinching = false;
284 this.cropData = false;
285 this.notifyEl.dom.innerHTML = this.emptyText;
291 if(this.fireEvent('resize', this) != false){
292 this.setThumbBoxPosition();
293 this.setCanvasPosition();
297 onFooterButtonClick : function(e, el, o, type)
301 this.onRotateLeft(e);
303 case 'rotate-right' :
304 this.onRotateRight(e);
307 this.beforeSelectFile(e);
322 this.fireEvent('footerbuttonclick', this, type);
325 beforeSelectFile : function(e)
329 if(this.fireEvent('beforeselectfile', this) != false){
330 this.selectorEl.dom.click();
334 onFileSelected : function(e)
338 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
342 var file = this.selectorEl.dom.files[0];
344 if(this.fireEvent('inspect', this, file) != false){
348 this.selectorEl.dom.value = '';
354 this.fireEvent('trash', this);
357 download : function(e)
359 this.fireEvent('download', this);
362 loadCanvas : function(src)
364 if(this.fireEvent('beforeloadcanvas', this, src) != false){
368 this.imageEl = document.createElement('img');
372 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
374 this.imageEl.src = src;
378 onLoadCanvas : function()
380 this.bodyEl.un('click', this.beforeSelectFile, this);
382 this.notifyEl.hide();
384 this.footerEl.show();
386 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
387 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
389 this.baseRotateLevel();
392 this.setThumbBoxSize();
395 this.setThumbBoxPosition();
397 this.baseScaleLevel();
403 this.canvasLoaded = true;
407 setCanvasPosition : function()
413 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
414 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
416 this.previewEl.setLeft(pw);
417 this.previewEl.setTop(ph);
421 onMouseDown : function(e)
425 this.dragable = true;
426 this.pinching = false;
428 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
429 this.dragable = false;
433 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
434 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
438 onMouseMove : function(e)
442 if(!this.canvasLoaded){
450 var minX = Math.ceil(this.thumbEl.getLeft(true));
451 var minY = Math.ceil(this.thumbEl.getTop(true));
453 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
454 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
456 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
457 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
462 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
463 var bgY = Math.ceil(y + this.previewEl.getTop(true));
465 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
466 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
468 this.previewEl.setLeft(bgX);
469 this.previewEl.setTop(bgY);
471 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
472 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
475 onMouseUp : function(e)
479 this.dragable = false;
482 onMouseWheel : function(e)
486 this.startScale = this.scale;
488 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
490 if(!this.zoomable()){
491 this.scale = this.startScale;
500 zoomable : function()
502 var minScale = this.thumbEl.getWidth() / this.minWidth;
504 if(this.minWidth < this.minHeight){
505 minScale = this.thumbEl.getHeight() / this.minHeight;
508 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
509 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
513 (this.rotate == 0 || this.rotate == 180) &&
515 width > this.imageEl.OriginWidth ||
516 height > this.imageEl.OriginHeight ||
517 (width < this.minWidth && height < this.minHeight)
525 (this.rotate == 90 || this.rotate == 270) &&
527 width > this.imageEl.OriginWidth ||
528 height > this.imageEl.OriginHeight ||
529 (width < this.minHeight && height < this.minWidth)
537 (this.rotate == 0 || this.rotate == 180) &&
539 width < this.minWidth ||
540 width > this.imageEl.OriginWidth ||
541 height < this.minHeight ||
542 height > this.imageEl.OriginHeight
550 (this.rotate == 90 || this.rotate == 270) &&
552 width < this.minHeight ||
553 width > this.imageEl.OriginWidth ||
554 height < this.minWidth ||
555 height > this.imageEl.OriginHeight
565 onRotateLeft : function(e)
567 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
569 var minScale = this.thumbEl.getWidth() / this.minWidth;
571 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
572 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
574 this.startScale = this.scale;
576 while (this.getScaleLevel() < minScale){
578 this.scale = this.scale + 1;
580 if(!this.zoomable()){
585 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
586 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
591 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
598 this.scale = this.startScale;
605 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
608 this.setThumbBoxSize();
609 this.setThumbBoxPosition();
610 this.setCanvasPosition();
615 this.fireEvent('rotate', this, 'left');
619 onRotateRight : function(e)
621 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
623 var minScale = this.thumbEl.getWidth() / this.minWidth;
625 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
626 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
628 this.startScale = this.scale;
630 while (this.getScaleLevel() < minScale){
632 this.scale = this.scale + 1;
634 if(!this.zoomable()){
639 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
640 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
645 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
652 this.scale = this.startScale;
659 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
662 this.setThumbBoxSize();
663 this.setThumbBoxPosition();
664 this.setCanvasPosition();
669 this.fireEvent('rotate', this, 'right');
672 onRotateFail : function()
674 this.errorEl.show(true);
678 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
683 this.previewEl.dom.innerHTML = '';
685 var canvasEl = document.createElement("canvas");
687 var contextEl = canvasEl.getContext("2d");
689 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
690 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
691 var center = this.imageEl.OriginWidth / 2;
693 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
694 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
695 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
696 center = this.imageEl.OriginHeight / 2;
699 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
701 contextEl.translate(center, center);
702 contextEl.rotate(this.rotate * Math.PI / 180);
704 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
706 this.canvasEl = document.createElement("canvas");
708 this.contextEl = this.canvasEl.getContext("2d");
710 switch (this.rotate) {
713 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
714 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
716 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
721 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
722 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
724 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
725 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);
729 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
734 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
735 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
737 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
738 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.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);
747 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
748 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
750 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
751 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
755 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);
762 this.previewEl.appendChild(this.canvasEl);
764 this.setCanvasPosition();
769 if(!this.canvasLoaded){
773 var imageCanvas = document.createElement("canvas");
775 var imageContext = imageCanvas.getContext("2d");
777 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
778 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
780 var center = imageCanvas.width / 2;
782 imageContext.translate(center, center);
784 imageContext.rotate(this.rotate * Math.PI / 180);
786 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
788 var canvas = document.createElement("canvas");
790 var context = canvas.getContext("2d");
792 canvas.width = this.minWidth;
793 canvas.height = this.minHeight;
795 switch (this.rotate) {
798 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
799 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
801 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
802 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
804 var targetWidth = this.minWidth - 2 * x;
805 var targetHeight = this.minHeight - 2 * y;
809 if((x == 0 && y == 0) || (x == 0 && y > 0)){
810 scale = targetWidth / width;
814 scale = targetHeight / height;
818 scale = targetWidth / width;
821 scale = targetHeight / height;
825 context.scale(scale, scale);
827 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
828 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
830 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
831 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
833 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
838 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
839 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
841 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
842 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
844 var targetWidth = this.minWidth - 2 * x;
845 var targetHeight = this.minHeight - 2 * y;
849 if((x == 0 && y == 0) || (x == 0 && y > 0)){
850 scale = targetWidth / width;
854 scale = targetHeight / height;
858 scale = targetWidth / width;
861 scale = targetHeight / height;
865 context.scale(scale, scale);
867 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
868 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
870 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
871 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
873 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
875 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
880 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
881 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
883 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
884 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
886 var targetWidth = this.minWidth - 2 * x;
887 var targetHeight = this.minHeight - 2 * y;
891 if((x == 0 && y == 0) || (x == 0 && y > 0)){
892 scale = targetWidth / width;
896 scale = targetHeight / height;
900 scale = targetWidth / width;
903 scale = targetHeight / height;
907 context.scale(scale, scale);
909 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
910 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
912 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
913 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
915 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
916 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
918 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
923 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
924 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
926 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
927 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
929 var targetWidth = this.minWidth - 2 * x;
930 var targetHeight = this.minHeight - 2 * y;
934 if((x == 0 && y == 0) || (x == 0 && y > 0)){
935 scale = targetWidth / width;
939 scale = targetHeight / height;
943 scale = targetWidth / width;
946 scale = targetHeight / height;
950 context.scale(scale, scale);
952 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
953 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
955 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
956 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
958 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
960 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
967 this.cropData = canvas.toDataURL(this.cropType);
969 this.fireEvent('crop', this, this.cropData);
975 setThumbBoxSize : function()
979 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
980 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
981 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
983 this.minWidth = width;
984 this.minHeight = height;
986 if(this.rotate == 90 || this.rotate == 270){
987 this.minWidth = height;
988 this.minHeight = width;
993 width = Math.ceil(this.minWidth * height / this.minHeight);
995 if(this.minWidth > this.minHeight){
997 height = Math.ceil(this.minHeight * width / this.minWidth);
1000 this.thumbEl.setStyle({
1001 width : width + 'px',
1002 height : height + 'px'
1009 setThumbBoxPosition : function()
1011 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1012 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1014 this.thumbEl.setLeft(x);
1015 this.thumbEl.setTop(y);
1019 baseRotateLevel : function()
1021 this.baseRotate = 1;
1024 typeof(this.exif) != 'undefined' &&
1025 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1026 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1028 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1031 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1035 baseScaleLevel : function()
1039 if(this.isDocument){
1041 if(this.baseRotate == 6 || this.baseRotate == 8){
1043 height = this.thumbEl.getHeight();
1044 this.baseScale = height / this.imageEl.OriginWidth;
1046 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1047 width = this.thumbEl.getWidth();
1048 this.baseScale = width / this.imageEl.OriginHeight;
1054 height = this.thumbEl.getHeight();
1055 this.baseScale = height / this.imageEl.OriginHeight;
1057 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1058 width = this.thumbEl.getWidth();
1059 this.baseScale = width / this.imageEl.OriginWidth;
1065 if(this.baseRotate == 6 || this.baseRotate == 8){
1067 width = this.thumbEl.getHeight();
1068 this.baseScale = width / this.imageEl.OriginHeight;
1070 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1071 height = this.thumbEl.getWidth();
1072 this.baseScale = height / this.imageEl.OriginHeight;
1075 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1076 height = this.thumbEl.getWidth();
1077 this.baseScale = height / this.imageEl.OriginHeight;
1079 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1080 width = this.thumbEl.getHeight();
1081 this.baseScale = width / this.imageEl.OriginWidth;
1088 width = this.thumbEl.getWidth();
1089 this.baseScale = width / this.imageEl.OriginWidth;
1091 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1092 height = this.thumbEl.getHeight();
1093 this.baseScale = height / this.imageEl.OriginHeight;
1096 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1098 height = this.thumbEl.getHeight();
1099 this.baseScale = height / this.imageEl.OriginHeight;
1101 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1102 width = this.thumbEl.getWidth();
1103 this.baseScale = width / this.imageEl.OriginWidth;
1111 getScaleLevel : function()
1113 return this.baseScale * Math.pow(1.1, this.scale);
1116 onTouchStart : function(e)
1118 if(!this.canvasLoaded){
1119 this.beforeSelectFile(e);
1123 var touches = e.browserEvent.touches;
1129 if(touches.length == 1){
1130 this.onMouseDown(e);
1134 if(touches.length != 2){
1140 for(var i = 0, finger; finger = touches[i]; i++){
1141 coords.push(finger.pageX, finger.pageY);
1144 var x = Math.pow(coords[0] - coords[2], 2);
1145 var y = Math.pow(coords[1] - coords[3], 2);
1147 this.startDistance = Math.sqrt(x + y);
1149 this.startScale = this.scale;
1151 this.pinching = true;
1152 this.dragable = false;
1156 onTouchMove : function(e)
1158 if(!this.pinching && !this.dragable){
1162 var touches = e.browserEvent.touches;
1169 this.onMouseMove(e);
1175 for(var i = 0, finger; finger = touches[i]; i++){
1176 coords.push(finger.pageX, finger.pageY);
1179 var x = Math.pow(coords[0] - coords[2], 2);
1180 var y = Math.pow(coords[1] - coords[3], 2);
1182 this.endDistance = Math.sqrt(x + y);
1184 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1186 if(!this.zoomable()){
1187 this.scale = this.startScale;
1195 onTouchEnd : function(e)
1197 this.pinching = false;
1198 this.dragable = false;
1202 process : function(file, crop)
1204 this.xhr = new XMLHttpRequest();
1206 if(typeof(this.file.id) != 'undefined' && this.file.id * 1 > 0){
1210 file.xhr = this.xhr;
1212 this.xhr.open(this.method, this.url, true);
1215 "Accept": "application/json",
1216 "Cache-Control": "no-cache",
1217 "X-Requested-With": "XMLHttpRequest"
1220 for (var headerName in headers) {
1221 var headerValue = headers[headerName];
1223 this.xhr.setRequestHeader(headerName, headerValue);
1229 this.xhr.onload = function()
1231 _this.xhrOnLoad(_this.xhr);
1234 this.xhr.onerror = function()
1236 _this.xhrOnError(_this.xhr);
1239 var formData = new FormData();
1241 formData.append('returnHTML', 'NO');
1244 formData.append('crop', crop);
1247 formData.append(this.paramName, file, file.name);
1249 if(this.fireEvent('arrange', this, formData) != false){
1250 this.xhr.send(formData);
1254 xhrOnLoad : function(xhr)
1256 if (xhr.readyState !== 4) {
1257 this.fireEvent('exception', this, xhr);
1261 var response = Roo.decode(xhr.responseText);
1263 if(!response.success){
1264 this.fireEvent('exception', this, xhr);
1268 var response = Roo.decode(xhr.responseText);
1270 this.fireEvent('upload', this, response);
1274 xhrOnError : function()
1276 Roo.log('xhr on error');
1278 var response = Roo.decode(xhr.responseText);
1284 uploadFromSource : function(file, crop)
1286 this.xhr = new XMLHttpRequest();
1288 this.xhr.open(this.method, this.url, true);
1291 "Accept": "application/json",
1292 "Cache-Control": "no-cache",
1293 "X-Requested-With": "XMLHttpRequest"
1296 for (var headerName in headers) {
1297 var headerValue = headers[headerName];
1299 this.xhr.setRequestHeader(headerName, headerValue);
1305 this.xhr.onload = function()
1307 _this.xhrOnLoad(_this.xhr);
1310 this.xhr.onerror = function()
1312 _this.xhrOnError(_this.xhr);
1315 var formData = new FormData();
1317 formData.append('returnHTML', 'NO');
1319 formData.append('crop', crop);
1321 if(typeof(file.filename) != 'undefined'){
1322 formData.append('filename', file.filename);
1325 if(typeof(file.mimetype) != 'undefined'){
1326 formData.append('mimetype', file.mimetype);
1329 if(this.fireEvent('arrange', this, formData) != false){
1330 this.xhr.send(formData);
1334 prepare : function(file)
1339 if(typeof(file) === 'string'){
1340 this.loadCanvas(file);
1344 if(!file || !this.urlAPI){
1349 this.cropType = file.type;
1353 if(this.fireEvent('prepare', this, this.file) != false){
1355 var reader = new FileReader();
1357 reader.onload = function (e) {
1358 if (e.target.error) {
1359 Roo.log(e.target.error);
1363 var buffer = e.target.result,
1364 dataView = new DataView(buffer),
1366 maxOffset = dataView.byteLength - 4,
1370 if (dataView.getUint16(0) === 0xffd8) {
1371 while (offset < maxOffset) {
1372 markerBytes = dataView.getUint16(offset);
1374 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1375 markerLength = dataView.getUint16(offset + 2) + 2;
1376 if (offset + markerLength > dataView.byteLength) {
1377 Roo.log('Invalid meta data: Invalid segment size.');
1381 if(markerBytes == 0xffe1){
1382 _this.parseExifData(
1389 offset += markerLength;
1399 var url = _this.urlAPI.createObjectURL(_this.file);
1401 _this.loadCanvas(url);
1406 reader.readAsArrayBuffer(this.file);
1412 parseExifData : function(dataView, offset, length)
1414 var tiffOffset = offset + 10,
1418 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1419 // No Exif data, might be XMP data instead
1423 // Check for the ASCII code for "Exif" (0x45786966):
1424 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1425 // No Exif data, might be XMP data instead
1428 if (tiffOffset + 8 > dataView.byteLength) {
1429 Roo.log('Invalid Exif data: Invalid segment size.');
1432 // Check for the two null bytes:
1433 if (dataView.getUint16(offset + 8) !== 0x0000) {
1434 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1437 // Check the byte alignment:
1438 switch (dataView.getUint16(tiffOffset)) {
1440 littleEndian = true;
1443 littleEndian = false;
1446 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1449 // Check for the TIFF tag marker (0x002A):
1450 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1451 Roo.log('Invalid Exif data: Missing TIFF marker.');
1454 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1455 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1460 tiffOffset + dirOffset,
1465 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1470 if (dirOffset + 6 > dataView.byteLength) {
1471 Roo.log('Invalid Exif data: Invalid directory offset.');
1474 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1475 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1476 if (dirEndOffset + 4 > dataView.byteLength) {
1477 Roo.log('Invalid Exif data: Invalid directory size.');
1480 for (i = 0; i < tagsNumber; i += 1) {
1484 dirOffset + 2 + 12 * i, // tag offset
1488 // Return the offset to the next directory:
1489 return dataView.getUint32(dirEndOffset, littleEndian);
1492 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1494 var tag = dataView.getUint16(offset, littleEndian);
1496 this.exif[tag] = this.getExifValue(
1500 dataView.getUint16(offset + 2, littleEndian), // tag type
1501 dataView.getUint32(offset + 4, littleEndian), // tag length
1506 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1508 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1517 Roo.log('Invalid Exif data: Invalid tag type.');
1521 tagSize = tagType.size * length;
1522 // Determine if the value is contained in the dataOffset bytes,
1523 // or if the value at the dataOffset is a pointer to the actual data:
1524 dataOffset = tagSize > 4 ?
1525 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1526 if (dataOffset + tagSize > dataView.byteLength) {
1527 Roo.log('Invalid Exif data: Invalid data offset.');
1531 return tagType.getValue(dataView, dataOffset, littleEndian);
1534 for (i = 0; i < length; i += 1) {
1535 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1538 if (tagType.ascii) {
1540 // Concatenate the chars:
1541 for (i = 0; i < values.length; i += 1) {
1543 // Ignore the terminating NULL byte(s):
1544 if (c === '\u0000') {
1556 Roo.apply(Roo.bootstrap.UploadCropbox, {
1558 'Orientation': 0x0112
1564 3: 180, //'bottom-right',
1565 // 4: 'bottom-left',
1567 6: 90, //'right-top',
1568 // 7: 'right-bottom',
1569 8: 270 //'left-bottom'
1573 // byte, 8-bit unsigned int:
1575 getValue: function (dataView, dataOffset) {
1576 return dataView.getUint8(dataOffset);
1580 // ascii, 8-bit byte:
1582 getValue: function (dataView, dataOffset) {
1583 return String.fromCharCode(dataView.getUint8(dataOffset));
1588 // short, 16 bit int:
1590 getValue: function (dataView, dataOffset, littleEndian) {
1591 return dataView.getUint16(dataOffset, littleEndian);
1595 // long, 32 bit int:
1597 getValue: function (dataView, dataOffset, littleEndian) {
1598 return dataView.getUint32(dataOffset, littleEndian);
1602 // rational = two long values, first is numerator, second is denominator:
1604 getValue: function (dataView, dataOffset, littleEndian) {
1605 return dataView.getUint32(dataOffset, littleEndian) /
1606 dataView.getUint32(dataOffset + 4, littleEndian);
1610 // slong, 32 bit signed int:
1612 getValue: function (dataView, dataOffset, littleEndian) {
1613 return dataView.getInt32(dataOffset, littleEndian);
1617 // srational, two slongs, first is numerator, second is denominator:
1619 getValue: function (dataView, dataOffset, littleEndian) {
1620 return dataView.getInt32(dataOffset, littleEndian) /
1621 dataView.getInt32(dataOffset + 4, littleEndian);
1631 cls : 'btn-group roo-upload-cropbox-rotate-left',
1632 action : 'rotate-left',
1636 cls : 'btn btn-default',
1637 html : '<i class="fa fa-undo"></i>'
1643 cls : 'btn-group roo-upload-cropbox-picture',
1648 cls : 'btn btn-default',
1649 html : '<i class="fa fa-picture-o"></i>'
1655 cls : 'btn-group roo-upload-cropbox-rotate-right',
1656 action : 'rotate-right',
1660 cls : 'btn btn-default',
1661 html : '<i class="fa fa-repeat"></i>'
1669 cls : 'btn-group roo-upload-cropbox-rotate-left',
1670 action : 'rotate-left',
1674 cls : 'btn btn-default',
1675 html : '<i class="fa fa-undo"></i>'
1681 cls : 'btn-group roo-upload-cropbox-download',
1682 action : 'download',
1686 cls : 'btn btn-default',
1687 html : '<i class="fa fa-download"></i>'
1693 cls : 'btn-group roo-upload-cropbox-crop',
1698 cls : 'btn btn-default',
1699 html : '<i class="fa fa-crop"></i>'
1705 cls : 'btn-group roo-upload-cropbox-trash',
1710 cls : 'btn btn-default',
1711 html : '<i class="fa fa-trash"></i>'
1717 cls : 'btn-group roo-upload-cropbox-rotate-right',
1718 action : 'rotate-right',
1722 cls : 'btn btn-default',
1723 html : '<i class="fa fa-repeat"></i>'
1731 cls : 'btn-group roo-upload-cropbox-rotate-left',
1732 action : 'rotate-left',
1736 cls : 'btn btn-default',
1737 html : '<i class="fa fa-undo"></i>'
1743 cls : 'btn-group roo-upload-cropbox-rotate-right',
1744 action : 'rotate-right',
1748 cls : 'btn btn-default',
1749 html : '<i class="fa fa-repeat"></i>'