index.js 7.56 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, Tooltip } 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
import { connect } from 'dva';
afc163's avatar
afc163 committed
6
import omit from 'omit.js';
jim's avatar
jim committed
7 8 9 10
import styles from './index.less';
import ThemeColor from './ThemeColor';
import BlockChecbox from './BlockChecbox';

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 PureComponent {
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 140
        handler={
          <div className={styles.handle}>
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 150
          </div>
        }
        onHandleClick={this.togglerContent}
陈帅's avatar
陈帅 committed
151 152 153 154 155
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
陈帅's avatar
陈帅 committed
156
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
陈帅's avatar
陈帅 committed
157 158 159 160 161
            <BlockChecbox
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
afc163's avatar
afc163 committed
162
                  title: formatMessage({ id: 'app.setting.pagestyle.dark' }),
陈帅's avatar
陈帅 committed
163 164 165 166
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
afc163's avatar
afc163 committed
167
                  title: formatMessage({ id: 'app.setting.pagestyle.light' }),
陈帅's avatar
陈帅 committed
168 169
                },
              ]}
afc163's avatar
afc163 committed
170 171
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
陈帅's avatar
陈帅 committed
172 173
            />
          </Body>
jim's avatar
jim committed
174

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

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

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

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

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

陈帅's avatar
陈帅 committed
210
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
陈帅's avatar
陈帅 committed
211 212 213 214 215 216 217 218 219
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
陈帅's avatar
陈帅 committed
220
              {formatMessage({ id: 'app.setting.weakmode' })}
陈帅's avatar
陈帅 committed
221 222
            </List.Item>
          </Body>
陈帅's avatar
陈帅 committed
223 224
          <Divider />
          <CopyToClipboard
afc163's avatar
afc163 committed
225
            text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
陈帅's avatar
陈帅 committed
226
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
陈帅's avatar
陈帅 committed
227
          >
afc163's avatar
afc163 committed
228
            <Button block icon="copy">
陈帅's avatar
陈帅 committed
229
              {formatMessage({ id: 'app.setting.copy' })}
陈帅's avatar
陈帅 committed
230 231
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
232 233 234
          <Alert
            type="warning"
            className={styles.productionHint}
afc163's avatar
afc163 committed
235 236 237 238 239 240 241 242 243 244 245 246
            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
247
          />
陈帅's avatar
陈帅 committed
248
        </div>
陈帅's avatar
陈帅 committed
249
      </Drawer>
jim's avatar
jim committed
250 251 252 253
    );
  }
}

254
export default SettingDrawer;