index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import React, { Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.less';
  4. import { Checkbox, Icon as AntDIcon } from 'antd';
  5. import Assets from '@src/components/Assets';
  6. import { formatSeconds, formatMinuteSecond, getMap } from '@src/services/Tools';
  7. import Icon from '../../../../components/Icon';
  8. import Button from '../../../../components/Button';
  9. import Navigation from '../../../../components/Navigation';
  10. import Answer from '../../../../components/Answer';
  11. import Calculator from '../../../../components/Calculator';
  12. import AnswerSelect from '../../../../components/AnswerSelect';
  13. import AnswerTable from '../../../../components/AnswerTable';
  14. import Editor from '../../../../components/Editor';
  15. import { QuestionType, ExaminationOrder } from '../../../../../Constant';
  16. const QuestionTypeMap = getMap(QuestionType, 'value');
  17. export default class extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. showTime: true,
  22. showNo: true,
  23. showCalculator: false,
  24. disorder: false,
  25. order: [],
  26. step: 0,
  27. answer: {},
  28. modal: null,
  29. };
  30. }
  31. onChangeQuestion(index, value) {
  32. const { question } = this.props;
  33. const { content } = question;
  34. const { answer = {} } = this.state;
  35. const type = content.type === 'double' ? 'double' : 'single';
  36. if (!answer.questions) {
  37. answer.questions = content.questions.map(() => {
  38. return {};
  39. });
  40. }
  41. answer.questions[index] = { [type]: value };
  42. console.log(answer);
  43. this.setState({ answer });
  44. }
  45. onChangeAwa(value) {
  46. const { answer = {} } = this.state;
  47. answer.awa = value;
  48. this.setState({ answer });
  49. }
  50. showConfirm(title, desc, cb) {
  51. this.showModal('confirm', title, desc, cb);
  52. }
  53. showToast(title, desc, cb) {
  54. this.showModal('toast', title, desc, cb);
  55. }
  56. showModal(type, title, desc, cb) {
  57. this.setState({ modal: { type, title, desc, cb } });
  58. }
  59. checkAnswer() {
  60. const { question } = this.props;
  61. const { answer } = this.state;
  62. let result = null;
  63. if (question.questionType === 'awa' && !answer.awa) result = 'Please answer the question first.';
  64. if (result) return this.showToast(null, result);
  65. return true;
  66. }
  67. hideModal(b) {
  68. if (b) {
  69. const { modal = {} } = this.state;
  70. if (modal.cb) modal.cb();
  71. }
  72. this.setState({ modal: null });
  73. }
  74. formatStrem(text) {
  75. if (!text) return '';
  76. const { question = { content: {} } } = this.props;
  77. const { table = {}, questions = [] } = question.content;
  78. text = text.replace(/#select#/g, "<span class='#select#' />");
  79. text = text.replace(/#table#/g, "<span class='#table#' />");
  80. setTimeout(() => {
  81. const selectList = document.getElementsByClassName('#select#');
  82. const tableList = document.getElementsByClassName('#table#');
  83. for (let i = 0; i < selectList.length; i += 1) {
  84. if (!questions[i]) break;
  85. ReactDOM.render(
  86. <AnswerSelect list={questions[i].select} type={'single'} onChange={v => this.onChangeQuestion(i, v)} />,
  87. selectList[i],
  88. );
  89. }
  90. if (table.row && table.col && table.header) {
  91. const columns = table.header.map((title, index) => {
  92. return { title, key: index };
  93. });
  94. for (let i = 0; i < tableList.length; i += 1) {
  95. ReactDOM.render(<AnswerTable list={columns} columns={columns} data={table.data} />, tableList[i]);
  96. }
  97. }
  98. }, 1);
  99. return text;
  100. }
  101. next() {
  102. const { flow } = this.props;
  103. const { answer } = this.state;
  104. if (this.checkAnswer()) {
  105. flow.submit(answer).then(() => {
  106. flow.next();
  107. });
  108. }
  109. }
  110. render() {
  111. const { modal } = this.state;
  112. const { scene, paper } = this.props;
  113. let content = null;
  114. switch (scene) {
  115. case 'start':
  116. content = paper.paperModule === 'examination' ? this.renderExaminationStart() : this.renderExerciseStart();
  117. break;
  118. case 'relax':
  119. content = this.renderRelax();
  120. break;
  121. default:
  122. content = this.renderDetail();
  123. break;
  124. }
  125. return (
  126. <div id="paper-process-base">
  127. {content}
  128. {modal ? this.renderModal() : ''}
  129. </div>
  130. );
  131. }
  132. renderContent() {
  133. const { question = { content: {} } } = this.props;
  134. const { step } = this.state;
  135. const { steps = [] } = question.content;
  136. return (
  137. <div className="block block-content">
  138. {steps.length > 0 && (
  139. <Navigation
  140. theme="process"
  141. list={question.content.steps}
  142. active={step}
  143. onChange={v => this.setState({ step: v })}
  144. />
  145. )}
  146. <div
  147. className="text"
  148. dangerouslySetInnerHTML={{ __html: this.formatStrem(steps.length > 0 ? steps[step].stem : question.stem) }}
  149. />
  150. </div>
  151. );
  152. }
  153. renderAnswer() {
  154. const { question = { content: {} } } = this.props;
  155. const { questions = [], type } = question.content;
  156. if (type === 'inline') return '';
  157. return (
  158. <div className="block block-answer">
  159. {question.questionType === 'awa' && <Editor onChange={v => this.onChangeAwa(v)} />}
  160. {questions.map((item, index) => {
  161. return (
  162. <div>
  163. <div className="text m-b-2" dangerouslySetInnerHTML={{ __html: item.description }} />
  164. <Answer
  165. list={item.select}
  166. type={type}
  167. first={item.first}
  168. second={item.second}
  169. direction={item.direction}
  170. onChange={v => this.onChangeQuestion(index, v)}
  171. />
  172. </div>
  173. );
  174. })}
  175. </div>
  176. );
  177. }
  178. renderDetail() {
  179. const { paper, userQuestion, question = { content: {} }, singleTime, stageTime, flow } = this.props;
  180. if (!userQuestion.id) return null;
  181. const { showCalculator, showTime, showNo } = this.state;
  182. const { typeset = 'one' } = question.content;
  183. return (
  184. <div className="layout">
  185. <div className="fixed">
  186. {QuestionTypeMap[question.questionType].long}
  187. {question.questionType === 'ir' && (
  188. <Assets
  189. className="calculator-icon"
  190. name="calculator_icon"
  191. onClick={() => this.setState({ showCalculator: !showCalculator })}
  192. />
  193. )}
  194. {/* <Assets className="collect-icon" name="collect_icon" onClick={() => {
  195. flow.toggleCollect();
  196. }} /> */}
  197. <div className="collect-icon">
  198. <Icon name="star" active={userQuestion.collect} onClick={() => flow.toggleCollect()} />
  199. </div>
  200. </div>
  201. <Calculator show={showCalculator} />
  202. <div className="layout-header">
  203. <div className="title">{paper.title}</div>
  204. <div className="right">
  205. <div
  206. className="block"
  207. onClick={() => {
  208. this.setState({ showTime: !showTime });
  209. }}
  210. >
  211. <Assets name="timeleft_icon" />
  212. {showTime && stageTime && `Time left ${formatMinuteSecond(stageTime)}`}
  213. {showTime && !stageTime && singleTime && `Time cost ${formatMinuteSecond(singleTime)}`}
  214. </div>
  215. <div
  216. className="block"
  217. onClick={() => {
  218. this.setState({ showNo: !showNo });
  219. }}
  220. >
  221. <Assets name="subjectnumber_icon" />
  222. {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
  223. </div>
  224. </div>
  225. </div>
  226. <div className={'layout-body'}>
  227. <div className={typeset}>
  228. {this.renderContent()}
  229. {this.renderAnswer()}
  230. </div>
  231. </div>
  232. <div className="layout-footer">
  233. <div className="help">
  234. <Assets name="help_icon" />
  235. Help
  236. </div>
  237. <div className="full">
  238. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  239. </div>
  240. <div className="next" onClick={() => this.next()}>
  241. Next
  242. <Assets name="next_icon" />
  243. </div>
  244. </div>
  245. </div>
  246. );
  247. }
  248. renderExaminationStart() {
  249. // const { paper, userQuestion, singleTime, stageTime, flow } = this.props;
  250. const { showTime } = this.state;
  251. const { paper, flow, startTime } = this.props;
  252. return (
  253. <div className="layout">
  254. <div className="fixed" />
  255. <div className="layout-header">
  256. <div className="title">{paper.title}</div>
  257. <div className="right">
  258. <div
  259. className="block"
  260. onClick={() => {
  261. this.setState({ showTime: !showTime });
  262. }}
  263. >
  264. <Assets name="timeleft_icon" />
  265. {showTime && startTime && `Time left ${formatMinuteSecond(startTime)}`}
  266. </div>
  267. {/* <div
  268. className="block"
  269. onClick={() => {
  270. this.setState({ showNo: !showNo });
  271. }}
  272. >
  273. <Assets name="subjectnumber_icon" />
  274. {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
  275. </div> */}
  276. </div>
  277. </div>
  278. <div className={'layout-body'}>{paper.isAdapt > 1 ? this.renderExaminationStartCAT() : this.renderExaminationStartDefault()}</div>
  279. <div className="layout-footer">
  280. <div className="help">
  281. <Assets name="help_icon" />
  282. Help
  283. </div>
  284. <div className="full">
  285. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  286. </div>
  287. <div className="next" onClick={() => this.next()}>
  288. Next
  289. <Assets name="next_icon" />
  290. </div>
  291. </div>
  292. </div>
  293. );
  294. }
  295. renderExerciseStart() {
  296. const { paper, flow, setting } = this.props;
  297. const { disorder } = setting;
  298. return (
  299. <div className="start">
  300. <div className="bg" />
  301. <div className="fixed-content">
  302. <div className="title">{paper.title}</div>
  303. <div className="desc">
  304. <div className="block">
  305. <div className="desc-title">
  306. <Assets name="subject_icon" />
  307. 题目总数
  308. </div>
  309. <div className="desc-info">{paper.questionNumber}</div>
  310. </div>
  311. <div className="block">
  312. <div className="desc-title">
  313. <Assets name="time_icon" />
  314. 建议用时
  315. </div>
  316. <div className="desc-info">{formatSeconds(paper.time)}</div>
  317. </div>
  318. </div>
  319. {paper.finishTimes > 0 && <div className="tip">
  320. <Checkbox className="m-r-1" checked={!disorder} onChange={() => flow.setSetting({ disorder: !!disorder })} />
  321. 题目选项乱序显示
  322. </div>}
  323. <div className="submit">
  324. <Button size="lager" radius onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? !disorder : false })}>
  325. 开始练习
  326. </Button>
  327. </div>
  328. </div>
  329. </div>
  330. );
  331. }
  332. renderExaminationStartCAT() {
  333. const { paper, flow, setting } = this.props;
  334. const { disorder, order, orderIndex } = setting;
  335. return (
  336. <div className="exercise-start default">
  337. <div className="title">Section Ordering</div>
  338. <div className="desc">Select the order in which the exam sections are to be administered.</div>
  339. <div className="desc tip">
  340. NOTE: You have 1 minutes to make your selection. If you do not make your selection within 1 minutes, the first
  341. option listed will be selected and you will view the exam in the following order: Analytical Writing
  342. Assessment, Integrated Reasoning, Quantitative, Verbal.
  343. </div>
  344. <div className="desc">
  345. Once you select your section order, you must view ALL questions in each section, in the order you have
  346. selected, before moving on to the next section. You will NOT be able to return to this screen.
  347. </div>
  348. <div className="block-list">
  349. {ExaminationOrder.map((row, index) => {
  350. return <div className="block-item">
  351. <div className="block-title" onClick={() => {
  352. flow.setSetting({ order: row.value, orderIndex: index });
  353. }}>
  354. <div className="block-title-border">
  355. {orderIndex === index && <AntDIcon type="check" />}
  356. <span>{row.label}</span>
  357. </div>
  358. </div>
  359. {row.list.map((r, i) => {
  360. return <div className="block-text">{i + 1}.{r.label} </div>;
  361. })}
  362. </div>;
  363. })}
  364. </div>
  365. <div className="bottom">
  366. {paper.finishTimes > 0 && <div className="text">
  367. <Checkbox checked={!disorder} onChange={() => flow.setSetting({ disorder: !!disorder })} /> 题目选项乱序显示
  368. </div>}
  369. <div className="text">
  370. Click{' '}
  371. <div className="next" onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? !disorder : false, order })}>
  372. Next
  373. <Assets name="next_icon" />
  374. </div>{' '}
  375. button to start the exam. You will begin the GMAT exam on the next screen.{' '}
  376. </div>
  377. </div>
  378. </div>
  379. );
  380. }
  381. renderExaminationStartDefault() {
  382. const { paper, flow, setting } = this.props;
  383. const { disorder, order, orderIndex } = setting;
  384. return (
  385. <div className="exercise-start cat">
  386. <div className="title">Section Ordering</div>
  387. <div className="block-list">
  388. {ExaminationOrder.map((row, index) => {
  389. return <div className="block-item" onClick={() => {
  390. this.setState({ order: row.value, orderIndex: index });
  391. }}>
  392. <div className={orderIndex === index ? 'block-item-body active' : 'block-item-body'}>
  393. {orderIndex === index && <AntDIcon type="check" style={{ color: '#fff' }} />}
  394. {row.list.map((r, i) => {
  395. return <div className="block-text" onClick={() => {
  396. if (order.indexOf(r.value) > 0) {
  397. // 取消
  398. order[i] = '';
  399. } else {
  400. // 选中
  401. order[i] = r.value;
  402. }
  403. flow.setSetting({ order });
  404. }}>
  405. <Checkbox checked={orderIndex === index ? order.indexOf(r.value) >= 0 : false} /> {r.label}{' '}
  406. </div>;
  407. })}
  408. </div>
  409. </div>;
  410. })}
  411. </div>
  412. <div className="bottom">
  413. {paper.finishTimes > 0 && <div className="text">
  414. <Checkbox checked={!disorder} onChange={() => flow.setSetting({ disorder: !!disorder })} /> 题目选项乱序显示
  415. </div>}
  416. <div className="text">
  417. Click{' '}
  418. <div className="next" onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? !disorder : false, order: order.filter(row => row) })}>
  419. Next
  420. <Assets name="next_icon" />
  421. </div>{' '}
  422. button to start the exam. You will begin the GMAT exam on the next screen.{' '}
  423. </div>
  424. <div className="text tip">*实战可选择考试顺序但无法选择考试内容。</div>
  425. </div>
  426. </div>
  427. );
  428. }
  429. renderRelax() {
  430. const { paper, stageTime, flow } = this.props;
  431. const { showTime } = this.state;
  432. return (
  433. <div className="layout">
  434. <div className="layout-header">
  435. <div className="title">{paper.title}</div>
  436. <div className="right">
  437. <div
  438. className="block"
  439. onClick={() => {
  440. this.setState({ showTime: !showTime });
  441. }}
  442. >
  443. <Assets name="timeleft_icon" />
  444. {showTime && stageTime && `Time left ${formatMinuteSecond(stageTime)}`}
  445. {/* {showTime && singleTime && `Time cost ${formatMinuteSecond(singleTime)}`} */}
  446. </div>
  447. {/* <div
  448. className="block"
  449. onClick={() => {
  450. this.setState({ showNo: !showNo });
  451. }}
  452. >
  453. <Assets name="subjectnumber_icon" />
  454. {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
  455. </div> */}
  456. </div>
  457. </div>
  458. <div className={'layout-body'}>
  459. <div className="relax">
  460. <div className="title">
  461. Optional Break <Icon name="question" />
  462. </div>
  463. <div className="time" dangerouslySetInnerHTML={{ __html: formatMinuteSecond(stageTime).split(':').map(row => row.replace(/([0-9])/g, '<div class="block">$1</div>')).join('<div class="div">:</div>') }} />
  464. </div>
  465. </div>
  466. <div className="layout-footer">
  467. <div className="help">
  468. <Assets name="help_icon" />
  469. Help
  470. </div>
  471. <div className="full">
  472. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  473. </div>
  474. <div className="next" onClick={() => this.next()}>
  475. Next
  476. <Assets name="next_icon" />
  477. </div>
  478. </div>
  479. </div>
  480. );
  481. }
  482. renderModal() {
  483. const { modal } = this.state;
  484. return (
  485. <div className="modal">
  486. <div className="mask" />
  487. <div className="body">
  488. <div className="title">{modal.title}</div>
  489. <div className="desc">{modal.desc}</div>
  490. {modal.type === 'confirm' ? (
  491. <div className="btn-list">
  492. <div className="btn" onClick={() => this.hideModal(true)}>
  493. <span className="t-d-l">Y</span>es
  494. </div>
  495. <div className="btn" onClick={() => this.hideModal(false)}>
  496. <span className="t-d-l">N</span>o
  497. </div>
  498. </div>
  499. ) : (<div className="btn-list">
  500. <div className="btn" onClick={() => this.hideModal(true)}>
  501. <span className="t-d-l">O</span>k
  502. </div>
  503. </div>)}
  504. </div>
  505. </div>
  506. );
  507. }
  508. }