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

569 lines
17 KiB
JavaScript
Raw Normal View History

2021-04-13 09:43:17 +00:00
import React from "react";
import PropTypes from "prop-types";
import Backdrop from "@appserver/components/backdrop";
2021-11-22 12:01:33 +00:00
import { desktop, size, tablet } from "@appserver/components/utils/device";
import { Provider } from "@appserver/components/utils/context";
import { isMobile, isFirefox, isMobileOnly } from "react-device-detect";
2019-09-12 12:28:13 +00:00
import Article from "./sub-components/article";
import SubArticleHeader from "./sub-components/article-header";
import SubArticleMainButton from "./sub-components/article-main-button";
import SubArticleBody from "./sub-components/article-body";
2019-09-12 12:28:13 +00:00
import ArticlePinPanel from "./sub-components/article-pin-panel";
import Section from "./sub-components/section";
import SubSectionHeader from "./sub-components/section-header";
import SubSectionFilter from "./sub-components/section-filter";
import SubSectionBody from "./sub-components/section-body";
import SubSectionBodyContent from "./sub-components/section-body-content";
import SubSectionPaging from "./sub-components/section-paging";
2019-09-12 12:28:13 +00:00
import SectionToggler from "./sub-components/section-toggler";
import ReactResizeDetector from "react-resize-detector";
import FloatingButton from "../FloatingButton";
2021-02-03 12:42:47 +00:00
import { inject, observer } from "mobx-react";
import Selecto from "react-selecto";
2021-11-22 12:01:33 +00:00
import styled, { css } from "styled-components";
2021-04-13 09:43:17 +00:00
const StyledSelectoWrapper = styled.div`
.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`
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;
}
`}
`;
function ArticleHeader() {
return null;
}
ArticleHeader.displayName = "ArticleHeader";
function ArticleMainButton() {
return null;
}
ArticleMainButton.displayName = "ArticleMainButton";
function ArticleBody() {
return null;
}
ArticleBody.displayName = "ArticleBody";
function SectionHeader() {
return null;
}
SectionHeader.displayName = "SectionHeader";
function SectionFilter() {
return null;
}
SectionFilter.displayName = "SectionFilter";
function SectionBody() {
return null;
}
SectionBody.displayName = "SectionBody";
function SectionPaging() {
return null;
}
SectionPaging.displayName = "SectionPaging";
class PageLayout extends React.Component {
static ArticleHeader = ArticleHeader;
static ArticleMainButton = ArticleMainButton;
static ArticleBody = ArticleBody;
static SectionHeader = SectionHeader;
static SectionFilter = SectionFilter;
static SectionBody = SectionBody;
static SectionPaging = SectionPaging;
constructor(props) {
super(props);
2020-12-04 11:21:51 +00:00
this.timeoutHandler = null;
this.intervalHandler = null;
this.scroll = null;
}
componentDidUpdate(prevProps) {
if (!this.scroll) {
this.scroll = document.getElementsByClassName("section-scroll")[0];
}
if (
this.props.hideAside &&
!this.props.isArticlePinned &&
this.props.hideAside !== prevProps.hideAside
) {
this.backdropClick();
}
}
componentDidMount() {
window.addEventListener("orientationchange", this.orientationChangeHandler);
this.orientationChangeHandler();
}
componentWillUnmount() {
window.removeEventListener(
"orientationchange",
this.orientationChangeHandler
);
2020-12-04 11:21:51 +00:00
2020-12-10 11:25:51 +00:00
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;
}
if (isEnoughWidth && isValueExist) {
this.pinArticle();
}
};
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) => {
2021-06-22 06:56:48 +00:00
if (this.props.dragging) return;
const items = e.selected;
2021-04-13 06:35:07 +00:00
this.props.setSelections(items);
};
dragCondition = (e) => {
const path = e.inputEvent.composedPath();
2021-06-18 11:10:20 +00:00
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,
2020-11-25 10:49:54 +00:00
showPrimaryButtonAlert,
showSecondaryProgressBar,
secondaryProgressBarValue,
secondaryProgressBarIcon,
2020-11-25 10:49:54 +00:00
showSecondaryButtonAlert,
uploadFiles,
viewAs,
2021-04-13 06:35:07 +00:00
//withBodyAutoFocus,
withBodyScroll,
children,
isHeaderVisible,
2021-04-13 06:35:07 +00:00
//headerBorderBottom,
2021-01-19 12:27:09 +00:00
onOpenUploadPanel,
isTabletView,
firstLoad,
dragging,
isArticleVisible,
isBackdropVisible,
isArticlePinned,
2021-09-07 13:41:10 +00:00
isDesktop,
} = this.props;
let articleHeaderContent = null;
let articleMainButtonContent = null;
let articleBodyContent = null;
let sectionHeaderContent = null;
let sectionFilterContent = null;
let sectionPagingContent = null;
let sectionBodyContent = 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;
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 (
<>
2021-04-13 06:35:07 +00:00
{isBackdropAvailable && (
<Backdrop
zIndex={400}
visible={isBackdropVisible}
2021-04-13 06:35:07 +00:00
onClick={this.backdropClick}
/>
)}
{isArticleAvailable && (
<Article
visible={isArticleVisible}
pinned={isArticlePinned}
2021-04-13 06:35:07 +00:00
firstLoad={firstLoad}
>
{isArticleHeaderAvailable && (
<SubArticleHeader>
{articleHeaderContent
? articleHeaderContent.props.children
: null}
</SubArticleHeader>
)}
{isArticleMainButtonAvailable && (
<SubArticleMainButton>
{articleMainButtonContent
? articleMainButtonContent.props.children
: null}
</SubArticleMainButton>
)}
{isArticleBodyAvailable && (
2021-09-07 13:41:10 +00:00
<SubArticleBody pinned={isArticlePinned} isDesktop={isDesktop}>
2021-04-13 06:35:07 +00:00
{articleBodyContent
? articleBodyContent.props.children
: null}
</SubArticleBody>
)}
{isArticleBodyAvailable && (
<ArticlePinPanel
pinned={isArticlePinned}
2021-04-13 06:35:07 +00:00
onPin={this.pinArticle}
onUnpin={this.unpinArticle}
/>
)}
</Article>
)}
{isSectionAvailable && (
<ReactResizeDetector
refreshRate={100}
refreshMode="debounce"
refreshOptions={{ trailing: true }}
>
{({ width, height }) => (
<Provider
value={{
sectionWidth: width,
sectionHeight: height,
}}
>
2021-04-13 06:35:07 +00:00
<Section
widthProp={width}
unpinArticle={this.unpinArticle}
pinned={isArticlePinned}
visible={isArticleVisible}
2021-04-13 06:35:07 +00:00
>
{isSectionHeaderAvailable && (
<SubSectionHeader
isHeaderVisible={isHeaderVisible}
isArticlePinned={isArticlePinned}
2021-10-06 12:44:39 +00:00
viewAs={viewAs}
>
2021-04-13 06:35:07 +00:00
{sectionHeaderContent
? sectionHeaderContent.props.children
: null}
</SubSectionHeader>
)}
2021-07-09 14:25:20 +00:00
2021-04-13 06:35:07 +00:00
{isSectionFilterAvailable && (
2021-07-09 14:25:20 +00:00
<>
2021-11-22 12:01:33 +00:00
<StyledMainBar id="main-bar" />
<SubSectionFilter
className="section-header_filter"
viewAs={viewAs}
>
2021-07-09 14:25:20 +00:00
{sectionFilterContent
? sectionFilterContent.props.children
: null}
</SubSectionFilter>
</>
2021-04-13 06:35:07 +00:00
)}
{isSectionBodyAvailable && (
<>
<SubSectionBody
onDrop={onDrop}
uploadFiles={uploadFiles}
withScroll={withBodyScroll}
autoFocus={isMobile || isTabletView ? false : true}
pinned={isArticlePinned}
2021-04-13 06:35:07 +00:00
viewAs={viewAs}
>
{isSectionFilterAvailable && (
<SubSectionFilter className="section-body_filter">
{sectionFilterContent
? sectionFilterContent.props.children
: null}
</SubSectionFilter>
)}
<SubSectionBodyContent>
{sectionBodyContent
? sectionBodyContent.props.children
: null}
2021-04-13 06:35:07 +00:00
</SubSectionBodyContent>
{isSectionPagingAvailable && (
<SubSectionPaging>
{sectionPagingContent
? sectionPagingContent.props.children
: null}
</SubSectionPaging>
)}
</SubSectionBody>
</>
)}
{showPrimaryProgressBar && showSecondaryProgressBar ? (
<>
<FloatingButton
className="layout-progress-bar"
icon={primaryProgressBarIcon}
percent={primaryProgressBarValue}
alert={showPrimaryButtonAlert}
onClick={onOpenUploadPanel}
/>
<FloatingButton
className="layout-progress-second-bar"
icon={secondaryProgressBarIcon}
percent={secondaryProgressBarValue}
alert={showSecondaryButtonAlert}
/>
</>
) : showPrimaryProgressBar && !showSecondaryProgressBar ? (
<FloatingButton
className="layout-progress-bar"
icon={primaryProgressBarIcon}
percent={primaryProgressBarValue}
2020-11-25 10:49:54 +00:00
alert={showPrimaryButtonAlert}
2021-01-19 12:27:09 +00:00
onClick={onOpenUploadPanel}
/>
2021-04-13 06:35:07 +00:00
) : !showPrimaryProgressBar && showSecondaryProgressBar ? (
<FloatingButton
2021-04-13 06:35:07 +00:00
className="layout-progress-bar"
icon={secondaryProgressBarIcon}
percent={secondaryProgressBarValue}
2020-11-25 10:49:54 +00:00
alert={showSecondaryButtonAlert}
/>
2021-04-13 06:35:07 +00:00
) : (
<></>
)}
{isArticleAvailable && (
<SectionToggler
visible={!isArticleVisible}
2021-04-13 06:35:07 +00:00
onClick={this.showArticle}
/>
)}
</Section>
</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"}
2021-06-22 21:00:42 +00:00
selectableTargets={[".files-item"]}
2021-06-23 12:39:43 +00:00
hitRate={0}
selectByClick={false}
selectFromInside={true}
ratio={0}
continueSelect={false}
onSelect={this.onSelect}
dragCondition={this.dragCondition}
scrollOptions={scrollOptions}
onScroll={this.onScroll}
/>
</StyledSelectoWrapper>
)}
</>
);
}
}
PageLayout.propTypes = {
children: PropTypes.any,
withBodyScroll: PropTypes.bool,
withBodyAutoFocus: PropTypes.bool,
showPrimaryProgressBar: PropTypes.bool,
primaryProgressBarValue: PropTypes.number,
2020-11-25 10:49:54 +00:00
showPrimaryButtonAlert: PropTypes.bool,
progressBarDropDownContent: PropTypes.any,
primaryProgressBarIcon: PropTypes.string,
showSecondaryProgressBar: PropTypes.bool,
secondaryProgressBarValue: PropTypes.number,
secondaryProgressBarIcon: PropTypes.string,
2020-11-25 10:49:54 +00:00
showSecondaryButtonAlert: PropTypes.bool,
onDrop: PropTypes.func,
setSelections: PropTypes.func,
uploadFiles: PropTypes.bool,
hideAside: PropTypes.bool,
viewAs: PropTypes.string,
2021-01-19 12:27:09 +00:00
uploadPanelVisible: PropTypes.bool,
onOpenUploadPanel: PropTypes.func,
2021-01-26 15:21:50 +00:00
isTabletView: PropTypes.bool,
isHeaderVisible: PropTypes.bool,
firstLoad: PropTypes.bool,
};
PageLayout.defaultProps = {
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;
export default inject(({ auth }) => {
const { isLoaded, settingsStore } = auth;
2021-03-03 09:05:08 +00:00
const {
isHeaderVisible,
isTabletView,
isArticlePinned,
isArticleVisible,
isBackdropVisible,
2021-03-03 09:05:08 +00:00
setArticlePinned,
setArticleVisibleOnUnpin,
setIsArticleVisible,
setIsBackdropVisible,
2021-09-07 13:41:10 +00:00
isDesktopClient,
2021-03-03 09:05:08 +00:00
} = settingsStore;
return {
2021-03-03 09:05:08 +00:00
isLoaded,
isTabletView,
2021-03-03 09:05:08 +00:00
isHeaderVisible,
isArticlePinned,
isArticleVisible,
setArticlePinned,
setArticleVisibleOnUnpin,
setIsArticleVisible,
isBackdropVisible,
setIsBackdropVisible,
2021-09-07 13:41:10 +00:00
isDesktop: isDesktopClient,
};
})(observer(PageLayout));