index.js 7.72 KB
Newer Older
1
import React, { Component } from 'react';
afc163's avatar
afc163 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert, Tooltip } from 'antd';
陈小聪's avatar
陈小聪 committed
3
import { formatMessage } from 'umi-plugin-react/locale';
陈帅's avatar
陈帅 committed
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
import styles from './index.less';
import ThemeColor from './ThemeColor';
9
import BlockCheckbox from './BlockCheckbox';
jim's avatar
jim committed
10

afc163's avatar
afc163 committed
11 12
const { Option } = Select;

jim's avatar
jim committed
13 14 15 16 17 18 19 20 21 22 23 24 25
const Body = ({ children, title, style }) => (
  <div
    style={{
      ...style,
      marginBottom: 24,
    }}
  >
    <h3 className={styles.title}>{title}</h3>
    {children}
  </div>
);

@connect(({ setting }) => ({ setting }))
26
class SettingDrawer extends Component {
afc163's avatar
afc163 committed
27 28 29 30
  state = {
    collapse: false,
  };

afc163's avatar
afc163 committed
31
  getLayoutSetting = () => {
陈帅's avatar
陈帅 committed
32
    const {
afc163's avatar
afc163 committed
33
      setting: { contentWidth, fixedHeader, layout, autoHideHeader, fixSiderbar },
陈帅's avatar
陈帅 committed
34
    } = this.props;
jim's avatar
jim committed
35 36
    return [
      {
afc163's avatar
afc163 committed
37
        title: formatMessage({ id: 'app.setting.content-width' }),
afc163's avatar
afc163 committed
38
        action: (
jim's avatar
jim committed
39
          <Select
afc163's avatar
afc163 committed
40
            value={contentWidth}
jim's avatar
jim committed
41
            size="small"
afc163's avatar
afc163 committed
42
            onSelect={value => this.changeSetting('contentWidth', value)}
jim's avatar
jim committed
43 44
            style={{ width: 80 }}
          >
afc163's avatar
afc163 committed
45 46 47 48 49 50
            {layout === 'sidemenu' ? null : (
              <Option value="Fixed">
                {formatMessage({ id: 'app.setting.content-width.fixed' })}
              </Option>
            )}
            <Option value="Fluid">
afc163's avatar
afc163 committed
51
              {formatMessage({ id: 'app.setting.content-width.fluid' })}
afc163's avatar
afc163 committed
52 53 54
            </Option>
          </Select>
        ),
jim's avatar
jim committed
55 56
      },
      {
陈帅's avatar
陈帅 committed
57
        title: formatMessage({ id: 'app.setting.fixedheader' }),
afc163's avatar
afc163 committed
58
        action: (
jim's avatar
jim committed
59 60 61 62
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
afc163's avatar
afc163 committed
63 64
          />
        ),
jim's avatar
jim committed
65 66
      },
      {
陈帅's avatar
陈帅 committed
67
        title: formatMessage({ id: 'app.setting.hideheader' }),
afc163's avatar
afc163 committed
68
        disabled: !fixedHeader,
afc163's avatar
locales  
afc163 committed
69
        disabledReason: formatMessage({ id: 'app.setting.hideheader.hint' }),
afc163's avatar
afc163 committed
70
        action: (
jim's avatar
jim committed
71 72 73 74
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
afc163's avatar
afc163 committed
75 76
          />
        ),
jim's avatar
jim committed
77 78
      },
      {
陈帅's avatar
陈帅 committed
79
        title: formatMessage({ id: 'app.setting.fixedsidebar' }),
afc163's avatar
afc163 committed
80
        disabled: layout === 'topmenu',
afc163's avatar
locales  
afc163 committed
81
        disabledReason: formatMessage({ id: 'app.setting.fixedsidebar.hint' }),
afc163's avatar
afc163 committed
82
        action: (
jim's avatar
jim committed
83 84 85 86
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
afc163's avatar
afc163 committed
87 88
          />
        ),
jim's avatar
jim committed
89
      },
afc163's avatar
afc163 committed
90
    ];
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
    nextState[key] = value;
    if (key === 'layout') {
afc163's avatar
afc163 committed
98
      nextState.contentWidth = value === 'topmenu' ? 'Fixed' : 'Fluid';
afc163's avatar
afc163 committed
99 100
    } else if (key === 'fixedHeader' && !value) {
      nextState.autoHideHeader = false;
jim's avatar
jim committed
101
    }
jim's avatar
jim committed
102
    this.setState(nextState, () => {
陈帅's avatar
陈帅 committed
103 104
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
105 106 107 108 109
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
陈帅's avatar
陈帅 committed
110

jim's avatar
jim committed
111
  togglerContent = () => {
afc163's avatar
afc163 committed
112 113
    const { collapse } = this.state;
    this.setState({ collapse: !collapse });
jim's avatar
jim committed
114
  };
陈帅's avatar
陈帅 committed
115

afc163's avatar
afc163 committed
116 117 118 119 120 121 122 123 124 125 126 127 128
  renderLayoutSettingItem = item => {
    const action = React.cloneElement(item.action, {
      disabled: item.disabled,
    });
    return (
      <Tooltip title={item.disabled ? item.disabledReason : ''} placement="left">
        <List.Item actions={[action]}>
          <span style={{ opacity: item.disabled ? '0.5' : '' }}>{item.title}</span>
        </List.Item>
      </Tooltip>
    );
  };

jim's avatar
jim committed
129
  render() {
陈帅's avatar
陈帅 committed
130
    const { setting } = this.props;
afc163's avatar
afc163 committed
131 132
    const { navTheme, primaryColor, layout, colorWeak } = setting;
    const { collapse } = this.state;
jim's avatar
jim committed
133
    return (
陈帅's avatar
陈帅 committed
134 135
      <Drawer
        visible={collapse}
136
        width={300}
陈帅's avatar
陈帅 committed
137
        onClose={this.togglerContent}
陈帅's avatar
陈帅 committed
138
        placement="right"
陈帅's avatar
陈帅 committed
139
        handler={
140
          <div className={styles.handle} onClick={this.togglerContent}>
afc163's avatar
afc163 committed
141 142 143 144 145 146 147
            <Icon
              type={collapse ? 'close' : 'setting'}
              style={{
                color: '#fff',
                fontSize: 20,
              }}
            />
陈帅's avatar
陈帅 committed
148 149
          </div>
        }
陈帅's avatar
陈帅 committed
150 151 152 153 154
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
陈帅's avatar
陈帅 committed
155
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
156
            <BlockCheckbox
陈帅's avatar
陈帅 committed
157 158 159 160
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
afc163's avatar
afc163 committed
161
                  title: formatMessage({ id: 'app.setting.pagestyle.dark' }),
陈帅's avatar
陈帅 committed
162 163 164 165
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
afc163's avatar
afc163 committed
166
                  title: formatMessage({ id: 'app.setting.pagestyle.light' }),
陈帅's avatar
陈帅 committed
167 168
                },
              ]}
afc163's avatar
afc163 committed
169 170
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
陈帅's avatar
陈帅 committed
171 172
            />
          </Body>
jim's avatar
jim committed
173

174
          <ThemeColor
陈帅's avatar
陈帅 committed
175
            title={formatMessage({ id: 'app.setting.themecolor' })}
afc163's avatar
afc163 committed
176 177
            value={primaryColor}
            onChange={color => this.changeSetting('primaryColor', color)}
178
          />
jim's avatar
jim committed
179

陈帅's avatar
陈帅 committed
180
          <Divider />
jim's avatar
jim committed
181

陈帅's avatar
陈帅 committed
182
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
183
            <BlockCheckbox
陈帅's avatar
陈帅 committed
184 185 186 187
              list={[
                {
                  key: 'sidemenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
afc163's avatar
afc163 committed
188
                  title: formatMessage({ id: 'app.setting.sidemenu' }),
陈帅's avatar
陈帅 committed
189 190 191 192
                },
                {
                  key: 'topmenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
afc163's avatar
afc163 committed
193
                  title: formatMessage({ id: 'app.setting.topmenu' }),
陈帅's avatar
陈帅 committed
194 195 196 197
                },
              ]}
              value={layout}
              onChange={value => this.changeSetting('layout', value)}
jim's avatar
jim committed
198
            />
陈帅's avatar
陈帅 committed
199
          </Body>
jim's avatar
jim committed
200

陈帅's avatar
陈帅 committed
201 202
          <List
            split={false}
afc163's avatar
afc163 committed
203
            dataSource={this.getLayoutSetting()}
afc163's avatar
afc163 committed
204
            renderItem={this.renderLayoutSettingItem}
陈帅's avatar
陈帅 committed
205
          />
jim's avatar
jim committed
206

陈帅's avatar
陈帅 committed
207 208
          <Divider />

陈帅's avatar
陈帅 committed
209
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
210 211 212 213 214 215 216 217 218 219 220 221 222 223
            <List
              split={false}
              renderItem={this.renderLayoutSettingItem}
              dataSource={[
                {
                  title: formatMessage({ id: 'app.setting.weakmode' }),
                  action: (
                    <Switch
                      size="small"
                      checked={!!colorWeak}
                      onChange={checked => this.changeSetting('colorWeak', checked)}
                    />
                  ),
                },
陈帅's avatar
陈帅 committed
224
              ]}
225
            />
陈帅's avatar
陈帅 committed
226
          </Body>
陈帅's avatar
陈帅 committed
227 228
          <Divider />
          <CopyToClipboard
afc163's avatar
afc163 committed
229
            text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
陈帅's avatar
陈帅 committed
230
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
陈帅's avatar
陈帅 committed
231
          >
afc163's avatar
afc163 committed
232
            <Button block icon="copy">
陈帅's avatar
陈帅 committed
233
              {formatMessage({ id: 'app.setting.copy' })}
陈帅's avatar
陈帅 committed
234 235
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
236 237 238
          <Alert
            type="warning"
            className={styles.productionHint}
afc163's avatar
afc163 committed
239 240 241 242 243 244 245 246 247 248 249 250
            message={
              <div>
                {formatMessage({ id: 'app.setting.production.hint' })}{' '}
                <a
                  href="https://u.ant.design/pro-v2-default-settings"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  src/defaultSettings.js
                </a>
              </div>
            }
afc163's avatar
afc163 committed
251
          />
陈帅's avatar
陈帅 committed
252
        </div>
陈帅's avatar
陈帅 committed
253
      </Drawer>
jim's avatar
jim committed
254 255 256 257
    );
  }
}

258
export default SettingDrawer;