text: /*! undefined v0.5.4 License: MIT */!function(){"use strict";!function(){angular.module("pmImageEditor",[])}(),function(){angular.module("pmImageEditor").factory("DraggableFactory",function(){var t=function(){this._originalMousePosition={top:0,left:0},this._position={top:0,left:0},this._size={width:0,height:0},this._parentSize={width:0,height:0}};return t.prototype.css=function(){return{top:this._position.top+"px",left:this._position.left+"px"}},t.prototype.dragStart=function(t,i,e){return this.setPosition(parseInt(i.css("top"),10)||0,parseInt(i.css("left"),10)||0).setOriginalMousePosition(t.screenY,t.screenX).setSize(i).setParentSize(e),this},t.prototype.updatePosition=function(t,i){return this._position.top=Math.max(t-this._originalMousePosition.top,0),this._position.left=Math.max(i-this._originalMousePosition.left,0),this._position.left+this._size.width>this._parentSize.width&&(this._position.left=this._parentSize.width-this._size.width),this._position.top+this._size.height>this._parentSize.height&&(this._position.top=this._parentSize.height-this._size.height),this},t.prototype.setPosition=function(t,i){return this._position={top:t,left:i},this},t.prototype.getPosition=function(){return this._position},t.prototype.setOriginalMousePosition=function(t,i){return this._originalMousePosition={top:t-this._position.top,left:i-this._position.left},this},t.prototype.getOriginalMousePosition=function(){return this._originalMousePosition},t.prototype.setSize=function(t){return this._size={width:parseInt(t.css("width"),10)||t[0].clientWidth,height:parseInt(t.css("height"),10)||t[0].clientHeight},this},t.prototype.getSize=function(){return this._size},t.prototype.setParentSize=function(t){return this._parentSize={width:parseInt(t.css("width"),10)||t[0].clientWidth,height:parseInt(t.css("height"),10)||t[0].clientHeight},this},t.prototype.getParentSize=function(){return this._parentSize},t}).controller("DraggableController",["$scope","$document","$rootScope","DraggableFactory",function(t,i,e,o){t.draggableFactory=new o,t.draggableUiParams=function(){return{element:t.element,position:t.draggableFactory.getPosition()}},t.draggableMousedown=function(e){e.preventDefault(),t.draggableFactory.dragStart(e,t.element,t.parentElement),i.on("mousemove",t.draggableMousemove),i.on("mouseup",t.draggableMouseup)},t.draggableMousemove=function(i){t.draggableFactory.updatePosition(i.screenY,i.screenX),t.element.css(t.draggableFactory.css()),angular.isFunction(t.onDrag)&&t.onDrag(t.draggableFactory.css())},t.draggableMouseup=function(o){i.unbind("mousemove",t.draggableMousemove),i.unbind("mouseup",t.draggableMouseup),e.$broadcast("dragStop",o,t.draggableUiParams())}}]).directive("draggable",function(){return{restrict:"A",controller:"DraggableController",link:function(t,i){t.element=i,t.parentElement=i.parent(),i.css({position:"absolute"}),i.on("mousedown",function(i){t.draggableMousedown(i)})}}})}(),function(){angular.module("pmImageEditor").directive("editorPanel",function(){return{restrict:"E",scope:{editorId:"@"},link:function(t,i){var e="crop,rotate-cw,rotate-acw,flip-h,flip-v,undo,redo",o=["Crop","Rotate Clockwise","Rotate Counterclockwise","Flip Horizontal","Flip Vertical","Undo","Redo"],s={};e.split(",").forEach(function(e,n){s[e]=angular.element('<span class="image-editor-'+e+'" title="'+o[n]+'" />').on("click",function(){t.$emit("editorButtonClick",{name:e})}),i.append(s[e])}),s.undo.addClass("disabled"),s.redo.addClass("disabled"),t.$on("disableButton",function(i,e,o){e===t.editorId&&s[o].addClass("disabled")}),t.$on("enableButton",function(i,e,o){e===t.editorId&&s[o].removeClass("disabled")})}}})}(),function(){angular.module("pmImageEditor").directive("imageSelection",function(){return{restrict:"E",scope:{editorId:"@",width:"@",height:"@"},template:'                <div class="image-editor-selection" editor-id="{{editorId}}" draggable resizable>                    <div class="image-editor-selection-border-top"></div>                    <div class="image-editor-selection-border-bottom"></div>                    <div class="image-editor-selection-border-left"></div>                    <div class="image-editor-selection-border-right"></div>                </div>                <div class="image-editor-overlay-top"></div>                <div class="image-editor-overlay-left"></div>                <div class="image-editor-overlay-bottom"></div>                <div class="image-editor-overlay-right"></div>',link:function(t,i){t.selection=angular.element(i[0].querySelector(".image-editor-selection")),t.overlay={top:angular.element(i[0].querySelector(".image-editor-overlay-top")),left:angular.element(i[0].querySelector(".image-editor-overlay-left")),bottom:angular.element(i[0].querySelector(".image-editor-overlay-bottom")),right:angular.element(i[0].querySelector(".image-editor-overlay-right"))},t.onDrag=function(i){t.setSelectionCss(i)},t.onResize=function(i){t.setSelectionCss(i)},t.setSelectionCss=function(i){t.selection.css(i);var e=t.getSelectionData();t.overlay.top.css({top:"0px",left:"0px",right:"0px",height:e.top+"px"}),t.overlay.bottom.css({top:e.top+e.height+"px",left:"0px",right:"0px",bottom:"0px"}),t.overlay.left.css({top:e.top+"px",left:"0px",width:e.left+"px",height:e.height+"px"}),t.overlay.right.css({top:e.top+"px",left:e.left+e.width+"px",right:"0px",height:e.height+"px"})},t.getSelectionData=function(){return{top:parseInt(t.selection.css("top"),10),left:parseInt(t.selection.css("left"),10),width:parseInt(t.selection.css("width"),10),height:parseInt(t.selection.css("height"),10)}};var e=function(){t.$emit("selectionChanged",t.editorId,t.getSelectionData())},o=function(){t.setSelectionCss({position:"absolute",top:"0px",left:"0px",width:t.width+"px",height:t.height+"px"}),e()};t.$on("updateSelection",function(i,e,o){e===t.editorId&&t.setSelectionCss(o)}),t.$on("dragStop",function(i,o,s){s.element.attr("editor-id")===t.editorId&&e()}),t.$on("resizeStop",function(i,o,s){s.element.attr("editor-id")===t.editorId&&e()}),o()}}})}(),function(){angular.module("pmImageEditor").factory("ResizableFactory",function(){var t=function(t){this._options=t,this._originalSize={width:0,height:0},this._originalPosition={top:0,left:0},this._originalMousePosition={top:0,left:0},this._position=this._originalPosition,this._size=this._originalSize,this._axis="se",this._ratio=1};return t.prototype.resizeStart=function(t,i){this.setOriginalMousePosition(t.screenY,t.screenX),this.setOriginalPosition(parseInt(i.css("top"),10)||0,parseInt(i.css("left"),10)||0),this.setParentSize(i.parent()),this.setOriginalSize(i),this._ratio=this._size.width/this._size.height;var e=t.target.className.match(/resizable-(se|sw|ne|nw|n|e|s|w)/i);this._axis=e&&e[1]?e[1]:"se",this.updateVirtualBoundaries()},t.prototype.getBoundaryData=function(t,i){var e=this.updateBoundaries(t-this._originalMousePosition.left,i-this._originalMousePosition.top);return e=this.updateRatio(e),e=this.respectSize(e),this.updateSizeAndPosition(e),this.fitContainer(e)},t.prototype.updateBoundaries=function(t,i){var e=this._originalSize,o=this._originalPosition,s={e:function(t){return{width:e.width+t}},w:function(t){var i=e,s=o;return{left:s.left+t,width:i.width-t}},n:function(t,i){var s=e,n=o;return{top:n.top+i,height:s.height-i}},s:function(t,i){return{height:e.height+i}},se:function(t,i){return angular.extend(this.s.apply(this,arguments),this.e.apply(this,[t,i]))},sw:function(t,i){return angular.extend(this.s.apply(this,arguments),this.w.apply(this,[t,i]))},ne:function(t,i){return angular.extend(this.n.apply(this,arguments),this.e.apply(this,[t,i]))},nw:function(t,i){return angular.extend(this.n.apply(this,arguments),this.w.apply(this,[t,i]))}};return s[this._axis](t,i)},t.prototype.updateRatio=function(t){return angular.isNumber(t.height)?t.width=t.height*this._ratio:angular.isNumber(t.width)&&(t.height=t.width/this._ratio),"sw"===this._axis&&(t.left=this._position.left+(this._size.width-t.width),t.top=null),"nw"===this._axis&&(t.top=this._position.top+(this._size.height-t.height),t.left=this._position.left+(this._size.width-t.width)),t},t.prototype.updateSizeAndPosition=function(t){angular.isNumber(t.left)&&(this._position.left=t.left),angular.isNumber(t.top)&&(this._position.top=t.top),angular.isNumber(t.height)&&(this._size.height=t.height),angular.isNumber(t.width)&&(this._size.width=t.width)},t.prototype.updateVirtualBoundaries=function(){var t,i,e,o,s,n=this._options;s={minWidth:angular.isNumber(n.minWidth)?n.minWidth:0,maxWidth:angular.isNumber(n.maxWidth)?n.maxWidth:1/0,minHeight:angular.isNumber(n.minHeight)?n.minHeight:0,maxHeight:angular.isNumber(n.maxHeight)?n.maxHeight:1/0},this._ratio&&(t=s.minHeight*this._ratio,e=s.minWidth/this._ratio,i=s.maxHeight*this._ratio,o=s.maxWidth/this._ratio,t>s.minWidth&&(s.minWidth=t),e>s.minHeight&&(s.minHeight=e),i<s.maxWidth&&(s.maxWidth=i),o<s.maxHeight&&(s.maxHeight=o)),this._vBoundaries=s},t.prototype.respectSize=function(t){var i=this._vBoundaries,e=this._axis,o=angular.isNumber(t.width)&&i.maxWidth&&i.maxWidth<t.width,s=angular.isNumber(t.height)&&i.maxHeight&&i.maxHeight<t.height,n=angular.isNumber(t.width)&&i.minWidth&&i.minWidth>t.width,r=angular.isNumber(t.height)&&i.minHeight&&i.minHeight>t.height,h=this._originalPosition.left+this._originalSize.width,a=this._position.top+this._size.height,l=/sw|nw|w/.test(e),p=/nw|ne|n/.test(e);return n&&(t.width=i.minWidth),r&&(t.height=i.minHeight),o&&(t.width=i.maxWidth),s&&(t.height=i.maxHeight),n&&l&&(t.left=h-i.minWidth),o&&l&&(t.left=h-i.maxWidth),r&&p&&(t.top=a-i.minHeight),s&&p&&(t.top=a-i.maxHeight),t.width||t.height||t.left||!t.top?t.width||t.height||t.top||!t.left||(t.left=null):t.top=null,t},t.prototype.fitContainer=function(t){var i,e,o=!0;return this._position.left<0&&(this._size.width+=this._position.left,i=this._size.height,this._ratio&&(this._size.height=this._size.width/this._ratio,o=!1),this._position.left=0,"nw"===this._axis&&(this._position.top+=i-this._size.height)),this._position.top<0&&(this._size.height+=this._position.top,e=this._size.width,this._ratio&&(this._size.width=this._size.height*this._ratio,o=!1),this._position.top=0,"nw"===this._axis&&(this._position.left+=e-this._size.width)),this._position.left+this._size.width>=this._parentData.width&&(this._size.width=this._parentData.width-this._position.left,i=this._size.height,this._ratio&&(this._size.height=this._size.width/this._ratio,o=!1),("ne"===this._axis||"n"===this._axis)&&(this._position.top+=i-this._size.height)),this._position.top+this._size.height>=this._parentData.height&&(this._size.height=this._parentData.height-this._position.top,e=this._size.width,this._ratio&&(this._size.width=this._size.height*this._ratio,o=!1),"sw"===this._axis&&(this._position.left+=e-this._size.width)),o||(t.left=this._position.left,t.top=this._position.top,t.width=this._size.width,t.height=this._size.height),t},t.prototype.setOriginalSize=function(t){return this._originalSize={width:parseInt(t.css("width"),10)||t[0].clientWidth,height:parseInt(t.css("height"),10)||t[0].clientHeight},this._size=angular.copy(this._originalSize),this},t.prototype.getOriginalSize=function(){return this._originalSize},t.prototype.setParentSize=function(t){return this._parentData={width:parseInt(t.css("width"),10)||t[0].clientWidth,height:parseInt(t.css("height"),10)||t[0].clientHeight},this},t.prototype.getParentSize=function(){return this._parentData},t.prototype.setOriginalPosition=function(t,i){return this._originalPosition={top:t,left:i},this._position=angular.copy(this._originalPosition),this},t.prototype.getOriginalPosition=function(){return this._originalPosition},t.prototype.setOriginalMousePosition=function(t,i){return this._originalMousePosition={top:t,left:i},this},t.prototype.getOriginalMousePosition=function(){return this._originalMousePosition},t.prototype.setOption=function(t,i){return this._options[t]=i,this},t.prototype.getOption=function(t){return this._options[t]},t.prototype.getSize=function(){return this._size},t.prototype.getPosition=function(){return this._position},t.prototype.getRatio=function(){return this._ratio},t.prototype.getAxis=function(){return this._axis},t.prototype.getVirtualBoundaries=function(){return this._vBoundaries},t}).controller("ResizableController",["$scope","$document","$rootScope","ResizableFactory",function(t,i,e,o){t.resizableFactory=new o({minHeight:20,minWidth:20}),t.resizableUiParams=function(){return{element:t.element,position:t.resizableFactory.getPosition(),size:t.resizableFactory.getSize()}},t.resizableHandleMousedown=function(e){e.preventDefault(),e.stopPropagation(),t.resizableFactory.resizeStart(e,t.element),i.on("mousemove",t.resizableMousemove),i.on("mouseup",t.resizableMouseup)},t.resizableMousemove=function(i){var e=t.resizableFactory.getBoundaryData(i.screenX,i.screenY),o={};angular.forEach(e,function(t,i){null!==t&&(o[i]=t+"px")}),t.element.css(o),angular.isFunction(t.onResize)&&t.onResize(o)},t.resizableMouseup=function(o){i.unbind("mousemove",t.resizableMousemove),i.unbind("mouseup",t.resizableMouseup),e.$broadcast("resizeStop",o,t.resizableUiParams())}}]).directive("resizable",function(){return{restrict:"A",controller:"ResizableController",link:function(t,i){t.element=i,t.parentElement=i.parent(),i.css({position:"absolute"});var e="n,e,w,s,nw,ne,sw,se";e.split(",").forEach(function(e){var o=angular.element('<span class="resizable-'+e+' resizable-handle"></span>');i.append(o),o.on("mousedown",function(i){t.resizableHandleMousedown(i)})})}}})}(),function(){angular.module("pmImageEditor").factory("ImageHistoryFactory",function(){var t=function(t){this.editor=t,this.reset()};return t.prototype.reset=function(){this.items=[],this.historyIndex=-1},t.prototype.addItem=function(){this.items=this.items.slice(0,this.historyIndex+1),this.items.push({top:this.editor.top,left:this.editor.left,selection:angular.copy(this.editor.selection),wasCroppedForRotation:this.editor.wasCroppedForRotation,isCropped:this.editor.isCropped,hFlip:this.editor.hFlip,vFlip:this.editor.vFlip,rotation:this.editor.rotation,width:this.editor.width,height:this.editor.height}),this.historyIndex=this.items.length-1},t.prototype.canUndo=function(){return this.historyIndex>0},t.prototype.canRedo=function(){return this.historyIndex<this.items.length-1},t.prototype.redo=function(){this.canRedo()&&this.applyHistory(1)},t.prototype.undo=function(){this.canUndo()&&this.applyHistory(-1)},t.prototype.applyHistory=function(t){this.historyIndex+=t,angular.forEach(this.current(),function(t,i){this.editor[i]=angular.isObject(t)?angular.copy(t):t},this)},t.prototype.current=function(){return this.items[this.historyIndex]},t}).factory("ImageEditorFactory",["ImageHistoryFactory",function(t){var i=function(i){this.options=i,this.visibleWidth=+(i.visibleWidth||0),this.ratio=1,this.naturalWidth=0,this.naturalHeight=0,this.history=new t(this),this.resetTransformations()};return i.prototype.resetTransformations=function(){this.top=0,this.left=0,this.selection={top:0,left:0,width:this.options.selectionWidth||0,height:this.options.selectionHeight||0},this.selection.width&&this.selection.height&&(this.selection.ratio=this.selection.width/this.selection.height),this.wasCroppedForRotation=0,this.isCropped=!1,this.hFlip=!1,this.vFlip=!1,this.rotation=0,this.width=this.visibleWidth,this.height=this.width/this.ratio,this.history.reset(),this.history.addItem()},i.prototype.css=function(){var t=[];return this.vFlip&&t.push("scaleX(-1)"),this.hFlip&&t.push("scaleY(-1)"),this.rotation&&t.push("rotate("+this.rotation+"deg)"),{position:"absolute",top:this.top+"px",left:this.left+"px",width:this.width+"px",height:this.height+"px",transform:t.length?t.join(" "):"none"}},i.prototype.parentSize=function(){var t=this.isCropped?this.selection.ratio:this.ratio,i=this.visibleWidth;return{width:i,height:this.rotation%180===this.wasCroppedForRotation?i/t:i*t}},i.prototype.parentCss=function(){var t=this.parentSize();return{width:t.width+"px",height:t.height+"px"}},i.prototype.selectionCss=function(){return{top:this.selection.top+"px",left:this.selection.left+"px",width:this.selection.width+"px",height:this.selection.height+"px"}},i.prototype.initImageData=function(t,i){this.ratio=t/i,this.naturalWidth=t,this.naturalHeight=i,this.resetTransformations()},i.prototype.setVisibleWidth=function(t){return this.visibleWidth=parseInt(t,10),this.naturalWidth&&this.naturalHeight&&this.initImageData(this.naturalWidth,this.naturalHeight),this},i.prototype.updateSelectionDimensions=function(t,i){var e=this.parentSize();if(!(t<=e.width&&i<=e.height))throw new RangeError("Incorrect selection size");return this.options.selectionWidth=t,this.options.selectionHeight=i,this.selection.top=0,this.selection.left=0,this.selection.width=t,this.selection.height=i,this},i.prototype.setSelection=function(t){return this.selection=t,this.selection.ratio=t.width/t.height,this.history.addItem(),this},i.prototype.crop=function(){var t=this.selection,i=this.visibleWidth/t.width;this.top=(this.top-t.top)*i,this.left=(this.left-t.left)*i,this.width=this.width*i,this.height=this.width/this.ratio,this.isCropped=!0,this.wasCroppedForRotation=this.rotation%180;var e=this.parentSize();this.selection.top=0,this.selection.left=0,this.selection.width=e.width,this.selection.height=e.height,this.history.addItem()},i.prototype.horizontalFlip=function(){this.hFlip=!this.hFlip;var t=this.parentSize();this.top=t.height-this.height-this.top,this.history.addItem()},i.prototype.verticalFlip=function(){this.vFlip=!this.vFlip;var t=this.parentSize();this.left=t.width-this.width-this.left,this.history.addItem()},i.prototype.rotate=function(t){this.rotation+="cw"===t?90:-90,this.rotation=(this.rotation+360)%360;var i=this.parentSize(),e=i.height/this.visibleWidth;this.height=this.height*e,this.width=this.height*this.ratio;var o=this.top*e,s=this.left*e,n=i.width,r=i.height,h=this.width,a=this.height,l=s+h/2,p=o+a/2;"cw"===t?(this.left=s-l-p+n,this.top=o+l-p):(this.left=s-l+p,this.top=o-l-p+r),this.selection.top=0,this.selection.left=0,this.history.addItem()},i}]).controller("ImageEditorController",["$scope","ImageEditorFactory",function(t,i){t.editor=new i({selectionWidth:+t.selectionWidth,selectionHeight:+t.selectionHeight,visibleWidth:t.width}),t.editorId=String(Math.random()).replace("0.","editor-"),t.editor.setVisibleWidth(t.width),t.$on("editorButtonClick",function(i,e){switch(i.stopPropagation(),e.name){case"crop":t.editor.crop();break;case"rotate-cw":t.editor.rotate("cw");break;case"rotate-acw":t.editor.rotate("acw");break;case"flip-v":t.editor.verticalFlip();break;case"flip-h":t.editor.horizontalFlip();break;case"undo":t.editor.history.undo();break;case"redo":t.editor.history.redo()}t.updateEditorCss()}),t.updateEditorCss=function(){t.imageElement&&(t.imageElement.css(t.editor.css()),t.imageElement.parent().css(t.editor.parentCss()),t.$broadcast("updateSelection",t.editorId,t.editor.selectionCss()),t.updateHistoryButtons(),t.state=angular.copy(t.editor.history.current()),setTimeout(function(){t.$apply()},0))},t.updateHistoryButtons=function(){t.$broadcast(t.editor.history.canUndo()?"enableButton":"disableButton",t.editorId,"undo"),t.$broadcast(t.editor.history.canRedo()?"enableButton":"disableButton",t.editorId,"redo")},t.$on("selectionChanged",function(i,e,o){e===t.editorId&&(i.stopPropagation(),t.editor.setSelection(o),t.updateEditorCss())}),t.$watch("width",function(i){t.editor.setVisibleWidth(i),t.updateEditorCss()});var e=function(i){+i&&(t.editor.updateSelectionDimensions(+t.selectionWidth,+t.selectionHeight),t.updateEditorCss())};t.$watch("selectionWidth",e),t.$watch("selectionHeight",e)}]).directive("imageEditor",function(){return{restrict:"E",scope:{image:"@",width:"@",selectionWidth:"@",selectionHeight:"@",state:"="},controller:"ImageEditorController",template:'                    <div class="image-editor-canvas">                        <img ng-src="{{image}}" />                        <image-selection                             width="{{selectionWidth}}"                            height="{{selectionHeight}}"                            editor-id="{{editorId}}"                        ></image-selection>                    </div>                    <editor-panel editor-id="{{editorId}}"></editor-panel>',link:function(t,i){t.imageElement=i.find("img"),t.imageElement[0].onload=function(){t.editor.initImageData(this.naturalWidth,this.naturalHeight),t.updateEditorCss()}}}})}()}();