index.js 6.34 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
afc163's avatar
afc163 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert } from 'antd';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
3
import { formatMessage } from 'umi/locale';
4
import { CopyToClipboard } from 'react-copy-to-clipboard';
jim's avatar
jim committed
5
import { connect } from 'dva';
afc163's avatar
afc163 committed
6
import omit from 'omit.js';
jim's avatar
jim committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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 }))
24
class SettingDrawer extends PureComponent {
afc163's avatar
afc163 committed
25 26 27 28
  state = {
    collapse: false,
  };

jim's avatar
jim committed
29
  getLayOutSetting = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
30
    const {
31
      setting: { grid, fixedHeader, layout, autoHideHeader, fixSiderbar },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
32
    } = this.props;
jim's avatar
jim committed
33 34
    return [
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
35
        title: formatMessage({ id: 'app.setting.gridmode' }),
jim's avatar
jim committed
36 37 38 39 40 41 42
        action: [
          <Select
            value={grid}
            size="small"
            onSelect={value => this.changeSetting('grid', value)}
            style={{ width: 80 }}
          >
afc163's avatar
afc163 committed
43 44
            <Select.Option value="Fixed">
              {formatMessage({ id: 'app.setting.gridmode.fixed' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
45 46 47 48
            </Select.Option>
            <Select.Option value="Fluid">
              {formatMessage({ id: 'app.setting.gridmode.fluid' })}
            </Select.Option>
jim's avatar
jim committed
49 50 51 52
          </Select>,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
53
        title: formatMessage({ id: 'app.setting.fixedheader' }),
jim's avatar
jim committed
54 55 56 57 58 59 60 61 62
        action: [
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
63
        title: formatMessage({ id: 'app.setting.hideheader' }),
64
        hide: !fixedHeader,
jim's avatar
jim committed
65 66 67 68 69 70 71 72 73
        action: [
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74
        title: formatMessage({ id: 'app.setting.fixedsidebar' }),
75
        hide: layout === 'topmenu',
jim's avatar
jim committed
76 77 78 79 80 81 82 83
        action: [
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
          />,
        ],
      },
84
    ].filter(item => !item.hide);
jim's avatar
jim committed
85
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
86

jim's avatar
jim committed
87
  changeSetting = (key, value) => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
88 89
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
90 91
    nextState[key] = value;
    if (key === 'layout') {
afc163's avatar
afc163 committed
92
      nextState.grid = value === 'topmenu' ? 'Fixed' : 'Fluid';
afc163's avatar
afc163 committed
93 94
    } else if (key === 'fixedHeader' && !value) {
      nextState.autoHideHeader = false;
jim's avatar
jim committed
95
    }
jim's avatar
jim committed
96
    this.setState(nextState, () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
97 98
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
99 100 101 102 103
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
104

jim's avatar
jim committed
105
  togglerContent = () => {
afc163's avatar
afc163 committed
106 107
    const { collapse } = this.state;
    this.setState({ collapse: !collapse });
jim's avatar
jim committed
108
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
109

jim's avatar
jim committed
110
  render() {
111
    const { setting } = this.props;
afc163's avatar
afc163 committed
112 113
    const { navTheme, primaryColor, layout, colorWeak } = setting;
    const { collapse } = this.state;
jim's avatar
jim committed
114
    return (
115 116
      <Drawer
        visible={collapse}
117
        width={300}
118
        onClose={this.togglerContent}
119
        placement="right"
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
120 121
        handler={
          <div className={styles.handle}>
afc163's avatar
afc163 committed
122 123 124 125 126 127 128
            <Icon
              type={collapse ? 'close' : 'setting'}
              style={{
                color: '#fff',
                fontSize: 20,
              }}
            />
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
129 130 131
          </div>
        }
        onHandleClick={this.togglerContent}
132 133 134 135 136
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
137
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
138 139 140 141 142 143 144 145 146 147 148
            <BlockChecbox
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
                },
              ]}
afc163's avatar
afc163 committed
149 150
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
151 152
            />
          </Body>
jim's avatar
jim committed
153

154
          <ThemeColor
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
155
            title={formatMessage({ id: 'app.setting.themecolor' })}
afc163's avatar
afc163 committed
156 157
            value={primaryColor}
            onChange={color => this.changeSetting('primaryColor', color)}
158
          />
jim's avatar
jim committed
159

160
          <Divider />
jim's avatar
jim committed
161

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
162
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
163 164 165 166 167 168 169 170 171 172 173 174 175
            <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
176
            />
177
          </Body>
jim's avatar
jim committed
178

179 180 181 182 183
          <List
            split={false}
            dataSource={this.getLayOutSetting()}
            renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
          />
jim's avatar
jim committed
184

185 186
          <Divider />

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
187
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
188 189 190 191 192 193 194 195 196
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
197
              {formatMessage({ id: 'app.setting.weakmode' })}
198 199
            </List.Item>
          </Body>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
200 201
          <Divider />
          <CopyToClipboard
afc163's avatar
afc163 committed
202
            text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
203
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
204
          >
afc163's avatar
afc163 committed
205
            <Button block icon="copy">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
206
              {formatMessage({ id: 'app.setting.copy' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
207 208
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
209 210 211 212 213
          <Alert
            type="warning"
            className={styles.productionHint}
            message={formatMessage({ id: 'app.setting.production.hint' })}
          />
214
        </div>
215
      </Drawer>
jim's avatar
jim committed
216 217 218 219
    );
  }
}

220
export default SettingDrawer;