From 680c0c3dee0067506fdf2db8f7c8c7a226dbbb1f Mon Sep 17 00:00:00 2001
From: jim
Date: Wed, 2 May 2018 15:55:46 +0800
Subject: [PATCH] Optimization parameter logic
---
src/components/Charts/WaterWave/index.js | 12 +--
src/components/Sidebar/LayoutSetting.js | 29 +------
src/components/Sidebar/index.js | 103 ++++++++---------------
src/components/Sidebar/index.less | 14 +++
src/layouts/BasicLayout.js | 10 +--
src/layouts/Header.js | 8 +-
src/layouts/LoadingPage.js | 21 +----
src/models/setting.js | 43 +++++++---
8 files changed, 98 insertions(+), 142 deletions(-)
diff --git a/src/components/Charts/WaterWave/index.js b/src/components/Charts/WaterWave/index.js
index f5ea26a0..2da301a0 100644
--- a/src/components/Charts/WaterWave/index.js
+++ b/src/components/Charts/WaterWave/index.js
@@ -30,11 +30,13 @@ export default class WaterWave extends PureComponent {
resize = () => {
requestAnimationFrame(() => {
- const { height } = this.props;
- const { offsetWidth } = this.root.parentNode;
- this.setState({
- radio: offsetWidth < height ? offsetWidth / height : 1,
- });
+ if (this.root) {
+ const { height } = this.props;
+ const { offsetWidth } = this.root.parentNode;
+ this.setState({
+ radio: offsetWidth < height ? offsetWidth / height : 1,
+ });
+ }
});
};
diff --git a/src/components/Sidebar/LayoutSetting.js b/src/components/Sidebar/LayoutSetting.js
index f4947402..b98b9601 100644
--- a/src/components/Sidebar/LayoutSetting.js
+++ b/src/components/Sidebar/LayoutSetting.js
@@ -1,39 +1,18 @@
import { Tooltip } from 'antd';
import React from 'react';
import NavSate from './navState';
+import style from './index.less';
const LayoutSetting = ({ value, onChange }) => {
return (
-
+
{['sidemenu', 'topmenu'].map(layout => (
-
onChange && onChange(layout)}
- key={layout}
- style={{
- width: 70,
- height: 44,
- textAlign: 'center',
- margin: 8,
- }}
- >
+
onChange && onChange(layout)} key={layout}>
))}
-
+
diff --git a/src/components/Sidebar/index.js b/src/components/Sidebar/index.js
index 1a538168..a68b0b30 100644
--- a/src/components/Sidebar/index.js
+++ b/src/components/Sidebar/index.js
@@ -1,6 +1,7 @@
import React, { PureComponent, Fragment } from 'react';
import { Select, message, List, Switch, Divider, Radio } from 'antd';
import DrawerMenu from 'rc-drawer-menu';
+import { connect } from 'dva';
import styles from './index.less';
import ThemeColor from './ThemeColor';
import LayoutSeting from './LayoutSetting';
@@ -30,45 +31,23 @@ const Body = ({ children, title, style }) => (
{children}
);
-
+@connect(({ setting }) => ({ setting }))
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() {
- this.colorChange(this.state.themeColor);
+ const { themeColor } = this.props.setting;
+ if (themeColor !== '#1890FF') {
+ this.colorChange(themeColor);
+ }
}
getLayOutSetting = () => {
- const { layout } = this.state;
+ const { grid, fixedHeader, autoHideHeader, fixSiderbar, layout } = this.props.setting;
return [
{
title: '栅格模式',
isShow: true,
action: [
this.changeSetting('layout', layout)}
+ value={layout}
+ onChange={value => this.changeSetting('layout', value)}
/>
this.changeSetting('colorWeak', value)}
style={{ width: 120 }}
>
diff --git a/src/components/Sidebar/index.less b/src/components/Sidebar/index.less
index a58f6896..44188567 100644
--- a/src/components/Sidebar/index.less
+++ b/src/components/Sidebar/index.less
@@ -10,6 +10,8 @@
position: fixed;
bottom: 50px;
right: 50px;
+ cursor: pointer;
+ z-index: 99;
box-shadow: 0 0 6px 0 rgba(0, 21, 41, 0.35);
img {
width: 28px;
@@ -37,6 +39,18 @@
}
}
}
+
+.layoutSetting {
+ margin: 5px;
+ display: flex;
+ .item {
+ cursor: pointer;
+ width: 70px;
+ height: 44px;
+ text-align: center;
+ margin: 8px;
+ }
+}
.color_block {
width: 38px;
height: 22px;
diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js
index 28843449..72c174c6 100644
--- a/src/layouts/BasicLayout.js
+++ b/src/layouts/BasicLayout.js
@@ -18,8 +18,6 @@ import Context from './MenuContext';
const { Content } = Layout;
const { AuthorizedRoute, check } = Authorized;
-const RightSidebar = connect(({ setting }) => ({ ...setting }))(Sidebar);
-
/**
* 获取面包屑映射
* @param {Object} menuData 菜单配置
@@ -103,12 +101,6 @@ class BasicLayout extends React.PureComponent {
payload: collapsed,
});
};
- changeSetting = setting => {
- this.props.dispatch({
- type: 'setting/changeSetting',
- payload: setting,
- });
- };
render() {
const { isMobile, redirectData, routerData, fixedHeader, match } = this.props;
const isTop = this.props.layout === 'topmenu';
@@ -164,7 +156,7 @@ class BasicLayout extends React.PureComponent {
{layout}
-
+
)}
diff --git a/src/layouts/Header.js b/src/layouts/Header.js
index 2bf8a6e1..f3968e15 100644
--- a/src/layouts/Header.js
+++ b/src/layouts/Header.js
@@ -18,9 +18,7 @@ class HeaderView extends PureComponent {
document.getElementById('root').addEventListener('scroll', this.handScroll);
}
componentWillUnmount() {
- document
- .getElementById('root')
- .removeEventListener('scroll', this.handScroll);
+ document.getElementById('root').removeEventListener('scroll', this.handScroll);
}
getHeadWidth = () => {
const { fixedHeader, layout, fixSiderbar } = this.props.setting;
@@ -34,7 +32,7 @@ class HeaderView extends PureComponent {
return 'calc(100% - 80px)';
}
};
- handleNoticeClear = (type) => {
+ handleNoticeClear = type => {
message.success(`清空了${type}`);
this.props.dispatch({
type: 'global/clearNotices',
@@ -60,7 +58,7 @@ class HeaderView extends PureComponent {
});
}
};
- handleNoticeVisibleChange = (visible) => {
+ handleNoticeVisibleChange = visible => {
if (visible) {
this.props.dispatch({
type: 'global/fetchNotices',
diff --git a/src/layouts/LoadingPage.js b/src/layouts/LoadingPage.js
index 5a4167be..f5a110b8 100644
--- a/src/layouts/LoadingPage.js
+++ b/src/layouts/LoadingPage.js
@@ -56,25 +56,12 @@ class LoadingPage extends PureComponent {
loading: false,
});
}
+ /**
+ * get setting from url params
+ */
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({
- type: 'setting/changeSetting',
- payload: setting,
+ type: 'setting/getSetting',
});
}
render() {
diff --git a/src/models/setting.js b/src/models/setting.js
index a7b3a6fb..783ff344 100644
--- a/src/models/setting.js
+++ b/src/models/setting.js
@@ -1,20 +1,37 @@
+const defaultSetting = {
+ collapse: false,
+ silderTheme: 'dark',
+ themeColor: '#1890FF',
+ layout: 'sidemenu',
+ grid: 'Fluid',
+ fixedHeader: false,
+ autoHideHeader: false,
+ fixSiderbar: false,
+ colorWeak: 'close',
+};
export default {
namespace: 'setting',
- state: {
- collapse: false,
- silderTheme: 'dark',
- themeColor: '#1890FF',
- layout: 'sidemenu',
- grid: 'Fluid',
- fixedHeader: false,
- autoHideHeader: false,
- fixSiderbar: false,
- colorWeak: 'close',
- },
+ state: defaultSetting,
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 }) {
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 => {
if (key === 'collapse') {
return;
@@ -23,7 +40,9 @@ export default {
if (value === true) {
value = 1;
}
- urlParams.searchParams.set(key, value);
+ if (defaultSetting[key] !== value) {
+ urlParams.searchParams.set(key, value);
+ }
});
window.history.replaceState(null, 'setting', urlParams.href);
return {
--
GitLab