-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathBasicLayout.js
More file actions
287 lines (268 loc) · 8 KB
/
BasicLayout.js
File metadata and controls
287 lines (268 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import React from 'react';
import { connect, router, routerRedux } from 'dva';
import { Layout } from 'antd';
import NavBar from 'components/NavBar';
import { LeftSideBar, RightSideBar } from 'components/SideBar';
import TopBar from 'components/TopBar';
import SkinToolbox from 'components/SkinToolbox';
import pathToRegexp from 'path-to-regexp';
import { enquireIsMobile } from '@/utils/enquireScreen';
import TabsLayout from './TabsLayout';
import $$ from 'cmn-utils';
import cx from 'classnames';
import isEqual from 'react-fast-compare';
import { SwitchTransition, CSSTransition } from 'react-transition-group';
import 'assets/styles/transition.less';
import './styles/basic.less';
const { Switch } = router;
const { Content, Header } = Layout;
/**
* 基本部局
* 可设置多种皮肤 theme: [light, grey, primary, info, warning, danger, alert, system, success, dark]
* 可设置多种布局 [header(固定头), sidebar(固定边栏), breadcrumb(固定面包蟹), tabLayout(标签布局)]
* @author weiq
*/
@connect(({ global }) => ({ global }))
export default class BasicLayout extends React.PureComponent {
constructor(props) {
super(props);
const user = $$.getStore('user', []);
const theme = $$.getStore('theme', {
leftSide: 'darkgrey', // 左边
navbar: 'light' // 顶部
});
if (!theme.layout) {
theme.layout = [
'fixedHeader',
'fixedSidebar',
'fixedBreadcrumbs'
// 'hidedBreadcrumbs',
// 'tabLayout',
];
}
this.state = {
collapsedLeftSide: false, // 左边栏开关控制
leftCollapsedWidth: 60, // 左边栏宽度
expandTopBar: false, // 头部多功能区开合
showSidebarHeader: false, // 左边栏头部开关
collapsedRightSide: true, // 右边栏开关
theme, // 皮肤设置
user,
currentMenu: this.getCurrentMenu(this.props),
isMobile: false
};
props.dispatch({
type: 'global/getMenu'
});
}
componentDidMount() {
this.checkLoginState();
this.unregisterEnquire = enquireIsMobile(ismobile => {
const { isMobile, theme } = this.state;
if (isMobile !== ismobile) {
// 如查是移动端则不固定侧边栏
if (ismobile && $$.isArray(theme.layout)) {
theme.layout = theme.layout.filter(item => item !== 'fixedSidebar');
}
this.setState({
isMobile: ismobile
});
}
});
}
// 检查有户是否登录
checkLoginState() {
const user = $$.getStore('user');
if (!user) {
this.props.dispatch(routerRedux.replace('/sign/login'));
}
}
componentDidUpdate(prevProps, prevState) {
if (
!isEqual(this.props.location.pathname, prevProps.location.pathname) ||
!isEqual(this.props.global.flatMenu, prevProps.global.flatMenu)
) {
this.setState({
currentMenu: this.getCurrentMenu(this.props) || {}
});
}
}
componentWillUnmount() {
// 清理监听
this.unregisterEnquire();
}
getCurrentMenu(props) {
const {
location: { pathname },
global
} = props || this.props;
const menu = this.getMeunMatchKeys(global.flatMenu, pathname)[0];
return menu;
}
getMeunMatchKeys = (flatMenu, path) => {
return flatMenu.filter(item => {
return pathToRegexp(item.path).test(path);
});
};
/**
* 顶部左侧菜单图标收缩控制
*/
onCollapseLeftSide = _ => {
const collapsedLeftSide =
this.state.leftCollapsedWidth === 0
? true
: !this.state.collapsedLeftSide;
const collapsedRightSide =
this.state.collapsedRightSide || !collapsedLeftSide;
this.setState({
collapsedLeftSide,
collapsedRightSide,
leftCollapsedWidth: 60
});
};
/**
* 完全关闭左边栏,即宽为0
*/
onCollapseLeftSideAll = _ => {
this.setState({
collapsedLeftSide: true,
leftCollapsedWidth: 0
});
};
/**
* 展开面包屑所在条中的多功能区
*/
onExpandTopBar = _ => {
this.setState({
expandTopBar: true
});
};
/**
* 与上面相反
*/
onCollapseTopBar = _ => {
this.setState({
expandTopBar: false
});
};
/**
* 切换左边栏中头部的开合
*/
toggleSidebarHeader = _ => {
this.setState({
showSidebarHeader: !this.state.showSidebarHeader
});
};
/**
* 切换右边栏
*/
toggleRightSide = _ => {
const { collapsedLeftSide, collapsedRightSide } = this.state;
this.setState({
collapsedLeftSide: collapsedRightSide ? true : collapsedLeftSide,
collapsedRightSide: !collapsedRightSide
});
};
onChangeTheme = theme => {
$$.setStore('theme', theme);
this.setState({
theme
});
};
render() {
const {
collapsedLeftSide,
leftCollapsedWidth,
expandTopBar,
showSidebarHeader,
collapsedRightSide,
theme,
user,
currentMenu,
isMobile
} = this.state;
const { routerData, location, global } = this.props;
const { menu, flatMenu } = global;
const { childRoutes } = routerData;
const classnames = cx('basic-layout', 'full-layout', {
fixed: theme.layout && theme.layout.indexOf('fixedSidebar') !== -1,
'fixed-header':
theme.layout && theme.layout.indexOf('fixedHeader') !== -1,
'fixed-breadcrumbs':
theme.layout && theme.layout.indexOf('fixedBreadcrumbs') !== -1,
'hided-breadcrumbs':
theme.layout && theme.layout.indexOf('hidedBreadcrumbs') !== -1
});
return (
<Layout className={classnames}>
<Header>
<NavBar
collapsed={collapsedLeftSide}
onCollapseLeftSide={this.onCollapseLeftSide}
onExpandTopBar={this.onExpandTopBar}
toggleSidebarHeader={this.toggleSidebarHeader}
theme={theme.navbar}
user={user}
isMobile={isMobile}
/>
</Header>
<Layout>
<LeftSideBar
collapsed={collapsedLeftSide}
leftCollapsedWidth={leftCollapsedWidth}
showHeader={showSidebarHeader}
onCollapse={this.onCollapseLeftSide}
onCollapseAll={this.onCollapseLeftSideAll}
location={location}
theme={theme.leftSide}
flatMenu={flatMenu}
currentMenu={currentMenu}
menu={menu}
user={user}
isMobile={isMobile}
/>
<Content>
{theme.layout.indexOf('tabLayout') >= 0 ? (
<TabsLayout childRoutes={childRoutes} location={location} />
) : (
<Layout className="full-layout">
<Header>
<TopBar
expand={expandTopBar}
toggleRightSide={this.toggleRightSide}
collapsedRightSide={collapsedRightSide}
onCollapse={this.onCollapseTopBar}
currentMenu={currentMenu}
location={location}
theme={theme}
/>
</Header>
<Content style={{ overflow: 'hidden' }}>
<SwitchTransition>
<CSSTransition
key={location.pathname}
classNames="fade"
timeout={500}
>
<Layout className="full-layout">
<Content className="router-page">
<Switch location={location}>{childRoutes}</Switch>
</Content>
</Layout>
</CSSTransition>
</SwitchTransition>
</Content>
</Layout>
)}
</Content>
<RightSideBar
collapsed={collapsedRightSide}
isMobile={isMobile}
onCollapse={this.toggleRightSide}
/>
</Layout>
<SkinToolbox onChangeTheme={this.onChangeTheme} theme={theme} />
</Layout>
);
}
}