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-preview'
159 cls : 'roo-upload-cropbox-thumb'
163 cls : 'roo-upload-cropbox-empty-notify',
164 html : this.emptyText
168 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
169 html : this.rotateNotify
175 cls : 'roo-upload-cropbox-footer',
178 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
188 onRender : function(ct, position)
190 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
192 if (this.buttons.length) {
194 Roo.each(this.buttons, function(bb) {
196 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
198 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
204 initEvents : function()
206 this.urlAPI = (window.createObjectURL && window) ||
207 (window.URL && URL.revokeObjectURL && URL) ||
208 (window.webkitURL && webkitURL);
210 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
211 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
213 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
214 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
216 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
217 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
220 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
221 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
223 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
224 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
227 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
228 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
229 this.footerEl.hide();
231 this.setThumbBoxSize();
237 this.fireEvent('initial', this);
244 window.addEventListener("resize", function() { _this.resize(); } );
246 this.bodyEl.on('click', this.beforeSelectFile, this);
249 this.bodyEl.on('touchstart', this.onTouchStart, this);
250 this.bodyEl.on('touchmove', this.onTouchMove, this);
251 this.bodyEl.on('touchend', this.onTouchEnd, this);
255 this.bodyEl.on('mousedown', this.onMouseDown, this);
256 this.bodyEl.on('mousemove', this.onMouseMove, this);
257 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
258 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
259 Roo.get(document).on('mouseup', this.onMouseUp, this);
269 this.dragable = false;
270 this.pinching = false;
273 this.cropData = false;
274 this.notifyEl.dom.innerHTML = this.emptyText;
280 if(this.fireEvent('resize', this) != false){
281 this.setThumbBoxPosition();
282 this.setCanvasPosition();
286 onFooterButtonClick : function(e, el, o, type)
290 this.onRotateLeft(e);
292 case 'rotate-right' :
293 this.onRotateRight(e);
296 this.beforeSelectFile(e);
311 this.fireEvent('footerbuttonclick', this, type);
314 beforeSelectFile : function(e)
316 this.fireEvent('beforeselectfile', this);
321 this.fireEvent('trash', this);
324 download : function(e)
326 this.fireEvent('download', this);
329 loadCanvas : function(src)
331 if(this.fireEvent('beforeloadcanvas', this, src) != false){
335 this.imageEl = document.createElement('img');
339 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
341 this.imageEl.src = src;
345 onLoadCanvas : function()
347 this.bodyEl.un('click', this.beforeSelectFile, this);
349 this.notifyEl.hide();
351 this.footerEl.show();
353 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
354 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
356 this.baseRotateLevel();
359 this.setThumbBoxSize();
362 this.setThumbBoxPosition();
364 this.baseScaleLevel();
370 this.canvasLoaded = true;
374 setCanvasPosition : function()
380 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
381 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
383 this.previewEl.setLeft(pw);
384 this.previewEl.setTop(ph);
388 onMouseDown : function(e)
392 this.dragable = true;
393 this.pinching = false;
395 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
396 this.dragable = false;
400 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
401 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
405 onMouseMove : function(e)
409 if(!this.canvasLoaded){
417 var minX = Math.ceil(this.thumbEl.getLeft(true));
418 var minY = Math.ceil(this.thumbEl.getTop(true));
420 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
421 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
423 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
424 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
429 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
430 var bgY = Math.ceil(y + this.previewEl.getTop(true));
432 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
433 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
435 this.previewEl.setLeft(bgX);
436 this.previewEl.setTop(bgY);
438 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
439 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
442 onMouseUp : function(e)
446 this.dragable = false;
449 onMouseWheel : function(e)
453 this.startScale = this.scale;
455 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
457 if(!this.zoomable()){
458 this.scale = this.startScale;
467 zoomable : function()
469 var minScale = this.thumbEl.getWidth() / this.minWidth;
471 if(this.minWidth < this.minHeight){
472 minScale = this.thumbEl.getHeight() / this.minHeight;
475 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
476 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
480 (this.rotate == 0 || this.rotate == 180) &&
482 width > this.imageEl.OriginWidth ||
483 height > this.imageEl.OriginHeight ||
484 (width < this.minWidth && height < this.minHeight)
492 (this.rotate == 90 || this.rotate == 270) &&
494 width > this.imageEl.OriginWidth ||
495 height > this.imageEl.OriginHeight ||
496 (width < this.minHeight && height < this.minWidth)
504 (this.rotate == 0 || this.rotate == 180) &&
506 width < this.minWidth ||
507 width > this.imageEl.OriginWidth ||
508 height < this.minHeight ||
509 height > this.imageEl.OriginHeight
517 (this.rotate == 90 || this.rotate == 270) &&
519 width < this.minHeight ||
520 width > this.imageEl.OriginWidth ||
521 height < this.minWidth ||
522 height > this.imageEl.OriginHeight
532 onRotateLeft : function(e)
534 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
536 var minScale = this.thumbEl.getWidth() / this.minWidth;
538 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
539 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
541 this.startScale = this.scale;
543 while (this.getScaleLevel() < minScale){
545 this.scale = this.scale + 1;
547 if(!this.zoomable()){
552 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
553 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
558 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
565 this.scale = this.startScale;
572 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
575 this.setThumbBoxSize();
576 this.setThumbBoxPosition();
577 this.setCanvasPosition();
582 this.fireEvent('rotate', this, 'left');
586 onRotateRight : function(e)
588 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
590 var minScale = this.thumbEl.getWidth() / this.minWidth;
592 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
593 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
595 this.startScale = this.scale;
597 while (this.getScaleLevel() < minScale){
599 this.scale = this.scale + 1;
601 if(!this.zoomable()){
606 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
607 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
612 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
619 this.scale = this.startScale;
626 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
629 this.setThumbBoxSize();
630 this.setThumbBoxPosition();
631 this.setCanvasPosition();
636 this.fireEvent('rotate', this, 'right');
639 onRotateFail : function()
641 this.errorEl.show(true);
645 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
650 this.previewEl.dom.innerHTML = '';
652 var canvasEl = document.createElement("canvas");
654 var contextEl = canvasEl.getContext("2d");
656 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
657 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
658 var center = this.imageEl.OriginWidth / 2;
660 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
661 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
662 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
663 center = this.imageEl.OriginHeight / 2;
666 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
668 contextEl.translate(center, center);
669 contextEl.rotate(this.rotate * Math.PI / 180);
671 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
673 this.canvasEl = document.createElement("canvas");
675 this.contextEl = this.canvasEl.getContext("2d");
677 switch (this.rotate) {
680 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
681 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
683 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
688 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
689 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
691 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
692 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);
696 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
701 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
702 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
704 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
705 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);
709 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);
714 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
715 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
717 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
718 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
722 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);
729 this.previewEl.appendChild(this.canvasEl);
731 this.setCanvasPosition();
736 if(!this.canvasLoaded){
740 var imageCanvas = document.createElement("canvas");
742 var imageContext = imageCanvas.getContext("2d");
744 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
745 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
747 var center = imageCanvas.width / 2;
749 imageContext.translate(center, center);
751 imageContext.rotate(this.rotate * Math.PI / 180);
753 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
755 var canvas = document.createElement("canvas");
757 var context = canvas.getContext("2d");
759 canvas.width = this.minWidth;
760 canvas.height = this.minHeight;
762 switch (this.rotate) {
765 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
766 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
768 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
769 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
771 var targetWidth = this.minWidth - 2 * x;
772 var targetHeight = this.minHeight - 2 * y;
776 if((x == 0 && y == 0) || (x == 0 && y > 0)){
777 scale = targetWidth / width;
781 scale = targetHeight / height;
785 scale = targetWidth / width;
788 scale = targetHeight / height;
792 context.scale(scale, scale);
794 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
795 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
797 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
798 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
800 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
805 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
806 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
808 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
809 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
811 var targetWidth = this.minWidth - 2 * x;
812 var targetHeight = this.minHeight - 2 * y;
816 if((x == 0 && y == 0) || (x == 0 && y > 0)){
817 scale = targetWidth / width;
821 scale = targetHeight / height;
825 scale = targetWidth / width;
828 scale = targetHeight / height;
832 context.scale(scale, scale);
834 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
835 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
837 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
838 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
840 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
842 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
847 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
848 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
850 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
851 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
853 var targetWidth = this.minWidth - 2 * x;
854 var targetHeight = this.minHeight - 2 * y;
858 if((x == 0 && y == 0) || (x == 0 && y > 0)){
859 scale = targetWidth / width;
863 scale = targetHeight / height;
867 scale = targetWidth / width;
870 scale = targetHeight / height;
874 context.scale(scale, scale);
876 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
877 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
879 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
880 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
882 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
883 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
885 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
890 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
891 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
893 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
894 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
896 var targetWidth = this.minWidth - 2 * x;
897 var targetHeight = this.minHeight - 2 * y;
901 if((x == 0 && y == 0) || (x == 0 && y > 0)){
902 scale = targetWidth / width;
906 scale = targetHeight / height;
910 scale = targetWidth / width;
913 scale = targetHeight / height;
917 context.scale(scale, scale);
919 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
920 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
922 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
923 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
925 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
927 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
934 this.cropData = canvas.toDataURL(this.cropType);
936 this.fireEvent('crop', this, this.cropData);
940 setThumbBoxSize : function()
944 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
945 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
946 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
948 this.minWidth = width;
949 this.minHeight = height;
951 if(this.rotate == 90 || this.rotate == 270){
952 this.minWidth = height;
953 this.minHeight = width;
958 width = Math.ceil(this.minWidth * height / this.minHeight);
960 if(this.minWidth > this.minHeight){
962 height = Math.ceil(this.minHeight * width / this.minWidth);
965 this.thumbEl.setStyle({
966 width : width + 'px',
967 height : height + 'px'
974 setThumbBoxPosition : function()
976 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
977 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
979 this.thumbEl.setLeft(x);
980 this.thumbEl.setTop(y);
984 baseRotateLevel : function()
989 typeof(this.exif) != 'undefined' &&
990 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
991 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
993 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
996 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1000 baseScaleLevel : function()
1004 if(this.isDocument){
1006 if(this.baseRotate == 6 || this.baseRotate == 8){
1008 height = this.thumbEl.getHeight();
1009 this.baseScale = height / this.imageEl.OriginWidth;
1011 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1012 width = this.thumbEl.getWidth();
1013 this.baseScale = width / this.imageEl.OriginHeight;
1019 height = this.thumbEl.getHeight();
1020 this.baseScale = height / this.imageEl.OriginHeight;
1022 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1023 width = this.thumbEl.getWidth();
1024 this.baseScale = width / this.imageEl.OriginWidth;
1030 if(this.baseRotate == 6 || this.baseRotate == 8){
1032 width = this.thumbEl.getHeight();
1033 this.baseScale = width / this.imageEl.OriginHeight;
1035 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1036 height = this.thumbEl.getWidth();
1037 this.baseScale = height / this.imageEl.OriginHeight;
1040 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1041 height = this.thumbEl.getWidth();
1042 this.baseScale = height / this.imageEl.OriginHeight;
1044 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1045 width = this.thumbEl.getHeight();
1046 this.baseScale = width / this.imageEl.OriginWidth;
1053 width = this.thumbEl.getWidth();
1054 this.baseScale = width / this.imageEl.OriginWidth;
1056 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1057 height = this.thumbEl.getHeight();
1058 this.baseScale = height / this.imageEl.OriginHeight;
1061 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1063 height = this.thumbEl.getHeight();
1064 this.baseScale = height / this.imageEl.OriginHeight;
1066 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1067 width = this.thumbEl.getWidth();
1068 this.baseScale = width / this.imageEl.OriginWidth;
1076 getScaleLevel : function()
1078 return this.baseScale * Math.pow(1.1, this.scale);
1081 onTouchStart : function(e)
1083 if(!this.canvasLoaded){
1084 this.beforeSelectFile(e);
1088 var touches = e.browserEvent.touches;
1094 if(touches.length == 1){
1095 this.onMouseDown(e);
1099 if(touches.length != 2){
1105 for(var i = 0, finger; finger = touches[i]; i++){
1106 coords.push(finger.pageX, finger.pageY);
1109 var x = Math.pow(coords[0] - coords[2], 2);
1110 var y = Math.pow(coords[1] - coords[3], 2);
1112 this.startDistance = Math.sqrt(x + y);
1114 this.startScale = this.scale;
1116 this.pinching = true;
1117 this.dragable = false;
1121 onTouchMove : function(e)
1123 if(!this.pinching && !this.dragable){
1127 var touches = e.browserEvent.touches;
1134 this.onMouseMove(e);
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.endDistance = Math.sqrt(x + y);
1149 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1151 if(!this.zoomable()){
1152 this.scale = this.startScale;
1160 onTouchEnd : function(e)
1162 this.pinching = false;
1163 this.dragable = false;
1167 process : function(file, crop)
1169 this.xhr = new XMLHttpRequest();
1171 if(typeof(file.id) != 'undefined' && file.id * 1 > 0){
1175 file.xhr = this.xhr;
1177 this.xhr.open(this.method, this.url, true);
1180 "Accept": "application/json",
1181 "Cache-Control": "no-cache",
1182 "X-Requested-With": "XMLHttpRequest"
1185 for (var headerName in headers) {
1186 var headerValue = headers[headerName];
1188 this.xhr.setRequestHeader(headerName, headerValue);
1194 this.xhr.onload = function()
1196 _this.xhrOnLoad(_this.xhr);
1199 this.xhr.onerror = function()
1201 _this.xhrOnError(_this.xhr);
1204 var formData = new FormData();
1206 formData.append('returnHTML', 'NO');
1209 formData.append('crop', crop);
1212 formData.append(this.paramName, file, file.name);
1214 if(this.fireEvent('prepare', this, formData) != false){
1215 this.xhr.send(formData);
1219 xhrOnLoad : function(xhr)
1221 if (xhr.readyState !== 4) {
1222 this.fireEvent('exception', this, xhr);
1226 var response = Roo.decode(xhr.responseText);
1228 if(!response.success){
1229 this.fireEvent('exception', this, xhr);
1233 var response = Roo.decode(xhr.responseText);
1235 this.fireEvent('upload', this, response);
1239 xhrOnError : function()
1241 Roo.log('xhr on error');
1243 var response = Roo.decode(xhr.responseText);
1249 prepare : function(file)
1254 if(typeof(file) === 'string'){
1255 this.loadCanvas(file);
1259 if(!file || !this.urlAPI){
1264 this.cropType = file.type;
1268 if(this.fireEvent('prepare', this, this.file) != false){
1270 var reader = new FileReader();
1272 reader.onload = function (e) {
1273 if (e.target.error) {
1274 Roo.log(e.target.error);
1278 var buffer = e.target.result,
1279 dataView = new DataView(buffer),
1281 maxOffset = dataView.byteLength - 4,
1285 if (dataView.getUint16(0) === 0xffd8) {
1286 while (offset < maxOffset) {
1287 markerBytes = dataView.getUint16(offset);
1289 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1290 markerLength = dataView.getUint16(offset + 2) + 2;
1291 if (offset + markerLength > dataView.byteLength) {
1292 Roo.log('Invalid meta data: Invalid segment size.');
1296 if(markerBytes == 0xffe1){
1297 _this.parseExifData(
1304 offset += markerLength;
1314 var url = _this.urlAPI.createObjectURL(_this.file);
1316 _this.loadCanvas(url);
1321 reader.readAsArrayBuffer(this.file);
1327 parseExifData : function(dataView, offset, length)
1329 var tiffOffset = offset + 10,
1333 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1334 // No Exif data, might be XMP data instead
1338 // Check for the ASCII code for "Exif" (0x45786966):
1339 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1340 // No Exif data, might be XMP data instead
1343 if (tiffOffset + 8 > dataView.byteLength) {
1344 Roo.log('Invalid Exif data: Invalid segment size.');
1347 // Check for the two null bytes:
1348 if (dataView.getUint16(offset + 8) !== 0x0000) {
1349 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1352 // Check the byte alignment:
1353 switch (dataView.getUint16(tiffOffset)) {
1355 littleEndian = true;
1358 littleEndian = false;
1361 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1364 // Check for the TIFF tag marker (0x002A):
1365 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1366 Roo.log('Invalid Exif data: Missing TIFF marker.');
1369 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1370 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1375 tiffOffset + dirOffset,
1380 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1385 if (dirOffset + 6 > dataView.byteLength) {
1386 Roo.log('Invalid Exif data: Invalid directory offset.');
1389 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1390 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1391 if (dirEndOffset + 4 > dataView.byteLength) {
1392 Roo.log('Invalid Exif data: Invalid directory size.');
1395 for (i = 0; i < tagsNumber; i += 1) {
1399 dirOffset + 2 + 12 * i, // tag offset
1403 // Return the offset to the next directory:
1404 return dataView.getUint32(dirEndOffset, littleEndian);
1407 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1409 var tag = dataView.getUint16(offset, littleEndian);
1411 this.exif[tag] = this.getExifValue(
1415 dataView.getUint16(offset + 2, littleEndian), // tag type
1416 dataView.getUint32(offset + 4, littleEndian), // tag length
1421 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1423 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1432 Roo.log('Invalid Exif data: Invalid tag type.');
1436 tagSize = tagType.size * length;
1437 // Determine if the value is contained in the dataOffset bytes,
1438 // or if the value at the dataOffset is a pointer to the actual data:
1439 dataOffset = tagSize > 4 ?
1440 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1441 if (dataOffset + tagSize > dataView.byteLength) {
1442 Roo.log('Invalid Exif data: Invalid data offset.');
1446 return tagType.getValue(dataView, dataOffset, littleEndian);
1449 for (i = 0; i < length; i += 1) {
1450 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1453 if (tagType.ascii) {
1455 // Concatenate the chars:
1456 for (i = 0; i < values.length; i += 1) {
1458 // Ignore the terminating NULL byte(s):
1459 if (c === '\u0000') {
1471 Roo.apply(Roo.bootstrap.UploadCropbox, {
1473 'Orientation': 0x0112
1479 3: 180, //'bottom-right',
1480 // 4: 'bottom-left',
1482 6: 90, //'right-top',
1483 // 7: 'right-bottom',
1484 8: 270 //'left-bottom'
1488 // byte, 8-bit unsigned int:
1490 getValue: function (dataView, dataOffset) {
1491 return dataView.getUint8(dataOffset);
1495 // ascii, 8-bit byte:
1497 getValue: function (dataView, dataOffset) {
1498 return String.fromCharCode(dataView.getUint8(dataOffset));
1503 // short, 16 bit int:
1505 getValue: function (dataView, dataOffset, littleEndian) {
1506 return dataView.getUint16(dataOffset, littleEndian);
1510 // long, 32 bit int:
1512 getValue: function (dataView, dataOffset, littleEndian) {
1513 return dataView.getUint32(dataOffset, littleEndian);
1517 // rational = two long values, first is numerator, second is denominator:
1519 getValue: function (dataView, dataOffset, littleEndian) {
1520 return dataView.getUint32(dataOffset, littleEndian) /
1521 dataView.getUint32(dataOffset + 4, littleEndian);
1525 // slong, 32 bit signed int:
1527 getValue: function (dataView, dataOffset, littleEndian) {
1528 return dataView.getInt32(dataOffset, littleEndian);
1532 // srational, two slongs, first is numerator, second is denominator:
1534 getValue: function (dataView, dataOffset, littleEndian) {
1535 return dataView.getInt32(dataOffset, littleEndian) /
1536 dataView.getInt32(dataOffset + 4, littleEndian);
1546 cls : 'btn-group roo-upload-cropbox-rotate-left',
1547 action : 'rotate-left',
1551 cls : 'btn btn-default',
1552 html : '<i class="fa fa-undo"></i>'
1558 cls : 'btn-group roo-upload-cropbox-picture',
1563 cls : 'btn btn-default',
1564 html : '<i class="fa fa-picture-o"></i>'
1570 cls : 'btn-group roo-upload-cropbox-rotate-right',
1571 action : 'rotate-right',
1575 cls : 'btn btn-default',
1576 html : '<i class="fa fa-repeat"></i>'
1584 cls : 'btn-group roo-upload-cropbox-rotate-left',
1585 action : 'rotate-left',
1589 cls : 'btn btn-default',
1590 html : '<i class="fa fa-undo"></i>'
1596 cls : 'btn-group roo-upload-cropbox-download',
1597 action : 'download',
1601 cls : 'btn btn-default',
1602 html : '<i class="fa fa-download"></i>'
1608 cls : 'btn-group roo-upload-cropbox-crop',
1613 cls : 'btn btn-default',
1614 html : '<i class="fa fa-crop"></i>'
1620 cls : 'btn-group roo-upload-cropbox-trash',
1625 cls : 'btn btn-default',
1626 html : '<i class="fa fa-trash"></i>'
1632 cls : 'btn-group roo-upload-cropbox-rotate-right',
1633 action : 'rotate-right',
1637 cls : 'btn btn-default',
1638 html : '<i class="fa fa-repeat"></i>'
1646 cls : 'btn-group roo-upload-cropbox-rotate-left',
1647 action : 'rotate-left',
1651 cls : 'btn btn-default',
1652 html : '<i class="fa fa-undo"></i>'
1658 cls : 'btn-group roo-upload-cropbox-rotate-right',
1659 action : 'rotate-right',
1663 cls : 'btn btn-default',
1664 html : '<i class="fa fa-repeat"></i>'