index.js 6.55 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
jim's avatar
jim committed
2
import { Select, message, List, Switch, Divider, Icon } from 'antd';
jim's avatar
jim committed
3
import DrawerMenu from 'rc-drawer';
jim's avatar
jim committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { connect } from 'dva';
import styles from './index.less';
import ThemeColor from './ThemeColor';
import BlockChecbox from './BlockChecbox';

const Body = ({ children, title, style }) => (
  <div
    style={{
      ...style,
      marginBottom: 24,
    }}
  >
    <h3 className={styles.title}>{title}</h3>
    {children}
  </div>
);

@connect(({ setting }) => ({ setting }))
class SettingDarwer extends PureComponent {
  componentDidMount() {
陈帅's avatar
陈帅 committed
24 25 26
    const {
      setting: { themeColor, colorWeak },
    } = this.props;
jim's avatar
jim committed
27
    if (themeColor !== '#1890FF') {
F-loat's avatar
F-loat committed
28 29 30
      window.less.refresh().then(() => {
        this.colorChange(themeColor);
      });
jim's avatar
jim committed
31
    }
jim's avatar
jim committed
32 33 34
    if (colorWeak === 'open') {
      document.body.className = 'colorWeak';
    }
jim's avatar
jim committed
35
  }
陈帅's avatar
陈帅 committed
36

jim's avatar
jim committed
37
  getLayOutSetting = () => {
陈帅's avatar
陈帅 committed
38 39 40
    const {
      setting: { grid, fixedHeader, autoHideHeader, fixSiderbar },
    } = this.props;
jim's avatar
jim committed
41 42 43 44 45 46 47 48 49 50
    return [
      {
        title: '栅格模式',
        action: [
          <Select
            value={grid}
            size="small"
            onSelect={value => this.changeSetting('grid', value)}
            style={{ width: 80 }}
          >
jim's avatar
jim committed
51 52
            <Select.Option value="Wide">定宽</Select.Option>
            <Select.Option value="Fluid">流式</Select.Option>
jim's avatar
jim committed
53 54 55 56
          </Select>,
        ],
      },
      {
jim's avatar
jim committed
57
        title: '固定 Header',
jim's avatar
jim committed
58 59 60 61 62 63 64 65 66 67
        action: [
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
        title: '下滑时隐藏 Header',
68
        hide: !fixedHeader,
jim's avatar
jim committed
69 70 71 72 73 74 75 76 77
        action: [
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
jim's avatar
jim committed
78
        title: '固定 Siderbar',
jim's avatar
jim committed
79 80 81 82 83 84 85 86
        action: [
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
          />,
        ],
      },
jim's avatar
jim committed
87 88 89
    ].filter(item => {
      return !item.hide;
    });
jim's avatar
jim committed
90
  };
陈帅's avatar
陈帅 committed
91

jim's avatar
jim committed
92
  changeSetting = (key, value) => {
陈帅's avatar
陈帅 committed
93 94
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
95 96 97 98 99 100 101 102
    nextState[key] = value;
    if (key === 'layout') {
      if (value === 'topmenu') {
        nextState.grid = 'Wide';
      } else {
        nextState.grid = 'Fluid';
      }
    }
jim's avatar
jim committed
103
    if (key === 'fixedHeader') {
104
      if (!value) {
jim's avatar
jim committed
105 106 107
        nextState.autoHideHeader = false;
      }
    }
jim's avatar
jim committed
108
    if (key === 'colorWeak') {
陈帅's avatar
陈帅 committed
109
      if (value) {
jim's avatar
jim committed
110 111 112 113 114
        document.body.className = 'colorWeak';
      } else {
        document.body.className = '';
      }
    }
jim's avatar
jim committed
115
    this.setState(nextState, () => {
陈帅's avatar
陈帅 committed
116 117
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
118 119 120 121 122
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
陈帅's avatar
陈帅 committed
123

jim's avatar
jim committed
124
  togglerContent = () => {
陈帅's avatar
陈帅 committed
125 126
    const { setting } = this.props;
    this.changeSetting('collapse', !setting.collapse);
jim's avatar
jim committed
127
  };
陈帅's avatar
陈帅 committed
128

jim's avatar
jim committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  colorChange = color => {
    this.changeSetting('themeColor', color);
    const hideMessage = message.loading('正在编译主题!', 0);
    setTimeout(() => {
      window.less
        .modifyVars({
          '@primary-color': color,
        })
        .then(() => {
          hideMessage();
        })
        .catch(() => {
          message.error(`Failed to update theme`);
        });
    }, 200);
  };
陈帅's avatar
陈帅 committed
145

jim's avatar
jim committed
146
  render() {
陈帅's avatar
陈帅 committed
147 148 149
    const {
      setting: { collapse, silderTheme, themeColor, layout, colorWeak },
    } = this.props;
jim's avatar
jim committed
150 151 152 153 154 155
    return (
      <div className={styles.settingDarwer}>
        <DrawerMenu
          parent={null}
          level={null}
          open={collapse}
qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
156
          mask={false}
jim's avatar
jim committed
157
          onHandleClick={this.togglerContent}
陈帅's avatar
陈帅 committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
          handler={
            <div className="drawer-handle">
              {!collapse ? (
                <Icon
                  type="setting"
                  style={{
                    color: '#FFF',
                    fontSize: 20,
                  }}
                />
              ) : (
                <Icon
                  type="close"
                  style={{
                    color: '#FFF',
                    fontSize: 20,
                  }}
                />
              )}
            </div>
jim's avatar
jim committed
178
          }
jim's avatar
jim committed
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
          placement="right"
          width="336px"
          style={{
            zIndex: 999,
          }}
          onMaskClick={this.togglerContent}
        >
          <div className={styles.content}>
            <Body title="整体风格设置">
              <BlockChecbox
                list={[
                  {
                    key: 'dark',
                    url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
                  },
                  {
                    key: 'light',
                    url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
                  },
                ]}
                value={silderTheme}
                onChange={value => this.changeSetting('silderTheme', value)}
              />
            </Body>

            <ThemeColor value={themeColor} onChange={this.colorChange} />

            <Divider />

            <Body title="导航设置 ">
              <BlockChecbox
                list={[
                  {
                    key: 'sidemenu',
                    url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
                  },
                  {
                    key: 'topmenu',
                    url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
                  },
                ]}
                value={layout}
                onChange={value => this.changeSetting('layout', value)}
              />
            </Body>

            <List
              split={false}
              dataSource={this.getLayOutSetting()}
              renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
            />
jim's avatar
jim committed
230 231 232

            <Divider />

qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
233
            <Body title="其他设置 ">
jim's avatar
jim committed
234 235
              <List.Item
                actions={[
陈帅's avatar
陈帅 committed
236
                  <Switch
jim's avatar
jim committed
237
                    size="small"
陈帅's avatar
陈帅 committed
238 239 240
                    checked={!!colorWeak}
                    onChange={checked => this.changeSetting('colorWeak', checked)}
                  />,
jim's avatar
jim committed
241 242 243 244 245
                ]}
              >
                色弱模式
              </List.Item>
            </Body>
jim's avatar
jim committed
246 247 248 249 250 251 252 253
          </div>
        </DrawerMenu>
      </div>
    );
  }
}

export default SettingDarwer;