angular-resizable-flex
Version:
Attribute directive built to resize flex elements
1 lines • 2.48 kB
JavaScript
(function(){require("angular").module("angularResizableFlex",[]).directive("resizableFlex",function(){return{restrict:"A",scope:{rfName:"@",rfDirection:"@",rfSize:"=",rfHandle:"=",rfDisabled:"=",rfInitCb:"&",rfDragCb:"&",rfDropCb:"&"},link:function(e,t){var n,r,i,a,s,o,u,l,c,d,f,m;return r={style:window.getComputedStyle(t[0],null)},e.$watch("rfSize",function(e){if(null!=e)return f(e)}),a=function(){return parseInt(t[0].style.flexBasis.replace("px",""))},f=function(e){return t[0].style.flexBasis=e+"px"},i=function(t){var n;return n="left"===e.rfDirection||"right"===e.rfDirection?"clientX":"clientY",null!=t.touches?t.touches[0][n]:t[n]},n=function(){return document.addEventListener("mouseup",o,!!d&&{passive:!0}),document.addEventListener("mousemove",l,!!d&&{passive:!0}),document.addEventListener("touchend",o,!!d&&{passive:!0}),document.addEventListener("touchmove",l,!!d&&{passive:!0})},m=function(){return document.removeEventListener("mouseup",o),document.removeEventListener("mousemove",l),document.removeEventListener("touchend",o),document.removeEventListener("touchmove",l)},u=function(t){if(!e.rfDisabled||1===t.which||t.touches)return n(),r.initialWidth=parseInt(r.style.getPropertyValue("width")),r.initialHeight=parseInt(r.style.getPropertyValue("height")),r.initialPos=i(t),r.handle.classList.add("rf-dragging")},l=function(n){var s,o;if(o=r.initialPos-i(n),s=function(){switch(e.rfDirection){case"top":return r.initialHeight+o;case"bottom":return r.initialHeight-o;case"right":return r.initialWidth-o;case"left":return r.initialWidth+o}}(),f(s),e.rfDragCb)return e.rfDragCb({rfObj:{name:e.rfName,size:a(),element:t}})},o=function(){if(m(),r.handle.classList.remove("rf-dragging"),e.rfDropCb)return e.rfDropCb({rfObj:{name:e.rfName,size:a(),element:t}})},d=!1,c=function(){var e;try{return e=Object.defineProperty({},"passive",{get:function(){return d=!0}}),window.addEventListener("test",null,e)}catch(e){e}},s=function(){return r.handle=document.createElement("div"),r.handle.setAttribute("class","rf-"+e.rfDirection),r.handle.innerHTML=e.rfHandle?e.rfHandle:"<span></span>",t[0].appendChild(r.handle),r.handle.addEventListener("mousedown",u,!!d&&{passive:!0}),r.handle.addEventListener("touchstart",u,!!d&&{passive:!0})},function(){return e.rfInitCb&&f(e.rfInitCb({rfObj:{name:e.rfName,element:t}})),c(),s()}(),e.$on("$destroy",function(){return m(),r.handle.removeEventListener("mousedown",u),r.handle.removeEventListener("touchstart",u)})}}})}).call(this);