UNPKG

@springernature/nn-charts

Version:
140 lines (136 loc) 8.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.zoomHandler = exports.sortByKey = exports.getWindowDimensions = exports.getPathToConnectNodes = exports.getMinAttributeValue = exports.getMaxAttributeValue = exports.getElementBoundingClientRectByClass = exports.expandBranch = exports.collapseTreeBranch = exports.collapseBranch = exports.appendToggleIcon = exports.alterToggleIconPath = void 0; var d3 = _interopRequireWildcard(require("d3")); var _constants = require("../constants"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var getMinAttributeValue = exports.getMinAttributeValue = function getMinAttributeValue(iterable, key) { return d3.min(iterable, function (element) { return element[key]; }); }; var getMaxAttributeValue = exports.getMaxAttributeValue = function getMaxAttributeValue(iterable, key) { return d3.max(iterable, function (element) { return element[key]; }); }; var zoomHandler = exports.zoomHandler = function zoomHandler() { var baseGroupClass = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "collapsible-tree-group"; var _d3$event$transform = d3.event.transform, x = _d3$event$transform.x, y = _d3$event$transform.y, k = _d3$event$transform.k; d3.select(".".concat(baseGroupClass)).attr("transform", "translate(".concat(x, ",").concat(y, ") scale(").concat(k, ")")); // correctly scale SVG Circles during user zoom in/out d3.selectAll(".nodeContent").attr("transform", "scale(".concat(k < 1 ? k : 1 / k, ")")); // Correctly scale SVG lines during user zoom in/out d3.selectAll(".link").style("stroke-width", function () { return d3.select(this).style("stroke-width") * (1 / k); }); }; /** * Creates a angled (connector) path from parent to the child nodes * @param {*} sourceNode * @param {*} destinationNode * @returns path - text string that defines connector SVG pth to draw */ var getPathToConnectNodes = exports.getPathToConnectNodes = function getPathToConnectNodes(sourceNode, destinationNode) { // Calculate the correct x positions for source and destination nodes var correctSX = sourceNode.recalculatedX ? sourceNode.recalculatedX : sourceNode.x; var correctDX = destinationNode.recalculatedX ? destinationNode.recalculatedX : destinationNode.x; var path = "M ".concat(sourceNode.y, " ").concat(correctSX, "\n L ").concat((sourceNode.y + destinationNode.y) * 0.6, " ").concat(correctSX, ", ").concat((sourceNode.y + destinationNode.y) * 0.6, " ").concat(correctDX, " ").concat(destinationNode.y, " ").concat(correctDX); return path; }; var getWindowDimensions = exports.getWindowDimensions = function getWindowDimensions() { var widthHolder = 0; var heightHolder = 0; if (window.matchMedia) { // Modern browsers var _window = window, innerWidth = _window.innerWidth, innerHeight = _window.innerHeight; widthHolder = innerWidth; heightHolder = innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { // For IE 6+ in 'standards compliant mode' widthHolder = document.documentElement.clientWidth; heightHolder = document.documentElement.clientHeight; } else if (document.body && document.body.clientHeight) { // For IE 4 compatible widthHolder = document.body.clientWidth; heightHolder = document.body.clientHeight; } return { widthHolder: widthHolder, heightHolder: heightHolder }; }; var _collapseTreeBranch = exports.collapseTreeBranch = function collapseTreeBranch(data) { var _data$children; if ((_data$children = data.children) !== null && _data$children !== void 0 && _data$children.length) { data._children = data.children; // Set the parent object in all the children data._children.forEach(function (d) { d.parent = data; _collapseTreeBranch(d); }); data.children = null; } }; var _expandBranch = exports.expandBranch = function expandBranch(d) { if (d !== null && d !== void 0 && d._children) { d.children = d._children; d.children.forEach(_expandBranch); d._children = null; } else if (d.children) d.children.forEach(_expandBranch); }; var _collapseBranch = exports.collapseBranch = function collapseBranch(d) { var _d$children; if ((_d$children = d.children) !== null && _d$children !== void 0 && _d$children.length) { d._children = d.children; d._children.forEach(_collapseBranch); d.children = null; } }; var getElementBoundingClientRectByClass = exports.getElementBoundingClientRectByClass = function getElementBoundingClientRectByClass(element) { return d3.selectAll(element).node().getBoundingClientRect(); }; var appendToggleIcon = exports.appendToggleIcon = function appendToggleIcon(nodeSelection) { var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { x: 5, y: 5, width: 24, height: 24, cursor: "pointer" }; var group = nodeSelection.append("g"); group.append("rect").attr(_objectSpread({}, attributes)); // Append the path representing the icon to the group group.append("path").attr("d", function (d) { var _ref, _d$children2; var isShowIcon = !!((_ref = (_d$children2 = d.children) !== null && _d$children2 !== void 0 ? _d$children2 : d._children) !== null && _ref !== void 0 && _ref.length); if (d.depth === 1 && isShowIcon) return d !== null && d !== void 0 && d.isNodeExpanded ? _constants.roundedMinusIconPath : _constants.roundedPlusIconPath; }).attr("class", "toggle-icon"); return group; }; var alterToggleIconPath = exports.alterToggleIconPath = function alterToggleIconPath(iconNode, className) { iconNode.select("path").attr("d", function () { return className === "plus" ? _constants.roundedPlusIconPath : _constants.roundedMinusIconPath; }); }; var sortByKey = exports.sortByKey = function sortByKey(list, key) { return list.sort(function (a, b) { var x = a[key]; var y = b[key]; return x < y ? -1 : x > y ? 1 : 0; }); };