yzQuestion.js 3.3 KB

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