7 * @class Roo.bootstrap.UploadCropbox
8 * @extends Roo.bootstrap.Component
9 * Bootstrap UploadCropbox class
10 * @cfg {String} emptyText show when image has been loaded
13 * Create a new UploadCropbox
14 * @param {Object} config The config object
17 Roo.bootstrap.UploadCropbox = function(config){
18 Roo.bootstrap.UploadCropbox.superclass.constructor.call(this, config);
22 * @event beforeSelectFile
23 * Fire before select file
24 * @param {Roo.EventObject} e
26 "beforeSelectFile" : true
31 Roo.extend(Roo.bootstrap.UploadCropbox, Roo.bootstrap.Component, {
33 emptyText : 'Click to upload image',
40 getAutoCreate : function()
44 cls : 'roo-upload-cropbox',
48 cls : 'roo-upload-cropbox-image-section',
52 cls : 'roo-upload-cropbox-canvas',
56 cls : 'roo-upload-cropbox-image'
62 cls : 'roo-upload-cropbox-thumb'
68 cls : 'roo-upload-cropbox-footer-section',
71 cls : 'btn-group btn-group-justified roo-upload-cropbox-btn-group',
79 cls : 'btn btn-default roo-upload-cropbox-rotate-left',
80 html : '<i class="fa fa-undo"></i>'
90 cls : 'btn btn-default roo-upload-cropbox-picture',
91 html : '<i class="fa fa-picture-o"></i>'
101 cls : 'btn btn-default roo-upload-cropbox-rotate-right',
102 html : '<i class="fa fa-repeat"></i>'
115 initEvents : function()
117 this.imageSection = this.el.select('.roo-upload-cropbox-image-section', true).first();
118 this.imageSection.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
120 this.imageCanvas = this.el.select('.roo-upload-cropbox-canvas', true).first();
121 this.imageCanvas.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
123 this.image = this.el.select('.roo-upload-cropbox-image', true).first();
124 this.image.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
126 this.thumb = this.el.select('.roo-upload-cropbox-thumb', true).first();
127 this.thumb.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
129 this.footerSection = this.el.select('.roo-upload-cropbox-footer-section', true).first();
130 this.footerSection.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
132 this.rotateLeft = this.el.select('.roo-upload-cropbox-rotate-left', true).first();
133 this.rotateLeft.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
135 this.pictureBtn = this.el.select('.roo-upload-cropbox-picture', true).first();
136 this.pictureBtn.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
138 this.rotateRight = this.el.select('.roo-upload-cropbox-rotate-right', true).first();
139 this.rotateRight.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
146 this.image.on('load', this.onLoadCanvasImage, this);
148 this.imageSection.on('click', this.beforeSelectFile, this);
150 this.imageSection.on('mousedown', this.onMouseDown, this);
152 this.imageSection.on('mousemove', this.onMouseMove, this);
154 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
156 this.imageSection.on(mousewheel, this.onMouseWheel, this);
158 Roo.get(document).on('mouseup', this.onMouseUp, this);
160 this.pictureBtn.on('click', this.beforeSelectFile, this);
162 this.rotateLeft.on('click', this.onRotateLeft, this);
164 this.rotateRight.on('click', this.onRotateRight, this);
170 this.image.un('load', this.onLoadCanvasImage, this);
172 this.imageSection.un('click', this.beforeSelectFile, this);
174 this.imageSection.un('mousedown', this.onMouseDown, this);
176 this.imageSection.un('mousemove', this.onMouseMove, this);
178 var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
180 this.imageSection.un(mousewheel, this.onMouseWheel, this);
182 Roo.get(document).un('mouseup', this.onMouseUp, this);
184 this.pictureBtn.un('click', this.beforeSelectFile, this);
186 this.rotateLeft.un('click', this.onRotateLeft, this);
188 this.rotateRight.un('click', this.onRotateRight, this);
195 this.dragable = false;
200 beforeSelectFile : function(e)
203 this.fireEvent('beforeSelectFile', this);
206 loadCanvasImage : function(src)
208 this.imageSection.un('click', this.beforeSelectFile, this);
214 this.image.attr('src', src);
218 onLoadCanvasImage : function(src)
222 this.image.OriginWidth = this.image.getWidth();
223 this.image.OriginHeight = this.image.getHeight();
225 this.setCanvasPosition();
228 setCanvasPosition : function()
230 var pw = (this.imageSection.getWidth(true) - this.image.getWidth()) / 2;
231 var ph = (this.imageSection.getHeight(true) - this.image.getHeight()) / 2;
233 this.imageCanvas.setLeft(pw);
234 this.imageCanvas.setTop(ph);
237 onMouseDown : function(e)
241 this.dragable = true;
242 this.mouseX = e.getPageX();
243 this.mouseY = e.getPageY();
247 onMouseMove : function(e)
255 var transform = new WebKitCSSMatrix(window.getComputedStyle(this.thumb.dom).webkitTransform);
257 var minX = this.thumb.getLeft(true) + transform.m41;
258 var minY = this.thumb.getTop(true) + transform.m42;
260 var maxX = minX + this.thumb.getWidth() - this.image.getWidth();
261 var maxY = minY + this.thumb.getHeight() - this.image.getHeight();
263 if(this.rotate == 90 || this.rotate == 270){
264 minX = this.thumb.getLeft(true) + transform.m41 - (this.image.getWidth() - this.image.getHeight()) / 2;
265 minY = this.thumb.getTop(true) + transform.m42 + (this.image.getWidth() - this.image.getHeight()) / 2;
267 maxX = minX + this.thumb.getWidth() - this.image.getHeight();
268 maxY = minY + this.thumb.getHeight() - this.image.getWidth();
271 var x = e.getPageX() - this.mouseX;
272 var y = e.getPageY() - this.mouseY;
274 var bgX = x + this.imageCanvas.getLeft(true);
275 var bgY = y + this.imageCanvas.getTop(true);
277 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
278 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
280 this.imageCanvas.setLeft(bgX);
281 this.imageCanvas.setTop(bgY);
283 this.mouseX = e.getPageX();
284 this.mouseY = e.getPageY();
287 onMouseUp : function(e)
291 this.dragable = false;
294 onMouseWheel : function(e)
298 var scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
300 var width = this.image.OriginWidth * Math.pow(1.1, scale);
301 var height = this.image.OriginHeight * Math.pow(1.1, scale)
304 e.getWheelDelta() == -1 &&
306 width < this.thumb.getWidth() ||
307 height < this.thumb.getHeight()
315 this.image.setWidth(width);
316 this.image.setHeight(height);
318 this.setCanvasPosition();
322 onRotateLeft : function(e)
326 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
328 this.imageCanvas.setStyle({
329 '-ms-transform' : 'rotate(' + this.rotate + 'deg)',
330 '-webkit-transform' : 'rotate(' + this.rotate + 'deg)',
331 'transform' : 'rotate(' + this.rotate + 'deg)',
334 this.setCanvasPosition();
337 onRotateRight : function(e)
341 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
343 this.imageCanvas.setStyle({
344 '-ms-transform' : 'rotate(' + this.rotate + 'deg)',
345 '-webkit-transform' : 'rotate(' + this.rotate + 'deg)',
346 'transform' : 'rotate(' + this.rotate + 'deg)',
349 this.setCanvasPosition();