UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

122 lines (121 loc) 5.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _makeNode = _interopRequireDefault(require("./makeNode")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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 _default = exports["default"] = function _default() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, sdk = _ref.sdk, parent = _ref.parent, attributes = _ref.attributes; var node = (0, _makeNode["default"])({ sdk: sdk, parent: parent, attributes: attributes }); var children = []; var appendChild = function appendChild(node) { var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref2$inherit = _ref2.inherit, inherit = _ref2$inherit === void 0 ? true : _ref2$inherit; node.setParent(instance, { inherit: inherit }); children.push(node); instance.trigger("nodeAdded", node); sdk.trigger("nodeAdded", instance, node); instance.trigger("".concat(node.type, "Added"), node); sdk.trigger("".concat(node.type, "Added"), instance, node); }; var removeChild = function removeChild(id) { children = children.filter(function (n) { return n.getId() !== id; }); instance.trigger("nodeRemoved", node); sdk.trigger("nodeRemoved", instance, node); instance.trigger("".concat(node.type, "Removed"), node); sdk.trigger("".concat(node.type, "Removed"), instance, node); }; var getNode = function getNode(attributes, options) { var nodes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [instance]; var target; nodes.some(function (child) { var match = child.match(attributes); if (!match && options !== null && options !== void 0 && options.inherit) return; if (match) { target = child; return true; } if (child.type === "container") { target = child.getNode(attributes, options, children); if (target) return true; } }); return target; }; var getNodes = function getNodes(attributes, options) { var nodes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var list = nodes ? children : [instance]; nodes = nodes || []; list.forEach(function (child) { var match = child.match(attributes); if (!match && options !== null && options !== void 0 && options.inherit) return; if (match) nodes.push(child); if (child.type === "container") child.getNodes(attributes, options, nodes); }); return nodes; }; var getApplicableNodes = function getApplicableNodes(attributes, options) { if (!node.match(attributes)) return [instance]; var ancestor = node.getAncestor(attributes); if (!ancestor) return getNodes(attributes, options); return ancestor.getNodes(attributes, options); }; var getChildren = function getChildren() { return children; }; var colorsByGroupId = {}; var getNextColor = function getNextColor(getNext, groupId, id) { if (!(groupId in colorsByGroupId)) colorsByGroupId[groupId] = {}; var byId = colorsByGroupId[groupId]; if (id in byId) return byId[id]; var chartColor = getNext(); byId[id] = chartColor; return chartColor; }; var destroy = function destroy() { if (!node) return; node.destroy(); children.forEach(function (node) { return node.destroy(); }); children = []; colorsByGroupId = {}; setTimeout(function () { return node = null; }, 2000); }; node.type = "container"; node.getApplicableNodes = getApplicableNodes; var instance = _objectSpread(_objectSpread({}, node), {}, { destroy: destroy, appendChild: appendChild, removeChild: removeChild, getNode: getNode, getNodes: getNodes, getChildren: getChildren, getNextColor: getNextColor, getRoot: function getRoot() { return sdk.getRoot(); } }); return instance; };