index.js 19 KB

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