page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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, getMap } 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: 'hidden',
  39. }, {
  40. key: 'parentName',
  41. type: 'input',
  42. name: '父节点',
  43. disabled: true,
  44. }, {
  45. key: 'titleZh',
  46. name: '中文名称',
  47. type: 'input',
  48. placeholder: '请输入中文名称',
  49. required: true,
  50. }, {
  51. key: 'titleEn',
  52. name: '英文名称',
  53. type: 'input',
  54. placeholder: '请输入英文名称',
  55. }, {
  56. key: 'description',
  57. name: '描述',
  58. type: 'textarea',
  59. placeholder: '请输入描述',
  60. }, {
  61. key: 'isSentence',
  62. type: 'hidden',
  63. }, {
  64. key: 'isExamination',
  65. type: 'hidden',
  66. }];
  67. this.exerciseItemListLimit = [{
  68. key: 'id',
  69. type: 'hidden',
  70. }, {
  71. key: 'titleZh',
  72. name: '中文名称',
  73. type: 'input',
  74. placeholder: '请输入中文名称',
  75. required: true,
  76. }, {
  77. key: 'titleEn',
  78. name: '英文名称',
  79. type: 'input',
  80. placeholder: '请输入英文名称',
  81. }, {
  82. key: 'description',
  83. name: '描述',
  84. type: 'textarea',
  85. placeholder: '请输入描述',
  86. }, {
  87. key: 'isSentence',
  88. type: 'hidden',
  89. }, {
  90. key: 'isExamination',
  91. type: 'hidden',
  92. }];
  93. this.examinationActionList = [{
  94. key: 'addChild',
  95. name: '增加子节点',
  96. }, {
  97. key: 'addBrother',
  98. name: '增加兄弟节点',
  99. selectNum: 1,
  100. }, {
  101. key: 'edit',
  102. name: '编辑',
  103. selectNum: 1,
  104. }, {
  105. key: 'delete',
  106. name: '批量删除',
  107. type: 'danger',
  108. needCheck: 1,
  109. }];
  110. this.examinationItemList = [{
  111. key: 'id',
  112. type: 'hidden',
  113. }, {
  114. key: 'parentId',
  115. type: 'hidden',
  116. }, {
  117. key: 'parentName',
  118. type: 'input',
  119. name: '父节点',
  120. disabled: true,
  121. }, {
  122. key: 'titleZh',
  123. name: '中文名称',
  124. type: 'input',
  125. placeholder: '请输入中文名称',
  126. required: true,
  127. }, {
  128. key: 'titleEn',
  129. name: '英文名称',
  130. type: 'input',
  131. placeholder: '请输入英文名称',
  132. }, {
  133. key: 'description',
  134. name: '描述',
  135. type: 'textarea',
  136. placeholder: '请输入描述',
  137. }, {
  138. key: 'isAdapt',
  139. type: 'hidden',
  140. }];
  141. this.examinationItemListLimit = [{
  142. key: 'id',
  143. type: 'hidden',
  144. }, {
  145. key: 'titleZh',
  146. name: '中文名称',
  147. type: 'input',
  148. placeholder: '请输入中文名称',
  149. required: true,
  150. }, {
  151. key: 'titleEn',
  152. name: '英文名称',
  153. type: 'input',
  154. placeholder: '请输入英文名称',
  155. }, {
  156. key: 'description',
  157. name: '描述',
  158. type: 'textarea',
  159. placeholder: '请输入描述',
  160. }, {
  161. key: 'isAdapt',
  162. type: 'hidden',
  163. }];
  164. this.state.tab = 'exercise';
  165. }
  166. initData() {
  167. this.refresh(this.state.tab);
  168. }
  169. refresh(tab) {
  170. if (!tab) {
  171. ({ tab } = this.state);
  172. }
  173. if (tab === 'exercise') {
  174. return this.refreshExercise();
  175. }
  176. return this.refreshExamination();
  177. }
  178. refreshExercise() {
  179. Exercise.allStruct().then(result => {
  180. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  181. this.exerciseItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  182. this.setState({
  183. exerciseList: list,
  184. exerciseMap: getMap(list, 'id'),
  185. exerciseStruct: formatTreeData(list.map(row => {
  186. if (row.level < 4) return row;
  187. row = Object.assign({}, row);
  188. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  189. e.preventDefault();
  190. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  191. Exercise.editStruct(row).then(() => {
  192. this.refresh();
  193. });
  194. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  195. return row;
  196. }), 'id', 'title', 'parentId'),
  197. });
  198. });
  199. }
  200. refreshExamination() {
  201. Examination.allStruct().then(result => {
  202. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  203. this.examinationItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  204. this.setState({
  205. examinationList: list,
  206. examinationMap: getMap(list, 'id'),
  207. examinationStruct: formatTreeData(list.map(row => {
  208. if (row.level !== 2) return row;
  209. row = Object.assign({}, row);
  210. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  211. e.preventDefault();
  212. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  213. Examination.editStruct(row).then(() => {
  214. this.refresh();
  215. });
  216. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  217. return row;
  218. }), 'id', 'title', 'parentId'),
  219. });
  220. });
  221. }
  222. addChildAction() {
  223. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  224. let itemList;
  225. let node;
  226. let dep;
  227. if (tab === 'exercise') {
  228. itemList = this.exerciseItemList;
  229. if (selectedKeys.length > 0) {
  230. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  231. }
  232. // 长难句还在该逻辑内,不允许添加下属节点
  233. if (node.level <= 1 || node.level >= 4) {
  234. asyncSMessage('不允许添加该节点的子节点', 'warn');
  235. return;
  236. }
  237. dep = {
  238. isSentence: node.isSentence,
  239. isCourse: node.isCourse,
  240. isData: node.isData,
  241. };
  242. } else {
  243. itemList = this.examinationItemList;
  244. if (selectedKeys.length > 0) {
  245. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])));
  246. }
  247. if (node.level <= 0 || node.level >= 3) {
  248. asyncSMessage('不允许添加该节点的子节点', 'warn');
  249. return;
  250. }
  251. dep = {
  252. isAdapt: node.isAdapt,
  253. };
  254. }
  255. asyncForm('新增', itemList, Object.assign({ parentId: node.id, parentName: node.title }, dep), data => {
  256. let handler;
  257. data.parentId = Number(data.parentId);
  258. if (tab === 'exercise') {
  259. handler = Exercise.addStruct(data);
  260. } else {
  261. handler = Examination.addStruct(data);
  262. }
  263. return handler.then(() => {
  264. asyncSMessage('新增成功!');
  265. this.refresh();
  266. });
  267. });
  268. }
  269. addBrotherAction() {
  270. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  271. let itemList;
  272. let node;
  273. let dep;
  274. if (tab === 'exercise') {
  275. itemList = this.exerciseItemList;
  276. if (selectedKeys.length > 0) {
  277. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  278. }
  279. if (node.level <= 2) {
  280. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  281. return;
  282. }
  283. dep = {
  284. isSentence: node.isSentence,
  285. isCourse: node.isCourse,
  286. isData: node.isData,
  287. parentName: this.state.exerciseMap[node.parentId].title,
  288. };
  289. } else {
  290. itemList = this.examinationItemList;
  291. if (selectedKeys.length > 0) {
  292. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])));
  293. }
  294. if (node.level <= 1) {
  295. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  296. return;
  297. }
  298. dep = {
  299. isAdapt: node.isAdapt,
  300. parentName: this.state.examinationMap[node.parentId].title,
  301. };
  302. }
  303. asyncForm('新增', itemList, Object.assign({ parentId: node.parentId, parentName: node.title }, dep), data => {
  304. let handler;
  305. data.parentId = Number(data.parentId);
  306. if (tab === 'exercise') {
  307. handler = Exercise.addStruct(data);
  308. } else {
  309. handler = Examination.addStruct(data);
  310. }
  311. return handler.then(() => {
  312. asyncSMessage('新增成功!');
  313. this.refresh();
  314. });
  315. });
  316. }
  317. editAction() {
  318. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  319. let itemList;
  320. let node;
  321. if (tab === 'exercise') {
  322. itemList = this.exerciseItemList;
  323. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  324. // row.parentId = `${row.parentId}`;
  325. return row;
  326. }));
  327. if (node.level <= 2) {
  328. itemList = this.exerciseItemListLimit;
  329. }
  330. if (node.level <= 2) {
  331. asyncSMessage('该节点不允许编辑', 'warn');
  332. return;
  333. }
  334. node.parentName = this.state.exerciseMap[node.parentId].title;
  335. } else {
  336. itemList = this.examinationItemList;
  337. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  338. // row.parentId = `${row.parentId}`;
  339. return row;
  340. }));
  341. if (node.level <= 1) {
  342. itemList = this.examinationItemListLimit;
  343. }
  344. // 千行可以被修改
  345. // if (node.extend) {
  346. // asyncSMessage('该节点不允许编辑', 'warn');
  347. // return;
  348. // }
  349. node.parentName = this.state.examinationMap[node.parentId].title;
  350. }
  351. asyncForm('编辑', itemList, node, data => {
  352. let handler;
  353. data.parentId = Number(data.parentId);
  354. if (tab === 'exercise') {
  355. handler = Exercise.editStruct(data);
  356. } else {
  357. handler = Examination.editStruct(data);
  358. }
  359. return handler.then(() => {
  360. asyncSMessage('编辑成功!');
  361. this.refresh();
  362. });
  363. });
  364. }
  365. deleteAction() {
  366. const { tab, checkedKeys } = this.state;
  367. asyncDelConfirm('删除确认', '是否删除选中节点?', () => {
  368. let handler;
  369. if (tab === 'exercise') {
  370. handler = Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  371. } else {
  372. handler = Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  373. }
  374. return handler.then(() => {
  375. asyncSMessage('删除成功!');
  376. this.refresh();
  377. });
  378. });
  379. }
  380. renderExamination() {
  381. const { loading } = this.state;
  382. return <Block loading={loading}>
  383. <ActionLayout
  384. itemList={this.exerciseActionList}
  385. selectedKeys={this.state.selectedKeys}
  386. checkedKeys={this.state.checkedKeys}
  387. onAction={key => this.onAction(key)} />
  388. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.examinationStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  389. this.setState({ selectedKeys });
  390. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  391. if (e.node.props.level > 2) {
  392. this.setState({ checkedKeys: { checkedKeys, halfCheckedKeys: e.halfCheckedKeys } });
  393. }
  394. }} />
  395. </Block>;
  396. }
  397. renderExercise() {
  398. const { loading } = this.state;
  399. return <Block loading={loading}>
  400. <ActionLayout
  401. itemList={this.exerciseActionList}
  402. selectedKeys={this.state.selectedKeys}
  403. checkedKeys={this.state.checkedKeys}
  404. onAction={key => this.onAction(key)} />
  405. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.exerciseStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  406. this.setState({ selectedKeys });
  407. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  408. if (e.node.props.level > 2) {
  409. this.setState({ checkedKeys: { checkedKeys, halfCheckedKeys: e.halfCheckedKeys } });
  410. }
  411. }} />
  412. </Block>;
  413. }
  414. renderView() {
  415. const { tab } = this.state;
  416. return <Block><Tabs activeKey={tab} onChange={(value) => {
  417. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  418. this.refresh(value);
  419. }}>
  420. <Tabs.TabPane tab="练习结构" key="exercise">
  421. {this.renderExercise()}
  422. </Tabs.TabPane>
  423. <Tabs.TabPane tab="模考结构" key="examination">
  424. {this.renderExamination()}
  425. </Tabs.TabPane>
  426. </Tabs></Block>;
  427. }
  428. }