UNPKG

@gpa-gemstone/react-interactive

Version:
196 lines (195 loc) 13 kB
"use strict"; // ****************************************************************************************************** // OverlayDrawer.tsx - Gbtc // // Copyright © 2023, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 12/25/2022 - Christoph Lackner // Generated original version of source code. // ****************************************************************************************************** var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var helper_functions_1 = require("@gpa-gemstone/helper-functions"); var styled_components_1 = __importDefault(require("styled-components")); /* top-left | top-right | bottom-right | bottom-left */ var ClosedOverlayDiv = styled_components_1.default.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n & {\n border-radius: ", "px\n ", "px\n ", "px\n ", "px;\n display: inline-block;\n font-size: 13px;\n position: fixed;\n z-index: 1050;\n color: #fff;\n background: rgba(34, 2, 0, 0.6);\n top: ", "px;\n left: ", "px;\n height: ", "px;\n width: ", "px;\n writing-Mode: ", ";\n text-Orientation: upright;\n cursor: pointer;\n vertical-align: middle;\n text-align: center;\n }"], ["\n & {\n border-radius: ", "px\n ", "px\n ", "px\n ", "px;\n display: inline-block;\n font-size: 13px;\n position: fixed;\n z-index: 1050;\n color: #fff;\n background: rgba(34, 2, 0, 0.6);\n top: ", "px;\n left: ", "px;\n height: ", "px;\n width: ", "px;\n writing-Mode: ", ";\n text-Orientation: upright;\n cursor: pointer;\n vertical-align: middle;\n text-align: center;\n }"])), function (props) { return props.Location === 'bottom' || props.Location === 'right' ? 4 : 0; }, function (props) { return props.Location === 'bottom' || props.Location === 'left' ? 4 : 0; }, function (props) { return props.Location === 'top' || props.Location === 'left' ? 4 : 0; }, function (props) { return props.Location === 'top' || props.Location === 'right' ? 4 : 0; }, function (props) { return props.Location === 'top' ? Math.floor(props.Top) : Math.ceil(props.Top); }, function (props) { return props.Location === 'left' ? Math.floor(props.Left) : Math.floor(props.Left); }, function (props) { return props.Location === 'bottom' ? Math.ceil(props.Height) : Math.floor(props.Height); }, function (props) { return props.Location === 'right' ? Math.ceil(props.Width) : Math.floor(props.Width); }, function (props) { return props.Location === 'bottom' || props.Location === 'top' ? 'horizontal-tb' : 'vertical-rl'; }); var OpenOverlayDiv = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n & {\n display: inline-block;\n position: fixed;\n transition: opacity 0.3s ease-out;\n z-index: 1050;\n color: #fff;\n background: rgba(34, 2, 0, 0.8);\n top: ", ";\n left: ", ";\n padding-left: 10px;\n padding-top: 10px;\n padding-bottom: 10px;\n padding-right: 10px;\n opacity: ", ";\n ", "\n }"], ["\n & {\n display: inline-block;\n position: fixed;\n transition: opacity 0.3s ease-out;\n z-index: 1050;\n color: #fff;\n background: rgba(34, 2, 0, 0.8);\n top: ", ";\n left: ", ";\n padding-left: 10px;\n padding-top: 10px;\n padding-bottom: 10px;\n padding-right: 10px;\n opacity: ", ";\n ", "\n }" // Props Description: // Title: the string displayed when the drawer is closed but hovered over // Open: indicates the initial state of the drawer // Location: location of the drawer in the component refferenced // Target: The data-drawer property of the target containing the drawer // GetOverride: This will be called with a callback to set the Drawer to open or closed form the parent // OnChange: Callback when the Drawer changes ])), function (props) { return "".concat(props.Top, "px"); }, function (props) { return "".concat(props.Left, "px"); }, function (props) { return props.Open ? '1.0' : '0'; }, function (props) { return !props.Open ? 'pointer-events: none;' : ''; }); // Props Description: // Title: the string displayed when the drawer is closed but hovered over // Open: indicates the initial state of the drawer // Location: location of the drawer in the component refferenced // Target: The data-drawer property of the target containing the drawer // GetOverride: This will be called with a callback to set the Drawer to open or closed form the parent // OnChange: Callback when the Drawer changes var OverlayDrawer = function (props) { var divRef = React.useRef(null); var _a = React.useState(0), top = _a[0], setTop = _a[1]; var _b = React.useState(0), left = _b[0], setLeft = _b[1]; var _c = React.useState(0), width = _c[0], setWidth = _c[1]; var _d = React.useState(0), height = _d[0], setHeight = _d[1]; var _e = React.useState(props.Open), open = _e[0], setOpen = _e[1]; var _f = React.useState(0), containerWidth = _f[0], setContainerWidth = _f[1]; var _g = React.useState(0), containerHeight = _g[0], setContainerHeight = _g[1]; var _h = React.useState(0), containerTop = _h[0], setContainerTop = _h[1]; var _j = React.useState(0), containerLeft = _j[0], setContainerLeft = _j[1]; var _k = React.useState(0), targetLeft = _k[0], setTargetLeft = _k[1]; var _l = React.useState(0), targetTop = _l[0], setTargetTop = _l[1]; var _m = React.useState(0), targetWidth = _m[0], setTargetWidth = _m[1]; var _o = React.useState(0), targetHeight = _o[0], setTargetHeight = _o[1]; var _p = React.useState(false), x = _p[0], setX = _p[1]; React.useEffect(function () { var target = document.querySelectorAll("[data-drawer".concat(props.Target === undefined ? '' : "=\"".concat(props.Target, "\""), "]")); if (target.length === 0) { setTargetHeight(0); setTargetWidth(0); setTargetLeft(-999); setTargetTop(-999); } else { var targetLocation = (0, helper_functions_1.GetNodeSize)(target[0]); setTargetHeight(targetLocation.height); setTargetWidth(targetLocation.width); setTargetLeft(targetLocation.left); setTargetTop(targetLocation.top); } var h = setTimeout(function () { setX(function (a) { return !a; }); }, 500); return function () { if (h !== null) clearTimeout(h); }; }, [x]); React.useEffect(function () { var size = UpdatePosition(); if (size == null) { setTop(0); setLeft(0); setWidth(0); setHeight(0); return; } setTop(size[1]); setLeft(size[0]); setWidth(size[2]); setHeight(size[3]); }, [props.Location, targetHeight, targetWidth, targetLeft, targetTop]); React.useEffect(function () { if (props.GetOverride !== undefined) props.GetOverride(changeStatus); }, [props.GetOverride]); React.useLayoutEffect(function () { var _a, _b, _c, _d; setContainerWidth((_b = (_a = divRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth) !== null && _b !== void 0 ? _b : width); setContainerHeight((_d = (_c = divRef.current) === null || _c === void 0 ? void 0 : _c.offsetHeight) !== null && _d !== void 0 ? _d : height); }); React.useEffect(function () { var l = 0; if (props.Location === 'bottom' || props.Location === 'top') l = left + 0.5 * width - 0.5 * containerWidth; if (props.Location === 'right') l = left + width - containerWidth; if (props.Location === 'left') l = left; setContainerLeft(l); }, [props.Location, left, containerWidth, width]); React.useEffect(function () { var t = 0; if (props.Location === 'right' || props.Location === 'left') t = top + 0.5 * height - 0.5 * containerHeight; if (props.Location === 'top') t = top; if (props.Location === 'bottom') t = top + height - containerHeight; setContainerTop(t); }, [props.Location, top, containerHeight, height]); function changeStatus(o) { setOpen(o); } function UpdatePosition() { var w = 0; var h = 0; var l = 0; var t = 0; if (props.Location === 'bottom' || props.Location === 'top') { w = targetWidth; h = 15; } if (props.Location === 'left' || props.Location === 'right') { h = targetHeight; w = 15; } if (props.Location === 'bottom' || props.Location === 'left' || props.Location === 'top') l = targetLeft; if (props.Location === 'right') l = targetLeft + targetWidth - w; if (props.Location === 'right' || props.Location === 'left' || props.Location === 'top') t = targetTop; if (props.Location === 'bottom') t = targetTop + targetHeight - h; return [l, t, w, h]; } return React.createElement(React.Fragment, null, !(props.HideHandle === undefined ? false : props.HideHandle) ? (!open ? React.createElement(ClosedOverlayDiv, { onClick: function () { setOpen(true); if (props.OnChange !== undefined) props.OnChange(true); }, Location: props.Location, Height: height, Left: left, Top: top, Width: width }, props.Title) : React.createElement(ClosedOverlayDiv, { onClick: function () { setOpen(false); if (props.OnChange !== undefined) props.OnChange(false); }, Location: props.Location, Height: (props.Location === 'top' || props.Location === 'bottom' ? height : containerHeight), Left: (props.Location === 'top' || props.Location === 'bottom' ? containerLeft : containerLeft + (props.Location === 'left' ? containerWidth : -(width))), Top: (props.Location === 'left' || props.Location === 'right' ? containerTop : containerTop + (props.Location === 'top' ? containerHeight : -(height))), Width: (props.Location === 'left' || props.Location === 'right' ? width : containerWidth) }, props.Title)) : null, React.createElement(OpenOverlayDiv, { Location: props.Location, Left: containerLeft, Top: containerTop, Open: open, ref: divRef, style: { minHeight: height, minWidth: width } }, props.children)); }; exports.default = OverlayDrawer; var templateObject_1, templateObject_2;