1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Page = exports.PageContextConsumer = exports.PageContextProvider = exports.PageContext = exports.pageContextDefaults = exports.PageLayouts = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const React = tslib_1.__importStar(require("react"));
|
6 | const page_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Page/page"));
|
7 | const react_styles_1 = require("@patternfly/react-styles");
|
8 | const global_breakpoint_xl_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/global_breakpoint_xl'));
|
9 | const util_1 = require("../../helpers/util");
|
10 | const Drawer_1 = require("../Drawer");
|
11 | const PageGroup_1 = require("./PageGroup");
|
12 | const resizeObserver_1 = require("../../helpers/resizeObserver");
|
13 | const util_2 = require("../../helpers/util");
|
14 | var PageLayouts;
|
15 | (function (PageLayouts) {
|
16 | PageLayouts["vertical"] = "vertical";
|
17 | PageLayouts["horizontal"] = "horizontal";
|
18 | })(PageLayouts = exports.PageLayouts || (exports.PageLayouts = {}));
|
19 | exports.pageContextDefaults = {
|
20 | isManagedSidebar: false,
|
21 | isNavOpen: false,
|
22 | onNavToggle: () => null,
|
23 | width: null,
|
24 | getBreakpoint: util_2.getBreakpoint
|
25 | };
|
26 | exports.PageContext = React.createContext(exports.pageContextDefaults);
|
27 | exports.PageContextProvider = exports.PageContext.Provider;
|
28 | exports.PageContextConsumer = exports.PageContext.Consumer;
|
29 | class Page extends React.Component {
|
30 | constructor(props) {
|
31 | super(props);
|
32 | this.mainRef = React.createRef();
|
33 | this.pageRef = React.createRef();
|
34 | this.observer = () => { };
|
35 | this.getWindowWidth = () => {
|
36 | if (util_1.canUseDOM) {
|
37 | return this.pageRef.current ? this.pageRef.current.clientWidth : window.innerWidth;
|
38 | }
|
39 | else {
|
40 | return 1200;
|
41 | }
|
42 | };
|
43 | this.isMobile = () =>
|
44 |
|
45 | this.getWindowWidth() < Number.parseInt(global_breakpoint_xl_1.default.value, 10);
|
46 | this.resize = () => {
|
47 | const { onPageResize } = this.props;
|
48 | const mobileView = this.isMobile();
|
49 | if (onPageResize) {
|
50 | onPageResize({ mobileView, windowSize: this.getWindowWidth() });
|
51 | }
|
52 | if (mobileView !== this.state.mobileView) {
|
53 | this.setState({ mobileView });
|
54 | }
|
55 | this.pageRef.current && this.setState({ width: this.pageRef.current.clientWidth });
|
56 | };
|
57 | this.handleResize = util_1.debounce(this.resize, 250);
|
58 | this.handleMainClick = () => {
|
59 | if (this.isMobile() && this.state.mobileIsNavOpen && this.mainRef.current) {
|
60 | this.setState({ mobileIsNavOpen: false });
|
61 | }
|
62 | };
|
63 | this.onNavToggleMobile = () => {
|
64 | this.setState(prevState => ({
|
65 | mobileIsNavOpen: !prevState.mobileIsNavOpen
|
66 | }));
|
67 | };
|
68 | this.onNavToggleDesktop = () => {
|
69 | this.setState(prevState => ({
|
70 | desktopIsNavOpen: !prevState.desktopIsNavOpen
|
71 | }));
|
72 | };
|
73 | const { isManagedSidebar, defaultManagedSidebarIsOpen } = props;
|
74 | const managedSidebarOpen = !isManagedSidebar ? true : defaultManagedSidebarIsOpen;
|
75 | this.state = {
|
76 | desktopIsNavOpen: managedSidebarOpen,
|
77 | mobileIsNavOpen: false,
|
78 | mobileView: false,
|
79 | width: null
|
80 | };
|
81 | }
|
82 | componentDidMount() {
|
83 | const { isManagedSidebar, onPageResize } = this.props;
|
84 | if (isManagedSidebar || onPageResize) {
|
85 | this.observer = resizeObserver_1.getResizeObserver(this.pageRef.current, this.handleResize);
|
86 | const currentRef = this.mainRef.current;
|
87 | if (currentRef) {
|
88 | currentRef.addEventListener('mousedown', this.handleMainClick);
|
89 | currentRef.addEventListener('touchstart', this.handleMainClick);
|
90 | }
|
91 |
|
92 | this.resize();
|
93 | }
|
94 | }
|
95 | componentWillUnmount() {
|
96 | const { isManagedSidebar, onPageResize } = this.props;
|
97 | if (isManagedSidebar || onPageResize) {
|
98 | this.observer();
|
99 | const currentRef = this.mainRef.current;
|
100 | if (currentRef) {
|
101 | currentRef.removeEventListener('mousedown', this.handleMainClick);
|
102 | currentRef.removeEventListener('touchstart', this.handleMainClick);
|
103 | }
|
104 | }
|
105 | }
|
106 | render() {
|
107 | const _a = this.props, { breadcrumb, isBreadcrumbWidthLimited, className, children, header, sidebar, notificationDrawer, isNotificationDrawerExpanded, onNotificationDrawerExpand, isTertiaryNavWidthLimited, skipToContent, role, mainContainerId, isManagedSidebar,
|
108 |
|
109 | defaultManagedSidebarIsOpen,
|
110 |
|
111 | onPageResize, getBreakpoint, mainAriaLabel, mainTabIndex, tertiaryNav, isTertiaryNavGrouped, isBreadcrumbGrouped, additionalGroupedContent, groupProps } = _a, rest = tslib_1.__rest(_a, ["breadcrumb", "isBreadcrumbWidthLimited", "className", "children", "header", "sidebar", "notificationDrawer", "isNotificationDrawerExpanded", "onNotificationDrawerExpand", "isTertiaryNavWidthLimited", "skipToContent", "role", "mainContainerId", "isManagedSidebar", "defaultManagedSidebarIsOpen", "onPageResize", "getBreakpoint", "mainAriaLabel", "mainTabIndex", "tertiaryNav", "isTertiaryNavGrouped", "isBreadcrumbGrouped", "additionalGroupedContent", "groupProps"]);
|
112 | const { mobileView, mobileIsNavOpen, desktopIsNavOpen, width } = this.state;
|
113 | const context = {
|
114 | isManagedSidebar,
|
115 | onNavToggle: mobileView ? this.onNavToggleMobile : this.onNavToggleDesktop,
|
116 | isNavOpen: mobileView ? mobileIsNavOpen : desktopIsNavOpen,
|
117 | width,
|
118 | getBreakpoint
|
119 | };
|
120 | let nav = null;
|
121 | if (tertiaryNav && isTertiaryNavWidthLimited) {
|
122 | nav = (React.createElement("div", { className: react_styles_1.css(page_1.default.pageMainNav, page_1.default.modifiers.limitWidth) },
|
123 | React.createElement("div", { className: react_styles_1.css(page_1.default.pageMainBody) }, tertiaryNav)));
|
124 | }
|
125 | else if (tertiaryNav) {
|
126 | nav = React.createElement("div", { className: react_styles_1.css(page_1.default.pageMainNav) }, tertiaryNav);
|
127 | }
|
128 | let crumb = null;
|
129 | if (breadcrumb && isBreadcrumbWidthLimited) {
|
130 | crumb = (React.createElement("section", { className: react_styles_1.css(page_1.default.pageMainBreadcrumb, page_1.default.modifiers.limitWidth) },
|
131 | React.createElement("div", { className: react_styles_1.css(page_1.default.pageMainBody) }, breadcrumb)));
|
132 | }
|
133 | else if (breadcrumb) {
|
134 | crumb = React.createElement("section", { className: react_styles_1.css(page_1.default.pageMainBreadcrumb) }, breadcrumb);
|
135 | }
|
136 | const isGrouped = isTertiaryNavGrouped || isBreadcrumbGrouped || additionalGroupedContent;
|
137 | const group = isGrouped ? (React.createElement(PageGroup_1.PageGroup, Object.assign({}, groupProps),
|
138 | isTertiaryNavGrouped && nav,
|
139 | isBreadcrumbGrouped && crumb,
|
140 | additionalGroupedContent)) : null;
|
141 | const main = (React.createElement("main", { ref: this.mainRef, role: role, id: mainContainerId, className: react_styles_1.css(page_1.default.pageMain), tabIndex: mainTabIndex, "aria-label": mainAriaLabel },
|
142 | group,
|
143 | !isTertiaryNavGrouped && nav,
|
144 | !isBreadcrumbGrouped && crumb,
|
145 | children));
|
146 | const panelContent = React.createElement(Drawer_1.DrawerPanelContent, null, notificationDrawer);
|
147 | return (React.createElement(exports.PageContextProvider, { value: context },
|
148 | React.createElement("div", Object.assign({ ref: this.pageRef }, rest, { className: react_styles_1.css(page_1.default.page, width !== null && 'pf-m-resize-observer', width !== null && `pf-m-breakpoint-${getBreakpoint(width)}`, className) }),
|
149 | skipToContent,
|
150 | header,
|
151 | sidebar,
|
152 | notificationDrawer && (React.createElement("div", { className: react_styles_1.css(page_1.default.pageDrawer) },
|
153 | React.createElement(Drawer_1.Drawer, { isExpanded: isNotificationDrawerExpanded, onExpand: onNotificationDrawerExpand },
|
154 | React.createElement(Drawer_1.DrawerContent, { panelContent: panelContent },
|
155 | React.createElement(Drawer_1.DrawerContentBody, null, main))))),
|
156 | !notificationDrawer && main)));
|
157 | }
|
158 | }
|
159 | exports.Page = Page;
|
160 | Page.displayName = 'Page';
|
161 | Page.defaultProps = {
|
162 | isManagedSidebar: false,
|
163 | isBreadcrumbWidthLimited: false,
|
164 | defaultManagedSidebarIsOpen: true,
|
165 | onPageResize: () => null,
|
166 | mainTabIndex: -1,
|
167 | isNotificationDrawerExpanded: false,
|
168 | onNotificationDrawerExpand: () => null,
|
169 | getBreakpoint: util_2.getBreakpoint
|
170 | };
|
171 |
|
\ | No newline at end of file |