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);
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){
345 this.cropType = this.selectorEl.dom.files[0].type;
353 this.fireEvent('trash', this);
356 download : function(e)
358 this.fireEvent('download', this);
361 loadCanvas : function(src)
363 if(this.fireEvent('beforeloadcanvas', this, src) != false){
367 this.imageEl = document.createElement('img');
371 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
373 this.imageEl.src = src;
377 onLoadCanvas : function()
379 this.bodyEl.un('click', this.beforeSelectFile, this);
381 this.notifyEl.hide();
383 this.footerEl.show();
385 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
386 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
388 this.baseRotateLevel();
391 this.setThumbBoxSize();
394 this.setThumbBoxPosition();
396 this.baseScaleLevel();
402 this.canvasLoaded = true;
406 setCanvasPosition : function()
412 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
413 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
415 this.previewEl.setLeft(pw);
416 this.previewEl.setTop(ph);
420 onMouseDown : function(e)
424 this.dragable = true;
425 this.pinching = false;
427 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
428 this.dragable = false;
432 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
433 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
437 onMouseMove : function(e)
441 if(!this.canvasLoaded){
449 var minX = Math.ceil(this.thumbEl.getLeft(true));
450 var minY = Math.ceil(this.thumbEl.getTop(true));
452 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
453 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
455 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
456 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
461 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
462 var bgY = Math.ceil(y + this.previewEl.getTop(true));
464 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
465 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
467 this.previewEl.setLeft(bgX);
468 this.previewEl.setTop(bgY);
470 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
471 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
474 onMouseUp : function(e)
478 this.dragable = false;
481 onMouseWheel : function(e)
485 this.startScale = this.scale;
487 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
489 if(!this.zoomable()){
490 this.scale = this.startScale;
499 zoomable : function()
501 var minScale = this.thumbEl.getWidth() / this.minWidth;
503 if(this.minWidth < this.minHeight){
504 minScale = this.thumbEl.getHeight() / this.minHeight;
507 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
508 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
512 (this.rotate == 0 || this.rotate == 180) &&
514 width > this.imageEl.OriginWidth ||
515 height > this.imageEl.OriginHeight ||
516 (width < this.minWidth && height < this.minHeight)
524 (this.rotate == 90 || this.rotate == 270) &&
526 width > this.imageEl.OriginWidth ||
527 height > this.imageEl.OriginHeight ||
528 (width < this.minHeight && height < this.minWidth)
536 (this.rotate == 0 || this.rotate == 180) &&
538 width < this.minWidth ||
539 width > this.imageEl.OriginWidth ||
540 height < this.minHeight ||
541 height > this.imageEl.OriginHeight
549 (this.rotate == 90 || this.rotate == 270) &&
551 width < this.minHeight ||
552 width > this.imageEl.OriginWidth ||
553 height < this.minWidth ||
554 height > this.imageEl.OriginHeight
564 onRotateLeft : function(e)
566 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
568 var minScale = this.thumbEl.getWidth() / this.minWidth;
570 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
571 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
573 this.startScale = this.scale;
575 while (this.getScaleLevel() < minScale){
577 this.scale = this.scale + 1;
579 if(!this.zoomable()){
584 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
585 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
590 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
597 this.scale = this.startScale;
604 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
607 this.setThumbBoxSize();
608 this.setThumbBoxPosition();
609 this.setCanvasPosition();
614 this.fireEvent('rotate', this, 'left');
618 onRotateRight : function(e)
620 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
622 var minScale = this.thumbEl.getWidth() / this.minWidth;
624 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
625 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
627 this.startScale = this.scale;
629 while (this.getScaleLevel() < minScale){
631 this.scale = this.scale + 1;
633 if(!this.zoomable()){
638 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
639 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
644 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
651 this.scale = this.startScale;
658 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
661 this.setThumbBoxSize();
662 this.setThumbBoxPosition();
663 this.setCanvasPosition();
668 this.fireEvent('rotate', this, 'right');
671 onRotateFail : function()
673 this.errorEl.show(true);
677 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
682 this.previewEl.dom.innerHTML = '';
684 var canvasEl = document.createElement("canvas");
686 var contextEl = canvasEl.getContext("2d");
688 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
689 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
690 var center = this.imageEl.OriginWidth / 2;
692 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
693 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
694 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
695 center = this.imageEl.OriginHeight / 2;
698 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
700 contextEl.translate(center, center);
701 contextEl.rotate(this.rotate * Math.PI / 180);
703 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
705 this.canvasEl = document.createElement("canvas");
707 this.contextEl = this.canvasEl.getContext("2d");
709 switch (this.rotate) {
712 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
713 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
715 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
720 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
721 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
723 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
724 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);
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.OriginWidth * this.getScaleLevel();
734 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
736 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
737 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);
741 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);
746 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
747 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
749 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
750 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
754 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);
761 this.previewEl.appendChild(this.canvasEl);
763 this.setCanvasPosition();
768 if(!this.canvasLoaded){
772 var imageCanvas = document.createElement("canvas");
774 var imageContext = imageCanvas.getContext("2d");
776 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
777 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
779 var center = imageCanvas.width / 2;
781 imageContext.translate(center, center);
783 imageContext.rotate(this.rotate * Math.PI / 180);
785 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
787 var canvas = document.createElement("canvas");
789 var context = canvas.getContext("2d");
791 canvas.width = this.minWidth;
792 canvas.height = this.minHeight;
794 switch (this.rotate) {
797 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
798 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
800 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
801 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
803 var targetWidth = this.minWidth - 2 * x;
804 var targetHeight = this.minHeight - 2 * y;
808 if((x == 0 && y == 0) || (x == 0 && y > 0)){
809 scale = targetWidth / width;
813 scale = targetHeight / height;
817 scale = targetWidth / width;
820 scale = targetHeight / height;
824 context.scale(scale, scale);
826 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
827 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
829 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
830 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
832 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
837 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
838 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
840 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
841 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
843 var targetWidth = this.minWidth - 2 * x;
844 var targetHeight = this.minHeight - 2 * y;
848 if((x == 0 && y == 0) || (x == 0 && y > 0)){
849 scale = targetWidth / width;
853 scale = targetHeight / height;
857 scale = targetWidth / width;
860 scale = targetHeight / height;
864 context.scale(scale, scale);
866 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
867 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
869 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
870 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
872 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
874 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
879 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
880 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
882 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
883 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
885 var targetWidth = this.minWidth - 2 * x;
886 var targetHeight = this.minHeight - 2 * y;
890 if((x == 0 && y == 0) || (x == 0 && y > 0)){
891 scale = targetWidth / width;
895 scale = targetHeight / height;
899 scale = targetWidth / width;
902 scale = targetHeight / height;
906 context.scale(scale, scale);
908 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
909 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
911 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
912 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
914 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
915 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
917 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
922 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
923 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
925 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
926 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
928 var targetWidth = this.minWidth - 2 * x;
929 var targetHeight = this.minHeight - 2 * y;
933 if((x == 0 && y == 0) || (x == 0 && y > 0)){
934 scale = targetWidth / width;
938 scale = targetHeight / height;
942 scale = targetWidth / width;
945 scale = targetHeight / height;
949 context.scale(scale, scale);
951 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
952 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
954 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
955 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
957 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
959 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
966 this.cropData = canvas.toDataURL(this.cropType);
968 this.fireEvent('crop', this, this.cropData);
972 setThumbBoxSize : function()
976 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
977 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
978 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
980 this.minWidth = width;
981 this.minHeight = height;
983 if(this.rotate == 90 || this.rotate == 270){
984 this.minWidth = height;
985 this.minHeight = width;
990 width = Math.ceil(this.minWidth * height / this.minHeight);
992 if(this.minWidth > this.minHeight){
994 height = Math.ceil(this.minHeight * width / this.minWidth);
997 this.thumbEl.setStyle({
998 width : width + 'px',
999 height : height + 'px'
1006 setThumbBoxPosition : function()
1008 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1009 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1011 this.thumbEl.setLeft(x);
1012 this.thumbEl.setTop(y);
1016 baseRotateLevel : function()
1018 this.baseRotate = 1;
1021 typeof(this.exif) != 'undefined' &&
1022 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1023 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1025 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1028 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1032 baseScaleLevel : function()
1036 if(this.isDocument){
1038 if(this.baseRotate == 6 || this.baseRotate == 8){
1040 height = this.thumbEl.getHeight();
1041 this.baseScale = height / this.imageEl.OriginWidth;
1043 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1044 width = this.thumbEl.getWidth();
1045 this.baseScale = width / this.imageEl.OriginHeight;
1051 height = this.thumbEl.getHeight();
1052 this.baseScale = height / this.imageEl.OriginHeight;
1054 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1055 width = this.thumbEl.getWidth();
1056 this.baseScale = width / this.imageEl.OriginWidth;
1062 if(this.baseRotate == 6 || this.baseRotate == 8){
1064 width = this.thumbEl.getHeight();
1065 this.baseScale = width / this.imageEl.OriginHeight;
1067 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1068 height = this.thumbEl.getWidth();
1069 this.baseScale = height / this.imageEl.OriginHeight;
1072 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1073 height = this.thumbEl.getWidth();
1074 this.baseScale = height / this.imageEl.OriginHeight;
1076 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1077 width = this.thumbEl.getHeight();
1078 this.baseScale = width / this.imageEl.OriginWidth;
1085 width = this.thumbEl.getWidth();
1086 this.baseScale = width / this.imageEl.OriginWidth;
1088 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1089 height = this.thumbEl.getHeight();
1090 this.baseScale = height / this.imageEl.OriginHeight;
1093 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1095 height = this.thumbEl.getHeight();
1096 this.baseScale = height / this.imageEl.OriginHeight;
1098 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1099 width = this.thumbEl.getWidth();
1100 this.baseScale = width / this.imageEl.OriginWidth;
1108 getScaleLevel : function()
1110 return this.baseScale * Math.pow(1.1, this.scale);
1113 onTouchStart : function(e)
1115 if(!this.canvasLoaded){
1116 this.beforeSelectFile(e);
1120 var touches = e.browserEvent.touches;
1126 if(touches.length == 1){
1127 this.onMouseDown(e);
1131 if(touches.length != 2){
1137 for(var i = 0, finger; finger = touches[i]; i++){
1138 coords.push(finger.pageX, finger.pageY);
1141 var x = Math.pow(coords[0] - coords[2], 2);
1142 var y = Math.pow(coords[1] - coords[3], 2);
1144 this.startDistance = Math.sqrt(x + y);
1146 this.startScale = this.scale;
1148 this.pinching = true;
1149 this.dragable = false;
1153 onTouchMove : function(e)
1155 if(!this.pinching && !this.dragable){
1159 var touches = e.browserEvent.touches;
1166 this.onMouseMove(e);
1172 for(var i = 0, finger; finger = touches[i]; i++){
1173 coords.push(finger.pageX, finger.pageY);
1176 var x = Math.pow(coords[0] - coords[2], 2);
1177 var y = Math.pow(coords[1] - coords[3], 2);
1179 this.endDistance = Math.sqrt(x + y);
1181 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1183 if(!this.zoomable()){
1184 this.scale = this.startScale;
1192 onTouchEnd : function(e)
1194 this.pinching = false;
1195 this.dragable = false;
1199 process : function(file, crop)
1201 this.xhr = new XMLHttpRequest();
1203 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
1207 file.xhr = this.xhr;
1209 this.xhr.open(this.method, this.url, true);
1212 "Accept": "application/json",
1213 "Cache-Control": "no-cache",
1214 "X-Requested-With": "XMLHttpRequest"
1217 for (var headerName in headers) {
1218 var headerValue = headers[headerName];
1220 this.xhr.setRequestHeader(headerName, headerValue);
1226 this.xhr.onload = function()
1228 _this.xhrOnLoad(_this.xhr);
1231 this.xhr.onerror = function()
1233 _this.xhrOnError(_this.xhr);
1236 var formData = new FormData();
1238 formData.append('returnHTML', 'NO');
1241 formData.append('crop', crop);
1244 formData.append(this.paramName, file, file.name);
1246 if(this.fireEvent('prepare', this, formData) != false){
1247 this.xhr.send(formData);
1251 xhrOnLoad : function(xhr)
1253 if (xhr.readyState !== 4) {
1254 this.fireEvent('exception', this, xhr);
1258 var response = Roo.decode(xhr.responseText);
1260 if(!response.success){
1261 this.fireEvent('exception', this, xhr);
1265 var response = Roo.decode(xhr.responseText);
1267 this.fireEvent('upload', this, response);
1271 xhrOnError : function()
1273 Roo.log('xhr on error');
1275 var response = Roo.decode(xhr.responseText);
1281 prepare : function(file)
1286 if(typeof(file) === 'string'){
1287 this.loadCanvas(file);
1291 if(!file || !this.urlAPI){
1296 this.cropType = file.type;
1300 if(this.fireEvent('prepare', this, this.file) != false){
1302 var reader = new FileReader();
1304 reader.onload = function (e) {
1305 if (e.target.error) {
1306 Roo.log(e.target.error);
1310 var buffer = e.target.result,
1311 dataView = new DataView(buffer),
1313 maxOffset = dataView.byteLength - 4,
1317 if (dataView.getUint16(0) === 0xffd8) {
1318 while (offset < maxOffset) {
1319 markerBytes = dataView.getUint16(offset);
1321 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1322 markerLength = dataView.getUint16(offset + 2) + 2;
1323 if (offset + markerLength > dataView.byteLength) {
1324 Roo.log('Invalid meta data: Invalid segment size.');
1328 if(markerBytes == 0xffe1){
1329 _this.parseExifData(
1336 offset += markerLength;
1346 var url = _this.urlAPI.createObjectURL(_this.file);
1348 _this.loadCanvas(url);
1353 reader.readAsArrayBuffer(this.file);
1359 parseExifData : function(dataView, offset, length)
1361 var tiffOffset = offset + 10,
1365 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1366 // No Exif data, might be XMP data instead
1370 // Check for the ASCII code for "Exif" (0x45786966):
1371 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1372 // No Exif data, might be XMP data instead
1375 if (tiffOffset + 8 > dataView.byteLength) {
1376 Roo.log('Invalid Exif data: Invalid segment size.');
1379 // Check for the two null bytes:
1380 if (dataView.getUint16(offset + 8) !== 0x0000) {
1381 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1384 // Check the byte alignment:
1385 switch (dataView.getUint16(tiffOffset)) {
1387 littleEndian = true;
1390 littleEndian = false;
1393 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1396 // Check for the TIFF tag marker (0x002A):
1397 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1398 Roo.log('Invalid Exif data: Missing TIFF marker.');
1401 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1402 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1407 tiffOffset + dirOffset,
1412 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1417 if (dirOffset + 6 > dataView.byteLength) {
1418 Roo.log('Invalid Exif data: Invalid directory offset.');
1421 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1422 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1423 if (dirEndOffset + 4 > dataView.byteLength) {
1424 Roo.log('Invalid Exif data: Invalid directory size.');
1427 for (i = 0; i < tagsNumber; i += 1) {
1431 dirOffset + 2 + 12 * i, // tag offset
1435 // Return the offset to the next directory:
1436 return dataView.getUint32(dirEndOffset, littleEndian);
1439 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1441 var tag = dataView.getUint16(offset, littleEndian);
1443 this.exif[tag] = this.getExifValue(
1447 dataView.getUint16(offset + 2, littleEndian), // tag type
1448 dataView.getUint32(offset + 4, littleEndian), // tag length
1453 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1455 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1464 Roo.log('Invalid Exif data: Invalid tag type.');
1468 tagSize = tagType.size * length;
1469 // Determine if the value is contained in the dataOffset bytes,
1470 // or if the value at the dataOffset is a pointer to the actual data:
1471 dataOffset = tagSize > 4 ?
1472 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1473 if (dataOffset + tagSize > dataView.byteLength) {
1474 Roo.log('Invalid Exif data: Invalid data offset.');
1478 return tagType.getValue(dataView, dataOffset, littleEndian);
1481 for (i = 0; i < length; i += 1) {
1482 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1485 if (tagType.ascii) {
1487 // Concatenate the chars:
1488 for (i = 0; i < values.length; i += 1) {
1490 // Ignore the terminating NULL byte(s):
1491 if (c === '\u0000') {
1503 Roo.apply(Roo.bootstrap.UploadCropbox, {
1505 'Orientation': 0x0112
1511 3: 180, //'bottom-right',
1512 // 4: 'bottom-left',
1514 6: 90, //'right-top',
1515 // 7: 'right-bottom',
1516 8: 270 //'left-bottom'
1520 // byte, 8-bit unsigned int:
1522 getValue: function (dataView, dataOffset) {
1523 return dataView.getUint8(dataOffset);
1527 // ascii, 8-bit byte:
1529 getValue: function (dataView, dataOffset) {
1530 return String.fromCharCode(dataView.getUint8(dataOffset));
1535 // short, 16 bit int:
1537 getValue: function (dataView, dataOffset, littleEndian) {
1538 return dataView.getUint16(dataOffset, littleEndian);
1542 // long, 32 bit int:
1544 getValue: function (dataView, dataOffset, littleEndian) {
1545 return dataView.getUint32(dataOffset, littleEndian);
1549 // rational = two long values, first is numerator, second is denominator:
1551 getValue: function (dataView, dataOffset, littleEndian) {
1552 return dataView.getUint32(dataOffset, littleEndian) /
1553 dataView.getUint32(dataOffset + 4, littleEndian);
1557 // slong, 32 bit signed int:
1559 getValue: function (dataView, dataOffset, littleEndian) {
1560 return dataView.getInt32(dataOffset, littleEndian);
1564 // srational, two slongs, first is numerator, second is denominator:
1566 getValue: function (dataView, dataOffset, littleEndian) {
1567 return dataView.getInt32(dataOffset, littleEndian) /
1568 dataView.getInt32(dataOffset + 4, littleEndian);
1578 cls : 'btn-group roo-upload-cropbox-rotate-left',
1579 action : 'rotate-left',
1583 cls : 'btn btn-default',
1584 html : '<i class="fa fa-undo"></i>'
1590 cls : 'btn-group roo-upload-cropbox-picture',
1595 cls : 'btn btn-default',
1596 html : '<i class="fa fa-picture-o"></i>'
1602 cls : 'btn-group roo-upload-cropbox-rotate-right',
1603 action : 'rotate-right',
1607 cls : 'btn btn-default',
1608 html : '<i class="fa fa-repeat"></i>'
1616 cls : 'btn-group roo-upload-cropbox-rotate-left',
1617 action : 'rotate-left',
1621 cls : 'btn btn-default',
1622 html : '<i class="fa fa-undo"></i>'
1628 cls : 'btn-group roo-upload-cropbox-download',
1629 action : 'download',
1633 cls : 'btn btn-default',
1634 html : '<i class="fa fa-download"></i>'
1640 cls : 'btn-group roo-upload-cropbox-crop',
1645 cls : 'btn btn-default',
1646 html : '<i class="fa fa-crop"></i>'
1652 cls : 'btn-group roo-upload-cropbox-trash',
1657 cls : 'btn btn-default',
1658 html : '<i class="fa fa-trash"></i>'
1664 cls : 'btn-group roo-upload-cropbox-rotate-right',
1665 action : 'rotate-right',
1669 cls : 'btn btn-default',
1670 html : '<i class="fa fa-repeat"></i>'
1678 cls : 'btn-group roo-upload-cropbox-rotate-left',
1679 action : 'rotate-left',
1683 cls : 'btn btn-default',
1684 html : '<i class="fa fa-undo"></i>'
1690 cls : 'btn-group roo-upload-cropbox-rotate-right',
1691 action : 'rotate-right',
1695 cls : 'btn btn-default',
1696 html : '<i class="fa fa-repeat"></i>'