yzQuestion.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import $ from 'jquery'
  2. import components from './box/yzComponents';
  3. let yzQuestion = function (cfg) {
  4. // content里面是box组件
  5. this.content = cfg.content || null;
  6. this.border = parseInt(cfg.border) || 1;
  7. this.minHeight = cfg.minHeight;
  8. this.pNum = cfg.pNum;
  9. this.pageNo = cfg.pageNo;
  10. this.problemId = cfg.problemId;
  11. this.questionId = cfg.questionId;
  12. this.rectNo = cfg.rectNo;
  13. this.score = cfg.score;
  14. this.sort = cfg.sort;
  15. this.title = cfg.title || '';
  16. this.inDetail = cfg.inDetail;
  17. this.isSubject = cfg.isSubject;
  18. this.blockindex = parseInt(cfg.blockindex);
  19. this.blocknum = parseInt(cfg.blocknum);
  20. // 这个是组件的dom
  21. this.contentDom = [];
  22. this.order = parseInt(cfg.order);
  23. this.domId = ('lsiten_question_'+Math.random()).replace('.','_');
  24. // dom相关属性
  25. this.vdom = null;
  26. };
  27. yzQuestion.prototype._mapKey = [
  28. 'A', 'B', 'C', 'D', 'E', 'F',
  29. 'G', 'H', 'I', 'J', 'K', 'L',
  30. 'M', 'N', 'O', 'P', 'Q', 'R',
  31. 'S', 'T', 'U', 'V', 'W', 'X',
  32. 'Y', 'Z'
  33. ];
  34. /**
  35. * function 生成客观题的dom
  36. */
  37. yzQuestion.prototype._renderObjectiveDom = function () {
  38. let ul = $('<ul id="'+ this.domId +'_ul" class="question-selection-ul"></ul>');
  39. let optionnum = parseInt(this.inDetail.split('-')[2]);
  40. for (let i=0; i<optionnum; i++) {
  41. ul.append('<li id="'+ this.domId +'_li" class="question-selection-li">'+ this._mapKey[i] +'</li>');
  42. }
  43. this.vdom.append(ul);
  44. };
  45. /**
  46. * function 生成主观题的dom
  47. * @TODO 生成content
  48. */
  49. yzQuestion.prototype._renderCommonDom = function () {
  50. let content = $('<div id="'+ this.domId +'_content" class="question-selection-content"></div>');
  51. content.css({height: (this.minHeight - 20) + 'px'});
  52. if (this.content) {
  53. this.content.renderDom();
  54. } else {
  55. this.content = new components({
  56. content: content
  57. });
  58. }
  59. this.vdom.append(content);
  60. // 冒泡监听事件
  61. this.vdom.on('click', ()=>{
  62. // 将题目和domId冒泡到顶层
  63. this.vdom.trigger('getQuestion',[this.pNum,this.content]);
  64. });
  65. };
  66. /**
  67. * function 生成title的dom
  68. * @TODO title可编辑
  69. */
  70. yzQuestion.prototype._renderTitleDom = function () {
  71. let title = $('<div id="'+ this.domId +'_title" class="question-selection-title"></div>');
  72. if (!this.isSubject) {
  73. title.html(this.pNum+this.title);
  74. } else
  75. {
  76. title.addClass('next-line');
  77. let sort = $('<div class="sort">' + this.pNum + '</div>');
  78. let subtitle = $('<div class="edit-title">' + this.title + '</div>');
  79. let score = $('<div class="score">(' + this.score + '分)</div>');
  80. title.append(sort);
  81. title.append(subtitle);
  82. title.append(score);
  83. }
  84. title.css({
  85. height: '20px',
  86. 'line-height': '20px'
  87. });
  88. this.vdom.append(title);
  89. };
  90. /**
  91. * function 将数据转换为dom的形式
  92. */
  93. yzQuestion.prototype.renderToDom = function () {
  94. this.vdom = $('<div id="'+ this.domId +'" class="question-selection"></div>');
  95. this.vdom.css({
  96. minHeight: this.minHeight
  97. })
  98. this._renderTitleDom();
  99. // 如果是客观题
  100. if (!this.isSubject) {
  101. let width = 100/this.allblock + '%';
  102. this.vdom.css({
  103. width: width
  104. });
  105. this._renderObjectiveDom();
  106. } else {
  107. this._renderCommonDom();
  108. }
  109. };
  110. export default yzQuestion;