7 * @class Roo.bootstrap.UploadCropbox
8 * @extends Roo.bootstrap.Component
9 * Bootstrap UploadCropbox class
10 * @cfg {String} emptyText show when image has been loaded
11 * @cfg {String} rotateNotify show when image too small to rotate
12 * @cfg {Number} errorTimeout default 3000
13 * @cfg {Number} minWidth default 300
14 * @cfg {Number} minHeight default 300
15 * @cfg {Array} buttons default ['rotateLeft', 'pictureBtn', 'rotateRight']
16 * @cfg {Boolean} isDocument (true|false) default false
17 * @cfg {String} url action url
18 * @cfg {String} paramName default 'imageUpload'
19 * @cfg {String} method default POST
20 * @cfg {Boolean} loadMask (true|false) default true
21 * @cfg {Boolean} loadingText default 'Loading...'
24 * Create a new UploadCropbox
25 * @param {Object} config The config object
28 Roo.bootstrap.UploadCropbox = function(config){
29 console.log("BOOTSTRAP UPLOAD CROPBOX CONSTRUCTOR");
30 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
34 * @event beforeselectfile
35 * Fire before select file
36 * @param {Roo.bootstrap.UploadCropbox} this
38 "beforeselectfile" : true,
41 * Fire after initEvent
42 * @param {Roo.bootstrap.UploadCropbox} this
47 * Fire after initEvent
48 * @param {Roo.bootstrap.UploadCropbox} this
49 * @param {String} data
54 * Fire when preparing the file data
55 * @param {Roo.bootstrap.UploadCropbox} this
56 * @param {Object} file
61 * Fire when get exception
62 * @param {Roo.bootstrap.UploadCropbox} this
63 * @param {XMLHttpRequest} xhr
67 * @event beforeloadcanvas
68 * Fire before load the canvas
69 * @param {Roo.bootstrap.UploadCropbox} this
72 "beforeloadcanvas" : true,
75 * Fire when trash image
76 * @param {Roo.bootstrap.UploadCropbox} this
81 * Fire when download the image
82 * @param {Roo.bootstrap.UploadCropbox} this
86 * @event footerbuttonclick
87 * Fire when footerbuttonclick
88 * @param {Roo.bootstrap.UploadCropbox} this
89 * @param {String} type
91 "footerbuttonclick" : true,
95 * @param {Roo.bootstrap.UploadCropbox} this
100 * Fire when rotate the image
101 * @param {Roo.bootstrap.UploadCropbox} this
102 * @param {String} pos
107 * Fire when inspect the file
108 * @param {Roo.bootstrap.UploadCropbox} this
109 * @param {Object} file
114 * Fire when xhr upload the file
115 * @param {Roo.bootstrap.UploadCropbox} this
116 * @param {Object} data
121 * Fire when arrange the file data
122 * @param {Roo.bootstrap.UploadCropbox} this
123 * @param {Object} formData
128 this.buttons = this.buttons || Roo.bootstrap.UploadCropbox.footer.DOCUMENT;
131 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
133 emptyText : 'Click to upload image',
134 rotateNotify : 'Image is too small to rotate',
149 cropType : 'image/jpeg',
151 canvasLoaded : false,
154 paramName : 'imageUpload',
156 loadingText : 'Loading...',
159 getAutoCreate : function()
163 cls : 'roo-upload-cropbox',
167 cls : 'roo-upload-cropbox-selector',
172 cls : 'roo-upload-cropbox-body',
173 style : 'cursor:pointer',
177 cls : 'roo-upload-cropbox-preview'
181 cls : 'roo-upload-cropbox-thumb'
185 cls : 'roo-upload-cropbox-empty-notify',
186 html : this.emptyText
190 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
191 html : this.rotateNotify
197 cls : 'roo-upload-cropbox-footer',
200 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
210 onRender : function(ct, position)
212 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
214 if (this.buttons.length) {
216 Roo.each(this.buttons, function(bb) {
218 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
220 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
226 this.maskEl = this.el;
230 initEvents : function()
232 this.urlAPI = (window.createObjectURL && window) ||
233 (window.URL && URL.revokeObjectURL && URL) ||
234 (window.webkitURL && webkitURL);
236 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
237 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
239 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
240 this.selectorEl.hide();
242 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
243 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
245 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
246 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
249 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
250 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
252 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
253 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
256 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
257 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
258 this.footerEl.hide();
260 this.setThumbBoxSize();
266 this.fireEvent('initial', this);
273 window.addEventListener("resize", function() { _this.resize(); } );
275 this.bodyEl.on('click', this.beforeSelectFile, this);
278 this.bodyEl.on('touchstart', this.onTouchStart, this);
279 this.bodyEl.on('touchmove', this.onTouchMove, this);
280 this.bodyEl.on('touchend', this.onTouchEnd, this);
284 this.bodyEl.on('mousedown', this.onMouseDown, this);
285 this.bodyEl.on('mousemove', this.onMouseMove, this);
286 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
287 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
288 Roo.get(document).on('mouseup', this.onMouseUp, this);
291 this.selectorEl.on('change', this.onFileSelected, this);
300 this.dragable = false;
301 this.pinching = false;
304 this.cropData = false;
305 this.notifyEl.dom.innerHTML = this.emptyText;
307 this.selectorEl.dom.value = '';
313 if(this.fireEvent('resize', this) != false){
314 this.setThumbBoxPosition();
315 this.setCanvasPosition();
319 onFooterButtonClick : function(e, el, o, type)
323 this.onRotateLeft(e);
325 case 'rotate-right' :
326 this.onRotateRight(e);
329 this.beforeSelectFile(e);
344 this.fireEvent('footerbuttonclick', this, type);
347 beforeSelectFile : function(e)
351 if(this.fireEvent('beforeselectfile', this) != false){
352 this.selectorEl.dom.click();
356 onFileSelected : function(e)
360 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
364 var file = this.selectorEl.dom.files[0];
366 if(this.fireEvent('inspect', this, file) != false){
374 this.fireEvent('trash', this);
377 download : function(e)
379 this.fireEvent('download', this);
382 loadCanvas : function(src)
384 if(this.fireEvent('beforeloadcanvas', this, src) != false){
388 this.imageEl = document.createElement('img');
392 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
394 this.imageEl.src = src;
398 onLoadCanvas : function()
400 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
401 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
403 this.bodyEl.un('click', this.beforeSelectFile, this);
405 this.notifyEl.hide();
407 this.footerEl.show();
409 this.baseRotateLevel();
412 this.setThumbBoxSize();
415 this.setThumbBoxPosition();
417 this.baseScaleLevel();
423 this.canvasLoaded = true;
426 this.maskEl.unmask();
431 setCanvasPosition : function()
437 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
438 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
440 this.previewEl.setLeft(pw);
441 this.previewEl.setTop(ph);
445 onMouseDown : function(e)
449 this.dragable = true;
450 this.pinching = false;
452 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
453 this.dragable = false;
457 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
458 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
462 onMouseMove : function(e)
466 if(!this.canvasLoaded){
474 var minX = Math.ceil(this.thumbEl.getLeft(true));
475 var minY = Math.ceil(this.thumbEl.getTop(true));
477 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
478 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
480 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
481 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
486 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
487 var bgY = Math.ceil(y + this.previewEl.getTop(true));
489 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
490 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
492 this.previewEl.setLeft(bgX);
493 this.previewEl.setTop(bgY);
495 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
496 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
499 onMouseUp : function(e)
503 this.dragable = false;
506 onMouseWheel : function(e)
510 this.startScale = this.scale;
512 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
514 if(!this.zoomable()){
515 this.scale = this.startScale;
524 zoomable : function()
526 var minScale = this.thumbEl.getWidth() / this.minWidth;
528 if(this.minWidth < this.minHeight){
529 minScale = this.thumbEl.getHeight() / this.minHeight;
532 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
533 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
537 (this.rotate == 0 || this.rotate == 180) &&
539 width > this.imageEl.OriginWidth ||
540 height > this.imageEl.OriginHeight ||
541 (width < this.minWidth && height < this.minHeight)
549 (this.rotate == 90 || this.rotate == 270) &&
551 width > this.imageEl.OriginWidth ||
552 height > this.imageEl.OriginHeight ||
553 (width < this.minHeight && height < this.minWidth)
561 (this.rotate == 0 || this.rotate == 180) &&
563 width < this.minWidth ||
564 width > this.imageEl.OriginWidth ||
565 height < this.minHeight ||
566 height > this.imageEl.OriginHeight
574 (this.rotate == 90 || this.rotate == 270) &&
576 width < this.minHeight ||
577 width > this.imageEl.OriginWidth ||
578 height < this.minWidth ||
579 height > this.imageEl.OriginHeight
589 onRotateLeft : function(e)
591 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
593 var minScale = this.thumbEl.getWidth() / this.minWidth;
595 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
596 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
598 this.startScale = this.scale;
600 while (this.getScaleLevel() < minScale){
602 this.scale = this.scale + 1;
604 if(!this.zoomable()){
609 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
610 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
615 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
622 this.scale = this.startScale;
629 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
632 this.setThumbBoxSize();
633 this.setThumbBoxPosition();
634 this.setCanvasPosition();
639 this.fireEvent('rotate', this, 'left');
643 onRotateRight : function(e)
645 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
647 var minScale = this.thumbEl.getWidth() / this.minWidth;
649 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
650 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
652 this.startScale = this.scale;
654 while (this.getScaleLevel() < minScale){
656 this.scale = this.scale + 1;
658 if(!this.zoomable()){
663 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
664 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
669 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
676 this.scale = this.startScale;
683 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
686 this.setThumbBoxSize();
687 this.setThumbBoxPosition();
688 this.setCanvasPosition();
693 this.fireEvent('rotate', this, 'right');
696 onRotateFail : function()
698 this.errorEl.show(true);
702 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
707 this.previewEl.dom.innerHTML = '';
709 var canvasEl = document.createElement("canvas");
711 var contextEl = canvasEl.getContext("2d");
713 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
714 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
715 var center = this.imageEl.OriginWidth / 2;
717 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
718 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
719 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
720 center = this.imageEl.OriginHeight / 2;
723 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
725 contextEl.translate(center, center);
726 contextEl.rotate(this.rotate * Math.PI / 180);
728 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
730 this.canvasEl = document.createElement("canvas");
732 this.contextEl = this.canvasEl.getContext("2d");
734 switch (this.rotate) {
737 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
738 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
740 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
745 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
746 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
748 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
749 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);
753 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
758 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
759 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
761 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
762 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);
766 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);
771 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
772 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
774 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
775 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
779 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);
786 this.previewEl.appendChild(this.canvasEl);
788 this.setCanvasPosition();
793 if(!this.canvasLoaded){
797 var imageCanvas = document.createElement("canvas");
799 var imageContext = imageCanvas.getContext("2d");
801 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
802 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
804 var center = imageCanvas.width / 2;
806 imageContext.translate(center, center);
808 imageContext.rotate(this.rotate * Math.PI / 180);
810 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
812 var canvas = document.createElement("canvas");
814 var context = canvas.getContext("2d");
816 canvas.width = this.minWidth;
817 canvas.height = this.minHeight;
819 switch (this.rotate) {
822 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
823 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
825 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
826 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
828 var targetWidth = this.minWidth - 2 * x;
829 var targetHeight = this.minHeight - 2 * y;
833 if((x == 0 && y == 0) || (x == 0 && y > 0)){
834 scale = targetWidth / width;
838 scale = targetHeight / height;
842 scale = targetWidth / width;
845 scale = targetHeight / height;
849 context.scale(scale, scale);
851 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
852 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
854 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
855 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
857 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
862 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
863 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
865 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
866 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
868 var targetWidth = this.minWidth - 2 * x;
869 var targetHeight = this.minHeight - 2 * y;
873 if((x == 0 && y == 0) || (x == 0 && y > 0)){
874 scale = targetWidth / width;
878 scale = targetHeight / height;
882 scale = targetWidth / width;
885 scale = targetHeight / height;
889 context.scale(scale, scale);
891 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
892 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
894 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
895 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
897 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
899 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
904 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
905 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
907 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
908 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
910 var targetWidth = this.minWidth - 2 * x;
911 var targetHeight = this.minHeight - 2 * y;
915 if((x == 0 && y == 0) || (x == 0 && y > 0)){
916 scale = targetWidth / width;
920 scale = targetHeight / height;
924 scale = targetWidth / width;
927 scale = targetHeight / height;
931 context.scale(scale, scale);
933 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
934 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
936 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
937 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
939 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
940 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
942 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
947 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
948 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
950 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
951 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
953 var targetWidth = this.minWidth - 2 * x;
954 var targetHeight = this.minHeight - 2 * y;
958 if((x == 0 && y == 0) || (x == 0 && y > 0)){
959 scale = targetWidth / width;
963 scale = targetHeight / height;
967 scale = targetWidth / width;
970 scale = targetHeight / height;
974 context.scale(scale, scale);
976 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
977 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
979 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
980 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
982 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
984 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
991 this.cropData = canvas.toDataURL(this.cropType);
993 if(this.fireEvent('crop', this, this.cropData) !== false){
994 this.process(this.file, this.cropData);
1001 setThumbBoxSize : function()
1005 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1006 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1007 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1009 this.minWidth = width;
1010 this.minHeight = height;
1012 if(this.rotate == 90 || this.rotate == 270){
1013 this.minWidth = height;
1014 this.minHeight = width;
1019 width = Math.ceil(this.minWidth * height / this.minHeight);
1021 if(this.minWidth > this.minHeight){
1023 height = Math.ceil(this.minHeight * width / this.minWidth);
1026 this.thumbEl.setStyle({
1027 width : width + 'px',
1028 height : height + 'px'
1035 setThumbBoxPosition : function()
1037 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1038 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1040 this.thumbEl.setLeft(x);
1041 this.thumbEl.setTop(y);
1045 baseRotateLevel : function()
1047 this.baseRotate = 1;
1050 typeof(this.exif) != 'undefined' &&
1051 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1052 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1054 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1057 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1061 baseScaleLevel : function()
1065 if(this.isDocument){
1067 if(this.baseRotate == 6 || this.baseRotate == 8){
1069 height = this.thumbEl.getHeight();
1070 this.baseScale = height / this.imageEl.OriginWidth;
1072 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1073 width = this.thumbEl.getWidth();
1074 this.baseScale = width / this.imageEl.OriginHeight;
1080 height = this.thumbEl.getHeight();
1081 this.baseScale = height / this.imageEl.OriginHeight;
1083 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1084 width = this.thumbEl.getWidth();
1085 this.baseScale = width / this.imageEl.OriginWidth;
1091 if(this.baseRotate == 6 || this.baseRotate == 8){
1093 width = this.thumbEl.getHeight();
1094 this.baseScale = width / this.imageEl.OriginHeight;
1096 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1097 height = this.thumbEl.getWidth();
1098 this.baseScale = height / this.imageEl.OriginHeight;
1101 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1102 height = this.thumbEl.getWidth();
1103 this.baseScale = height / this.imageEl.OriginHeight;
1105 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1106 width = this.thumbEl.getHeight();
1107 this.baseScale = width / this.imageEl.OriginWidth;
1114 width = this.thumbEl.getWidth();
1115 this.baseScale = width / this.imageEl.OriginWidth;
1117 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1118 height = this.thumbEl.getHeight();
1119 this.baseScale = height / this.imageEl.OriginHeight;
1122 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1124 height = this.thumbEl.getHeight();
1125 this.baseScale = height / this.imageEl.OriginHeight;
1127 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1128 width = this.thumbEl.getWidth();
1129 this.baseScale = width / this.imageEl.OriginWidth;
1137 getScaleLevel : function()
1139 return this.baseScale * Math.pow(1.1, this.scale);
1142 onTouchStart : function(e)
1144 if(!this.canvasLoaded){
1145 this.beforeSelectFile(e);
1149 var touches = e.browserEvent.touches;
1155 if(touches.length == 1){
1156 this.onMouseDown(e);
1160 if(touches.length != 2){
1166 for(var i = 0, finger; finger = touches[i]; i++){
1167 coords.push(finger.pageX, finger.pageY);
1170 var x = Math.pow(coords[0] - coords[2], 2);
1171 var y = Math.pow(coords[1] - coords[3], 2);
1173 this.startDistance = Math.sqrt(x + y);
1175 this.startScale = this.scale;
1177 this.pinching = true;
1178 this.dragable = false;
1182 onTouchMove : function(e)
1184 if(!this.pinching && !this.dragable){
1188 var touches = e.browserEvent.touches;
1195 this.onMouseMove(e);
1201 for(var i = 0, finger; finger = touches[i]; i++){
1202 coords.push(finger.pageX, finger.pageY);
1205 var x = Math.pow(coords[0] - coords[2], 2);
1206 var y = Math.pow(coords[1] - coords[3], 2);
1208 this.endDistance = Math.sqrt(x + y);
1210 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1212 if(!this.zoomable()){
1213 this.scale = this.startScale;
1221 onTouchEnd : function(e)
1223 this.pinching = false;
1224 this.dragable = false;
1228 process : function(file, crop)
1231 this.maskEl.mask(this.loadingText);
1234 this.xhr = new XMLHttpRequest();
1236 file.xhr = this.xhr;
1238 this.xhr.open(this.method, this.url, true);
1241 "Accept": "application/json",
1242 "Cache-Control": "no-cache",
1243 "X-Requested-With": "XMLHttpRequest"
1246 for (var headerName in headers) {
1247 var headerValue = headers[headerName];
1249 this.xhr.setRequestHeader(headerName, headerValue);
1255 this.xhr.onload = function()
1257 _this.xhrOnLoad(_this.xhr);
1260 this.xhr.onerror = function()
1262 _this.xhrOnError(_this.xhr);
1265 var formData = new FormData();
1267 formData.append('returnHTML', 'NO');
1270 formData.append('crop', crop);
1273 if(typeof(file) != 'undefined' && (typeof(file.id) == 'undefined' || file.id * 1 < 1)){
1274 formData.append(this.paramName, file, file.name);
1277 if(typeof(file.filename) != 'undefined'){
1278 formData.append('filename', file.filename);
1281 if(typeof(file.mimetype) != 'undefined'){
1282 formData.append('mimetype', file.mimetype);
1285 if(this.fireEvent('arrange', this, formData) != false){
1286 this.xhr.send(formData);
1290 xhrOnLoad : function(xhr)
1293 this.maskEl.unmask();
1296 if (xhr.readyState !== 4) {
1297 this.fireEvent('exception', this, xhr);
1301 var response = Roo.decode(xhr.responseText);
1303 if(!response.success){
1304 this.fireEvent('exception', this, xhr);
1308 var response = Roo.decode(xhr.responseText);
1310 this.fireEvent('upload', this, response);
1314 xhrOnError : function()
1317 this.maskEl.unmask();
1320 Roo.log('xhr on error');
1322 var response = Roo.decode(xhr.responseText);
1328 prepare : function(file)
1331 this.maskEl.mask(this.loadingText);
1337 if(typeof(file) === 'string'){
1338 this.loadCanvas(file);
1342 if(!file || !this.urlAPI){
1347 this.cropType = file.type;
1351 if(this.fireEvent('prepare', this, this.file) != false){
1353 var reader = new FileReader();
1355 reader.onload = function (e) {
1356 if (e.target.error) {
1357 Roo.log(e.target.error);
1361 var buffer = e.target.result,
1362 dataView = new DataView(buffer),
1364 maxOffset = dataView.byteLength - 4,
1368 if (dataView.getUint16(0) === 0xffd8) {
1369 while (offset < maxOffset) {
1370 markerBytes = dataView.getUint16(offset);
1372 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1373 markerLength = dataView.getUint16(offset + 2) + 2;
1374 if (offset + markerLength > dataView.byteLength) {
1375 Roo.log('Invalid meta data: Invalid segment size.');
1379 if(markerBytes == 0xffe1){
1380 _this.parseExifData(
1387 offset += markerLength;
1397 var url = _this.urlAPI.createObjectURL(_this.file);
1399 _this.loadCanvas(url);
1404 reader.readAsArrayBuffer(this.file);
1410 parseExifData : function(dataView, offset, length)
1412 var tiffOffset = offset + 10,
1416 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1417 // No Exif data, might be XMP data instead
1421 // Check for the ASCII code for "Exif" (0x45786966):
1422 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1423 // No Exif data, might be XMP data instead
1426 if (tiffOffset + 8 > dataView.byteLength) {
1427 Roo.log('Invalid Exif data: Invalid segment size.');
1430 // Check for the two null bytes:
1431 if (dataView.getUint16(offset + 8) !== 0x0000) {
1432 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1435 // Check the byte alignment:
1436 switch (dataView.getUint16(tiffOffset)) {
1438 littleEndian = true;
1441 littleEndian = false;
1444 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1447 // Check for the TIFF tag marker (0x002A):
1448 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1449 Roo.log('Invalid Exif data: Missing TIFF marker.');
1452 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1453 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1458 tiffOffset + dirOffset,
1463 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1468 if (dirOffset + 6 > dataView.byteLength) {
1469 Roo.log('Invalid Exif data: Invalid directory offset.');
1472 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1473 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1474 if (dirEndOffset + 4 > dataView.byteLength) {
1475 Roo.log('Invalid Exif data: Invalid directory size.');
1478 for (i = 0; i < tagsNumber; i += 1) {
1482 dirOffset + 2 + 12 * i, // tag offset
1486 // Return the offset to the next directory:
1487 return dataView.getUint32(dirEndOffset, littleEndian);
1490 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1492 var tag = dataView.getUint16(offset, littleEndian);
1494 this.exif[tag] = this.getExifValue(
1498 dataView.getUint16(offset + 2, littleEndian), // tag type
1499 dataView.getUint32(offset + 4, littleEndian), // tag length
1504 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1506 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1515 Roo.log('Invalid Exif data: Invalid tag type.');
1519 tagSize = tagType.size * length;
1520 // Determine if the value is contained in the dataOffset bytes,
1521 // or if the value at the dataOffset is a pointer to the actual data:
1522 dataOffset = tagSize > 4 ?
1523 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1524 if (dataOffset + tagSize > dataView.byteLength) {
1525 Roo.log('Invalid Exif data: Invalid data offset.');
1529 return tagType.getValue(dataView, dataOffset, littleEndian);
1532 for (i = 0; i < length; i += 1) {
1533 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1536 if (tagType.ascii) {
1538 // Concatenate the chars:
1539 for (i = 0; i < values.length; i += 1) {
1541 // Ignore the terminating NULL byte(s):
1542 if (c === '\u0000') {
1554 Roo.apply(Roo.bootstrap.UploadCropbox, {
1556 'Orientation': 0x0112
1562 3: 180, //'bottom-right',
1563 // 4: 'bottom-left',
1565 6: 90, //'right-top',
1566 // 7: 'right-bottom',
1567 8: 270 //'left-bottom'
1571 // byte, 8-bit unsigned int:
1573 getValue: function (dataView, dataOffset) {
1574 return dataView.getUint8(dataOffset);
1578 // ascii, 8-bit byte:
1580 getValue: function (dataView, dataOffset) {
1581 return String.fromCharCode(dataView.getUint8(dataOffset));
1586 // short, 16 bit int:
1588 getValue: function (dataView, dataOffset, littleEndian) {
1589 return dataView.getUint16(dataOffset, littleEndian);
1593 // long, 32 bit int:
1595 getValue: function (dataView, dataOffset, littleEndian) {
1596 return dataView.getUint32(dataOffset, littleEndian);
1600 // rational = two long values, first is numerator, second is denominator:
1602 getValue: function (dataView, dataOffset, littleEndian) {
1603 return dataView.getUint32(dataOffset, littleEndian) /
1604 dataView.getUint32(dataOffset + 4, littleEndian);
1608 // slong, 32 bit signed int:
1610 getValue: function (dataView, dataOffset, littleEndian) {
1611 return dataView.getInt32(dataOffset, littleEndian);
1615 // srational, two slongs, first is numerator, second is denominator:
1617 getValue: function (dataView, dataOffset, littleEndian) {
1618 return dataView.getInt32(dataOffset, littleEndian) /
1619 dataView.getInt32(dataOffset + 4, littleEndian);
1629 cls : 'btn-group roo-upload-cropbox-rotate-left',
1630 action : 'rotate-left',
1634 cls : 'btn btn-default',
1635 html : '<i class="fa fa-undo"></i>'
1641 cls : 'btn-group roo-upload-cropbox-picture',
1646 cls : 'btn btn-default',
1647 html : '<i class="fa fa-picture-o"></i>'
1653 cls : 'btn-group roo-upload-cropbox-rotate-right',
1654 action : 'rotate-right',
1658 cls : 'btn btn-default',
1659 html : '<i class="fa fa-repeat"></i>'
1667 cls : 'btn-group roo-upload-cropbox-rotate-left',
1668 action : 'rotate-left',
1672 cls : 'btn btn-default',
1673 html : '<i class="fa fa-undo"></i>'
1679 cls : 'btn-group roo-upload-cropbox-download',
1680 action : 'download',
1684 cls : 'btn btn-default',
1685 html : '<i class="fa fa-download"></i>'
1691 cls : 'btn-group roo-upload-cropbox-crop',
1696 cls : 'btn btn-default',
1697 html : '<i class="fa fa-crop"></i>'
1703 cls : 'btn-group roo-upload-cropbox-trash',
1708 cls : 'btn btn-default',
1709 html : '<i class="fa fa-trash"></i>'
1715 cls : 'btn-group roo-upload-cropbox-rotate-right',
1716 action : 'rotate-right',
1720 cls : 'btn btn-default',
1721 html : '<i class="fa fa-repeat"></i>'
1729 cls : 'btn-group roo-upload-cropbox-rotate-left',
1730 action : 'rotate-left',
1734 cls : 'btn btn-default',
1735 html : '<i class="fa fa-undo"></i>'
1741 cls : 'btn-group roo-upload-cropbox-rotate-right',
1742 action : 'rotate-right',
1746 cls : 'btn btn-default',
1747 html : '<i class="fa fa-repeat"></i>'