page.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.less';
  4. import { Checkbox } from 'antd';
  5. import Assets from '@src/components/Assets';
  6. import Page from '@src/containers/Page';
  7. import Button from '../../../components/Button';
  8. import Navigation from '../../../components/Navigation';
  9. import Answer from '../../../components/Answer';
  10. import Calculator from '../../../components/Calculator';
  11. import AnswerSelect from '../../../components/AnswerSelect';
  12. import AnswerTable from '../../../components/AnswerTable';
  13. import { Question } from '../../../stores/question';
  14. import Editor from '../../../components/Editor';
  15. export default class extends Page {
  16. initState() {
  17. return {
  18. showCalculator: false,
  19. start: !this.props.core.query.r,
  20. reportId: this.props.core.query.r,
  21. type: this.props.core.query.type,
  22. disorder: false,
  23. step: 0,
  24. info: {},
  25. reportInfo: {},
  26. questionInfo: {},
  27. answer: {},
  28. };
  29. }
  30. initData() {
  31. const { start } = this.state;
  32. Question.getPaper(this.params.id).then(result => {
  33. this.setState({ info: result });
  34. });
  35. if (!start) {
  36. this.continue();
  37. }
  38. }
  39. onChangeQuestion(index, value) {
  40. const { question = {}, answer = {} } = this.state;
  41. answer.questions[index] = { [question.type]: value };
  42. this.setState({ answer });
  43. }
  44. onChangeAwa(value) {
  45. const { answer = {} } = this.state;
  46. answer.awa = value;
  47. this.setState({ answer });
  48. }
  49. start() {
  50. const { type, info, disorder } = this.state;
  51. Question.start(type, info.id, disorder).then(result => {
  52. this.setState({ reportInfo: result });
  53. this.next();
  54. });
  55. }
  56. continue() {
  57. const { reportId } = this.state;
  58. Question.continue(reportId).then(result => {
  59. this.setState({ reportInfo: result });
  60. this.next();
  61. });
  62. }
  63. next() {
  64. const { reportInfo } = this.state;
  65. Question.next(reportInfo.id).then(result => {
  66. this.setState({
  67. questionInfo: result,
  68. question: result.question,
  69. answer: { questions: [], subject: [], predicate: [], object: [], options: [], awa: '' },
  70. });
  71. });
  72. }
  73. submit() {
  74. const { question, answer } = this.state;
  75. Question.submit(question.questionNoId, answer).then(() => {
  76. this.next();
  77. });
  78. }
  79. finish() {
  80. const { reportInfo } = this.state;
  81. Question.finish(reportInfo.id).then(() => {});
  82. }
  83. formatStrem(text) {
  84. if (!text) return '';
  85. const { question = { content: {} } } = this.state;
  86. const { table = {}, questions = [] } = question.content;
  87. text = text.replace(/#select#/g, "<span class='#select#' />");
  88. text = text.replace(/#table#/g, "<span class='#table#' />");
  89. setTimeout(() => {
  90. const selectList = document.getElementsByClassName('#select#');
  91. const tableList = document.getElementsByClassName('#table#');
  92. for (let i = 0; i < selectList.length; i += 1) {
  93. ReactDOM.render(
  94. <AnswerSelect list={questions[i].select} onChange={v => this.onChangeQuestion(i, v)} />,
  95. selectList[i],
  96. );
  97. }
  98. for (let i = 0; i < tableList.length; i += 1) {
  99. ReactDOM.render(
  100. <AnswerTable list={table.header || []} columns={table.header} data={table.data} />,
  101. tableList[i],
  102. );
  103. }
  104. }, 1);
  105. return text;
  106. }
  107. renderView() {
  108. const { start } = this.state;
  109. if (!start) return this.renderStart();
  110. return this.renderDetail();
  111. }
  112. renderCotent() {
  113. const { question = { content: {} }, step } = this.state;
  114. const { steps = [] } = question.content;
  115. return (
  116. <div className="block block-content">
  117. {steps.length > 0 && <Navigation list={question.content.steps} active={step} onChange={() => {}} />}
  118. <div className="text">{this.formatStrem(steps.length > 0 ? steps[step].stem : question.stem)}</div>
  119. </div>
  120. );
  121. }
  122. renderAnswer() {
  123. const { question = { content: {} } } = this.state;
  124. const { questions = [] } = question.content;
  125. if (question.type === 'inline') return '';
  126. return (
  127. <div className="block block-answer">
  128. <Editor onChange={v => this.onChangeAwa(v)} />
  129. {question.type === 'awa' && <Editor onChange={v => this.onChangeAwa(v)} />}
  130. {questions.map((item, index) => {
  131. return (
  132. <div>
  133. <div className="text m-b-2">{item.description}</div>
  134. <Answer
  135. list={item.select}
  136. type={question.type}
  137. direction={question.direction}
  138. onChange={v => this.onChangeQuestion(index, v)}
  139. />
  140. </div>
  141. );
  142. })}
  143. </div>
  144. );
  145. }
  146. renderDetail() {
  147. const { showCalculator, info, question = { content: {} } } = this.state;
  148. const { typeset = 'one' } = question.content;
  149. return (
  150. <div className="layout">
  151. <div className="fixed">
  152. Analytical Writing Assessment
  153. <Assets
  154. className="calculator-icon"
  155. name="calculator_icon"
  156. onClick={() => this.setState({ showCalculator: !showCalculator })}
  157. />
  158. <Assets className="collect-icon" name="collect_icon" />
  159. </div>
  160. <Calculator show={showCalculator} />
  161. <div className="layout-header">
  162. <div className="title">{info.title}</div>
  163. <div className="right">
  164. <div className="block">
  165. <Assets name="timeleft_icon" />
  166. Time left 00:02
  167. </div>
  168. <div className="block">
  169. <Assets name="subjectnumber_icon" />
  170. {question.no} of {info.questionNumer}
  171. </div>
  172. </div>
  173. </div>
  174. <div className={`layout-body ${typeset}`}>
  175. {this.renderCotent()}
  176. {this.renderAnswer()}
  177. </div>
  178. <div className="layout-footer">
  179. <div className="help">
  180. <Assets name="help_icon" />
  181. Help
  182. </div>
  183. <div className="full">
  184. <Assets name="fullscreen_icon" />
  185. </div>
  186. <div className="next" onClick={() => this.next()}>
  187. Next
  188. <Assets name="next_icon" />
  189. </div>
  190. </div>
  191. </div>
  192. );
  193. }
  194. renderStart() {
  195. const { info, disorder } = this.state;
  196. return (
  197. <div className="start">
  198. <div className="bg" />
  199. <div className="fixed-content">
  200. <div className="title">{info.title}</div>
  201. <div className="desc">
  202. <div className="block">
  203. <div className="desc-title">
  204. <Assets name="subject_icon" />
  205. 题目总数
  206. </div>
  207. <div className="desc-info">{info.questionNumer}</div>
  208. </div>
  209. <div className="block">
  210. <div className="desc-title">
  211. <Assets name="time_icon" />
  212. 建议用时
  213. </div>
  214. <div className="desc-info">{info.time}</div>
  215. </div>
  216. </div>
  217. <div className="tip">
  218. <Checkbox className="m-r-1" checked={disorder} onChange={() => this.setState({ disorder: !disorder })} />
  219. 题目选项乱序显示
  220. </div>
  221. <div className="submit">
  222. <Button size="lager" radius onClick={() => this.start()}>
  223. 开始练习
  224. </Button>
  225. </div>
  226. </div>
  227. </div>
  228. );
  229. }
  230. }