page.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import React from 'react';
  2. import { Tabs, Button, Icon } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import TreeLayout from '@src/layouts/TreeLayout';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import { formatTreeData } from '@src/services/Tools';
  9. import { asyncDelConfirm, asyncForm, asyncSMessage } from '@src/services/AsyncTools';
  10. // import { System } from '../../../stores/system';
  11. import { Examination } from '../../../stores/examination';
  12. import { Exercise } from '../../../stores/exercise';
  13. export default class extends Page {
  14. constructor(props) {
  15. super(props);
  16. this.exerciseActionList = [{
  17. key: 'addChild',
  18. name: '增加子节点',
  19. }, {
  20. key: 'addBrother',
  21. name: '增加兄弟节点',
  22. selectNum: 1,
  23. }, {
  24. key: 'edit',
  25. name: '编辑',
  26. selectNum: 1,
  27. }, {
  28. key: 'delete',
  29. name: '批量删除',
  30. type: 'danger',
  31. needCheck: 1,
  32. }];
  33. this.exerciseItemList = [{
  34. key: 'id',
  35. type: 'hidden',
  36. }, {
  37. key: 'parentId',
  38. type: 'tree',
  39. name: '父节点',
  40. tree: [],
  41. fieldNames: { label: 'title', value: 'value', children: 'children' },
  42. notFoundContent: null,
  43. onChange: (value) => {
  44. console.log(value);
  45. },
  46. }, {
  47. key: 'titleZh',
  48. name: '中文名称',
  49. type: 'input',
  50. placeholder: '请输入中文名称',
  51. required: true,
  52. }, {
  53. key: 'titleEn',
  54. name: '英文名称',
  55. type: 'input',
  56. placeholder: '请输入英文名称',
  57. required: true,
  58. }, {
  59. key: 'description',
  60. name: '描述',
  61. type: 'textarea',
  62. placeholder: '请输入描述',
  63. required: true,
  64. }];
  65. this.examinationActionList = [{
  66. key: 'addChild',
  67. name: '增加子节点',
  68. }, {
  69. key: 'addBrother',
  70. name: '增加兄弟节点',
  71. selectNum: 1,
  72. }, {
  73. key: 'edit',
  74. name: '编辑',
  75. selectNum: 1,
  76. }, {
  77. key: 'delete',
  78. name: '批量删除',
  79. type: 'danger',
  80. needCheck: 1,
  81. }];
  82. this.examinationItemList = [{
  83. key: 'id',
  84. type: 'hidden',
  85. }, {
  86. key: 'parentId',
  87. type: 'tree',
  88. name: '父节点',
  89. tree: [],
  90. fieldNames: { label: 'title', value: 'value', children: 'children' },
  91. notFoundContent: null,
  92. onChange: (value) => {
  93. console.log(value);
  94. },
  95. }, {
  96. key: 'titleZh',
  97. name: '中文名称',
  98. type: 'input',
  99. placeholder: '请输入中文名称',
  100. required: true,
  101. }, {
  102. key: 'titleEn',
  103. name: '英文名称',
  104. type: 'input',
  105. placeholder: '请输入英文名称',
  106. required: true,
  107. }, {
  108. key: 'description',
  109. name: '描述',
  110. type: 'textarea',
  111. placeholder: '请输入描述',
  112. required: true,
  113. }];
  114. this.state.tab = 'exercise';
  115. }
  116. initData() {
  117. this.refresh();
  118. }
  119. refresh() {
  120. const { tab } = this.state;
  121. if (tab === 'exercise') {
  122. return this.refreshExercise();
  123. }
  124. return this.refreshExamination();
  125. }
  126. refreshExercise() {
  127. Exercise.allStruct().then(result => {
  128. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  129. this.exerciseItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list));
  130. this.setState({
  131. exerciseList: list,
  132. exerciseStruct: formatTreeData(list.map(row => {
  133. if (row.level < 4) return row;
  134. row = Object.assign({}, row);
  135. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  136. e.preventDefault();
  137. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  138. Exercise.editStruct(row).then(() => {
  139. this.refresh();
  140. });
  141. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  142. return row;
  143. }), 'id', 'title'),
  144. });
  145. });
  146. }
  147. refreshExamination() {
  148. Examination.allStruct().then(result => {
  149. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  150. this.examinationItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list));
  151. this.setState({ examinationList: list, examinationStruct: formatTreeData(list, 'id', 'title') });
  152. });
  153. }
  154. addChildAction() {
  155. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  156. let itemList;
  157. let node = 0;
  158. if (tab === 'exercise') {
  159. itemList = this.exerciseItemList;
  160. if (selectedKeys.length > 0) {
  161. node = exerciseList.filter(row => row.id === Number(selectedKeys[0]))[0].id;
  162. }
  163. } else {
  164. itemList = this.examinationItemList;
  165. if (selectedKeys.length > 0) {
  166. node = examinationList.filter(row => row.id === Number(selectedKeys[0]))[0].id;
  167. }
  168. }
  169. asyncForm('新增', itemList, { parentId: `${node}` }, data => {
  170. console.log(data);
  171. let handler;
  172. if (tab === 'exercise') {
  173. handler = Exercise.addStruct(data);
  174. } else {
  175. handler = Examination.addStruct(data);
  176. }
  177. return handler.then(() => {
  178. asyncSMessage('新增成功!');
  179. this.refresh();
  180. });
  181. });
  182. }
  183. addBrotherAction() {
  184. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  185. let itemList;
  186. let node;
  187. if (tab === 'exercise') {
  188. itemList = this.exerciseItemList;
  189. if (selectedKeys.length > 0) {
  190. node = exerciseList.filter(row => row.id === Number(selectedKeys[0]))[0].parentId;
  191. }
  192. } else {
  193. itemList = this.examinationItemList;
  194. if (selectedKeys.length > 0) {
  195. node = examinationList.filter(row => row.id === Number(selectedKeys[0]))[0].parentId;
  196. }
  197. }
  198. asyncForm('新增', itemList, { parentId: `${node}` }, data => {
  199. let handler;
  200. if (tab === 'exercise') {
  201. handler = Exercise.addStruct(data);
  202. } else {
  203. handler = Examination.addStruct(data);
  204. }
  205. return handler.then(() => {
  206. asyncSMessage('新增成功!');
  207. this.refresh();
  208. });
  209. });
  210. }
  211. editAction() {
  212. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  213. let itemList;
  214. let list;
  215. if (tab === 'exercise') {
  216. itemList = this.exerciseItemList;
  217. list = exerciseList;
  218. } else {
  219. itemList = this.examinationItemList;
  220. list = examinationList;
  221. }
  222. asyncForm('编辑', itemList, list.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  223. row.parentId = `${row.parentId}`;
  224. return row;
  225. })[0], data => {
  226. let handler;
  227. if (tab === 'exercise') {
  228. handler = Exercise.editStruct(data);
  229. } else {
  230. handler = Examination.editStruct(data);
  231. }
  232. return handler.then(() => {
  233. asyncSMessage('编辑成功!');
  234. this.refresh();
  235. });
  236. });
  237. }
  238. deleteAction() {
  239. const { tab, checkedKeys } = this.state;
  240. asyncDelConfirm('删除确认', '是否删除选中节点?', () => {
  241. if (tab === 'exercise') {
  242. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  243. }
  244. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  245. }).then(() => {
  246. asyncSMessage('删除成功!');
  247. this.refresh();
  248. });
  249. }
  250. renderExamination() {
  251. const { loading } = this.state;
  252. return <Block loading={loading}>
  253. <ActionLayout
  254. itemList={this.exerciseActionList}
  255. selectedKeys={this.state.selectedKeys}
  256. checkedKeys={this.state.checkedKeys}
  257. onAction={key => this.onAction(key)} />
  258. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.examinationStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  259. this.setState({ selectedKeys });
  260. }} checkedKeys={this.state.checkedKeys} onCheck={(checkedKeys) => {
  261. this.setState({ checkedKeys });
  262. }} />
  263. </Block>;
  264. }
  265. renderExercise() {
  266. const { loading } = this.state;
  267. return <Block loading={loading}>
  268. <ActionLayout
  269. itemList={this.exerciseActionList}
  270. selectedKeys={this.state.selectedKeys}
  271. checkedKeys={this.state.checkedKeys}
  272. onAction={key => this.onAction(key)} />
  273. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.exerciseStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  274. this.setState({ selectedKeys });
  275. }} checkedKeys={this.state.checkedKeys} onCheck={(checkedKeys) => {
  276. this.setState({ checkedKeys });
  277. }} />
  278. </Block>;
  279. }
  280. renderView() {
  281. const { tab } = this.state;
  282. return <Block><Tabs activeKey={tab} onChange={(value) => {
  283. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  284. this.refresh();
  285. }}>
  286. <Tabs.TabPane tab="练习结构" key="exercise">
  287. {this.renderExercise()}
  288. </Tabs.TabPane>
  289. <Tabs.TabPane tab="模考结构" key="examination">
  290. {this.renderExamination()}
  291. </Tabs.TabPane>
  292. </Tabs></Block>;
  293. }
  294. }