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.STANDARD;
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()
161 console.log("BOOTSTRAP UPLOADCROPBOX GETAUTOCREATE");
164 cls : 'roo-upload-cropbox',
168 cls : 'roo-upload-cropbox-selector',
173 cls : 'roo-upload-cropbox-body',
174 style : 'cursor:pointer',
178 cls : 'roo-upload-cropbox-preview'
182 cls : 'roo-upload-cropbox-thumb'
186 cls : 'roo-upload-cropbox-empty-notify',
187 html : this.emptyText
191 cls : 'roo-upload-cropbox-error-notify alert alert-danger',
192 html : this.rotateNotify
198 cls : 'roo-upload-cropbox-footer',
201 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
211 onRender : function(ct, position)
213 console.log("BOOTSTRAP UPLOADCROPBOX ONRENDER");
215 console.log(position);
216 Roo.bootstrap.UploadCropbox.superclass.onRender.call(this, ct, position);
218 if (this.buttons.length) {
220 Roo.each(this.buttons, function(bb) {
222 var btn = this.el.select('.roo-upload-cropbox-footer div.roo-upload-cropbox-btn-group').first().createChild(bb);
224 btn.on('click', this.onFooterButtonClick.createDelegate(this, [bb.action], true));
230 this.maskEl = this.el;
234 initEvents : function()
236 this.urlAPI = (window.createObjectURL && window) ||
237 (window.URL && URL.revokeObjectURL && URL) ||
238 (window.webkitURL && webkitURL);
240 this.bodyEl = this.el.select('.roo-upload-cropbox-body', true).first();
241 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
243 this.selectorEl = this.el.select('.roo-upload-cropbox-selector', true).first();
244 this.selectorEl.hide();
246 this.previewEl = this.el.select('.roo-upload-cropbox-preview', true).first();
247 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
249 this.thumbEl = this.el.select('.roo-upload-cropbox-thumb', true).first();
250 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
253 this.notifyEl = this.el.select('.roo-upload-cropbox-empty-notify', true).first();
254 this.notifyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
256 this.errorEl = this.el.select('.roo-upload-cropbox-error-notify', true).first();
257 this.errorEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
260 this.footerEl = this.el.select('.roo-upload-cropbox-footer', true).first();
261 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
262 this.footerEl.hide();
264 this.setThumbBoxSize();
270 this.fireEvent('initial', this);
277 window.addEventListener("resize", function() { _this.resize(); } );
279 this.bodyEl.on('click', this.beforeSelectFile, this);
282 this.bodyEl.on('touchstart', this.onTouchStart, this);
283 this.bodyEl.on('touchmove', this.onTouchMove, this);
284 this.bodyEl.on('touchend', this.onTouchEnd, this);
288 this.bodyEl.on('mousedown', this.onMouseDown, this);
289 this.bodyEl.on('mousemove', this.onMouseMove, this);
290 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
291 this.bodyEl.on(mousewheel, this.onMouseWheel, this);
292 Roo.get(document).on('mouseup', this.onMouseUp, this);
295 this.selectorEl.on('change', this.onFileSelected, this);
304 this.dragable = false;
305 this.pinching = false;
308 this.cropData = false;
309 this.notifyEl.dom.innerHTML = this.emptyText;
311 this.selectorEl.dom.value = '';
317 if(this.fireEvent('resize', this) != false){
318 this.setThumbBoxPosition();
319 this.setCanvasPosition();
323 onFooterButtonClick : function(e, el, o, type)
327 this.onRotateLeft(e);
329 case 'rotate-right' :
330 this.onRotateRight(e);
333 this.beforeSelectFile(e);
348 this.fireEvent('footerbuttonclick', this, type);
351 beforeSelectFile : function(e)
355 if(this.fireEvent('beforeselectfile', this) != false){
356 this.selectorEl.dom.click();
360 onFileSelected : function(e)
364 if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
368 var file = this.selectorEl.dom.files[0];
370 if(this.fireEvent('inspect', this, file) != false){
378 this.fireEvent('trash', this);
381 download : function(e)
383 this.fireEvent('download', this);
386 loadCanvas : function(src)
388 if(this.fireEvent('beforeloadcanvas', this, src) != false){
392 this.imageEl = document.createElement('img');
396 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
398 this.imageEl.src = src;
402 onLoadCanvas : function()
404 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
405 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
407 this.bodyEl.un('click', this.beforeSelectFile, this);
409 this.notifyEl.hide();
411 this.footerEl.show();
413 this.baseRotateLevel();
416 this.setThumbBoxSize();
419 this.setThumbBoxPosition();
421 this.baseScaleLevel();
427 this.canvasLoaded = true;
430 this.maskEl.unmask();
435 setCanvasPosition : function()
441 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
442 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
444 this.previewEl.setLeft(pw);
445 this.previewEl.setTop(ph);
449 onMouseDown : function(e)
453 this.dragable = true;
454 this.pinching = false;
456 if(this.isDocument && (this.canvasEl.width < this.thumbEl.getWidth() || this.canvasEl.height < this.thumbEl.getHeight())){
457 this.dragable = false;
461 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
462 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
466 onMouseMove : function(e)
470 if(!this.canvasLoaded){
478 var minX = Math.ceil(this.thumbEl.getLeft(true));
479 var minY = Math.ceil(this.thumbEl.getTop(true));
481 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
482 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
484 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
485 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
490 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
491 var bgY = Math.ceil(y + this.previewEl.getTop(true));
493 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
494 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
496 this.previewEl.setLeft(bgX);
497 this.previewEl.setTop(bgY);
499 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
500 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
503 onMouseUp : function(e)
507 this.dragable = false;
510 onMouseWheel : function(e)
514 this.startScale = this.scale;
516 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
518 if(!this.zoomable()){
519 this.scale = this.startScale;
528 zoomable : function()
530 var minScale = this.thumbEl.getWidth() / this.minWidth;
532 if(this.minWidth < this.minHeight){
533 minScale = this.thumbEl.getHeight() / this.minHeight;
536 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel() / minScale);
537 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel() / minScale);
541 (this.rotate == 0 || this.rotate == 180) &&
543 width > this.imageEl.OriginWidth ||
544 height > this.imageEl.OriginHeight ||
545 (width < this.minWidth && height < this.minHeight)
553 (this.rotate == 90 || this.rotate == 270) &&
555 width > this.imageEl.OriginWidth ||
556 height > this.imageEl.OriginHeight ||
557 (width < this.minHeight && height < this.minWidth)
565 (this.rotate == 0 || this.rotate == 180) &&
567 width < this.minWidth ||
568 width > this.imageEl.OriginWidth ||
569 height < this.minHeight ||
570 height > this.imageEl.OriginHeight
578 (this.rotate == 90 || this.rotate == 270) &&
580 width < this.minHeight ||
581 width > this.imageEl.OriginWidth ||
582 height < this.minWidth ||
583 height > this.imageEl.OriginHeight
593 onRotateLeft : function(e)
595 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
597 var minScale = this.thumbEl.getWidth() / this.minWidth;
599 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
600 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
602 this.startScale = this.scale;
604 while (this.getScaleLevel() < minScale){
606 this.scale = this.scale + 1;
608 if(!this.zoomable()){
613 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
614 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
619 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
626 this.scale = this.startScale;
633 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
636 this.setThumbBoxSize();
637 this.setThumbBoxPosition();
638 this.setCanvasPosition();
643 this.fireEvent('rotate', this, 'left');
647 onRotateRight : function(e)
649 if(!this.isDocument && (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())){
651 var minScale = this.thumbEl.getWidth() / this.minWidth;
653 var bw = Math.ceil(this.canvasEl.width / this.getScaleLevel());
654 var bh = Math.ceil(this.canvasEl.height / this.getScaleLevel());
656 this.startScale = this.scale;
658 while (this.getScaleLevel() < minScale){
660 this.scale = this.scale + 1;
662 if(!this.zoomable()){
667 Math.ceil(bw * this.getScaleLevel()) < this.thumbEl.getHeight() ||
668 Math.ceil(bh * this.getScaleLevel()) < this.thumbEl.getWidth()
673 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
680 this.scale = this.startScale;
687 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
690 this.setThumbBoxSize();
691 this.setThumbBoxPosition();
692 this.setCanvasPosition();
697 this.fireEvent('rotate', this, 'right');
700 onRotateFail : function()
702 this.errorEl.show(true);
706 (function() { _this.errorEl.hide(true); }).defer(this.errorTimeout);
711 this.previewEl.dom.innerHTML = '';
713 var canvasEl = document.createElement("canvas");
715 var contextEl = canvasEl.getContext("2d");
717 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
718 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
719 var center = this.imageEl.OriginWidth / 2;
721 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
722 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
723 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
724 center = this.imageEl.OriginHeight / 2;
727 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
729 contextEl.translate(center, center);
730 contextEl.rotate(this.rotate * Math.PI / 180);
732 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
734 this.canvasEl = document.createElement("canvas");
736 this.contextEl = this.canvasEl.getContext("2d");
738 switch (this.rotate) {
741 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
742 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
744 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
749 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
750 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
752 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
753 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);
757 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
762 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
763 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
765 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
766 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);
770 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);
775 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
776 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
778 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
779 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
783 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);
790 this.previewEl.appendChild(this.canvasEl);
792 this.setCanvasPosition();
797 if(!this.canvasLoaded){
801 var imageCanvas = document.createElement("canvas");
803 var imageContext = imageCanvas.getContext("2d");
805 imageCanvas.width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
806 imageCanvas.height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? this.imageEl.OriginWidth : this.imageEl.OriginHeight;
808 var center = imageCanvas.width / 2;
810 imageContext.translate(center, center);
812 imageContext.rotate(this.rotate * Math.PI / 180);
814 imageContext.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
816 var canvas = document.createElement("canvas");
818 var context = canvas.getContext("2d");
820 canvas.width = this.minWidth;
821 canvas.height = this.minHeight;
823 switch (this.rotate) {
826 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
827 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
829 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
830 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
832 var targetWidth = this.minWidth - 2 * x;
833 var targetHeight = this.minHeight - 2 * y;
837 if((x == 0 && y == 0) || (x == 0 && y > 0)){
838 scale = targetWidth / width;
842 scale = targetHeight / height;
846 scale = targetWidth / width;
849 scale = targetHeight / height;
853 context.scale(scale, scale);
855 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
856 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
858 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
859 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
861 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
866 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
867 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
869 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
870 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
872 var targetWidth = this.minWidth - 2 * x;
873 var targetHeight = this.minHeight - 2 * y;
877 if((x == 0 && y == 0) || (x == 0 && y > 0)){
878 scale = targetWidth / width;
882 scale = targetHeight / height;
886 scale = targetWidth / width;
889 scale = targetHeight / height;
893 context.scale(scale, scale);
895 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
896 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
898 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
899 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
901 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
903 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
908 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getWidth() / this.getScaleLevel());
909 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getHeight() / this.getScaleLevel());
911 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
912 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
914 var targetWidth = this.minWidth - 2 * x;
915 var targetHeight = this.minHeight - 2 * y;
919 if((x == 0 && y == 0) || (x == 0 && y > 0)){
920 scale = targetWidth / width;
924 scale = targetHeight / height;
928 scale = targetWidth / width;
931 scale = targetHeight / height;
935 context.scale(scale, scale);
937 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
938 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
940 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
941 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
943 sx += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
944 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight) : 0;
946 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
951 var width = (this.thumbEl.getWidth() / this.getScaleLevel() > this.imageEl.OriginHeight) ? this.imageEl.OriginHeight : (this.thumbEl.getWidth() / this.getScaleLevel());
952 var height = (this.thumbEl.getHeight() / this.getScaleLevel() > this.imageEl.OriginWidth) ? this.imageEl.OriginWidth : (this.thumbEl.getHeight() / this.getScaleLevel());
954 var x = (this.thumbEl.getLeft(true) > this.previewEl.getLeft(true)) ? 0 : ((this.previewEl.getLeft(true) - this.thumbEl.getLeft(true)) / this.getScaleLevel());
955 var y = (this.thumbEl.getTop(true) > this.previewEl.getTop(true)) ? 0 : ((this.previewEl.getTop(true) - this.thumbEl.getTop(true)) / this.getScaleLevel());
957 var targetWidth = this.minWidth - 2 * x;
958 var targetHeight = this.minHeight - 2 * y;
962 if((x == 0 && y == 0) || (x == 0 && y > 0)){
963 scale = targetWidth / width;
967 scale = targetHeight / height;
971 scale = targetWidth / width;
974 scale = targetHeight / height;
978 context.scale(scale, scale);
980 var sx = Math.min(this.canvasEl.width - this.thumbEl.getWidth(), this.thumbEl.getLeft(true) - this.previewEl.getLeft(true));
981 var sy = Math.min(this.canvasEl.height - this.thumbEl.getHeight(), this.thumbEl.getTop(true) - this.previewEl.getTop(true));
983 sx = sx < 0 ? 0 : (sx / this.getScaleLevel());
984 sy = sy < 0 ? 0 : (sy / this.getScaleLevel());
986 sy += (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? 0 : Math.abs(this.imageEl.OriginWidth - this.imageEl.OriginHeight);
988 context.drawImage(imageCanvas, sx, sy, width, height, x, y, width, height);
995 this.cropData = canvas.toDataURL(this.cropType);
997 if(this.fireEvent('crop', this, this.cropData) !== false){
998 this.process(this.file, this.cropData);
1005 setThumbBoxSize : function()
1009 if(this.isDocument && typeof(this.imageEl) != 'undefined'){
1010 width = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.max(this.minWidth, this.minHeight) : Math.min(this.minWidth, this.minHeight);
1011 height = (this.imageEl.OriginWidth > this.imageEl.OriginHeight) ? Math.min(this.minWidth, this.minHeight) : Math.max(this.minWidth, this.minHeight);
1013 this.minWidth = width;
1014 this.minHeight = height;
1016 if(this.rotate == 90 || this.rotate == 270){
1017 this.minWidth = height;
1018 this.minHeight = width;
1023 width = Math.ceil(this.minWidth * height / this.minHeight);
1025 if(this.minWidth > this.minHeight){
1027 height = Math.ceil(this.minHeight * width / this.minWidth);
1030 this.thumbEl.setStyle({
1031 width : width + 'px',
1032 height : height + 'px'
1039 setThumbBoxPosition : function()
1041 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
1042 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
1044 this.thumbEl.setLeft(x);
1045 this.thumbEl.setTop(y);
1049 baseRotateLevel : function()
1051 this.baseRotate = 1;
1054 typeof(this.exif) != 'undefined' &&
1055 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
1056 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
1058 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
1061 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
1065 baseScaleLevel : function()
1069 if(this.isDocument){
1071 if(this.baseRotate == 6 || this.baseRotate == 8){
1073 height = this.thumbEl.getHeight();
1074 this.baseScale = height / this.imageEl.OriginWidth;
1076 if(this.imageEl.OriginHeight * this.baseScale > this.thumbEl.getWidth()){
1077 width = this.thumbEl.getWidth();
1078 this.baseScale = width / this.imageEl.OriginHeight;
1084 height = this.thumbEl.getHeight();
1085 this.baseScale = height / this.imageEl.OriginHeight;
1087 if(this.imageEl.OriginWidth * this.baseScale > this.thumbEl.getWidth()){
1088 width = this.thumbEl.getWidth();
1089 this.baseScale = width / this.imageEl.OriginWidth;
1095 if(this.baseRotate == 6 || this.baseRotate == 8){
1097 width = this.thumbEl.getHeight();
1098 this.baseScale = width / this.imageEl.OriginHeight;
1100 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
1101 height = this.thumbEl.getWidth();
1102 this.baseScale = height / this.imageEl.OriginHeight;
1105 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1106 height = this.thumbEl.getWidth();
1107 this.baseScale = height / this.imageEl.OriginHeight;
1109 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
1110 width = this.thumbEl.getHeight();
1111 this.baseScale = width / this.imageEl.OriginWidth;
1118 width = this.thumbEl.getWidth();
1119 this.baseScale = width / this.imageEl.OriginWidth;
1121 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
1122 height = this.thumbEl.getHeight();
1123 this.baseScale = height / this.imageEl.OriginHeight;
1126 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
1128 height = this.thumbEl.getHeight();
1129 this.baseScale = height / this.imageEl.OriginHeight;
1131 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
1132 width = this.thumbEl.getWidth();
1133 this.baseScale = width / this.imageEl.OriginWidth;
1141 getScaleLevel : function()
1143 return this.baseScale * Math.pow(1.1, this.scale);
1146 onTouchStart : function(e)
1148 if(!this.canvasLoaded){
1149 this.beforeSelectFile(e);
1153 var touches = e.browserEvent.touches;
1159 if(touches.length == 1){
1160 this.onMouseDown(e);
1164 if(touches.length != 2){
1170 for(var i = 0, finger; finger = touches[i]; i++){
1171 coords.push(finger.pageX, finger.pageY);
1174 var x = Math.pow(coords[0] - coords[2], 2);
1175 var y = Math.pow(coords[1] - coords[3], 2);
1177 this.startDistance = Math.sqrt(x + y);
1179 this.startScale = this.scale;
1181 this.pinching = true;
1182 this.dragable = false;
1186 onTouchMove : function(e)
1188 if(!this.pinching && !this.dragable){
1192 var touches = e.browserEvent.touches;
1199 this.onMouseMove(e);
1205 for(var i = 0, finger; finger = touches[i]; i++){
1206 coords.push(finger.pageX, finger.pageY);
1209 var x = Math.pow(coords[0] - coords[2], 2);
1210 var y = Math.pow(coords[1] - coords[3], 2);
1212 this.endDistance = Math.sqrt(x + y);
1214 this.scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
1216 if(!this.zoomable()){
1217 this.scale = this.startScale;
1225 onTouchEnd : function(e)
1227 this.pinching = false;
1228 this.dragable = false;
1232 process : function(file, crop)
1235 this.maskEl.mask(this.loadingText);
1238 this.xhr = new XMLHttpRequest();
1240 file.xhr = this.xhr;
1242 this.xhr.open(this.method, this.url, true);
1245 "Accept": "application/json",
1246 "Cache-Control": "no-cache",
1247 "X-Requested-With": "XMLHttpRequest"
1250 for (var headerName in headers) {
1251 var headerValue = headers[headerName];
1253 this.xhr.setRequestHeader(headerName, headerValue);
1259 this.xhr.onload = function()
1261 _this.xhrOnLoad(_this.xhr);
1264 this.xhr.onerror = function()
1266 _this.xhrOnError(_this.xhr);
1269 var formData = new FormData();
1271 formData.append('returnHTML', 'NO');
1274 formData.append('crop', crop);
1277 if(typeof(file) != 'undefined' && (typeof(file.id) == 'undefined' || file.id * 1 < 1)){
1278 formData.append(this.paramName, file, file.name);
1281 if(typeof(file.filename) != 'undefined'){
1282 formData.append('filename', file.filename);
1285 if(typeof(file.mimetype) != 'undefined'){
1286 formData.append('mimetype', file.mimetype);
1289 if(this.fireEvent('arrange', this, formData) != false){
1290 this.xhr.send(formData);
1294 xhrOnLoad : function(xhr)
1297 this.maskEl.unmask();
1300 if (xhr.readyState !== 4) {
1301 this.fireEvent('exception', this, xhr);
1305 var response = Roo.decode(xhr.responseText);
1307 if(!response.success){
1308 this.fireEvent('exception', this, xhr);
1312 var response = Roo.decode(xhr.responseText);
1314 this.fireEvent('upload', this, response);
1318 xhrOnError : function()
1321 this.maskEl.unmask();
1324 Roo.log('xhr on error');
1326 var response = Roo.decode(xhr.responseText);
1332 prepare : function(file)
1335 this.maskEl.mask(this.loadingText);
1341 if(typeof(file) === 'string'){
1342 this.loadCanvas(file);
1346 if(!file || !this.urlAPI){
1351 this.cropType = file.type;
1355 if(this.fireEvent('prepare', this, this.file) != false){
1357 var reader = new FileReader();
1359 reader.onload = function (e) {
1360 if (e.target.error) {
1361 Roo.log(e.target.error);
1365 var buffer = e.target.result,
1366 dataView = new DataView(buffer),
1368 maxOffset = dataView.byteLength - 4,
1372 if (dataView.getUint16(0) === 0xffd8) {
1373 while (offset < maxOffset) {
1374 markerBytes = dataView.getUint16(offset);
1376 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
1377 markerLength = dataView.getUint16(offset + 2) + 2;
1378 if (offset + markerLength > dataView.byteLength) {
1379 Roo.log('Invalid meta data: Invalid segment size.');
1383 if(markerBytes == 0xffe1){
1384 _this.parseExifData(
1391 offset += markerLength;
1401 var url = _this.urlAPI.createObjectURL(_this.file);
1403 _this.loadCanvas(url);
1408 reader.readAsArrayBuffer(this.file);
1414 parseExifData : function(dataView, offset, length)
1416 var tiffOffset = offset + 10,
1420 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1421 // No Exif data, might be XMP data instead
1425 // Check for the ASCII code for "Exif" (0x45786966):
1426 if (dataView.getUint32(offset + 4) !== 0x45786966) {
1427 // No Exif data, might be XMP data instead
1430 if (tiffOffset + 8 > dataView.byteLength) {
1431 Roo.log('Invalid Exif data: Invalid segment size.');
1434 // Check for the two null bytes:
1435 if (dataView.getUint16(offset + 8) !== 0x0000) {
1436 Roo.log('Invalid Exif data: Missing byte alignment offset.');
1439 // Check the byte alignment:
1440 switch (dataView.getUint16(tiffOffset)) {
1442 littleEndian = true;
1445 littleEndian = false;
1448 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
1451 // Check for the TIFF tag marker (0x002A):
1452 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
1453 Roo.log('Invalid Exif data: Missing TIFF marker.');
1456 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
1457 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1462 tiffOffset + dirOffset,
1467 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
1472 if (dirOffset + 6 > dataView.byteLength) {
1473 Roo.log('Invalid Exif data: Invalid directory offset.');
1476 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
1477 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
1478 if (dirEndOffset + 4 > dataView.byteLength) {
1479 Roo.log('Invalid Exif data: Invalid directory size.');
1482 for (i = 0; i < tagsNumber; i += 1) {
1486 dirOffset + 2 + 12 * i, // tag offset
1490 // Return the offset to the next directory:
1491 return dataView.getUint32(dirEndOffset, littleEndian);
1494 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
1496 var tag = dataView.getUint16(offset, littleEndian);
1498 this.exif[tag] = this.getExifValue(
1502 dataView.getUint16(offset + 2, littleEndian), // tag type
1503 dataView.getUint32(offset + 4, littleEndian), // tag length
1508 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
1510 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
1519 Roo.log('Invalid Exif data: Invalid tag type.');
1523 tagSize = tagType.size * length;
1524 // Determine if the value is contained in the dataOffset bytes,
1525 // or if the value at the dataOffset is a pointer to the actual data:
1526 dataOffset = tagSize > 4 ?
1527 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
1528 if (dataOffset + tagSize > dataView.byteLength) {
1529 Roo.log('Invalid Exif data: Invalid data offset.');
1533 return tagType.getValue(dataView, dataOffset, littleEndian);
1536 for (i = 0; i < length; i += 1) {
1537 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
1540 if (tagType.ascii) {
1542 // Concatenate the chars:
1543 for (i = 0; i < values.length; i += 1) {
1545 // Ignore the terminating NULL byte(s):
1546 if (c === '\u0000') {
1558 Roo.apply(Roo.bootstrap.UploadCropbox, {
1560 'Orientation': 0x0112
1566 3: 180, //'bottom-right',
1567 // 4: 'bottom-left',
1569 6: 90, //'right-top',
1570 // 7: 'right-bottom',
1571 8: 270 //'left-bottom'
1575 // byte, 8-bit unsigned int:
1577 getValue: function (dataView, dataOffset) {
1578 return dataView.getUint8(dataOffset);
1582 // ascii, 8-bit byte:
1584 getValue: function (dataView, dataOffset) {
1585 return String.fromCharCode(dataView.getUint8(dataOffset));
1590 // short, 16 bit int:
1592 getValue: function (dataView, dataOffset, littleEndian) {
1593 return dataView.getUint16(dataOffset, littleEndian);
1597 // long, 32 bit int:
1599 getValue: function (dataView, dataOffset, littleEndian) {
1600 return dataView.getUint32(dataOffset, littleEndian);
1604 // rational = two long values, first is numerator, second is denominator:
1606 getValue: function (dataView, dataOffset, littleEndian) {
1607 return dataView.getUint32(dataOffset, littleEndian) /
1608 dataView.getUint32(dataOffset + 4, littleEndian);
1612 // slong, 32 bit signed int:
1614 getValue: function (dataView, dataOffset, littleEndian) {
1615 return dataView.getInt32(dataOffset, littleEndian);
1619 // srational, two slongs, first is numerator, second is denominator:
1621 getValue: function (dataView, dataOffset, littleEndian) {
1622 return dataView.getInt32(dataOffset, littleEndian) /
1623 dataView.getInt32(dataOffset + 4, littleEndian);
1633 cls : 'btn-group roo-upload-cropbox-rotate-left',
1634 action : 'rotate-left',
1638 cls : 'btn btn-default',
1639 html : '<i class="fa fa-undo"></i>'
1645 cls : 'btn-group roo-upload-cropbox-picture',
1650 cls : 'btn btn-default',
1651 html : '<i class="fa fa-picture-o"></i>'
1657 cls : 'btn-group roo-upload-cropbox-rotate-right',
1658 action : 'rotate-right',
1662 cls : 'btn btn-default',
1663 html : '<i class="fa fa-repeat"></i>'
1671 cls : 'btn-group roo-upload-cropbox-rotate-left',
1672 action : 'rotate-left',
1676 cls : 'btn btn-default',
1677 html : '<i class="fa fa-undo"></i>'
1683 cls : 'btn-group roo-upload-cropbox-download',
1684 action : 'download',
1688 cls : 'btn btn-default',
1689 html : '<i class="fa fa-download"></i>'
1695 cls : 'btn-group roo-upload-cropbox-crop',
1700 cls : 'btn btn-default',
1701 html : '<i class="fa fa-crop"></i>'
1707 cls : 'btn-group roo-upload-cropbox-trash',
1712 cls : 'btn btn-default',
1713 html : '<i class="fa fa-trash"></i>'
1719 cls : 'btn-group roo-upload-cropbox-rotate-right',
1720 action : 'rotate-right',
1724 cls : 'btn btn-default',
1725 html : '<i class="fa fa-repeat"></i>'
1733 cls : 'btn-group roo-upload-cropbox-rotate-left',
1734 action : 'rotate-left',
1738 cls : 'btn btn-default',
1739 html : '<i class="fa fa-undo"></i>'
1745 cls : 'btn-group roo-upload-cropbox-rotate-right',
1746 action : 'rotate-right',
1750 cls : 'btn btn-default',
1751 html : '<i class="fa fa-repeat"></i>'