DocSpace-client/packages/asc-web-common/components/PageLayout/index.js

600 lines
18 KiB
JavaScript
Raw Normal View History

import Backdrop from "@appserver/components/backdrop";
import { Provider } from "@appserver/components/utils/context";
2022-02-08 12:11:31 +00:00
import { desktop, size, tablet } from "@appserver/components/utils/device";
import { inject, observer } from "mobx-react";
import PropTypes from "prop-types";
import React from "react";
import { isFirefox, isMobile, isMobileOnly } from "react-device-detect";
import ReactResizeDetector from "react-resize-detector";
import Selecto from "react-selecto";
import styled, { css } from "styled-components";
import FloatingButton from "../FloatingButton";
2019-09-12 12:28:13 +00:00
import Article from "./sub-components/article";
2022-02-08 12:11:31 +00:00
import SubArticleBody from "./sub-components/article-body";
import SubArticleHeader from "./sub-components/article-header";
import SubArticleMainButton from "./sub-components/article-main-button";
2019-09-12 12:28:13 +00:00
import ArticlePinPanel from "./sub-components/article-pin-panel";
2022-02-11 14:35:18 +00:00
import InfoPanel from "./sub-components/info-panel";
import SubInfoPanelBody from "./sub-components/info-panel-body";
import SubInfoPanelHeader from "./sub-components/info-panel-header";
2019-09-12 12:28:13 +00:00
import Section from "./sub-components/section";
import SubSectionBody from "./sub-components/section-body";
import SubSectionBodyContent from "./sub-components/section-body-content";
2022-02-08 12:11:31 +00:00
import SubSectionFilter from "./sub-components/section-filter";
import SubSectionHeader from "./sub-components/section-header";
import SubSectionPaging from "./sub-components/section-paging";
2019-09-12 12:28:13 +00:00
import SectionToggler from "./sub-components/section-toggler";
2021-04-13 09:43:17 +00:00
const StyledSelectoWrapper = styled.div`
2022-03-03 10:43:43 +00:00
.selecto-selection {
z-index: 200;
}
2021-04-13 09:43:17 +00:00
`;
2021-11-22 12:01:33 +00:00
const StyledMainBar = styled.div`
2022-03-03 10:43:43 +00:00
box-sizing: border-box;
${!isMobile
? css`
padding-right: 24px;
@media ${tablet} {
padding-right: 16px;
}
`
: css`
margin-top: 10px;
padding-right: 0px;
@media ${desktop} {
padding-right: 10px;
}
`}
2021-11-22 12:01:33 +00:00
`;
function ArticleHeader() {
2022-03-03 10:43:43 +00:00
return null;
}
ArticleHeader.displayName = "ArticleHeader";
function ArticleMainButton() {
2022-03-03 10:43:43 +00:00
return null;
}
ArticleMainButton.displayName = "ArticleMainButton";
function ArticleBody() {
2022-03-03 10:43:43 +00:00
return null;
}
ArticleBody.displayName = "ArticleBody";
function SectionHeader() {
2022-03-03 10:43:43 +00:00
return null;
}
SectionHeader.displayName = "SectionHeader";
function SectionFilter() {
2022-03-03 10:43:43 +00:00
return null;
}
SectionFilter.displayName = "SectionFilter";
function SectionBody() {
2022-03-03 10:43:43 +00:00
return null;
}
SectionBody.displayName = "SectionBody";
function SectionPaging() {
2022-03-03 10:43:43 +00:00
return null;
}
SectionPaging.displayName = "SectionPaging";
2022-02-11 14:35:18 +00:00
function InfoPanelBody() {
2022-03-03 10:43:43 +00:00
return null;
2022-02-08 12:11:31 +00:00
}
2022-02-11 14:35:18 +00:00
InfoPanelBody.displayName = "InfoPanelBody";
2022-02-09 09:13:16 +00:00
2022-02-11 14:35:18 +00:00
function InfoPanelHeader() {
2022-03-03 10:43:43 +00:00
return null;
2022-02-09 09:13:16 +00:00
}
2022-02-11 14:35:18 +00:00
InfoPanelHeader.displayName = "InfoPanelHeader";
2022-02-08 12:11:31 +00:00
class PageLayout extends React.Component {
2022-03-03 10:43:43 +00:00
static ArticleHeader = ArticleHeader;
static ArticleMainButton = ArticleMainButton;
static ArticleBody = ArticleBody;
static SectionHeader = SectionHeader;
static SectionFilter = SectionFilter;
static SectionBody = SectionBody;
static SectionPaging = SectionPaging;
static InfoPanelBody = InfoPanelBody;
static InfoPanelHeader = InfoPanelHeader;
constructor(props) {
super(props);
this.timeoutHandler = null;
this.intervalHandler = null;
this.scroll = null;
}
componentDidUpdate(prevProps) {
if (!this.scroll) {
this.scroll = document.getElementsByClassName("section-scroll")[0];
}
2022-03-03 10:43:43 +00:00
if (
this.props.hideAside &&
!this.props.isArticlePinned &&
this.props.hideAside !== prevProps.hideAside
) {
this.backdropClick();
}
2022-03-03 10:43:43 +00:00
}
componentDidMount() {
window.addEventListener("orientationchange", this.orientationChangeHandler);
this.orientationChangeHandler();
}
componentWillUnmount() {
window.removeEventListener(
"orientationchange",
this.orientationChangeHandler
);
if (this.intervalHandler) clearInterval(this.intervalHandler);
if (this.timeoutHandler) clearTimeout(this.timeoutHandler);
}
orientationChangeHandler = () => {
const isValueExist = !!this.props.isArticlePinned;
const isEnoughWidth = screen.availWidth > size.smallTablet;
const isPortrait =
isFirefox &&
isMobileOnly &&
screen.orientation.type === "portrait-primary";
if ((!isEnoughWidth && isValueExist) || isPortrait) {
this.backdropClick();
return;
}
2022-03-03 10:43:43 +00:00
if (isEnoughWidth && isValueExist) {
this.pinArticle();
}
2022-03-03 10:43:43 +00:00
};
backdropClick = () => {
this.props.setArticlePinned(false);
this.props.setIsBackdropVisible(false);
this.props.setIsArticleVisible(false);
isMobile && this.props.setArticleVisibleOnUnpin(false);
};
pinArticle = () => {
this.props.setIsBackdropVisible(false);
this.props.setIsArticleVisible(true);
this.props.setArticlePinned(true);
isMobile && this.props.setArticleVisibleOnUnpin(false);
};
unpinArticle = () => {
this.props.setIsBackdropVisible(true);
this.props.setIsArticleVisible(true);
this.props.setArticlePinned(false);
isMobile && this.props.setArticleVisibleOnUnpin(true);
};
showArticle = () => {
this.props.setArticlePinned(false);
this.props.setIsBackdropVisible(true);
this.props.setIsArticleVisible(true);
isMobile && this.props.setArticleVisibleOnUnpin(true);
};
onSelect = (e) => {
if (this.props.dragging) return;
const items = e.selected;
this.props.setSelections(items);
};
dragCondition = (e) => {
const path = e.inputEvent.composedPath();
const isBackdrop = path.some(
(x) => x.classList && x.classList.contains("backdrop-active")
);
const notSelectablePath = path.some(
(x) => x.classList && x.classList.contains("not-selectable")
);
const isDraggable = path.some(
(x) => x.classList && x.classList.contains("draggable")
);
if (notSelectablePath || isBackdrop || isDraggable) {
return false;
} else return true;
};
onScroll = (e) => {
this.scroll.scrollBy(e.direction[0] * 10, e.direction[1] * 10);
};
render() {
const {
onDrop,
showPrimaryProgressBar,
primaryProgressBarIcon,
primaryProgressBarValue,
showPrimaryButtonAlert,
showSecondaryProgressBar,
secondaryProgressBarValue,
secondaryProgressBarIcon,
showSecondaryButtonAlert,
uploadFiles,
viewAs,
//withBodyAutoFocus,
withBodyScroll,
children,
isHeaderVisible,
//headerBorderBottom,
onOpenUploadPanel,
isTabletView,
firstLoad,
dragging,
isArticleVisible,
isBackdropVisible,
isArticlePinned,
isDesktop,
} = this.props;
let articleHeaderContent = null;
let articleMainButtonContent = null;
let articleBodyContent = null;
let sectionHeaderContent = null;
let sectionFilterContent = null;
let sectionPagingContent = null;
let sectionBodyContent = null;
let infoPanelBodyContent = null;
let infoPanelHeaderContent = null;
React.Children.forEach(children, (child) => {
const childType =
child && child.type && (child.type.displayName || child.type.name);
switch (childType) {
case ArticleHeader.displayName:
articleHeaderContent = child;
break;
case ArticleMainButton.displayName:
articleMainButtonContent = child;
break;
case ArticleBody.displayName:
articleBodyContent = child;
break;
case SectionHeader.displayName:
sectionHeaderContent = child;
break;
case SectionFilter.displayName:
sectionFilterContent = child;
break;
case SectionPaging.displayName:
sectionPagingContent = child;
break;
case SectionBody.displayName:
sectionBodyContent = child;
break;
case InfoPanelBody.displayName:
infoPanelBodyContent = child;
break;
case InfoPanelHeader.displayName:
infoPanelHeaderContent = child;
break;
default:
break;
}
});
const isArticleHeaderAvailable = !!articleHeaderContent,
isArticleMainButtonAvailable = !!articleMainButtonContent,
isArticleBodyAvailable = !!articleBodyContent,
isArticleAvailable =
isArticleHeaderAvailable ||
isArticleMainButtonAvailable ||
isArticleBodyAvailable,
isSectionHeaderAvailable = !!sectionHeaderContent,
isSectionFilterAvailable = !!sectionFilterContent,
isSectionPagingAvailable = !!sectionPagingContent,
isSectionBodyAvailable =
!!sectionBodyContent ||
isSectionFilterAvailable ||
isSectionPagingAvailable,
isSectionAvailable =
isSectionHeaderAvailable ||
isSectionFilterAvailable ||
isSectionBodyAvailable ||
isSectionPagingAvailable ||
isArticleAvailable,
isBackdropAvailable = isArticleAvailable;
const renderPageLayout = () => {
return (
<>
{isBackdropAvailable && (
<Backdrop
zIndex={400}
visible={isBackdropVisible}
onClick={this.backdropClick}
/>
)}
{isArticleAvailable && (
<Article
visible={isArticleVisible}
pinned={isArticlePinned}
firstLoad={firstLoad}
>
{isArticleHeaderAvailable && (
<SubArticleHeader>
{articleHeaderContent
? articleHeaderContent.props.children
: null}
</SubArticleHeader>
)}
{isArticleMainButtonAvailable && (
<SubArticleMainButton>
{articleMainButtonContent
? articleMainButtonContent.props.children
: null}
</SubArticleMainButton>
)}
{isArticleBodyAvailable && (
<SubArticleBody pinned={isArticlePinned} isDesktop={isDesktop}>
{articleBodyContent
? articleBodyContent.props.children
: null}
</SubArticleBody>
)}
{isArticleBodyAvailable && (
<ArticlePinPanel
pinned={isArticlePinned}
onPin={this.pinArticle}
onUnpin={this.unpinArticle}
/>
)}
</Article>
)}
{isSectionAvailable && (
<ReactResizeDetector
refreshRate={100}
refreshMode="debounce"
refreshOptions={{ trailing: true }}
>
{({ width, height }) => (
<Provider
value={{
sectionWidth: width,
sectionHeight: height,
}}
>
<Section
widthProp={width}
unpinArticle={this.unpinArticle}
pinned={isArticlePinned}
visible={isArticleVisible}
>
{isSectionHeaderAvailable && (
<SubSectionHeader
isHeaderVisible={isHeaderVisible}
isArticlePinned={isArticlePinned}
viewAs={viewAs}
>
{sectionHeaderContent
? sectionHeaderContent.props.children
: null}
</SubSectionHeader>
2021-04-13 06:35:07 +00:00
)}
2022-03-03 10:43:43 +00:00
{isSectionFilterAvailable && (
<>
<StyledMainBar id="main-bar" />
<SubSectionFilter
className="section-header_filter"
viewAs={viewAs}
2022-02-08 12:11:31 +00:00
>
2022-03-03 10:43:43 +00:00
{sectionFilterContent
? sectionFilterContent.props.children
: null}
</SubSectionFilter>
</>
2021-04-13 06:35:07 +00:00
)}
2022-03-03 10:43:43 +00:00
{isSectionBodyAvailable && (
<>
<SubSectionBody
onDrop={onDrop}
uploadFiles={uploadFiles}
withScroll={withBodyScroll}
autoFocus={isMobile || isTabletView ? false : true}
pinned={isArticlePinned}
viewAs={viewAs}
2022-02-08 12:11:31 +00:00
>
2022-03-03 10:43:43 +00:00
{isSectionFilterAvailable && (
<SubSectionFilter className="section-body_filter">
{sectionFilterContent
? sectionFilterContent.props.children
: null}
</SubSectionFilter>
)}
<SubSectionBodyContent>
{sectionBodyContent
? sectionBodyContent.props.children
: null}
</SubSectionBodyContent>
{isSectionPagingAvailable && (
<SubSectionPaging>
{sectionPagingContent
? sectionPagingContent.props.children
: null}
</SubSectionPaging>
)}
</SubSectionBody>
</>
2022-02-08 12:11:31 +00:00
)}
2022-03-03 10:43:43 +00:00
{showPrimaryProgressBar && showSecondaryProgressBar ? (
<>
<FloatingButton
className="layout-progress-bar"
icon={primaryProgressBarIcon}
percent={primaryProgressBarValue}
alert={showPrimaryButtonAlert}
onClick={onOpenUploadPanel}
2022-02-08 12:11:31 +00:00
/>
2022-03-03 10:43:43 +00:00
<FloatingButton
className="layout-progress-second-bar"
icon={secondaryProgressBarIcon}
percent={secondaryProgressBarValue}
alert={showSecondaryButtonAlert}
/>
</>
) : showPrimaryProgressBar && !showSecondaryProgressBar ? (
<FloatingButton
className="layout-progress-bar"
icon={primaryProgressBarIcon}
percent={primaryProgressBarValue}
alert={showPrimaryButtonAlert}
onClick={onOpenUploadPanel}
/>
) : !showPrimaryProgressBar && showSecondaryProgressBar ? (
<FloatingButton
className="layout-progress-bar"
icon={secondaryProgressBarIcon}
percent={secondaryProgressBarValue}
alert={showSecondaryButtonAlert}
/>
) : (
<></>
)}
{isArticleAvailable && (
<SectionToggler
visible={!isArticleVisible}
onClick={this.showArticle}
/>
)}
</Section>
<InfoPanel>
<SubInfoPanelHeader>
{infoPanelHeaderContent}
</SubInfoPanelHeader>
<SubInfoPanelBody>{infoPanelBodyContent}</SubInfoPanelBody>
</InfoPanel>
</Provider>
)}
</ReactResizeDetector>
)}
</>
);
};
const scrollOptions = this.scroll
? {
container: this.scroll,
throttleTime: 0,
threshold: 100,
}
: {};
return (
<>
{renderPageLayout()}
{!isMobile && uploadFiles && !dragging && (
<StyledSelectoWrapper>
<Selecto
boundContainer={".section-wrapper"}
dragContainer={".section-body"}
selectableTargets={[".files-item"]}
hitRate={0}
selectByClick={false}
selectFromInside={true}
ratio={0}
continueSelect={false}
onSelect={this.onSelect}
dragCondition={this.dragCondition}
scrollOptions={scrollOptions}
onScroll={this.onScroll}
/>
</StyledSelectoWrapper>
)}
</>
);
}
}
PageLayout.propTypes = {
2022-03-03 10:43:43 +00:00
children: PropTypes.any,
withBodyScroll: PropTypes.bool,
withBodyAutoFocus: PropTypes.bool,
showPrimaryProgressBar: PropTypes.bool,
primaryProgressBarValue: PropTypes.number,
showPrimaryButtonAlert: PropTypes.bool,
progressBarDropDownContent: PropTypes.any,
primaryProgressBarIcon: PropTypes.string,
showSecondaryProgressBar: PropTypes.bool,
secondaryProgressBarValue: PropTypes.number,
secondaryProgressBarIcon: PropTypes.string,
showSecondaryButtonAlert: PropTypes.bool,
onDrop: PropTypes.func,
setSelections: PropTypes.func,
uploadFiles: PropTypes.bool,
hideAside: PropTypes.bool,
viewAs: PropTypes.string,
uploadPanelVisible: PropTypes.bool,
onOpenUploadPanel: PropTypes.func,
isTabletView: PropTypes.bool,
isHeaderVisible: PropTypes.bool,
firstLoad: PropTypes.bool,
};
PageLayout.defaultProps = {
2022-03-03 10:43:43 +00:00
withBodyScroll: true,
withBodyAutoFocus: false,
};
PageLayout.ArticleHeader = ArticleHeader;
PageLayout.ArticleMainButton = ArticleMainButton;
PageLayout.ArticleBody = ArticleBody;
PageLayout.SectionHeader = SectionHeader;
PageLayout.SectionFilter = SectionFilter;
PageLayout.SectionBody = SectionBody;
PageLayout.SectionPaging = SectionPaging;
2022-02-11 14:35:18 +00:00
PageLayout.InfoPanelHeader = InfoPanelHeader;
PageLayout.InfoPanelBody = InfoPanelBody;
export default inject(({ auth }) => {
2022-03-03 10:43:43 +00:00
const { isLoaded, settingsStore } = auth;
const {
isHeaderVisible,
isTabletView,
isArticlePinned,
isArticleVisible,
isBackdropVisible,
setArticlePinned,
setArticleVisibleOnUnpin,
setIsArticleVisible,
setIsBackdropVisible,
isDesktopClient,
} = settingsStore;
return {
isLoaded,
isTabletView,
isHeaderVisible,
isArticlePinned,
isArticleVisible,
setArticlePinned,
setArticleVisibleOnUnpin,
setIsArticleVisible,
isBackdropVisible,
setIsBackdropVisible,
isDesktop: isDesktopClient,
};
})(observer(PageLayout));