UNPKG

@trap_stevo/legendarybuilderproreact-ui

Version:

The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton

191 lines 8.91 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; 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; } import React, { createContext, useContext, useState, useEffect, useRef, useCallback } from "react"; import hudEventManager from "../HUDManagers/HUDEventManager.js"; import HUDToaster from "./HUDToaster.js"; var HUDWindowContext = /*#__PURE__*/createContext(); export var HUDWindowProvider = function HUDWindowProvider(_ref) { var _ref$toasterContainer = _ref.toasterContainerConfigurationSettings, toasterContainerConfigurationSettings = _ref$toasterContainer === void 0 ? {} : _ref$toasterContainer, _ref$toasterContentCo = _ref.toasterContentConfigurationSettings, toasterContentConfigurationSettings = _ref$toasterContentCo === void 0 ? {} : _ref$toasterContentCo, _ref$toasterItemConfi = _ref.toasterItemConfigurationSettings, toasterItemConfigurationSettings = _ref$toasterItemConfi === void 0 ? {} : _ref$toasterItemConfi, _ref$toasterListConfi = _ref.toasterListConfigurationSettings, toasterListConfigurationSettings = _ref$toasterListConfi === void 0 ? {} : _ref$toasterListConfi, _ref$toasterListHeigh = _ref.toasterListHeight, toasterListHeight = _ref$toasterListHeigh === void 0 ? "569px" : _ref$toasterListHeigh, _ref$toasterHoverDela = _ref.toasterHoverDelay, toasterHoverDelay = _ref$toasterHoverDela === void 0 ? 500 : _ref$toasterHoverDela, _ref$toasterListMode = _ref.toasterListMode, toasterListMode = _ref$toasterListMode === void 0 ? false : _ref$toasterListMode, _ref$defaultZIndex = _ref.defaultZIndex, defaultZIndex = _ref$defaultZIndex === void 0 ? 1000 : _ref$defaultZIndex, children = _ref.children; var _useState = useState([]), _useState2 = _slicedToArray(_useState, 2), minimizedWindows = _useState2[0], setMinimizedWindows = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), toasterVisible = _useState4[0], setToasterVisible = _useState4[1]; var _useState5 = useState(defaultZIndex), _useState6 = _slicedToArray(_useState5, 2), highestZIndex = _useState6[0], setHighestZIndex = _useState6[1]; var _useState7 = useState([]), _useState8 = _slicedToArray(_useState7, 2), windows = _useState8[0], setWindows = _useState8[1]; var windowContentsRef = useRef([]); var _useState9 = useState(windowContentsRef.current), _useState10 = _slicedToArray(_useState9, 2), windowContents = _useState10[0], setWindowContents = _useState10[1]; useEffect(function () { var handleWindowMinimized = function handleWindowMinimized(windowId, windowContent) { setMinimizedWindows(function (prev) { return [].concat(_toConsumableArray(prev), [{ id: windowId, content: windowContent }]); }); setToasterVisible(true); return; }; var handleWindowRestored = function handleWindowRestored(windowId) { setMinimizedWindows(function (prev) { return prev.filter(function (window) { return window.id !== windowId; }); }); return; }; hudEventManager.on("windowMinimized", handleWindowMinimized); hudEventManager.on("windowRestored", handleWindowRestored); return function () { hudEventManager.off("windowMinimized", handleWindowMinimized); hudEventManager.off("windowRestored", handleWindowRestored); return; }; }, []); var handleWindowRegister = useCallback(function (windowId, windowElement) { setWindows(function (prevWindows) { var windowExists = prevWindows.some(function (window) { return window.id === windowId; }); if (windowExists) { return prevWindows; } return [].concat(_toConsumableArray(prevWindows), [{ id: windowId, zIndex: highestZIndex, element: windowElement }]); }); return; }, [highestZIndex]); var handleWindowUnregister = useCallback(function (windowId) { var contentAttributeID = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "windowID"; setWindows(function (prevWindows) { return prevWindows.filter(function (window) { return window.id !== windowId; }); }); setWindowContents(function (prevWindowContents) { var updatedWindowContents = prevWindowContents.filter(function (windowContent) { return (windowContent[contentAttributeID] !== undefined && windowContent[contentAttributeID] !== null && windowContent[contentAttributeID]) !== windowId; }); windowContentsRef.current = updatedWindowContents; return updatedWindowContents; }); return; }, []); var bringToFront = useCallback(function (windowId, setWindowZIndex) { setHighestZIndex(function (prevZIndex) { var newZIndex = prevZIndex + 1; if (setWindowZIndex) { setWindowZIndex(newZIndex); } return newZIndex; }); setWindows(function (prevWindows) { return prevWindows.map(function (window) { return window.id === windowId ? _objectSpread({}, window) : window; }); }); return; }, [highestZIndex]); var minimizeWindow = useCallback(function (windowId) { var currentContent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var window = windows.find(function (windowContent) { return windowContent.id === windowId; }); if (window) { var content = currentContent ? currentContent : window.element; hudEventManager.emit("windowMinimized", windowId, content); } }, [windows]); var closeWindow = useCallback(function (windowId) { var contentAttributeID = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "windowID"; setWindows(function (prevWindows) { return prevWindows.filter(function (window) { return window.id !== windowId; }); }); setWindowContents(function (prevWindowContents) { var updatedWindowContents = prevWindowContents.filter(function (windowContent) { return (windowContent[contentAttributeID] !== undefined && windowContent[contentAttributeID] !== null && windowContent[contentAttributeID]) !== windowId; }); windowContentsRef.current = updatedWindowContents; return updatedWindowContents; }); }, [windows, windowContents]); var handleRestore = useCallback(function (windowId) { hudEventManager.emit("windowRestored", windowId); bringToFront(windowId); return; }, [bringToFront]); var addWindowContents = useCallback(function (windowContent) { setWindowContents(function (prevWindowContents) { var updatedWindowContents = [].concat(_toConsumableArray(prevWindowContents), [windowContent]); windowContentsRef.current = updatedWindowContents; return updatedWindowContents; }); }, [windowContents]); return /*#__PURE__*/React.createElement(HUDWindowContext.Provider, { value: { handleWindowUnregister: handleWindowUnregister, handleWindowRegister: handleWindowRegister, addWindowContents: addWindowContents, minimizeWindow: minimizeWindow, bringToFront: bringToFront, closeWindow: closeWindow, windowContentsRef: windowContentsRef, setWindowContents: setWindowContents, windowContents: windowContents } }, children, /*#__PURE__*/React.createElement(HUDToaster, { items: minimizedWindows, onRestore: handleRestore, visible: toasterVisible, containerConfigurationSettings: toasterContainerConfigurationSettings, contentConfigurationSettings: toasterContentConfigurationSettings, itemConfigurationSettings: toasterItemConfigurationSettings, listConfigurationSettings: toasterListConfigurationSettings, listHeight: toasterListHeight, hoverDelay: toasterHoverDelay, listMode: toasterListMode })); }; export var useHUDWindow = function useHUDWindow() { var context = useContext(HUDWindowContext); if (!context) { throw new Error('Must use within an HUDWindowProvider'); } return context; };