/
BasicLayout.js
427 lines (408 loc) · 12.4 KB
/
BasicLayout.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { Layout, Icon, message,Tabs,Button,Dropdown,Menu } from 'antd';
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
import { Route, Redirect, Switch, routerRedux } from 'dva/router';
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
import { enquireScreen } from 'enquire-js';
import GlobalHeader from '../components/GlobalHeader';
import GlobalFooter from '../components/GlobalFooter';
import SiderMenu from '../components/SiderMenu';
import Home from '../routes/Home/Home';
import NotFound from '../routes/Exception/404';
import { getRoutes } from '../utils/utils';
import Authorized from '../utils/Authorized';
import { getMenuData } from '../common/menu';
import logo from '../assets/logo.svg';
const { Content, Header, Footer } = Layout;
const { AuthorizedRoute, check } = Authorized;
const { TabPane } = Tabs;
/**
* 根据菜单取得重定向地址.
*/
const redirectData = [];
const getRedirect = item => {
if (item && item.children) {
if (item.children[0] && item.children[0].path) {
redirectData.push({
from: `${item.path}`,
to: `${item.children[0].path}`,
});
item.children.forEach(children => {
getRedirect(children);
});
}
}
};
getMenuData().forEach(getRedirect);
/**
* 获取面包屑映射
* @param {Object} menuData 菜单配置
* @param {Object} routerData 路由配置
*/
const getBreadcrumbNameMap = (menuData, routerData) => {
const result = {};
const childResult = {};
for (const i of menuData) {
if (!routerData[i.path]) {
result[i.path] = i;
}
if (i.children) {
Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData));
}
}
return Object.assign({}, routerData, result, childResult);
};
const query = {
'screen-xs': {
maxWidth: 575,
},
'screen-sm': {
minWidth: 576,
maxWidth: 767,
},
'screen-md': {
minWidth: 768,
maxWidth: 991,
},
'screen-lg': {
minWidth: 992,
maxWidth: 1199,
},
'screen-xl': {
minWidth: 1200,
},
};
let isMobile;
enquireScreen(b => {
isMobile = b;
});
class BasicLayout extends React.PureComponent {
static childContextTypes = {
location: PropTypes.object,
breadcrumbNameMap: PropTypes.object,
};
state = {
isMobile,
tabList:[{closable: false,
content: <Home onHandlePage={this.onHandlePage}/>,
exact: true,
key: "/home",
name: "Home",
path: "/home"
}],
tabListKey:['/home'],
activeKey:'/home',
};
getChildContext() {
const { location, routerData } = this.props;
return {
location,
breadcrumbNameMap: getBreadcrumbNameMap(getMenuData(), routerData),
};
}
componentDidMount() {
this.props.history.push({ pathname : '/' })
enquireScreen(mobile => {
this.setState({
isMobile: mobile,
});
});
this.props.dispatch({
type: 'user/fetchCurrent',
});
}
getPageTitle() {
const { routerData, location } = this.props;
const { pathname } = location;
let title = 'Ant Tabs';
if (routerData[pathname] && routerData[pathname].name) {
title = `${routerData[pathname].name} - Ant Tabs`;
}
return title;
}
getBashRedirect = () => {
// According to the url parameter to redirect
// 这里是重定向的,重定向到 url 的 redirect 参数所示地址
const urlParams = new URL(window.location.href);
const redirect = urlParams.searchParams.get('redirect');
// Remove the parameters in the url
if (redirect) {
urlParams.searchParams.delete('redirect');
window.history.replaceState(null, 'redirect', urlParams.href);
} else {
const { routerData } = this.props;
// get the first authorized route path in routerData
const authorizedPath = Object.keys(routerData).find(
item => check(routerData[item].authority, item) && item !== '/'
);
return authorizedPath;
}
return redirect;
};
handleMenuCollapse = collapsed => {
this.props.dispatch({
type: 'global/changeLayoutCollapsed',
payload: collapsed,
});
};
handleNoticeClear = type => {
message.success(`清空了${type}`);
this.props.dispatch({
type: 'global/clearNotices',
payload: type,
});
};
handleMenuClick = ({ key }) => {
if (key === 'triggerError') {
const {tabListKey} = this.state;
if(!tabListKey.includes('/exception/trigger')){
this.onHandlePage({key:'/exception/trigger'})
this.props.dispatch(routerRedux.push('/exception/trigger'));
}else{
this.setState({activeKey:'/exception/trigger'})
}
return;
}
if (key === 'logout') {
this.props.dispatch({
type: 'login/logout',
});
}
};
handleNoticeVisibleChange = visible => {
if (visible) {
this.props.dispatch({
type: 'global/fetchNotices',
});
}
};
onHandlePage =(e)=>{//点击左侧菜单
const {routerData, match} = this.props,{key} = e;
const tabLists = getRoutes(match.path, routerData);
const {tabListKey,tabList} = this.state
this.setState({
activeKey:key
})
tabLists.map((v) => {
if(v.key === key){
if(tabList.length === 0){
v.closable = false
this.state.tabList.push(v)
}else{
if(!tabListKey.includes(v.key)){
if(v.content){
v.content = {
...v.content,
props:Object.assign({},v.content.props,{onHandlePage:this.onHandlePage})
}
}
this.state.tabList.push(v)
}
}
}
})
this.setState({
tabListKey:tabList.map((va)=>va.key)
})
}
// 切换 tab页 router.push(key);
onChange = key => {
this.setState({ activeKey:key });
this.props.history.push({ pathname : key })
};
onEdit = (targetKey, action) => {
this[action](targetKey);
}
remove = (targetKey) => {
let {activeKey} = this.state;
let lastIndex;
this.state.tabList.forEach((pane, i) => {
if (pane.key === targetKey) {
lastIndex = i - 1;
}
});
const tabList = []
this.state.tabList.map(pane => {
if(pane.key !== targetKey){
tabList.push(pane)
}
});
if (lastIndex >= 0 && activeKey === targetKey) {
activeKey = tabList[lastIndex].key;
}
this.props.history.push({ pathname : activeKey })
this.setState({ tabList, activeKey,tabListKey:tabList.map((va)=>va.key) });
}
onClickHover=(e)=>{
console.log(e)
// message.info(`Click on item ${key}`);
let { key } = e,{activeKey,tabList,tabListKey} = this.state;
if(key === '1'){
tabList= tabList.filter((v)=>v.key !== activeKey || v.key === '/home')
tabListKey = tabListKey.filter((v)=>v !== activeKey || v === '/home')
this.setState({
activeKey:'/home',
tabList,
tabListKey
})
}else if(key === '2'){
tabList= tabList.filter((v)=>v.key === activeKey || v.key === '/home')
tabListKey = tabListKey.filter((v)=>v === activeKey || v === '/home')
this.setState({
activeKey,
tabList,
tabListKey
})
}else if(key === '3'){
tabList= tabList.filter((v)=>v.key === '/home')
tabListKey = tabListKey.filter((v)=>v === '/home')
this.setState({
activeKey:'/home',
tabList,
tabListKey
})
}
}
render() {
const {
currentUser,
collapsed,
fetchingNotices,
notices,
location,
} = this.props,{tabList,isMobile} = this.state,{pathname} = location;
if(pathname === '/'){
this.state.activeKey = '/home'
// this.props.history.push({ pathname : '/home' })
}
const menu = (
<Menu onClick={this.onClickHover}>
<Menu.Item key="1">关闭当前标签页</Menu.Item>
<Menu.Item key="2">关闭其他标签页</Menu.Item>
<Menu.Item key="3">关闭全部标签页</Menu.Item>
</Menu>
);
const operations = (
<Dropdown overlay={menu} >
<a className="ant-dropdown-link" href="#">
Hover me<Icon type="down" />
</a>
</Dropdown>
);
const layout = (
<Layout>
<SiderMenu
logo={logo}
// 不带Authorized参数的情况下如果没有权限,会强制跳到403界面
// If you do not have the Authorized parameter
// you will be forced to jump to the 403 interface without permission
Authorized={Authorized}
menuData={getMenuData()}
collapsed={collapsed}
location={location}
isMobile={this.state.isMobile}
onCollapse={this.handleMenuCollapse}
onHandlePage ={this.onHandlePage}
/>
<Layout>
<Header style={{ padding: 0 }}>
<GlobalHeader
logo={logo}
currentUser={currentUser}
fetchingNotices={fetchingNotices}
notices={notices}
collapsed={collapsed}
isMobile={this.state.isMobile}
onNoticeClear={this.handleNoticeClear}
onCollapse={this.handleMenuCollapse}
onMenuClick={this.handleMenuClick}
onNoticeVisibleChange={this.handleNoticeVisibleChange}
/>
</Header>
<Content className="globalTabs" style={{ margin: '2px 0px 0px', height: '100%' }}>
{/*<Switch>*/}
{/*{redirectData.map(item => (*/}
{/*<Redirect key={item.from} exact from={item.from} to={item.to} />*/}
{/*))}*/}
{/*{getRoutes(match.path, routerData).map(item => (*/}
{/*<AuthorizedRoute*/}
{/*key={item.key}*/}
{/*path={item.path}*/}
{/*component={item.component}*/}
{/*exact={item.exact}*/}
{/*authority={item.authority}*/}
{/*redirectPath="/exception/403"*/}
{/*/>*/}
{/*))}*/}
<Tabs
// className={styles.tabs}
activeKey={this.state.activeKey}
onChange={this.onChange}
tabBarExtraContent={operations}
tabBarStyle={{background:'#fff'}}
tabPosition="top"
tabBarGutter={-1}
hideAdd
type="editable-card"
onEdit={this.onEdit}
>
{tabList.map(item => (
<TabPane tab={item.name} key={item.key} closable={item.closable}>
{item.content ? item.content:<NotFound/>}
</TabPane>
))}
</Tabs>
{/*<Redirect exact from="/" to={bashRedirect} />*/}
{/*<Route render={NotFound} />*/}
{/*</Switch>*/}
</Content>
<Footer style={{ padding: 0 }}>
<GlobalFooter
links={[
{
key: 'Pro 首页',
title: 'Pro 首页',
href: 'https://github.com/kuhami',
blankTarget: true,
},
{
key: 'github',
title: <Icon type="github" />,
href: 'https://github.com/kuhami/react-ant-pro',
blankTarget: true,
},
{
key: 'Ant Tabs',
title: 'Ant Tabs',
href: 'https://github.com/kuhami',
blankTarget: true,
},
]}
copyright={
<Fragment>
Copyright <Icon type="copyright" /> 2018 Ant Tabs体验技术部出品
</Fragment>
}
/>
</Footer>
</Layout>
</Layout>
);
return (
<DocumentTitle title={this.getPageTitle()}>
<ContainerQuery query={query}>
{params => <div className={classNames(params)}>{layout}</div>}
</ContainerQuery>
</DocumentTitle>
);
}
}
export default connect(({ user, global, loading }) => ({
currentUser: user.currentUser,
collapsed: global.collapsed,
fetchingNotices: loading.effects['global/fetchNotices'],
notices: global.notices,
}))(BasicLayout);