UNPKG

@gpa-gemstone/react-interactive

Version:
193 lines (192 loc) 13.2 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 }"])), 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;' : ''; }); 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 = (0, helper_functions_1.useGetContainerPosition)(divRef), containerHeight = _f.height, containerWidth = _f.width; var _g = React.useState(0), containerTop = _g[0], setContainerTop = _g[1]; var _h = React.useState(0), containerLeft = _h[0], setContainerLeft = _h[1]; var _j = React.useState(0), targetLeft = _j[0], setTargetLeft = _j[1]; var _k = React.useState(0), targetTop = _k[0], setTargetTop = _k[1]; var _l = React.useState(0), targetWidth = _l[0], setTargetWidth = _l[1]; var _m = React.useState(0), targetHeight = _m[0], setTargetHeight = _m[1]; React.useEffect(function () { var targetElement = null; var selector = "[data-drawer".concat(props.Target === undefined ? '' : "=\"".concat(props.Target, "\""), "]"); var updateTargetSize = function () { var targets = document.querySelectorAll(selector); var target = targets.length === 0 ? null : targets[0]; if (targetElement !== target) { if (targetElement != null) resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(targetElement); targetElement = target; if (targetElement != null) resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(targetElement); } if (targetElement == null) { setTargetHeight(0); setTargetWidth(0); setTargetLeft(-999); setTargetTop(-999); return; } var targetLocation = (0, helper_functions_1.GetNodeSize)(targetElement); setTargetHeight(targetLocation.height); setTargetWidth(targetLocation.width); setTargetLeft(targetLocation.left); setTargetTop(targetLocation.top); }; //In older browsers ResizeObserver or MutationObserver might not be defined var resizeObserver = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(updateTargetSize); var mutationObserver = typeof MutationObserver === 'undefined' ? null : new MutationObserver(updateTargetSize); updateTargetSize(); resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(document.body); mutationObserver === null || mutationObserver === void 0 ? void 0 : mutationObserver.observe(document.body, { attributes: true, attributeFilter: ['class', 'data-drawer', 'style'], childList: true, subtree: true }); window.addEventListener('scroll', updateTargetSize, true); return function () { resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect(); mutationObserver === null || mutationObserver === void 0 ? void 0 : mutationObserver.disconnect(); window.removeEventListener('scroll', updateTargetSize, true); }; }, [props.Target]); 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.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;