/*! cropit - v0.5.0 */ (function(n,t){typeof exports=="object"&&typeof module=="object"?module.exports=t(require("jquery")):typeof define=="function"&&define.amd?define(["jquery"],t):typeof exports=="object"?exports.cropit=t(require("jquery")):n.cropit=t(n.jQuery)})(this,function(n){return function(n){function t(r){if(i[r])return i[r].exports;var u=i[r]={exports:{},id:r,loaded:!1};return n[r].call(u.exports,u,u.exports,t),u.loaded=!0,u.exports}var i={};return t.m=n,t.c=i,t.p="",t(0)}([function(n,t,i){function o(n){return n&&n.__esModule?n:{"default":n}}var e=Array.prototype.slice,c=i(1),r=o(c),l=i(2),a=o(l),u=i(4),v=i(6),s=function(n,t){return n.each(function(){var n=r["default"].data(this,u.PLUGIN_KEY);n&&t(n)})},h=function(n,t,i){var f=n.first().data(u.PLUGIN_KEY);return!f||!r["default"].isFunction(f[t])?null:f[t](i)},f={init:function(n){return this.each(function(){if(!r["default"].data(this,u.PLUGIN_KEY)){var t=new a["default"](r["default"],this,n);r["default"].data(this,u.PLUGIN_KEY,t)}})},destroy:function(){return this.each(function(){r["default"].removeData(this,u.PLUGIN_KEY)})},isZoomable:function(){return h(this,"isZoomable")},"export":function(n){return h(this,"getCroppedImageData",n)}},y=function(n,t){return s(n,function(n){n[t]()})},p=function(n,t,i){if(v.exists(i))return s(n,function(n){n[t]=i});var r=n.first().data(u.PLUGIN_KEY);return r[t]};r["default"].fn.cropit=function(n){return f[n]?f[n].apply(this,Array.prototype.slice.call(arguments,1)):["imageState","imageSrc","offset","previewSize","imageSize","zoom","initialZoom","exportZoom","minZoom","maxZoom"].indexOf(n)>=0?p.apply(undefined,[this].concat(e.call(arguments))):["rotateCW","rotateCCW","disable","reenable"].indexOf(n)>=0?y.apply(undefined,[this].concat(e.call(arguments))):f.init.apply(this,arguments)}},function(t){t.exports=n},function(n,t,i){function o(n){return n&&n.__esModule?n:{"default":n}}function s(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function");}var e;Object.defineProperty(t,"__esModule",{value:!0});e=function(){function n(n,t){for(var i,r=0;r").addClass(r.CLASS_NAMES.PREVIEW_IMAGE).attr("alt","").css({transformOrigin:"top left",webkitTransformOrigin:"top left",willChange:"transform"});this.$imageContainer=u["default"]("
").addClass(r.CLASS_NAMES.PREVIEW_IMAGE_CONTAINER).css({position:"absolute",overflow:"hidden",left:0,top:0,width:"100%",height:"100%"}).append(this.$image);this.$preview.append(this.$imageContainer);this.options.imageBackground&&(this.bgBorderWidthArray=u["default"].isArray(this.options.imageBackgroundBorderWidth)?this.options.imageBackgroundBorderWidth:[0,1,2,3].map(function(){return n.options.imageBackgroundBorderWidth}),this.$bg=u["default"]("").addClass(r.CLASS_NAMES.PREVIEW_BACKGROUND).attr("alt","").css({position:"relative",left:this.bgBorderWidthArray[3],top:this.bgBorderWidthArray[0],transformOrigin:"top left",webkitTransformOrigin:"top left",willChange:"transform"}),this.$bgContainer=u["default"]("
").addClass(r.CLASS_NAMES.PREVIEW_BACKGROUND_CONTAINER).css({position:"absolute",zIndex:0,top:-this.bgBorderWidthArray[0],right:-this.bgBorderWidthArray[1],bottom:-this.bgBorderWidthArray[2],left:-this.bgBorderWidthArray[3]}).append(this.$bg),this.bgBorderWidthArray[0]>0&&this.$bgContainer.css("overflow","hidden"),this.$preview.prepend(this.$bgContainer));this.initialZoom=this.options.initialZoom;this.imageLoaded=!1;this.moveContinue=!1;this.zoomer=new l["default"];this.options.allowDragNDrop&&u["default"].event.addProp("dataTransfer");this.bindListeners();this.options.imageState&&this.options.imageState.src&&this.loadImage(this.options.imageState.src)}},{key:"bindListeners",value:function(){this.$fileInput.on("change.cropit",this.onFileChange.bind(this));this.$imageContainer.on(r.EVENTS.PREVIEW,this.onPreviewEvent.bind(this));this.$zoomSlider.on(r.EVENTS.ZOOM_INPUT,this.onZoomSliderChange.bind(this));if(this.options.allowDragNDrop){this.$imageContainer.on("dragover.cropit dragleave.cropit",this.onDragOver.bind(this));this.$imageContainer.on("drop.cropit",this.onDrop.bind(this))}}},{key:"unbindListeners",value:function(){this.$fileInput.off("change.cropit");this.$imageContainer.off(r.EVENTS.PREVIEW);this.$imageContainer.off("dragover.cropit dragleave.cropit drop.cropit");this.$zoomSlider.off(r.EVENTS.ZOOM_INPUT)}},{key:"onFileChange",value:function(n){this.options.onFileChange(n);this.$fileInput.get(0).files&&this.loadFile(this.$fileInput.get(0).files[0])}},{key:"loadFile",value:function(n){var t=new FileReader;if(n&&n.type.match("image")){t.readAsDataURL(n);t.onload=this.onFileReaderLoaded.bind(this);t.onerror=this.onFileReaderError.bind(this);this.onFileDropped(n.name)}else n&&this.onFileReaderError()}},{key:"onFileReaderLoaded",value:function(n){this.loadImage(n.target.result)}},{key:"onFileReaderError",value:function(){this.options.onFileReaderError()}},{key:"onFileDropped",value:function(n){this.options.onFileDropped(n)}},{key:"onDragOver",value:function(n){n.preventDefault();n.dataTransfer.dropEffect="copy";this.$preview.toggleClass(r.CLASS_NAMES.DRAG_HOVERED,n.type==="dragover")}},{key:"onDrop",value:function(n){var i=this,t;n.preventDefault();n.stopPropagation();t=Array.prototype.slice.call(n.dataTransfer.files,0);t.some(function(n){return n.type.match("image")?(i.loadFile(n),!0):!1});this.$preview.removeClass(r.CLASS_NAMES.DRAG_HOVERED)}},{key:"loadImage",value:function(n){var i=this,t;n&&(this.options.onImageLoading(),this.setImageLoadingClass(),n.indexOf("data")===0?this.preImage.src=n:(t=new XMLHttpRequest,t.onload=function(n){if(n.target.status>=300){i.onImageError.call(i,r.ERRORS.IMAGE_FAILED_TO_LOAD);return}i.loadFile(n.target.response)},t.open("GET",n),t.responseType="blob",t.send()))}},{key:"onPreImageLoaded",value:function(){if(this.shouldRejectImage({imageWidth:this.preImage.width,imageHeight:this.preImage.height,previewSize:this.previewSize,maxZoom:this.options.maxZoom,exportZoom:this.options.exportZoom,smallImage:this.options.smallImage})){this.onImageError(r.ERRORS.SMALL_IMAGE);this.image.src&&this.setImageLoadedClass();return}this.image.src=this.preImage.src}},{key:"onImageLoaded",value:function(){this.rotation=0;this.setupZoomer(this.options.imageState&&this.options.imageState.zoom||this._initialZoom);this.options.imageState&&this.options.imageState.offset?this.offset=this.options.imageState.offset:this.centerImage();this.options.imageState={};this.$image.attr("src",this.image.src);this.options.imageBackground&&this.$bg.attr("src",this.image.src);this.setImageLoadedClass();this.imageLoaded=!0;this.options.onImageLoaded()}},{key:"onImageError",value:function(){this.options.onImageError.apply(this,arguments);this.removeImageLoadingClass()}},{key:"setImageLoadingClass",value:function(){this.$preview.removeClass(r.CLASS_NAMES.IMAGE_LOADED).addClass(r.CLASS_NAMES.IMAGE_LOADING)}},{key:"setImageLoadedClass",value:function(){this.$preview.removeClass(r.CLASS_NAMES.IMAGE_LOADING).addClass(r.CLASS_NAMES.IMAGE_LOADED)}},{key:"removeImageLoadingClass",value:function(){this.$preview.removeClass(r.CLASS_NAMES.IMAGE_LOADING)}},{key:"getEventPosition",value:function(n){return n.originalEvent&&n.originalEvent.touches&&n.originalEvent.touches[0]&&(n=n.originalEvent.touches[0]),n.clientX&&n.clientY?{x:n.clientX,y:n.clientY}:void 0}},{key:"onPreviewEvent",value:function(n){if(this.imageLoaded){if(this.moveContinue=!1,this.$imageContainer.off(r.EVENTS.PREVIEW_MOVE),n.type==="mousedown"||n.type==="touchstart"){this.origin=this.getEventPosition(n);this.moveContinue=!0;this.$imageContainer.on(r.EVENTS.PREVIEW_MOVE,this.onMove.bind(this))}else u["default"](document.body).focus();return n.stopPropagation(),!1}}},{key:"onMove",value:function(n){var t=this.getEventPosition(n);return this.moveContinue&&t&&(this.offset={x:this.offset.x+t.x-this.origin.x,y:this.offset.y+t.y-this.origin.y}),this.origin=t,n.stopPropagation(),!1}},{key:"fixOffset",value:function(n){if(!this.imageLoaded)return n;var t={x:n.x,y:n.y};return this.options.freeMove||(t.x=this.imageWidth*this.zoom>=this.previewSize.width?Math.min(0,Math.max(t.x,this.previewSize.width-this.imageWidth*this.zoom)):Math.max(0,Math.min(t.x,this.previewSize.width-this.imageWidth*this.zoom)),t.y=this.imageHeight*this.zoom>=this.previewSize.height?Math.min(0,Math.max(t.y,this.previewSize.height-this.imageHeight*this.zoom)):Math.max(0,Math.min(t.y,this.previewSize.height-this.imageHeight*this.zoom))),t.x=f.round(t.x),t.y=f.round(t.y),t}},{key:"centerImage",value:function(){this.image.width&&this.image.height&&this.zoom&&(this.offset={x:(this.previewSize.width-this.imageWidth*this.zoom)/2,y:(this.previewSize.height-this.imageHeight*this.zoom)/2})}},{key:"onZoomSliderChange",value:function(){if(this.imageLoaded){this.zoomSliderPos=Number(this.$zoomSlider.val());var n=this.zoomer.getZoom(this.zoomSliderPos);n!==this.zoom&&(this.zoom=n)}}},{key:"enableZoomSlider",value:function(){this.$zoomSlider.removeAttr("disabled");this.options.onZoomEnabled()}},{key:"disableZoomSlider",value:function(){this.$zoomSlider.attr("disabled",!0);this.options.onZoomDisabled()}},{key:"setupZoomer",value:function(n){this.zoomer.setup({imageSize:this.imageSize,previewSize:this.previewSize,exportZoom:this.options.exportZoom,maxZoom:this.options.maxZoom,minZoom:this.options.minZoom,smallImage:this.options.smallImage});this.zoom=f.exists(n)?n:this._zoom;this.isZoomable()?this.enableZoomSlider():this.disableZoomSlider()}},{key:"fixZoom",value:function(n){return this.zoomer.fixZoom(n)}},{key:"isZoomable",value:function(){return this.zoomer.isZoomable()}},{key:"renderImage",value:function(){var n="\n translate("+this.rotatedOffset.x+"px, "+this.rotatedOffset.y+"px)\n scale("+this.zoom+")\n rotate("+this.rotation+"deg)";this.$image.css({transform:n,webkitTransform:n});this.options.imageBackground&&this.$bg.css({transform:n,webkitTransform:n})}},{key:"rotateCW",value:function(){this.rotation=this.shouldRejectImage({imageWidth:this.image.height,imageHeight:this.image.width,previewSize:this.previewSize,maxZoom:this.options.maxZoom,exportZoom:this.options.exportZoom,smallImage:this.options.smallImage})?(this.rotation+180)%360:(this.rotation+90)%360}},{key:"rotateCCW",value:function(){this.rotation=this.shouldRejectImage({imageWidth:this.image.height,imageHeight:this.image.width,previewSize:this.previewSize,maxZoom:this.options.maxZoom,exportZoom:this.options.exportZoom,smallImage:this.options.smallImage})?(this.rotation+180)%360:(this.rotation+270)%360}},{key:"shouldRejectImage",value:function(n){var u=n.imageWidth,f=n.imageHeight,t=n.previewSize,i=n.maxZoom,r=n.exportZoom,e=n.smallImage;return e!=="reject"?!1:u*i").attr({width:this.previewSize.width*t,height:this.previewSize.height*t}).get(0),i=r.getContext("2d");return n.type==="image/jpeg"&&(i.fillStyle=n.fillBg,i.fillRect(0,0,r.width,r.height)),i.translate(this.rotatedOffset.x*t,this.rotatedOffset.y*t),i.rotate(this.rotation*Math.PI/180),i.drawImage(this.image,0,0,e.width,e.height),r.toDataURL(n.type,n.quality)}}},{key:"disable",value:function(){this.unbindListeners();this.disableZoomSlider();this.$el.addClass(r.CLASS_NAMES.DISABLED)}},{key:"reenable",value:function(){this.bindListeners();this.enableZoomSlider();this.$el.removeClass(r.CLASS_NAMES.DISABLED)}},{key:"$",value:function(n){return this.$el?this.$el.find(n):null}},{key:"offset",set:function(n){if(n&&f.exists(n.x)&&f.exists(n.y)){this._offset=this.fixOffset(n);this.renderImage();this.options.onOffsetChange(n)}},get:function(){return this._offset}},{key:"zoom",set:function(n){if(n=this.fixZoom(n),this.imageLoaded){var t=this.zoom,i=this.previewSize.width/2-(this.previewSize.width/2-this.offset.x)*n/t,r=this.previewSize.height/2-(this.previewSize.height/2-this.offset.y)*n/t;this._zoom=n;this.offset={x:i,y:r}}else this._zoom=n;this.zoomSliderPos=this.zoomer.getSliderPos(this.zoom);this.$zoomSlider.val(this.zoomSliderPos);this.options.onZoomChange(n)},get:function(){return this._zoom}},{key:"rotatedOffset",get:function(){return{x:this.offset.x+(this.rotation===90?this.image.height*this.zoom:0)+(this.rotation===180?this.image.width*this.zoom:0),y:this.offset.y+(this.rotation===180?this.image.height*this.zoom:0)+(this.rotation===270?this.image.width*this.zoom:0)}}},{key:"rotation",set:function(n){this._rotation=n;this.imageLoaded&&this.setupZoomer()},get:function(){return this._rotation}},{key:"imageState",get:function(){return{src:this.image.src,offset:this.offset,zoom:this.zoom}}},{key:"imageSrc",get:function(){return this.image.src},set:function(n){this.loadImage(n)}},{key:"imageWidth",get:function(){return this.rotation%180==0?this.image.width:this.image.height}},{key:"imageHeight",get:function(){return this.rotation%180==0?this.image.height:this.image.width}},{key:"imageSize",get:function(){return{width:this.imageWidth,height:this.imageHeight}}},{key:"initialZoom",get:function(){return this.options.initialZoom},set:function(n){this.options.initialZoom=n;this._initialZoom=n==="min"?0:n==="image"?1:0}},{key:"exportZoom",get:function(){return this.options.exportZoom},set:function(n){this.options.exportZoom=n;this.setupZoomer()}},{key:"minZoom",get:function(){return this.options.minZoom},set:function(n){this.options.minZoom=n;this.setupZoomer()}},{key:"maxZoom",get:function(){return this.options.maxZoom},set:function(n){this.options.maxZoom=n;this.setupZoomer()}},{key:"previewSize",get:function(){return this._previewSize},set:function(n){!n||n.width<=0||n.height<=0||(this._previewSize={width:n.width,height:n.height},this.$preview.css({width:this.previewSize.width,height:this.previewSize.height}),this.imageLoaded&&this.setupZoomer())}}]),n}();t["default"]=v;n.exports=t["default"]},function(n,t){function u(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function");}var i,r;Object.defineProperty(t,"__esModule",{value:!0});i=function(){function n(n,t){for(var i,r=0;r