1 line
26 KiB
JavaScript
1 line
26 KiB
JavaScript
|
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else if(typeof exports==="object"){factory(require("jquery"))}else{factory(jQuery)}})(function($){"use strict";var $window=$(window),$document=$(document),location=window.location,TRUE=true,FALSE=false,NULL=null,NAN=NaN,INFINITY=Infinity,STRING_UNDEFINED="undefined",STRING_DIRECTIVE="directive",CROPPER_NAMESPACE=".cropper",REGEXP_DIRECTIVES=/^(e|n|w|s|ne|nw|sw|se|all|crop|move|zoom)$/,REGEXP_OPTIONS=/^(x|y|width|height)$/,REGEXP_PROPERTIES=/^(naturalWidth|naturalHeight|width|height|aspectRatio|ratio|rotate)$/,CLASS_MODAL="cropper-modal",CLASS_HIDDEN="cropper-hidden",CLASS_INVISIBLE="cropper-invisible",CLASS_MOVE="cropper-move",CLASS_CROP="cropper-crop",CLASS_DISABLED="cropper-disabled",EVENT_MOUSE_DOWN="mousedown touchstart",EVENT_MOUSE_MOVE="mousemove touchmove",EVENT_MOUSE_UP="mouseup mouseleave touchend touchleave touchcancel",EVENT_WHEEL="wheel mousewheel DOMMouseScroll",EVENT_RESIZE="resize"+CROPPER_NAMESPACE,EVENT_DBLCLICK="dblclick",EVENT_BUILD="build"+CROPPER_NAMESPACE,EVENT_BUILT="built"+CROPPER_NAMESPACE,EVENT_DRAG_START="dragstart"+CROPPER_NAMESPACE,EVENT_DRAG_MOVE="dragmove"+CROPPER_NAMESPACE,EVENT_DRAG_END="dragend"+CROPPER_NAMESPACE,isNumber=function(n){return typeof n==="number"},isFunction=function(obj){return typeof obj==="function"&&typeof obj.nodeType!=="number"&&typeof obj.item!=="function"},toArray=function(obj,offset){var args=[];if(isNumber(offset)){args.push(offset)}return args.slice.apply(obj,args)},proxy=function(fn,context){var args=toArray(arguments,2);return function(){return fn.apply(context,args.concat(toArray(arguments)))}},addTimestamp=function(url){var timestamp="timestamp="+(new Date).getTime();return url+(url.indexOf("?")===-1?"?":"&")+timestamp},Cropper=function(element,options){this.element=element;this.$element=$(element);this.defaults=$.extend({},Cropper.DEFAULTS,$.isPlainObject(options)?options:{});this.$original=NULL;this.ready=FALSE;this.built=FALSE;this.cropped=FALSE;this.rotated=FALSE;this.disabled=FALSE;this.replaced=FALSE;this.init()},sqrt=Math.sqrt,min=Math.min,max=Math.max,abs=Math.abs,sin=Math.sin,cos=Math.cos,num=parseFloat;Cropper.prototype={constructor:Cropper,support:{canvas:isFunction($("<canvas>")[0].getContext)},init:function(){var defaults=this.defaults;$.each(defaults,function(i,n){switch(i){case"aspectRatio":defaults[i]=abs(num(n))||NAN;break;case"autoCropArea":defaults[i]=abs(num(n))||.8;break;case"minWidth":case"minHeight":defaults[i]=abs(num(n))||0;break;case"maxWidth":case"maxHeight":defaults[i]=abs(num(n))||INFINITY;break}});this.image={rotate:0};this.load()},load:function(){var _this=this,$this=this.$element,element=this.element,image=this.image,crossOrigin="",$clone,url;if($this.is("img")){url=$this.prop("src")}else if($this.is("canvas")&&this.support.canvas){url=element.toDataURL()}if(!url){return}if(this.replaced){image.rotate=0}if(this.defaults.checkImageOrigin&&this.isCrossOriginURL(url)){crossOrigin=" crossOrigin";url=addTimestamp(url)}this.$clone=$clone=$("<img"+crossOrigin+' src="'+url+'">');$clone.one("load",function(){image.naturalWidth=this.naturalWidth||$clone.width();image.naturalHeight=this.naturalHeight||$clone.height();image.aspectRatio=image.naturalWidth/image.naturalHeight;_this.url=url;_this.ready=TRUE;_this.build()});$clone.addClass(CLASS_INVISIBLE).prependTo("body")},isCrossOriginURL:function(url){var parts=url.match(/^(https?:)\/\/([^\:\/\?#]+):?(\d*)/i);if(parts&&(parts[1]!==location.protocol||parts[2]!==location.hostname||parts[3]!==location.port)){return TRUE}return FALSE},build:function(){var $this=this.$element,defaults=this.defaults,buildEvent,$cropper;if(!this.ready){return}if(this.built){this.unbuild()}$this.one(EVENT_BUILD,defaults.build);buildEvent=$.Event(EVENT_BUILD);$this.trigger(buildEvent);if(buildEvent.isDefaultPrevented()){return}this.$cropper=$cropper=$(Cropper.TEMPLATE);$this.addClass(CLASS_HIDDEN);this.$clone.removeClass(CLASS_INVISIBLE).prependTo($cropper);if(!this.rotated){this.$original=this.$clone.clone();this.$original.ad
|