index.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. showHelp() {
  147. this.setState({ showHelp: true });
  148. }
  149. next() {
  150. const { flow } = this.props;
  151. const { answer } = this.state;
  152. if (this.checkAnswer()) {
  153. this.showConfirm('Answer Confirmation', 'Click Yes to confirm your answer and continue to the next \n question. ', 560, () => {
  154. flow.submit(answer).then(() => {
  155. flow.next();
  156. });
  157. });
  158. }
  159. }
  160. stage() {
  161. const { flow } = this.props;
  162. flow.next();
  163. }
  164. render() {
  165. const { modal, showHelp } = this.state;
  166. const { scene, paper } = this.props;
  167. let content = null;
  168. switch (scene) {
  169. case 'start':
  170. content = paper.paperModule === 'examination' ? this.renderExaminationStart() : this.renderExerciseStart();
  171. break;
  172. case 'relax':
  173. content = this.renderRelax();
  174. break;
  175. default:
  176. content = this.renderDetail();
  177. break;
  178. }
  179. return (
  180. <div id="paper-process-base">
  181. {content}
  182. {modal ? this.renderModal() : ''}
  183. {showHelp ? this.renderHelp() : ''}
  184. </div>
  185. );
  186. }
  187. renderContent() {
  188. const { question = { content: {} } } = this.props;
  189. const { step } = this.state;
  190. const { steps = [] } = question.content;
  191. return (
  192. <div className="block block-content">
  193. {steps.length > 0 && (
  194. <Navigation
  195. theme="process"
  196. list={question.content.steps}
  197. active={step}
  198. onChange={v => this.setState({ step: v })}
  199. />
  200. )}
  201. <div
  202. className="text"
  203. dangerouslySetInnerHTML={{ __html: this.formatStrem(steps.length > 0 ? steps[step].stem : question.stem) }}
  204. />
  205. </div>
  206. );
  207. }
  208. renderAnswer() {
  209. const { question = { content: {} } } = this.props;
  210. const { questions = [], type } = question.content;
  211. if (type === 'inline') return '';
  212. return (
  213. <div className="block block-answer">
  214. {question.questionType === 'awa' && <Editor onChange={v => this.onChangeAwa(v)} />}
  215. {questions.map((item, index) => {
  216. return (
  217. <div>
  218. <div className="text m-b-2" dangerouslySetInnerHTML={{ __html: item.description }} />
  219. <Answer
  220. list={item.select}
  221. type={type}
  222. first={item.first}
  223. second={item.second}
  224. direction={item.direction}
  225. onChange={v => this.onChangeQuestion(index, v)}
  226. />
  227. </div>
  228. );
  229. })}
  230. </div>
  231. );
  232. }
  233. renderDetail() {
  234. const { paper, userQuestion, question = { content: {} }, totalTime, stageTime, totalNumber, flow } = this.props;
  235. if (!userQuestion.id) return null;
  236. const { showCalculator, showTime, showNo } = this.state;
  237. const { typeset = 'one' } = question.content;
  238. return (
  239. <div className="layout">
  240. <div className="fixed">
  241. {QuestionTypeMap[question.questionType].long}
  242. {question.questionType === 'ir' && (
  243. <Assets
  244. className="calculator-icon"
  245. name="calculator_icon"
  246. onClick={() => this.setState({ showCalculator: !showCalculator })}
  247. />
  248. )}
  249. {/* <Assets className="collect-icon" name="collect_icon" onClick={() => {
  250. flow.toggleCollect();
  251. }} /> */}
  252. <div className="collect-icon">
  253. <Icon name="star" active={userQuestion.collect} onClick={() => flow.toggleCollect()} />
  254. </div>
  255. </div>
  256. <Calculator show={showCalculator} />
  257. <div className="layout-header">
  258. <div className="title">{paper.title}</div>
  259. <div className="right">
  260. <div
  261. className="block"
  262. onClick={() => {
  263. this.setState({ showTime: !showTime });
  264. }}
  265. >
  266. <Assets name="timeleft_icon" />
  267. {showTime && stageTime && `Time left ${formatMinuteSecond(stageTime)}`}
  268. {showTime && !stageTime && totalTime && `Time cost ${formatMinuteSecond(totalTime)}`}
  269. </div>
  270. <div
  271. className="block"
  272. onClick={() => {
  273. this.setState({ showNo: !showNo });
  274. }}
  275. >
  276. <Assets name="subjectnumber_icon" />
  277. {showNo && `${userQuestion.stageNo} of ${totalNumber}`}
  278. </div>
  279. </div>
  280. </div>
  281. <div className={'layout-body'}>
  282. <div className={typeset}>
  283. {this.renderContent()}
  284. {this.renderAnswer()}
  285. </div>
  286. </div>
  287. <div className="layout-footer">
  288. <div className="help">
  289. <Assets name="help_icon" />
  290. Help
  291. </div>
  292. <div className="full">
  293. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  294. </div>
  295. <div className="next" onClick={() => this.next()}>
  296. Next
  297. <Assets name="next_icon" />
  298. </div>
  299. </div>
  300. </div>
  301. );
  302. }
  303. renderExaminationStart() {
  304. // const { paper, userQuestion, singleTime, stageTime, flow } = this.props;
  305. const { showTime } = this.state;
  306. const { paper, flow, startTime, setting } = this.props;
  307. const { disorder = true, order } = setting;
  308. return (
  309. <div className="layout">
  310. <div className="fixed" />
  311. <div className="layout-header">
  312. <div className="title">{paper.title}</div>
  313. <div className="right">
  314. <div
  315. className="block"
  316. onClick={() => {
  317. this.setState({ showTime: !showTime });
  318. }}
  319. >
  320. <Assets name="timeleft_icon" />
  321. {showTime && startTime && `Time left ${formatMinuteSecond(startTime)}`}
  322. </div>
  323. {/* <div
  324. className="block"
  325. onClick={() => {
  326. this.setState({ showNo: !showNo });
  327. }}
  328. >
  329. <Assets name="subjectnumber_icon" />
  330. {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
  331. </div> */}
  332. </div>
  333. </div>
  334. <div className={'layout-body'}>{paper.isAdapt > 1 ? this.renderExaminationStartCAT() : this.renderExaminationStartDefault()}</div>
  335. <div className="layout-footer">
  336. <div className="help">
  337. <Assets name="help_icon" />
  338. Help
  339. </div>
  340. <div className="full">
  341. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  342. </div>
  343. <div className="next" onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? disorder : false, order: order.filter(row => row) })}>
  344. Next
  345. <Assets name="next_icon" />
  346. </div>
  347. </div>
  348. </div>
  349. );
  350. }
  351. renderExerciseStart() {
  352. const { paper, flow, setting } = this.props;
  353. const { disorder = true } = setting;
  354. return (
  355. <div className="start">
  356. <div className="bg" />
  357. <div className="fixed-content">
  358. <div className="title">{paper.title}</div>
  359. <div className="desc">
  360. <div className="block">
  361. <div className="desc-title">
  362. <Assets name="subject_icon" />
  363. 题目总数
  364. </div>
  365. <div className="desc-info">{paper.questionNumber}</div>
  366. </div>
  367. <div className="block">
  368. <div className="desc-title">
  369. <Assets name="time_icon" />
  370. 建议用时
  371. </div>
  372. <div className="desc-info">{formatSeconds(paper.time)}</div>
  373. </div>
  374. </div>
  375. {paper.finishTimes > 0 && <div className="tip">
  376. <Checkbox className="m-r-1" checked={disorder} onChange={() => flow.setSetting({ disorder: !disorder })} />
  377. 题目选项乱序显示
  378. </div>}
  379. <div className="submit">
  380. <Button size="lager" radius onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? disorder : false })}>
  381. 开始练习
  382. </Button>
  383. </div>
  384. </div>
  385. </div>
  386. );
  387. }
  388. renderExaminationStartCAT() {
  389. const { paper, flow, setting } = this.props;
  390. const { disorder = true, order = [], orderIndex } = setting;
  391. return (
  392. <div className="exercise-start default">
  393. <div className="title">Section Ordering</div>
  394. <div className="desc">Select the order in which the exam sections are to be administered.</div>
  395. <div className="desc tip">
  396. NOTE: You have 1 minute to make your selection. If you do not make your selection within 1 minute, the first
  397. option listed will be selected and you will view the exam in the following order: Analytical Writing
  398. Assessment, Integrated Reasoning, Quantitative, Verbal.
  399. </div>
  400. <div className="desc">
  401. Once you select your section order, you must view ALL questions in each section, in the order you have
  402. selected, before moving on to the next section. You will NOT be able to return to this screen.
  403. </div>
  404. <div className="block-list">
  405. {ExaminationOrder.map((row, index) => {
  406. return <div className="block-item">
  407. <div className="block-title" onClick={() => {
  408. flow.setSetting({ order: row.value, orderIndex: index });
  409. }}>
  410. <div className="block-title-border">
  411. {orderIndex === index && <AntDIcon type="check" />}
  412. <span>{row.label}</span>
  413. </div>
  414. </div>
  415. {row.list.map((r, i) => {
  416. return <div className="block-text">{i + 1}.{r.label} </div>;
  417. })}
  418. </div>;
  419. })}
  420. </div>
  421. <div className="bottom">
  422. {paper.finishTimes > 0 && <div className="text">
  423. <Checkbox checked={disorder} onChange={() => flow.setSetting({ disorder: !disorder })} /> 题目选项乱序显示
  424. </div>}
  425. <div className="text">
  426. Click{' '}
  427. <div className="next" onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? disorder : false, order: order.filter(row => row) })}>
  428. Next
  429. <Assets name="next_icon" />
  430. </div>{' '}
  431. button to start the exam. You will begin the GMAT exam on the next screen.{' '}
  432. </div>
  433. </div>
  434. </div>
  435. );
  436. }
  437. renderExaminationStartDefault() {
  438. const { paper, flow, setting } = this.props;
  439. const { disorder = true, order = [], orderIndex } = setting;
  440. return (
  441. <div className="exercise-start cat">
  442. <div className="title">Section Ordering</div>
  443. <div className="block-list">
  444. {ExaminationOrder.map((row, index) => {
  445. return <div className="block-item" onClick={() => {
  446. flow.setSetting({ order: row.value, orderIndex: index });
  447. }}>
  448. <div className={orderIndex === index ? 'block-item-body active' : 'block-item-body'}>
  449. {orderIndex === index && <AntDIcon type="check" style={{ color: '#fff' }} />}
  450. {row.list.map((r, i) => {
  451. return <div className="block-text" onClick={() => {
  452. if (order.indexOf(r.value) >= 0) {
  453. // 取消
  454. order[i] = '';
  455. } else {
  456. // 选中
  457. order[i] = r.value;
  458. }
  459. flow.setSetting({ order });
  460. }}>
  461. <Checkbox checked={orderIndex === index ? order.indexOf(r.value) >= 0 : false} /> {r.label}{' '}
  462. </div>;
  463. })}
  464. </div>
  465. </div>;
  466. })}
  467. </div>
  468. <div className="bottom">
  469. {paper.finishTimes > 0 && <div className="text">
  470. <Checkbox checked={disorder} onChange={() => flow.setSetting({ disorder: !disorder })} /> 题目选项乱序显示
  471. </div>}
  472. <div className="text">
  473. Click{' '}
  474. <div className="next" onClick={() => flow.start({ disorder: paper.finishTimes > 0 ? disorder : false, order: order.filter(row => row) })}>
  475. Next
  476. <Assets name="next_icon" />
  477. </div>{' '}
  478. button to start the exam. You will begin the GMAT exam on the next screen.{' '}
  479. </div>
  480. <div className="text tip">*实战可选择考试顺序但无法选择考试内容。</div>
  481. </div>
  482. </div>
  483. );
  484. }
  485. renderRelax() {
  486. const { paper, stageTime, flow } = this.props;
  487. const { showTime } = this.state;
  488. return (
  489. <div className="layout">
  490. <div className="layout-header">
  491. <div className="title">{paper.title}</div>
  492. <div className="right">
  493. <div
  494. className="block"
  495. onClick={() => {
  496. this.setState({ showTime: !showTime });
  497. }}
  498. >
  499. <Assets name="timeleft_icon" />
  500. {showTime && stageTime && `Time left ${formatMinuteSecond(stageTime)}`}
  501. {/* {showTime && singleTime && `Time cost ${formatMinuteSecond(singleTime)}`} */}
  502. </div>
  503. {/* <div
  504. className="block"
  505. onClick={() => {
  506. this.setState({ showNo: !showNo });
  507. }}
  508. >
  509. <Assets name="subjectnumber_icon" />
  510. {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
  511. </div> */}
  512. </div>
  513. </div>
  514. <div className={'layout-body'}>
  515. <div className="relax">
  516. <div className="title">
  517. Optional Break <Icon name="question" />
  518. </div>
  519. <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>') }} />
  520. </div>
  521. </div>
  522. <div className="layout-footer">
  523. <div className="help">
  524. <Assets name="help_icon" onClick={() => this.showHelp()} />
  525. Help
  526. </div>
  527. <div className="full">
  528. <Assets name="fullscreen_icon" onClick={() => flow.toggleFullscreen()} />
  529. </div>
  530. <div className="next" onClick={() => this.stage()}>
  531. Next
  532. <Assets name="next_icon" />
  533. </div>
  534. </div>
  535. </div>
  536. );
  537. }
  538. renderModal() {
  539. const { modal } = this.state;
  540. return (
  541. <div className="modal">
  542. <div className="mask" />
  543. <div style={{ width: modal.width }} className="body">
  544. <div className="title">{modal.title}</div>
  545. <div className="desc">{modal.desc}</div>
  546. {modal.type === 'confirm' && (
  547. <div className="btn-list">
  548. <div className="btn" onClick={() => this.hideModal(true)}>
  549. <span className="t-d-l">Y</span>es
  550. </div>
  551. <div className="btn" onClick={() => this.hideModal(false)}>
  552. <span className="t-d-l">N</span>o
  553. </div>
  554. </div>
  555. )}
  556. {modal.type === 'toast' && (<div className="btn-list">
  557. <div className="btn" onClick={() => this.hideModal(true)}>
  558. <span className="t-d-l">O</span>k
  559. </div>
  560. </div>)}
  561. </div>
  562. </div>
  563. );
  564. }
  565. renderHelp() {
  566. // const { helpInfo } = this.state;
  567. // return (
  568. // <div className="modal">
  569. // <div className="mask" />
  570. // <div style={{ width: modal.width }} className="body">
  571. // <div className="title">{modal.title}</div>
  572. // <div className="desc">{modal.desc}</div>
  573. // </div>
  574. // </div>
  575. // );
  576. }
  577. }