index.js 6.8 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
陈帅's avatar
陈帅 committed
2
import { Select, message, List, Switch, Divider, Icon, Button } from 'antd';
jim's avatar
jim committed
3
import DrawerMenu from 'rc-drawer';
陈帅's avatar
陈帅 committed
4
import { CopyToClipboard } from 'react-copy-to-clipboard';
jim's avatar
jim committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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
25 26 27
    const {
      setting: { themeColor, colorWeak },
    } = this.props;
jim's avatar
jim committed
28
    if (themeColor !== '#1890FF') {
F-loat's avatar
F-loat committed
29 30 31
      window.less.refresh().then(() => {
        this.colorChange(themeColor);
      });
jim's avatar
jim committed
32
    }
jim's avatar
jim committed
33 34 35
    if (colorWeak === 'open') {
      document.body.className = 'colorWeak';
    }
jim's avatar
jim committed
36
  }
陈帅's avatar
陈帅 committed
37

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

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

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

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

jim's avatar
jim committed
148
  render() {
陈帅's avatar
陈帅 committed
149 150
    const { setting } = this.props;
    const { collapse, silderTheme, themeColor, layout, colorWeak } = setting;
jim's avatar
jim committed
151
    return (
陈帅's avatar
陈帅 committed
152
      <DrawerMenu
陈帅's avatar
陈帅 committed
153
        getContainer={null}
陈帅's avatar
陈帅 committed
154 155 156 157 158 159 160 161 162 163 164 165 166
        level={null}
        open={collapse}
        mask={false}
        onHandleClick={this.togglerContent}
        handler={
          <div className="drawer-handle">
            {!collapse ? (
              <Icon
                type="setting"
                style={{
                  color: '#FFF',
                  fontSize: 20,
                }}
jim's avatar
jim committed
167
              />
陈帅's avatar
陈帅 committed
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
            ) : (
              <Icon
                type="close"
                style={{
                  color: '#FFF',
                  fontSize: 20,
                }}
              />
            )}
          </div>
        }
        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>
jim's avatar
jim committed
203

陈帅's avatar
陈帅 committed
204
          <ThemeColor value={themeColor} onChange={this.colorChange} />
jim's avatar
jim committed
205

陈帅's avatar
陈帅 committed
206
          <Divider />
jim's avatar
jim committed
207

陈帅's avatar
陈帅 committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221
          <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)}
jim's avatar
jim committed
222
            />
陈帅's avatar
陈帅 committed
223
          </Body>
jim's avatar
jim committed
224

陈帅's avatar
陈帅 committed
225 226 227 228 229
          <List
            split={false}
            dataSource={this.getLayOutSetting()}
            renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
          />
jim's avatar
jim committed
230

陈帅's avatar
陈帅 committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
          <Divider />

          <Body title="其他设置 ">
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
              色弱模式
            </List.Item>
          </Body>
陈帅's avatar
陈帅 committed
246 247 248 249 250 251 252 253 254 255 256 257 258
          <Divider />
          <CopyToClipboard
            text={JSON.stringify(setting)}
            onCopy={() => message.success('copy success')}
          >
            <Button
              style={{
                width: 224,
              }}
            >
              <Icon type="copy" />拷贝代码
            </Button>
          </CopyToClipboard>
陈帅's avatar
陈帅 committed
259 260
        </div>
      </DrawerMenu>
jim's avatar
jim committed
261 262 263 264 265
    );
  }
}

export default SettingDarwer;