@netdata/charts
Version:
Netdata frontend SDK and chart utilities
140 lines (139 loc) • 9.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.alignment = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _netdataUi = require("@netdata/netdata-ui");
var _provider = require("../../provider");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["id", "align", "right", "fixed", "children", "uiName"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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 alignment = exports.alignment = {
chartMiddle: "chartMiddle",
elementMiddle: "elementMiddle",
elementRight: "elementRight",
elementLeft: "elementLeft",
elementFull: "elementFull"
};
var calcByAlignment = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, alignment.chartMiddle, function (_ref) {
var _element$firstChild;
var from = _ref.from,
chartWidth = _ref.chartWidth,
element = _ref.element;
return [60, Math.min(from - 24, 60 + chartWidth / 2 + ((_element$firstChild = element.firstChild) === null || _element$firstChild === void 0 ? void 0 : _element$firstChild.offsetWidth) / 2)];
}), alignment.chartLeft, function (_ref2) {
var _element$firstChild2;
var chartWidth = _ref2.chartWidth,
element = _ref2.element;
return [0, ((_element$firstChild2 = element.firstChild) === null || _element$firstChild2 === void 0 ? void 0 : _element$firstChild2.offsetWidth) - chartWidth];
}), alignment.elementMiddle, function (_ref3) {
var _element$firstChild3;
var from = _ref3.from,
width = _ref3.width,
element = _ref3.element;
return [from, from + width / 2 + ((_element$firstChild3 = element.firstChild) === null || _element$firstChild3 === void 0 ? void 0 : _element$firstChild3.offsetWidth) / 2];
}), alignment.elementRight, function (_ref4) {
var from = _ref4.from,
width = _ref4.width;
return [from, from + width];
}), alignment.elementLeft, function (_ref5) {
var from = _ref5.from;
return [from, from];
});
var HorizontalContainer = (0, _styledComponents["default"])(_netdataUi.Flex).withConfig({
displayName: "container__HorizontalContainer",
componentId: "sc-n9fh2a-0"
})(["position:absolute;", ";", ";", ";", ";", ";", ";overflow:hidden;"], function (_ref6) {
var noTransform = _ref6.noTransform;
return noTransform ? "" : "transform: translateY(-50%)";
}, function (_ref7) {
var top = _ref7.top;
return top && "top: ".concat(top, ";");
}, function (_ref8) {
var bottom = _ref8.bottom;
return bottom && "bottom: ".concat(bottom, ";");
}, function (_ref9) {
var left = _ref9.left;
return left && "left: ".concat(left, ";");
}, function (_ref10) {
var right = _ref10.right;
return right && "right: ".concat(right, ";");
}, function (_ref11) {
var noEvents = _ref11.noEvents;
return noEvents && "pointer-events: none;";
});
var getHorizontalPosition = function getHorizontalPosition() {
var align = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : alignment.elementMiddle;
var chart = arguments.length > 1 ? arguments[1] : undefined;
var area = arguments.length > 2 ? arguments[2] : undefined;
var element = arguments.length > 3 ? arguments[3] : undefined;
var uiName = arguments.length > 4 ? arguments[4] : undefined;
var from = area.from,
width = area.width;
var chartWidth = chart.getUI(uiName).getChartWidth();
var calcAlignment = calcByAlignment[align] || calcByAlignment.elementMiddle;
return calcAlignment({
from: from,
width: width,
chartWidth: chartWidth,
element: element
});
};
var Container = function Container(_ref12) {
var id = _ref12.id,
align = _ref12.align,
_ref12$right = _ref12.right,
right = _ref12$right === void 0 ? 0 : _ref12$right,
fixed = _ref12.fixed,
children = _ref12.children,
uiName = _ref12.uiName,
rest = _objectWithoutProperties(_ref12, _excluded);
var ref = (0, _react.useRef)();
var _useState = (0, _react.useState)(),
_useState2 = _slicedToArray(_useState, 2),
area = _useState2[0],
setArea = _useState2[1];
var chart = (0, _provider.useChart)();
var updateRight = function updateRight(area) {
if (!chart || !chart.getUI(uiName) || !area || !ref.current) return;
var _getHorizontalPositio = getHorizontalPosition(align, chart, area, ref.current, uiName),
_getHorizontalPositio2 = _slicedToArray(_getHorizontalPositio, 2),
calculatedRight = _getHorizontalPositio2[1];
ref.current.style.right = "calc(100% - ".concat(calculatedRight + right, "px)");
};
(0, _react.useLayoutEffect)(function () {
return !fixed && chart.getUI(uiName).on("overlayedAreaChanged:".concat(id), function (area) {
updateRight(area);
setArea(function (s) {
return !!s !== !!area ? area : s;
});
});
}, []);
(0, _react.useLayoutEffect)(function () {
return !fixed && updateRight(area);
}, [area]);
if (!area && !fixed) return null;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(HorizontalContainer, _objectSpread(_objectSpread({
ref: ref
}, rest), {}, {
children: children
}));
};
var _default = exports["default"] = /*#__PURE__*/(0, _react.memo)(Container);