index.js 20 KB

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