index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import React, { PureComponent } from 'react';
  2. import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert } from 'antd';
  3. import { formatMessage } from 'umi/locale';
  4. import { CopyToClipboard } from 'react-copy-to-clipboard';
  5. import { connect } from 'dva';
  6. import styles from './index.less';
  7. import ThemeColor from './ThemeColor';
  8. import BlockChecbox from './BlockChecbox';
  9. const Body = ({ children, title, style }) => (
  10. <div
  11. style={{
  12. ...style,
  13. marginBottom: 24,
  14. }}
  15. >
  16. <h3 className={styles.title}>{title}</h3>
  17. {children}
  18. </div>
  19. );
  20. @connect(({ setting }) => ({ setting }))
  21. class SettingDrawer extends PureComponent {
  22. state = {
  23. collapse: false,
  24. };
  25. getLayOutSetting = () => {
  26. const {
  27. setting: { grid, fixedHeader, layout, autoHideHeader, fixSiderbar },
  28. } = this.props;
  29. return [
  30. {
  31. title: formatMessage({ id: 'app.setting.gridmode' }),
  32. action: [
  33. <Select
  34. value={grid}
  35. size="small"
  36. onSelect={value => this.changeSetting('grid', value)}
  37. style={{ width: 80 }}
  38. >
  39. <Select.Option value="Wide">
  40. {formatMessage({ id: 'app.setting.gridmode.wide' })}
  41. </Select.Option>
  42. <Select.Option value="Fluid">
  43. {formatMessage({ id: 'app.setting.gridmode.fluid' })}
  44. </Select.Option>
  45. </Select>,
  46. ],
  47. },
  48. {
  49. title: formatMessage({ id: 'app.setting.fixedheader' }),
  50. action: [
  51. <Switch
  52. size="small"
  53. checked={!!fixedHeader}
  54. onChange={checked => this.changeSetting('fixedHeader', checked)}
  55. />,
  56. ],
  57. },
  58. {
  59. title: formatMessage({ id: 'app.setting.hideheader' }),
  60. hide: !fixedHeader,
  61. action: [
  62. <Switch
  63. size="small"
  64. checked={!!autoHideHeader}
  65. onChange={checked => this.changeSetting('autoHideHeader', checked)}
  66. />,
  67. ],
  68. },
  69. {
  70. title: formatMessage({ id: 'app.setting.fixedsidebar' }),
  71. hide: layout === 'topmenu',
  72. action: [
  73. <Switch
  74. size="small"
  75. checked={!!fixSiderbar}
  76. onChange={checked => this.changeSetting('fixSiderbar', checked)}
  77. />,
  78. ],
  79. },
  80. ].filter(item => !item.hide);
  81. };
  82. changeSetting = (key, value) => {
  83. const { setting } = this.props;
  84. const nextState = { ...setting };
  85. nextState[key] = value;
  86. if (key === 'layout') {
  87. nextState.grid = value === 'topmenu' ? 'Wide' : 'Fluid';
  88. } else if (key === 'fixedHeader' && !value) {
  89. nextState.autoHideHeader = false;
  90. }
  91. this.setState(nextState, () => {
  92. const { dispatch } = this.props;
  93. dispatch({
  94. type: 'setting/changeSetting',
  95. payload: this.state,
  96. });
  97. });
  98. };
  99. togglerContent = () => {
  100. const { collapse } = this.state;
  101. this.setState({ collapse: !collapse });
  102. };
  103. render() {
  104. const { setting } = this.props;
  105. const { navTheme, primaryColor, layout, colorWeak } = setting;
  106. const { collapse } = this.state;
  107. return (
  108. <Drawer
  109. visible={collapse}
  110. width={300}
  111. onClose={this.togglerContent}
  112. placement="right"
  113. handler={
  114. <div className={styles.handle}>
  115. <Icon
  116. type={collapse ? 'close' : 'setting'}
  117. style={{
  118. color: '#fff',
  119. fontSize: 20,
  120. }}
  121. />
  122. </div>
  123. }
  124. onHandleClick={this.togglerContent}
  125. style={{
  126. zIndex: 999,
  127. }}
  128. >
  129. <div className={styles.content}>
  130. <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
  131. <BlockChecbox
  132. list={[
  133. {
  134. key: 'dark',
  135. url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
  136. },
  137. {
  138. key: 'light',
  139. url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
  140. },
  141. ]}
  142. value={navTheme}
  143. onChange={value => this.changeSetting('navTheme', value)}
  144. />
  145. </Body>
  146. <ThemeColor
  147. title={formatMessage({ id: 'app.setting.themecolor' })}
  148. value={primaryColor}
  149. onChange={color => this.changeSetting('primaryColor', color)}
  150. />
  151. <Divider />
  152. <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
  153. <BlockChecbox
  154. list={[
  155. {
  156. key: 'sidemenu',
  157. url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
  158. },
  159. {
  160. key: 'topmenu',
  161. url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
  162. },
  163. ]}
  164. value={layout}
  165. onChange={value => this.changeSetting('layout', value)}
  166. />
  167. </Body>
  168. <List
  169. split={false}
  170. dataSource={this.getLayOutSetting()}
  171. renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
  172. />
  173. <Divider />
  174. <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
  175. <List.Item
  176. actions={[
  177. <Switch
  178. size="small"
  179. checked={!!colorWeak}
  180. onChange={checked => this.changeSetting('colorWeak', checked)}
  181. />,
  182. ]}
  183. >
  184. {formatMessage({ id: 'app.setting.weakmode' })}
  185. </List.Item>
  186. </Body>
  187. <Divider />
  188. <CopyToClipboard
  189. text={JSON.stringify(setting)}
  190. onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
  191. >
  192. <Button block icon="copy">
  193. {formatMessage({ id: 'app.setting.copy' })}
  194. </Button>
  195. </CopyToClipboard>
  196. <Alert
  197. type="warning"
  198. className={styles.productionHint}
  199. message={formatMessage({ id: 'app.setting.production.hint' })}
  200. />
  201. </div>
  202. </Drawer>
  203. );
  204. }
  205. }
  206. export default SettingDrawer;