@anthologen/cytoscape-node-html-label
Version:
1 lines • 4.27 kB
JavaScript
!function(){"use strict";var p=function(t,e){if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var n,o=t.length>>>0,i=e,a=0;a<o;a++)if(n=t[a],e.call(i,n,a,t))return n},i=(t.prototype.updateParams=function(t){var e=t.tpl,e=void 0===e?function(){return""}:e,n=(t.cssClass,t.halign),o=t.valign,i=t.halignBox,t=t.valignBox,a={top:-.5,left:-.5,center:0,right:.5,bottom:.5};this._align=[a[void 0===n?"center":n],a[void 0===o?"center":o],100*(a[void 0===i?"center":i]-.5),100*(a[void 0===t?"center":t]-.5)],this.tpl=e},t.prototype.updateData=function(t){for(;this._node.firstChild;)this._node.removeChild(this._node.firstChild);for(var e=(new DOMParser).parseFromString(this.tpl(t),"text/html").body.children,n=0;n<e.length;++n){var o=e[n];this._node.appendChild(o)}},t.prototype.getNode=function(){return this._node},t.prototype.updatePosition=function(t){this._renderPosition(t)},t.prototype.initStyles=function(t){this._node.style.position="absolute",t&&t.length&&this._node.classList.add(t)},t.prototype._renderPosition=function(t){var e=this._position,n=t.x+this._align[0]*t.w,t=t.y+this._align[1]*t.h;e&&e[0]===n&&e[1]===t||(this._position=[n,t],e="translate(".concat(this._align[2],"%,").concat(this._align[3],"%) ")+"translate(".concat(n.toFixed(2),"px,").concat(t.toFixed(2),"px) "),(n=this._node.style).webkitTransform=e,n.msTransform=e,n.transform=e)},t);function t(t,e){var n=t.node,o=t.position,o=void 0===o?null:o,t=t.data,t=void 0===t?null:t;this.updateParams(e),this._node=n,this.initStyles(e.cssClass),t&&this.updateData(t),o&&this.updatePosition(o)}e.prototype.addOrUpdateElem=function(t,e,n){void 0===n&&(n={});var o=this._elements[t];o?(o.updateParams(e),o.updateData(n.data),o.updatePosition(n.position)):(o=document.createElement("div"),this._node.appendChild(o),this._elements[t]=new i({node:o,data:n.data,position:n.position},e))},e.prototype.removeElemById=function(t){this._elements[t]&&(this._node.removeChild(this._elements[t].getNode()),delete this._elements[t])},e.prototype.updateElemPosition=function(t,e){t=this._elements[t];t&&t.updatePosition(e)},e.prototype.updatePanZoom=function(t){var e=t.pan,t=t.zoom,e="translate(".concat(e.x,"px,").concat(e.y,"px) scale(").concat(t,")"),t=this._node.style,n="top left";t.webkitTransform=e,t.msTransform=e,t.transform=e,t.webkitTransformOrigin=n,t.msTransformOrigin=n,t.transformOrigin=n};var c=e;function e(t){this._node=t,this._elements={}}function n(o,t,i){var a=t&&"object"==typeof t?t:[],r=function(){var t=o.container(),e=document.createElement("div"),n=t.querySelector("canvas"),t=t.querySelector("[class^='cy-node-html']");t&&n.parentNode.removeChild(t);t=e.style;t.position="absolute",t["z-index"]=10,t.width="500px",t.margin="0px",t.padding="0px",t.border="0px",t.outline="0px",t.outline="0px",i&&!0!==i.enablePointerEvents&&(t["pointer-events"]="none");return n.parentNode.appendChild(e),new c(e)}();return o.one("render",function(t){var n;n=t.cy,a.forEach(function(e){n.elements(e.query).forEach(function(t){t.isNode()&&r.addOrUpdateElem(t.id(),e,{position:d(t),data:t.data()})})}),s(t)}),o.on("add",function(t){var e=t.target,t=p(a.slice().reverse(),function(t){return e.is(t.query)});t&&r.addOrUpdateElem(e.id(),t,{position:d(e),data:e.data()})}),o.on("layoutstop",function(t){var e=t.cy;a.forEach(function(t){e.elements(t.query).forEach(function(t){t.isNode()&&r.updateElemPosition(t.id(),d(t))})})}),o.on("remove",function(t){r.removeElemById(t.target.id())}),o.on("data",e),o.on("style",e),o.on("pan zoom",s),o.on("position bounds",function(t){r.updateElemPosition(t.target.id(),d(t.target))}),o;function e(n){setTimeout(function(){var e=n.target,t=p(a.slice().reverse(),function(t){return e.is(t.query)});t&&!e.removed()?r.addOrUpdateElem(e.id(),t,{position:d(e),data:e.data()}):r.removeElemById(e.id())},0)}function s(t){t=t.cy;r.updatePanZoom({pan:t.pan(),zoom:t.zoom()})}function d(t){return{w:t.width(),h:t.height(),x:t.position("x"),y:t.position("y")}}}function o(t){t&&t("core","nodeHtmlLabel",function(t,e){return n(this,t,e)})}"undefined"!=typeof module&&module.exports?module.exports=function(t){o(t)}:"undefined"!=typeof define&&define.amd&&define("cytoscape-nodeHtmlLabel",function(){return o}),"undefined"!=typeof cytoscape&&o(cytoscape)}();