Commit 680c0c3d authored by jim's avatar jim Committed by 陈帅

Optimization parameter logic

parent 56ce1efa
...@@ -30,11 +30,13 @@ export default class WaterWave extends PureComponent { ...@@ -30,11 +30,13 @@ export default class WaterWave extends PureComponent {
resize = () => { resize = () => {
requestAnimationFrame(() => { requestAnimationFrame(() => {
const { height } = this.props; if (this.root) {
const { offsetWidth } = this.root.parentNode; const { height } = this.props;
this.setState({ const { offsetWidth } = this.root.parentNode;
radio: offsetWidth < height ? offsetWidth / height : 1, this.setState({
}); radio: offsetWidth < height ? offsetWidth / height : 1,
});
}
}); });
}; };
......
import { Tooltip } from 'antd'; import { Tooltip } from 'antd';
import React from 'react'; import React from 'react';
import NavSate from './navState'; import NavSate from './navState';
import style from './index.less';
const LayoutSetting = ({ value, onChange }) => { const LayoutSetting = ({ value, onChange }) => {
return ( return (
<div <div className={style.layoutSetting}>
style={{
margin: 5,
display: 'flex',
}}
>
{['sidemenu', 'topmenu'].map(layout => ( {['sidemenu', 'topmenu'].map(layout => (
<div <div className={style.item} onClick={() => onChange && onChange(layout)} key={layout}>
onClick={() => onChange && onChange(layout)}
key={layout}
style={{
width: 70,
height: 44,
textAlign: 'center',
margin: 8,
}}
>
<NavSate type={layout} state={value === layout ? 'active' : 'default'} alt={layout} /> <NavSate type={layout} state={value === layout ? 'active' : 'default'} alt={layout} />
</div> </div>
))} ))}
<Tooltip title="等待后期实现!"> <Tooltip title="等待后期实现!">
<div <div key="topside" className={style.item}>
key="topside"
style={{
width: 70,
height: 44,
textAlign: 'center',
margin: 8,
}}
>
<NavSate type="topside" state="disable" alt="topside" /> <NavSate type="topside" state="disable" alt="topside" />
</div> </div>
</Tooltip> </Tooltip>
......
import React, { PureComponent, Fragment } from 'react'; import React, { PureComponent, Fragment } from 'react';
import { Select, message, List, Switch, Divider, Radio } from 'antd'; import { Select, message, List, Switch, Divider, Radio } from 'antd';
import DrawerMenu from 'rc-drawer-menu'; import DrawerMenu from 'rc-drawer-menu';
import { connect } from 'dva';
import styles from './index.less'; import styles from './index.less';
import ThemeColor from './ThemeColor'; import ThemeColor from './ThemeColor';
import LayoutSeting from './LayoutSetting'; import LayoutSeting from './LayoutSetting';
...@@ -30,45 +31,23 @@ const Body = ({ children, title, style }) => ( ...@@ -30,45 +31,23 @@ const Body = ({ children, title, style }) => (
{children} {children}
</div> </div>
); );
@connect(({ setting }) => ({ setting }))
class Sidebar extends PureComponent { class Sidebar extends PureComponent {
static getDerivedStateFromProps(nextProps, prevState) {
const nextState = {};
Object.keys(nextProps).forEach(key => {
if (nextProps[key] && prevState[key] !== undefined) {
nextState[key] = nextProps[key];
}
});
return nextState;
}
constructor(props) {
super(props);
this.defaultstate = {
collapse: false,
silderTheme: 'dark',
themeColor: '#1890FF',
layout: 'sidemenu',
grid: 'Fluid',
fixedHeader: false,
autoHideHeader: false,
fixSiderbar: false,
colorWeak: 'close',
};
const propsState = this.propsToState(props);
this.state = { ...this.defaultstate, ...propsState };
}
componentDidMount() { componentDidMount() {
this.colorChange(this.state.themeColor); const { themeColor } = this.props.setting;
if (themeColor !== '#1890FF') {
this.colorChange(themeColor);
}
} }
getLayOutSetting = () => { getLayOutSetting = () => {
const { layout } = this.state; const { grid, fixedHeader, autoHideHeader, fixSiderbar, layout } = this.props.setting;
return [ return [
{ {
title: '栅格模式', title: '栅格模式',
isShow: true, isShow: true,
action: [ action: [
<Select <Select
value={this.state.grid} value={grid}
onSelect={value => this.changeSetting('grid', value)} onSelect={value => this.changeSetting('grid', value)}
style={{ width: 120 }} style={{ width: 120 }}
> >
...@@ -82,7 +61,7 @@ class Sidebar extends PureComponent { ...@@ -82,7 +61,7 @@ class Sidebar extends PureComponent {
isShow: true, isShow: true,
action: [ action: [
<Switch <Switch
checked={!!this.state.fixedHeader} checked={!!fixedHeader}
onChange={checked => this.changeSetting('fixedHeader', checked)} onChange={checked => this.changeSetting('fixedHeader', checked)}
/>, />,
], ],
...@@ -92,7 +71,7 @@ class Sidebar extends PureComponent { ...@@ -92,7 +71,7 @@ class Sidebar extends PureComponent {
isShow: true, isShow: true,
action: [ action: [
<Switch <Switch
checked={!!this.state.autoHideHeader} checked={!!autoHideHeader}
onChange={checked => this.changeSetting('autoHideHeader', checked)} onChange={checked => this.changeSetting('autoHideHeader', checked)}
/>, />,
], ],
...@@ -100,7 +79,7 @@ class Sidebar extends PureComponent { ...@@ -100,7 +79,7 @@ class Sidebar extends PureComponent {
{ {
title: 'Fix Siderbar', title: 'Fix Siderbar',
isShow: layout === 'sidemenu', isShow: layout === 'sidemenu',
action: [<Switch checked={!!this.state.fixSiderbar} onChange={this.fixSiderbar} />], action: [<Switch checked={!!fixSiderbar} onChange={this.fixSiderbar} />],
}, },
].filter(item => item.isShow); ].filter(item => item.isShow);
}; };
...@@ -118,48 +97,34 @@ class Sidebar extends PureComponent { ...@@ -118,48 +97,34 @@ class Sidebar extends PureComponent {
} }
} }
this.setState(nextState, () => { this.setState(nextState, () => {
if (this.props.onChange) { this.props.dispatch({
this.props.onChange(this.state); type: 'setting/changeSetting',
} payload: this.state,
});
}); });
}; };
propsToState = props => {
const nextState = {};
Object.keys(props).forEach(key => {
if (props[key] && this.defaultstate[key] !== undefined) {
nextState[key] = props[key];
}
});
return nextState;
};
togglerContent = () => { togglerContent = () => {
this.changeSetting('collapse', !this.state.collapse); this.changeSetting('collapse', !this.props.setting.collapse);
}; };
colorChange = color => { colorChange = color => {
this.changeSetting('themeColor', color); this.changeSetting('themeColor', color);
this.setState( const hideMessage = message.loading('正在编译主题!', 0);
{ window.less
themeColor: color, .modifyVars({
}, '@primary-color': color,
() => { })
const hideMessage = message.loading('正在编译主题!', 0); .then(() => {
window.less hideMessage();
.modifyVars({ })
'@primary-color': color, .catch(() => {
}) message.error(`Failed to update theme`);
.then(() => { });
hideMessage();
})
.catch(() => {
message.error(`Failed to update theme`);
});
}
);
}; };
render() { render() {
const radioStyle = { const radioStyle = {
display: 'block', display: 'block',
}; };
const { collapse, silderTheme, themeColor, layout, colorWeak } = this.props.setting;
return ( return (
<div className={styles.sidebar}> <div className={styles.sidebar}>
<div className={styles.mini_bar} onClick={this.togglerContent}> <div className={styles.mini_bar} onClick={this.togglerContent}>
...@@ -172,7 +137,7 @@ class Sidebar extends PureComponent { ...@@ -172,7 +137,7 @@ class Sidebar extends PureComponent {
parent={null} parent={null}
level={null} level={null}
iconChild={null} iconChild={null}
open={this.state.collapse} open={collapse}
placement="right" placement="right"
width="336px" width="336px"
onMaskClick={this.togglerContent} onMaskClick={this.togglerContent}
...@@ -186,7 +151,7 @@ class Sidebar extends PureComponent { ...@@ -186,7 +151,7 @@ class Sidebar extends PureComponent {
> >
<RadioGroup <RadioGroup
onChange={({ target }) => this.changeSetting('silderTheme', target.value)} onChange={({ target }) => this.changeSetting('silderTheme', target.value)}
value={this.state.silderTheme} value={silderTheme}
> >
<Radio style={radioStyle} value="dark"> <Radio style={radioStyle} value="dark">
<ColorBlock color="#002140" title="深色导航" /> <ColorBlock color="#002140" title="深色导航" />
...@@ -195,13 +160,13 @@ class Sidebar extends PureComponent { ...@@ -195,13 +160,13 @@ class Sidebar extends PureComponent {
<ColorBlock color="#E9E9E9" title="浅色导航" /> <ColorBlock color="#E9E9E9" title="浅色导航" />
</Radio> </Radio>
</RadioGroup> </RadioGroup>
<ThemeColor value={this.state.themeColor} onChange={this.colorChange} /> <ThemeColor value={themeColor} onChange={this.colorChange} />
</Body> </Body>
<Divider style={{ margin: 0 }} /> <Divider style={{ margin: 0 }} />
<Body title="导航设置 "> <Body title="导航设置 ">
<LayoutSeting <LayoutSeting
value={this.state.layout} value={layout}
onChange={layout => this.changeSetting('layout', layout)} onChange={value => this.changeSetting('layout', value)}
/> />
<List <List
split={false} split={false}
...@@ -218,7 +183,7 @@ class Sidebar extends PureComponent { ...@@ -218,7 +183,7 @@ class Sidebar extends PureComponent {
title: '色弱模式', title: '色弱模式',
action: [ action: [
<Select <Select
value={this.state.colorWeak} value={colorWeak}
onSelect={value => this.changeSetting('colorWeak', value)} onSelect={value => this.changeSetting('colorWeak', value)}
style={{ width: 120 }} style={{ width: 120 }}
> >
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
position: fixed; position: fixed;
bottom: 50px; bottom: 50px;
right: 50px; right: 50px;
cursor: pointer;
z-index: 99;
box-shadow: 0 0 6px 0 rgba(0, 21, 41, 0.35); box-shadow: 0 0 6px 0 rgba(0, 21, 41, 0.35);
img { img {
width: 28px; width: 28px;
...@@ -37,6 +39,18 @@ ...@@ -37,6 +39,18 @@
} }
} }
} }
.layoutSetting {
margin: 5px;
display: flex;
.item {
cursor: pointer;
width: 70px;
height: 44px;
text-align: center;
margin: 8px;
}
}
.color_block { .color_block {
width: 38px; width: 38px;
height: 22px; height: 22px;
......
...@@ -18,8 +18,6 @@ import Context from './MenuContext'; ...@@ -18,8 +18,6 @@ import Context from './MenuContext';
const { Content } = Layout; const { Content } = Layout;
const { AuthorizedRoute, check } = Authorized; const { AuthorizedRoute, check } = Authorized;
const RightSidebar = connect(({ setting }) => ({ ...setting }))(Sidebar);
/** /**
* 获取面包屑映射 * 获取面包屑映射
* @param {Object} menuData 菜单配置 * @param {Object} menuData 菜单配置
...@@ -103,12 +101,6 @@ class BasicLayout extends React.PureComponent { ...@@ -103,12 +101,6 @@ class BasicLayout extends React.PureComponent {
payload: collapsed, payload: collapsed,
}); });
}; };
changeSetting = setting => {
this.props.dispatch({
type: 'setting/changeSetting',
payload: setting,
});
};
render() { render() {
const { isMobile, redirectData, routerData, fixedHeader, match } = this.props; const { isMobile, redirectData, routerData, fixedHeader, match } = this.props;
const isTop = this.props.layout === 'topmenu'; const isTop = this.props.layout === 'topmenu';
...@@ -164,7 +156,7 @@ class BasicLayout extends React.PureComponent { ...@@ -164,7 +156,7 @@ class BasicLayout extends React.PureComponent {
<Context.Provider value={this.getContext()}> <Context.Provider value={this.getContext()}>
<div className={classNames(params)}> <div className={classNames(params)}>
{layout} {layout}
<RightSidebar onChange={this.changeSetting} /> <Sidebar />
</div> </div>
</Context.Provider> </Context.Provider>
)} )}
......
...@@ -18,9 +18,7 @@ class HeaderView extends PureComponent { ...@@ -18,9 +18,7 @@ class HeaderView extends PureComponent {
document.getElementById('root').addEventListener('scroll', this.handScroll); document.getElementById('root').addEventListener('scroll', this.handScroll);
} }
componentWillUnmount() { componentWillUnmount() {
document document.getElementById('root').removeEventListener('scroll', this.handScroll);
.getElementById('root')
.removeEventListener('scroll', this.handScroll);
} }
getHeadWidth = () => { getHeadWidth = () => {
const { fixedHeader, layout, fixSiderbar } = this.props.setting; const { fixedHeader, layout, fixSiderbar } = this.props.setting;
...@@ -34,7 +32,7 @@ class HeaderView extends PureComponent { ...@@ -34,7 +32,7 @@ class HeaderView extends PureComponent {
return 'calc(100% - 80px)'; return 'calc(100% - 80px)';
} }
}; };
handleNoticeClear = (type) => { handleNoticeClear = type => {
message.success(`清空了${type}`); message.success(`清空了${type}`);
this.props.dispatch({ this.props.dispatch({
type: 'global/clearNotices', type: 'global/clearNotices',
...@@ -60,7 +58,7 @@ class HeaderView extends PureComponent { ...@@ -60,7 +58,7 @@ class HeaderView extends PureComponent {
}); });
} }
}; };
handleNoticeVisibleChange = (visible) => { handleNoticeVisibleChange = visible => {
if (visible) { if (visible) {
this.props.dispatch({ this.props.dispatch({
type: 'global/fetchNotices', type: 'global/fetchNotices',
......
...@@ -56,25 +56,12 @@ class LoadingPage extends PureComponent { ...@@ -56,25 +56,12 @@ class LoadingPage extends PureComponent {
loading: false, loading: false,
}); });
} }
/**
* get setting from url params
*/
initSetting() { initSetting() {
const setting = {
collapse: false,
silderTheme: 'dark',
themeColor: '#1890FF',
layout: 'sidemenu',
grid: 'Fluid',
fixedHeader: false,
autoHideHeader: false,
fixSiderbar: false,
colorWeak: 'close',
};
const urlParams = new URL(window.location.href);
Object.keys(setting).forEach(key => {
setting[key] = urlParams.searchParams.get(key);
});
this.props.dispatch({ this.props.dispatch({
type: 'setting/changeSetting', type: 'setting/getSetting',
payload: setting,
}); });
} }
render() { render() {
......
const defaultSetting = {
collapse: false,
silderTheme: 'dark',
themeColor: '#1890FF',
layout: 'sidemenu',
grid: 'Fluid',
fixedHeader: false,
autoHideHeader: false,
fixSiderbar: false,
colorWeak: 'close',
};
export default { export default {
namespace: 'setting', namespace: 'setting',
state: { state: defaultSetting,
collapse: false,
silderTheme: 'dark',
themeColor: '#1890FF',
layout: 'sidemenu',
grid: 'Fluid',
fixedHeader: false,
autoHideHeader: false,
fixSiderbar: false,
colorWeak: 'close',
},
reducers: { reducers: {
getSetting(state) {
const setting = { ...state };
const urlParams = new URL(window.location.href);
Object.keys(state).forEach(key => {
if (urlParams.searchParams.has(key)) {
const value = urlParams.searchParams.get(key);
setting[key] = value;
}
});
return setting;
},
changeSetting(state, { payload }) { changeSetting(state, { payload }) {
const urlParams = new URL(window.location.href); const urlParams = new URL(window.location.href);
Object.keys(defaultSetting).forEach(key => {
if (urlParams.searchParams.has(key)) {
urlParams.searchParams.delete(key);
}
});
Object.keys(payload).forEach(key => { Object.keys(payload).forEach(key => {
if (key === 'collapse') { if (key === 'collapse') {
return; return;
...@@ -23,7 +40,9 @@ export default { ...@@ -23,7 +40,9 @@ export default {
if (value === true) { if (value === true) {
value = 1; value = 1;
} }
urlParams.searchParams.set(key, value); if (defaultSetting[key] !== value) {
urlParams.searchParams.set(key, value);
}
}); });
window.history.replaceState(null, 'setting', urlParams.href); window.history.replaceState(null, 'setting', urlParams.href);
return { return {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment