index.js 6.53 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 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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 }))
23
class SettingDrawer extends PureComponent {
jim's avatar
jim committed
24
  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)}
          />,
        ],
      },
79
    ].filter(item => !item.hide);
jim's avatar
jim committed
80
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
81

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

jim's avatar
jim committed
107
  togglerContent = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
108 109
    const { setting } = this.props;
    this.changeSetting('collapse', !setting.collapse);
jim's avatar
jim committed
110
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
111

jim's avatar
jim committed
112
  render() {
113
    const { setting } = this.props;
afc163's avatar
afc163 committed
114
    const { collapse, navTheme, primaryColor, layout, colorWeak } = setting;
jim's avatar
jim committed
115
    return (
116 117
      <Drawer
        visible={collapse}
118
        width={300}
119
        onClose={this.togglerContent}
120
        placement="right"
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        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}
143 144 145 146 147
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
148
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
149 150 151 152 153 154 155 156 157 158 159
            <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
160 161
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
162 163
            />
          </Body>
jim's avatar
jim committed
164

165
          <ThemeColor
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
166
            title={formatMessage({ id: 'app.setting.themecolor' })}
afc163's avatar
afc163 committed
167 168
            value={primaryColor}
            onChange={color => this.changeSetting('primaryColor', color)}
169
          />
jim's avatar
jim committed
170

171
          <Divider />
jim's avatar
jim committed
172

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
173
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
174 175 176 177 178 179 180 181 182 183 184 185 186
            <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
187
            />
188
          </Body>
jim's avatar
jim committed
189

190 191 192 193 194
          <List
            split={false}
            dataSource={this.getLayOutSetting()}
            renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
          />
jim's avatar
jim committed
195

196 197
          <Divider />

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
198
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
199 200 201 202 203 204 205 206 207
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
208
              {formatMessage({ id: 'app.setting.weakmode' })}
209 210
            </List.Item>
          </Body>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
211 212 213
          <Divider />
          <CopyToClipboard
            text={JSON.stringify(setting)}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
214
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
215
          >
afc163's avatar
afc163 committed
216
            <Button block icon="copy">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
217
              {formatMessage({ id: 'app.setting.copy' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
218 219
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
220 221 222 223 224
          <Alert
            type="warning"
            className={styles.productionHint}
            message={formatMessage({ id: 'app.setting.production.hint' })}
          />
225
        </div>
226
      </Drawer>
jim's avatar
jim committed
227 228 229 230
    );
  }
}

231
export default SettingDrawer;