1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-19 23:48:51 +00:00
seahub/frontend/src/app.js

351 lines
12 KiB
JavaScript
Raw Normal View History

2018-12-21 07:40:59 +00:00
import React, { Component } from 'react';
import ReactDom from 'react-dom';
import { Router, navigate } from '@gatsbyjs/reach-router';
2019-05-23 02:10:46 +00:00
import MediaQuery from 'react-responsive';
import { Modal } from 'reactstrap';
2024-05-06 09:09:47 +00:00
import { siteRoot } from './utils/constants';
2018-12-13 12:42:51 +00:00
import { Utils } from './utils/utils';
2019-03-15 08:06:51 +00:00
import SystemNotification from './components/system-notification';
import EventBus from './components/common/event-bus';
import Header from './components/header';
2018-09-21 06:16:15 +00:00
import SidePanel from './components/side-panel';
import ResizeBar from './components/resize-bar';
import {
DRAG_HANDLER_HEIGHT,
INIT_SIDE_PANEL_RATE,
MAX_SIDE_PANEL_RATE,
MIN_SIDE_PANEL_RATE
} from './components/resize-bar/constants';
2018-09-21 06:16:15 +00:00
import FilesActivities from './pages/dashboard/files-activities';
import MyFileActivities from './pages/dashboard/my-file-activities';
import Starred from './pages/starred/starred';
import LinkedDevices from './pages/linked-devices/linked-devices';
import ShareAdminLibraries from './pages/share-admin/libraries';
import ShareAdminFolders from './pages/share-admin/folders';
import ShareAdminShareLinks from './pages/share-admin/share-links';
import ShareAdminUploadLinks from './pages/share-admin/upload-links';
import SharedLibraries from './pages/shared-libs/shared-libs';
import ShareWithOCM from './pages/share-with-ocm/shared-with-ocm';
2021-09-27 09:44:23 +00:00
import OCMViaWebdav from './pages/ocm-via-webdav/ocm-via-webdav';
import OCMRepoDir from './pages/share-with-ocm/remote-dir-view';
2018-11-30 09:18:41 +00:00
import MyLibraries from './pages/my-libs/my-libs';
2018-12-08 10:29:13 +00:00
import MyLibDeleted from './pages/my-libs/my-libs-deleted';
import PublicSharedView from './pages/shared-with-all';
2019-02-20 03:54:25 +00:00
import LibContentView from './pages/lib-content-view/lib-content-view';
2018-12-10 10:33:50 +00:00
import Group from './pages/groups/group-view';
2019-04-12 05:28:15 +00:00
import InvitationsView from './pages/invitations/invitations-view';
2018-12-07 16:01:23 +00:00
import Wikis from './pages/wikis/wikis';
import Libraries from './pages/libraries';
2018-09-21 06:16:15 +00:00
import './css/layout.css';
import './css/toolbar.css';
import './css/search.css';
2018-11-26 09:53:18 +00:00
2018-09-21 06:16:15 +00:00
class App extends Component {
constructor(props) {
super(props);
if (window.location.pathname === '/groups/') {
window.location.href = window.location.origin + '/libraries/';
}
2018-09-21 06:16:15 +00:00
this.state = {
isSidePanelClosed: false,
isSidePanelFolded: localStorage.getItem('sf_user_side_nav_folded') == 'true' || false,
currentTab: '',
2018-12-13 06:40:09 +00:00
pathPrefix: [],
inResizing: false,
sidePanelRate: parseFloat(localStorage.getItem('sf_side_panel_rate') || INIT_SIDE_PANEL_RATE),
2018-09-21 06:16:15 +00:00
};
this.dirViewPanels = ['libraries', 'my-libs', 'shared-libs', 'org']; // and group
2019-04-22 10:05:18 +00:00
window.onpopstate = this.onpopstate;
const eventBus = new EventBus();
this.eventBus = eventBus;
this.resizeBarRef = React.createRef();
this.dragHandlerRef = React.createRef();
2019-04-22 10:05:18 +00:00
}
onpopstate = (event) => {
if (event.state && event.state.currentTab && event.state.pathPrefix) {
let { currentTab, pathPrefix } = event.state;
2024-07-18 03:58:42 +00:00
this.setState({ currentTab, pathPrefix });
2019-04-22 10:05:18 +00:00
}
};
2018-09-21 06:16:15 +00:00
UNSAFE_componentWillMount() {
2019-08-22 12:23:08 +00:00
if (!Utils.isDesktop()) {
2019-05-23 02:10:46 +00:00
this.setState({
isSidePanelClosed: true
});
}
}
2024-07-18 03:58:42 +00:00
navigateClientUrlToLib = () => {
if (window.location.hash && window.location.hash.indexOf('common/lib') != -1) {
let splitUrlArray = window.location.hash.split('/');
2019-06-28 03:38:20 +00:00
let repoID = splitUrlArray[splitUrlArray.length - 2];
let url = siteRoot + 'library/' + repoID + '/';
2024-07-18 03:58:42 +00:00
navigate(url, { repalce: true });
}
};
2018-11-01 09:52:59 +00:00
componentDidMount() {
// url from client e.g. http://127.0.0.1:8000/#common/lib/34e7fb92-e91d-499d-bcde-c30ea8af9828/
// navigate to library page http://127.0.0.1:8000/library/34e7fb92-e91d-499d-bcde-c30ea8af9828/
this.navigateClientUrlToLib();
let currentTab;
// when visit the siteRoot page, highlight the 'Files' tab in the side nav.
if (location.pathname == siteRoot) {
currentTab = 'libraries';
} else {
let href = window.location.href.split('/');
currentTab = href[href.length - 2];
}
2024-07-18 03:58:42 +00:00
this.setState({ currentTab: currentTab });
2018-11-01 09:52:59 +00:00
}
onCloseSidePanel = () => {
this.setState({
isSidePanelClosed: !this.state.isSidePanelClosed
});
};
onShowSidePanel = () => {
this.setState({
isSidePanelClosed: !this.state.isSidePanelClosed
});
};
2018-12-13 12:42:51 +00:00
onSearchedClick = (selectedItem) => {
if (selectedItem.is_dir === true) {
2024-07-18 03:58:42 +00:00
this.setState({ currentTab: '', pathPrefix: [] });
2018-12-13 12:42:51 +00:00
let url = siteRoot + 'library/' + selectedItem.repo_id + '/' + selectedItem.repo_name + selectedItem.path;
2024-07-18 03:58:42 +00:00
navigate(url, { repalce: true });
2018-12-13 12:42:51 +00:00
} else {
2018-12-14 13:52:54 +00:00
let url = siteRoot + 'lib/' + selectedItem.repo_id + '/file' + Utils.encodePath(selectedItem.path);
2019-10-15 02:16:48 +00:00
let isWeChat = Utils.isWeChat();
if (!isWeChat) {
let newWindow = window.open('about:blank');
newWindow.location.href = url;
} else {
location.href = url;
}
2018-12-13 12:42:51 +00:00
}
};
2018-11-26 06:00:32 +00:00
2018-12-19 02:44:23 +00:00
onGroupChanged = (groupID) => {
2024-07-18 03:58:42 +00:00
setTimeout(function () {
2018-12-20 10:22:07 +00:00
let url;
2018-12-19 02:44:23 +00:00
if (groupID) {
2018-12-20 10:22:07 +00:00
url = siteRoot + 'group/' + groupID + '/';
2018-12-19 02:44:23 +00:00
}
else {
url = siteRoot + 'libraries/';
2018-12-19 02:44:23 +00:00
}
window.location = url.toString();
}, 1);
};
2018-12-19 02:44:23 +00:00
2018-12-11 09:52:19 +00:00
tabItemClick = (tabName, groupID) => {
2018-12-13 06:40:09 +00:00
let pathPrefix = [];
if (groupID || this.dirViewPanels.indexOf(tabName) > -1) {
pathPrefix = this.generatorPrefix(tabName, groupID);
}
2018-12-11 09:52:19 +00:00
this.setState({
currentTab: tabName,
pathPrefix: pathPrefix
2019-04-22 10:05:18 +00:00
}, () => {
let { currentTab, pathPrefix } = this.state;
2024-07-18 03:58:42 +00:00
window.history.replaceState({ currentTab: currentTab, pathPrefix: pathPrefix }, null);
2018-12-11 09:52:19 +00:00
});
2019-08-22 12:23:08 +00:00
if (!Utils.isDesktop() && !this.state.isSidePanelClosed) {
2019-05-23 02:10:46 +00:00
this.setState({ isSidePanelClosed: true });
}
};
2018-11-01 09:52:59 +00:00
2018-12-13 06:40:09 +00:00
generatorPrefix = (tabName, groupID) => {
let pathPrefix = [];
if (groupID) {
let navTab = {
2018-12-13 06:40:09 +00:00
url: siteRoot + 'group/' + groupID + '/',
showName: tabName,
name: tabName,
id: groupID,
};
pathPrefix.push(navTab);
2018-12-13 06:40:09 +00:00
} else {
let navTab = {
url: siteRoot + tabName + '/',
2018-12-13 08:35:11 +00:00
showName: this.getTabShowName(tabName),
2018-12-13 06:40:09 +00:00
name: tabName,
id: null,
};
pathPrefix.push(navTab);
}
return pathPrefix;
};
2018-12-13 06:40:09 +00:00
getTabShowName = (tabName) => {
switch (tabName) {
case 'libraries': {
return 'Files';
}
case 'my-libs': {
return 'My Libraries';
}
case 'shared-libs': {
return 'Shared with me';
}
case 'org': {
return 'Shared with all';
}
2018-12-11 09:52:19 +00:00
}
};
2018-11-30 09:18:41 +00:00
2019-05-23 02:10:46 +00:00
toggleSidePanel = () => {
this.setState({
isSidePanelClosed: !this.state.isSidePanelClosed
});
};
2019-05-23 02:10:46 +00:00
toggleFoldSideNav = () => {
this.setState({
isSidePanelFolded: !this.state.isSidePanelFolded
}, () => {
localStorage.setItem('sf_user_side_nav_folded', this.state.isSidePanelFolded);
});
};
onResizeMouseUp = () => {
if (this.state.inResizing) {
this.setState({
inResizing: false
});
}
localStorage.setItem('sf_side_panel_rate', this.state.sidePanelRate);
};
onResizeMouseDown = () => {
this.setState({
inResizing: true
});
};
onResizeMouseMove = (e) => {
let rate = e.nativeEvent.clientX / window.innerWidth;
this.setState({
sidePanelRate: Math.max(Math.min(rate, MAX_SIDE_PANEL_RATE), MIN_SIDE_PANEL_RATE),
});
};
onResizeMouseOver = (event) => {
if (!this.dragHandlerRef.current) return;
const { top } = this.resizeBarRef.current.getBoundingClientRect();
const dragHandlerRefTop = event.pageY - top - DRAG_HANDLER_HEIGHT / 2;
this.setDragHandlerTop(dragHandlerRefTop);
};
setDragHandlerTop = (top) => {
this.dragHandlerRef.current.style.top = top + 'px';
};
render() {
const { currentTab, isSidePanelClosed, isSidePanelFolded, sidePanelRate, inResizing } = this.state;
let mainPanelStyle = {};
let sidePanelStyle = {};
if (!isSidePanelFolded) {
mainPanelStyle = {
userSelect: inResizing ? 'none' : '',
flex: sidePanelRate ? `1 0 ${(1 - sidePanelRate) * 100}%` : `0 0 ${100 - INIT_SIDE_PANEL_RATE * 100}%`,
};
sidePanelStyle = {
userSelect: inResizing ? 'none' : '',
flex: sidePanelRate ? `0 0 ${sidePanelRate * 100}%` : `0 0 ${INIT_SIDE_PANEL_RATE * 100}%`,
};
}
2018-09-21 06:16:15 +00:00
return (
2019-03-15 08:06:51 +00:00
<React.Fragment>
<SystemNotification />
<Header
isSidePanelClosed={isSidePanelClosed}
onCloseSidePanel={this.onCloseSidePanel}
onShowSidePanel={this.onShowSidePanel}
onSearchedClick={this.onSearchedClick}
eventBus={this.eventBus}
/>
<div
id="main"
className="user-panel"
onMouseMove={inResizing ? this.onResizeMouseMove : null}
onMouseUp={this.onResizeMouseUp}
>
<SidePanel
isSidePanelClosed={isSidePanelClosed}
isSidePanelFolded={isSidePanelFolded}
onCloseSidePanel={this.onCloseSidePanel}
currentTab={currentTab}
tabItemClick={this.tabItemClick}
toggleFoldSideNav={this.toggleFoldSideNav}
style={sidePanelStyle}
/>
{!isSidePanelFolded &&
<ResizeBar
resizeBarRef={this.resizeBarRef}
dragHandlerRef={this.dragHandlerRef}
resizeBarStyle={{ left: `calc(${sidePanelRate ? sidePanelRate * 100 + '%' : `${INIT_SIDE_PANEL_RATE * 100}%`} - 1px)` }}
dragHandlerStyle={{ height: DRAG_HANDLER_HEIGHT }}
onResizeMouseDown={this.onResizeMouseDown}
onResizeMouseOver={this.onResizeMouseOver}
/>
}
<div className="main-panel" style={mainPanelStyle}>
<Router className="reach-router">
<Libraries path={siteRoot} />
<Libraries path={siteRoot + 'libraries'} />
<MyLibraries path={siteRoot + 'my-libs'} />
<MyLibDeleted path={siteRoot + 'my-libs/deleted/'} />
<ShareAdminShareLinks path={siteRoot + 'share-admin-share-links'} />
<ShareAdminUploadLinks path={siteRoot + 'share-admin-upload-links'} />
<PublicSharedView path={siteRoot + 'org/'} />
<Wikis
path={siteRoot + 'published'}
sidePanelRate={sidePanelRate}
isSidePanelFolded={isSidePanelFolded}
/>
<Starred path={siteRoot + 'starred'} />
<InvitationsView path={siteRoot + 'invitations/'} />
<FilesActivities path={siteRoot + 'dashboard'} />
<MyFileActivities path={siteRoot + 'my-activities'} />
<Group path={siteRoot + 'group/:groupID'} onGroupChanged={this.onGroupChanged} />
<LinkedDevices path={siteRoot + 'linked-devices'} />
<ShareAdminLibraries path={siteRoot + 'share-admin-libs'} />
<ShareAdminFolders path={siteRoot + 'share-admin-folders'} />
<SharedLibraries path={siteRoot + 'shared-libs'} />
<ShareWithOCM path={siteRoot + 'shared-with-ocm'} />
<OCMViaWebdav path={siteRoot + 'ocm-via-webdav'} />
<OCMRepoDir
path={siteRoot + 'remote-library/:providerID/:repoID/*'}
pathPrefix={this.state.pathPrefix}
onTabNavClick={this.tabItemClick}
/>
<LibContentView
path={siteRoot + 'library/:repoID/*'}
pathPrefix={this.state.pathPrefix}
isSidePanelFolded={isSidePanelFolded}
onTabNavClick={this.tabItemClick}
eventBus={this.eventBus}
/>
2019-03-15 08:06:51 +00:00
</Router>
</div>
2019-07-11 06:42:32 +00:00
<MediaQuery query="(max-width: 767.8px)">
<Modal zIndex="1030" isOpen={!isSidePanelClosed} toggle={this.toggleSidePanel} contentClassName="d-none"></Modal>
2019-05-23 02:10:46 +00:00
</MediaQuery>
2019-03-15 08:06:51 +00:00
</div>
</React.Fragment>
2018-09-21 06:16:15 +00:00
);
}
}
ReactDom.render(<App />, document.getElementById('wrapper'));