index.js 6.49 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
陈帅's avatar
陈帅 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button } from 'antd';
陈帅's avatar
陈帅 committed
3
import { formatMessage } from 'umi/locale';
陈帅'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 {
  getLayOutSetting = () => {
陈帅's avatar
陈帅 committed
25
    const {
26
      setting: { grid, fixedHeader, layout, autoHideHeader, fixSiderbar },
陈帅's avatar
陈帅 committed
27
    } = this.props;
jim's avatar
jim committed
28 29
    return [
      {
陈帅's avatar
陈帅 committed
30
        title: formatMessage({ id: 'app.setting.gridmode' }),
jim's avatar
jim committed
31 32 33 34 35 36 37
        action: [
          <Select
            value={grid}
            size="small"
            onSelect={value => this.changeSetting('grid', value)}
            style={{ width: 80 }}
          >
陈帅's avatar
陈帅 committed
38 39 40 41 42 43
            <Select.Option value="Wide">
              {formatMessage({ id: 'app.setting.gridmode.wide' })}
            </Select.Option>
            <Select.Option value="Fluid">
              {formatMessage({ id: 'app.setting.gridmode.fluid' })}
            </Select.Option>
jim's avatar
jim committed
44 45 46 47
          </Select>,
        ],
      },
      {
陈帅's avatar
陈帅 committed
48
        title: formatMessage({ id: 'app.setting.fixedheader' }),
jim's avatar
jim committed
49 50 51 52 53 54 55 56 57
        action: [
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
陈帅's avatar
陈帅 committed
58
        title: formatMessage({ id: 'app.setting.hideheader' }),
59
        hide: !fixedHeader,
jim's avatar
jim committed
60 61 62 63 64 65 66 67 68
        action: [
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
陈帅's avatar
陈帅 committed
69
        title: formatMessage({ id: 'app.setting.fixedsidebar' }),
70
        hide: layout === 'topmenu',
jim's avatar
jim committed
71 72 73 74 75 76 77 78
        action: [
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
          />,
        ],
      },
jim's avatar
jim committed
79 80 81
    ].filter(item => {
      return !item.hide;
    });
jim's avatar
jim committed
82
  };
陈帅's avatar
陈帅 committed
83

jim's avatar
jim committed
84
  changeSetting = (key, value) => {
陈帅's avatar
陈帅 committed
85 86
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
87 88 89 90 91 92 93 94
    nextState[key] = value;
    if (key === 'layout') {
      if (value === 'topmenu') {
        nextState.grid = 'Wide';
      } else {
        nextState.grid = 'Fluid';
      }
    }
jim's avatar
jim committed
95
    if (key === 'fixedHeader') {
96
      if (!value) {
jim's avatar
jim committed
97 98 99
        nextState.autoHideHeader = false;
      }
    }
jim's avatar
jim committed
100
    this.setState(nextState, () => {
陈帅's avatar
陈帅 committed
101 102
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
103 104 105 106 107
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
陈帅's avatar
陈帅 committed
108

jim's avatar
jim committed
109
  togglerContent = () => {
陈帅's avatar
陈帅 committed
110 111
    const { setting } = this.props;
    this.changeSetting('collapse', !setting.collapse);
jim's avatar
jim committed
112
  };
陈帅's avatar
陈帅 committed
113

jim's avatar
jim committed
114
  render() {
陈帅's avatar
陈帅 committed
115 116
    const { setting } = this.props;
    const { collapse, silderTheme, themeColor, layout, colorWeak } = setting;
jim's avatar
jim committed
117
    return (
陈帅's avatar
陈帅 committed
118
      <Drawer
陈帅's avatar
陈帅 committed
119
        firstEnter={true}
陈帅's avatar
陈帅 committed
120 121 122
        visible={collapse}
        width={273}
        onClose={this.togglerContent}
陈帅's avatar
陈帅 committed
123
        placement="right"
陈帅's avatar
陈帅 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        handler={
          <div className={styles.handle}>
            {!collapse ? (
              <Icon
                type="setting"
                style={{
                  color: '#FFF',
                  fontSize: 20,
                }}
              />
            ) : (
              <Icon
                type="close"
                style={{
                  color: '#FFF',
                  fontSize: 20,
                }}
              />
            )}
          </div>
        }
        onHandleClick={this.togglerContent}
陈帅's avatar
陈帅 committed
146 147 148 149 150
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
陈帅's avatar
陈帅 committed
151
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
陈帅's avatar
陈帅 committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            <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
167

168
          <ThemeColor
陈帅's avatar
陈帅 committed
169
            title={formatMessage({ id: 'app.setting.themecolor' })}
170 171 172
            value={themeColor}
            onChange={color => this.changeSetting('themeColor', color)}
          />
jim's avatar
jim committed
173

陈帅's avatar
陈帅 committed
174
          <Divider />
jim's avatar
jim committed
175

陈帅's avatar
陈帅 committed
176
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
陈帅's avatar
陈帅 committed
177 178 179 180 181 182 183 184 185 186 187 188 189
            <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
190
            />
陈帅's avatar
陈帅 committed
191
          </Body>
jim's avatar
jim committed
192

陈帅's avatar
陈帅 committed
193 194 195 196 197
          <List
            split={false}
            dataSource={this.getLayOutSetting()}
            renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
          />
jim's avatar
jim committed
198

陈帅's avatar
陈帅 committed
199 200
          <Divider />

陈帅's avatar
陈帅 committed
201
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
陈帅's avatar
陈帅 committed
202 203 204 205 206 207 208 209 210
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
陈帅's avatar
陈帅 committed
211
              {formatMessage({ id: 'app.setting.weakmode' })}
陈帅's avatar
陈帅 committed
212 213
            </List.Item>
          </Body>
陈帅's avatar
陈帅 committed
214 215 216
          <Divider />
          <CopyToClipboard
            text={JSON.stringify(setting)}
陈帅's avatar
陈帅 committed
217
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
陈帅's avatar
陈帅 committed
218 219 220 221 222 223
          >
            <Button
              style={{
                width: 224,
              }}
            >
陈帅's avatar
陈帅 committed
224
              <Icon type="copy" />
陈帅's avatar
陈帅 committed
225
              {formatMessage({ id: 'app.setting.copy' })}
陈帅's avatar
陈帅 committed
226 227
            </Button>
          </CopyToClipboard>
陈帅's avatar
陈帅 committed
228
        </div>
陈帅's avatar
陈帅 committed
229
      </Drawer>
jim's avatar
jim committed
230 231 232 233 234
    );
  }
}

export default SettingDarwer;