3 lines
9.6 KiB
JavaScript
3 lines
9.6 KiB
JavaScript
define("local_treestudyplan/simpleline/simpleline",["exports","./css-calc"],(function(_exports,_cssCalc){function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}function _typeof(obj){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.SimpleLine=void 0;var obj,key,value,specsCopy=function specsCopy(to,from){for(var ix in to)from.hasOwnProperty(ix)&&("object"==_typeof(to[ix])&&"object"==_typeof(from[ix])?Array.isArray(to[ix])?Array.isArray(from[ix])&&(to[ix]=Array.from(from[ix])):specsCopy(to[ix],from[ix]):to[ix]=from[ix])},getElementPosition=function(el,reference){if(!(el&&el instanceof HTMLElement))return{x:0,y:0};if(reference&&reference instanceof HTMLElement||(reference=document.querySelector("body")),el.offsetParent===reference)return{x:el.offsetLeft,y:el.offsetTop};var elR=el.getBoundingClientRect(),refR=reference.getBoundingClientRect();return{x:elR.left-refR.left,y:elR.top-refR.top}},SimpleLine=function(){function SimpleLine(start,end,config){var func,delay,timer,_this=this;if(function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}(this,SimpleLine),this.svg=null,this.id=SimpleLine.idCounter++,this.setConfig(config),start instanceof HTMLElement)this.start=start;else{if(!("string"==typeof start||this.start instanceof String))return void console.error("Start element not string or dom element",start);if(this.startSelector=start,this.start=document.querySelector(start),!(this.start instanceof HTMLElement))return void console.error("Cannot find start element:",start)}if(end instanceof HTMLElement)this.end=end;else{if(!("string"==typeof end||this.end instanceof String))return void console.error("End element not string or dom element",start);if(this.endSelector=end,this.end=document.querySelector(end),!(this.end instanceof HTMLElement))return void console.error("Cannot find end element:",end)}this.resizeObserver=new ResizeObserver((func=function(){_this.update()},delay=20,function(){for(var _len=arguments.length,args=new Array(_len),_key=0;_key<_len;_key++)args[_key]=arguments[_key];var context=this;clearTimeout(timer),timer=setTimeout((function(){func.apply(context,args)}),delay)})),this.resizeObserver.observe(this.start),this.resizeObserver.observe(this.end),this.mutationObserver=new MutationObserver((function(mutations_list){mutations_list.forEach((function(mutation){mutation.removedNodes.forEach((function(removed_node){removed_node!=this.start&&removed_node!=this.end||(console.warning("Element removed",removed_node),this.remove())}))}))})),this.mutationObserver.observe(this.start.parentElement,{subtree:!1,childList:!0}),this.mutationObserver.observe(this.end.parentElement,{subtree:!1,childList:!0}),this.positionCheck(),this.specs.autorefresh>0&&(this.refreshTimer=setInterval((function(){_this.positionCheck()}),this.specs.autorefresh)),this.active=!0,this.update()}var Constructor,protoProps,staticProps;return Constructor=SimpleLine,(protoProps=[{key:"setConfig",value:function(config){var _this2=this;this.specs||(this.specs={autorefresh:10,class:"",color:"",anchors:{start:["middle","right"],end:["middle","left"]},gravity:{start:1,end:1},stroke:"4px"}),config&&"object"==_typeof(config)&&specsCopy(this.specs,config),this.svg&&(clearInterval(this.refreshTimer),this.specs.autorefresh>0&&(this.refreshTimer=setInterval((function(){_this2.positionCheck()}),this.specs.autorefresh)),this.update())}},{key:"cssClass",get:function(){return this.specs.class},set:function(cssClass){this.specs.class=cssClass,this.update()}},{key:"positionCheck",value:function(){var startPos={x:this.start.offsetLeft,y:this.start.offsetTop},endPos={x:this.end.offsetLeft,y:this.end.offsetTop},needUpdate=!1;this.startPos&&(needUpdate=needUpdate||this.startPos.x!=startPos.x||this.startPos.y!=startPos.y),this.startPos=startPos,this.endPos&&(needUpdate=needUpdate||this.endPos.x!=endPos.x||this.endPos.y!=endPos.y),this.endPos=endPos,needUpdate&&this.update()}},{key:"getContainer",value:function(){var container=this.start.offsetParent;return container||("fixed"==getComputedStyle(this.start).position?container=document.querySelector("body"):console.error("Start element has no offsetParent. likely ")),container}},{key:"getAnchorPoint",value:function(anchor){var x,dirX,y,dirY,el=this.start;return"start"!=anchor&&(anchor="end",el=this.end),this.specs.anchors[anchor].includes("left")?(x=0,dirX=-1):this.specs.anchors[anchor].includes("right")?(x=el.offsetWidth-1,dirX=1):(x=el.offsetWidth/2,dirX=0),this.specs.anchors[anchor].includes("top")?(y=0,dirY=-1):this.specs.anchors[anchor].includes("bottom")?(y=el.offsetHeight-1,dirY=1):(y=el.offsetHeight/2,dirY=0),{x:x,y:y,dir:{x:dirX,y:dirY}}}},{key:"svgDefs",value:function(){var defs=document.createElementNS("http://www.w3.org/2000/svg","defs"),marker=document.createElementNS("http://www.w3.org/2000/svg","marker");marker.setAttribute("id","simpleline-".concat(this.id,"-arrow")),marker.setAttribute("markerUnits","strokeWidth"),marker.setAttribute("viewBox","-8 -8 16 16"),marker.setAttribute("orient","auto"),marker.setAttribute("markerWidth","4"),marker.setAttribute("markerHeight","4"),defs.appendChild(marker);var polygon=document.createElementNS("http://www.w3.org/2000/svg","polygon");return polygon.setAttribute("fill","currentColor"),polygon.setAttribute("points","-8,-8 8,0 -8,8 -5,0"),marker.append(polygon),defs}},{key:"update",value:function(){if(this.active){var container=this.getContainer();if(container){this.svg&&this.svg instanceof SVGElement?container!==this.svg.offsetParent&&container.appendChild(this.svg):(this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.setAttribute("id","simpleline-".concat(this.id)),this.svg.setAttribute("class","simpleline ".concat(this.specs.class)),this.svg.style.position="absolute",this.svg.style.pointerEvents="none",container.appendChild(this.svg),this.svg.appendChild(this.svgDefs()));var startAnchor=this.getAnchorPoint("start"),endAnchor=this.getAnchorPoint("end"),strokeWpx=(0,_cssCalc.calc)(this.specs.stroke);endAnchor.x+=endAnchor.dir.x*strokeWpx*1.5,endAnchor.y+=endAnchor.dir.y*strokeWpx*1.5;var elStartPos=getElementPosition(this.start,container),elEndPos=getElementPosition(this.end,container),w=Math.max(elStartPos.x+startAnchor.x,elEndPos.x+endAnchor.x)-Math.min(elStartPos.x+startAnchor.x,elEndPos.x+endAnchor.x),h=Math.max(elStartPos.y+startAnchor.y,elEndPos.y+endAnchor.y)-Math.min(elStartPos.y+startAnchor.y,elEndPos.y+endAnchor.y),weight=Math.sqrt(h*h+w*w)/2,cStartPos={x:elStartPos.x+startAnchor.x,y:elStartPos.y+startAnchor.y,dirx:elStartPos.x+startAnchor.x+startAnchor.dir.x*this.specs.gravity.start*weight,diry:elStartPos.y+startAnchor.y+startAnchor.dir.y*this.specs.gravity.start*weight},cEndPos={x:elEndPos.x+endAnchor.x,y:elEndPos.y+endAnchor.y,dirx:elEndPos.x+endAnchor.x+endAnchor.dir.x*this.specs.gravity.end*weight,diry:elEndPos.y+endAnchor.y+endAnchor.dir.y*this.specs.gravity.end*weight},margin=isNaN(this.specs.stroke)?25:5*this.specs.stroke,bounds={x:Math.min(cStartPos.x,cEndPos.x,cStartPos.dirx,cEndPos.dirx)-margin,y:Math.min(cStartPos.y,cEndPos.y,cStartPos.diry,cEndPos.diry)-margin,w:Math.max(cStartPos.x,cEndPos.x,cStartPos.dirx,cEndPos.dirx)-Math.min(cStartPos.x,cEndPos.x,cStartPos.dirx,cEndPos.dirx)+2*margin,h:Math.max(cStartPos.y,cEndPos.y,cStartPos.diry,cEndPos.diry)-Math.min(cStartPos.y,cEndPos.y,cStartPos.diry,cEndPos.diry)+2*margin},startPos={x:cStartPos.x-bounds.x,y:cStartPos.y-bounds.y,dirx:cStartPos.dirx-bounds.x,diry:cStartPos.diry-bounds.y},endPos={x:cEndPos.x-bounds.x,y:cEndPos.y-bounds.y,dirx:cEndPos.dirx-bounds.x,diry:cEndPos.diry-bounds.y};this.svg.setAttribute("viewBox","0 0 ".concat(bounds.w,", ").concat(bounds.h)),this.svg.setAttribute("width","".concat(bounds.w,"px")),this.svg.setAttribute("height","".concat(bounds.h,"px")),this.svg.style.color=this.specs.color,this.svg.style.left="".concat(bounds.x,"px"),this.svg.style.top="".concat(bounds.y,"px"),this.svg.style.width="".concat(bounds.w,"px"),this.svg.style.height="".concat(bounds.h,"px"),this.line||(this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("marker-end","url(#simpleline-".concat(this.id,"-arrow)")),this.svg.appendChild(this.line));var strokeWidth=this.specs.stroke;isNaN(strokeWidth)||0==strokeWidth||(strokeWidth="".concat(strokeWidth,"px")),this.line.style.stroke="currentColor",this.line.style.fill="none",this.line.style.strokeWidth=strokeWidth,this.line.setAttribute("d","M ".concat(startPos.x," ").concat(startPos.y,"\n C ").concat(startPos.dirx," ").concat(startPos.diry,", ").concat(endPos.dirx," ").concat(endPos.diry,", ").concat(endPos.x," ").concat(endPos.y))}}}},{key:"remove",value:function(){this.svg.remove(),this.svg=null,this.line=null,clearInterval(this.refreshTimer),this.resizeObserver.disconnect(),this.mutationObserver.disconnect(),this.active=!1}}])&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Object.defineProperty(Constructor,"prototype",{writable:!1}),SimpleLine}();_exports.SimpleLine=SimpleLine,value=0,(key="idCounter")in(obj=SimpleLine)?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value}));
|
|
|
|
//# sourceMappingURL=simpleline.min.js.map
|