www.gusucode.com > OpenSNS PHP开源社区交友系统 v2.8.1源码程序 > os/Public/zui/lib/imgcutter/zui.imgcutter.min.js

    /*!
 * ZUI - v1.2.0 - 2014-11-18
 * http://zui.sexy
 * GitHub: https://github.com/easysoft/zui.git 
 * Copyright (c) 2014 cnezsoft.com; Licensed MIT
 */

!function(a,b){"use strict";var c=function(b,c){this.$=a(b),this.initOptions(c),this.init()};c.DEFAULTS={coverColor:"#000",coverOpacity:.6,fixedRatio:!1,defaultWidth:128,defaultHeight:128,minWidth:48,minHeight:48},c.prototype.callEvent=function(b,c){return a.callEvent(this.options[b],c)},c.prototype.initOptions=function(b){this.options=a.extend({},c.DEFAULTS,this.$.data(),b),this.options.coverOpacityIE=100*this.options.coverOpacity,this.clipWidth=this.options.defaultWidth,this.clipHeight=this.options.defaultHeight},c.prototype.init=function(){this.initDom(),this.initSize(),this.bindEvents()},c.prototype.initDom=function(){this.$canvas=this.$.children(".canvas"),this.$img=this.$canvas.children("img"),this.$actions=this.$.children(".actions"),this.$btn=this.$.find(".img-cutter-submit"),this.$preview=this.$.find(".img-cutter-preview"),this.options.img=this.$img.attr("src"),this.$canvas.append('<div class="cover" style="background: {coverColor}; opacity: {coverOpacity}; filter:alpha(opacity={coverOpacityIE});"></div><div class="controller" style="width: {defaultWidth}px; height: {defaultHeight}px"><div class="control" data-direction="top"></div><div class="control" data-direction="right"></div><div class="control" data-direction="bottom"></div><div class="control" data-direction="left"></div><div class="control" data-direction="top-left"></div><div class="control" data-direction="top-right"></div><div class="control" data-direction="bottom-left"></div><div class="control" data-direction="bottom-right"></div></div><div class="cliper"><img src="{img}"/></div>'.format(this.options)),this.$cover=this.$canvas.children(".cover"),this.$controller=this.$canvas.children(".controller"),this.$cliper=this.$canvas.children(".cliper"),this.$chipImg=this.$cliper.children("img"),this.options.fixedRatio&&this.$.addClass("fixed-ratio")},c.prototype.initSize=function(){var a=this;"undefined"==typeof a.imgWidth&&window.imgReady(a.options.img,function(){a.imgWidth=this.width,a.imgHeight=this.height,a.callEvent("ready")});var c=setInterval(function(){"undefined"!=typeof a.imgWidth&&(clearInterval(c),a.width=b.min(a.imgWidth,a.$.width()),a.$canvas.css("width",this.width),a.$cliper.css("width",this.width),a.height=a.$canvas.height(),"undefined"==typeof a.left&&(a.left=b.floor((a.width-a.$controller.width())/2),a.top=b.floor((a.height-a.$controller.height())/2)),a.refreshSize())},0)},c.prototype.refreshSize=function(a){var c=this.options;this.clipWidth=b.max(c.minWidth,b.min(this.width,this.clipWidth)),this.clipHeight=b.max(c.minHeight,b.min(this.height,this.clipHeight)),c.fixedRatio&&(a&&"height"===a?(this.clipWidth=b.max(c.minWidth,b.min(this.width,this.clipHeight*c.defaultWidth/c.defaultHeight)),this.clipHeight=this.clipWidth*c.defaultHeight/c.defaultWidth):(this.clipHeight=b.max(c.minHeight,b.min(this.height,this.clipWidth*c.defaultHeight/c.defaultWidth)),this.clipWidth=this.clipHeight*c.defaultWidth/c.defaultHeight)),this.left=b.min(this.width-this.clipWidth,b.max(0,this.left)),this.top=b.min(this.height-this.clipHeight,b.max(0,this.top)),this.right=this.left+this.clipWidth,this.bottom=this.top+this.clipHeight,this.$controller.css({left:this.left,top:this.top,width:this.clipWidth,height:this.clipHeight}),this.$cliper.css("clip","rect({0}px {1}px {2}px {3}px".format(this.top,this.left+this.clipWidth,this.top+this.clipHeight,this.left)),this.callEvent("change",{top:this.top,left:this.left,bottom:this.bottom,right:this.right,width:this.clipWidth,height:this.clipHeight})},c.prototype.bindEvents=function(){var c=this,d=this.options;this.$.resize(a.proxy(this.initSize,this)),this.$btn.hover(function(){c.$.toggleClass("hover")}).click(function(){var b={originWidth:c.imgWidth,originHeight:c.imgHeight,width:c.width,height:c.height,left:c.left,top:c.top,right:c.right,bottom:c.bottom,scaled:c.imgWidth!=c.width||c.imgHeight!=c.height};if(c.callEvent("before",b)){var e=d.post||d.get||d.url||null;null!==e&&a.ajax({type:d.post?"POST":"GET",url:e,data:b}).done(function(a){c.callEvent("done",a)}).fail(function(a){c.callEvent("fail",a)}).always(function(a){c.callEvent("always",a)})}}),this.$controller.draggable({move:!1,container:this.$canvas,drag:function(a){c.left+=a.smallOffset.x,c.top+=a.smallOffset.y,c.refreshSize()}}),this.$controller.children(".control").draggable({move:!1,container:this.$canvas,stopPropagation:!0,drag:function(a){var e=a.element.data("direction"),f=a.smallOffset,g=!1;switch(e){case"left":case"top-left":case"bottom-left":c.left+=f.x,c.left=b.min(c.right-d.minWidth,b.max(0,c.left)),c.clipWidth=c.right-c.left;break;case"right":case"top-right":case"bottom-right":c.clipWidth+=f.x,c.clipWidth=b.min(c.width-c.left,b.max(d.minWidth,c.clipWidth))}switch(e){case"top":case"top-left":case"top-right":c.top+=f.y,c.top=b.min(c.bottom-d.minHeight,b.max(0,c.top)),c.clipHeight=c.bottom-c.top,g=!0;break;case"bottom":case"bottom-left":case"bottom-right":c.clipHeight+=f.y,c.clipHeight=b.min(c.height-c.top,b.max(d.minHeight,c.clipHeight)),g=!0}c.refreshSize(g)}})},a.fn.imgCutter=function(b){return this.each(function(){var d=a(this),e=d.data("zui.imgCutter"),f="object"==typeof b&&b;e||d.data("zui.imgCutter",e=new c(this,f)),"string"==typeof b&&e[b]()})},a.fn.imgCutter.Constructor=c,a(function(){a('[data-toggle="imgCutter"]').imgCutter()})}(jQuery,Math);