|
@@ -1,5 +1,6 @@
|
|
|
import $ from './dom-core';
|
|
|
import {isEmptyElement} from './util.js'
|
|
|
+import {gennerOneLine} from './lineUtil.js'
|
|
|
let questionUtils = function () {
|
|
|
|
|
|
}
|
|
@@ -37,7 +38,7 @@ questionUtils.prototype = {
|
|
|
let row = childs[i];
|
|
|
let $row = $(row);
|
|
|
let $nextRow = $row.clone(false);
|
|
|
- if (bSizeBorder.bottom > csize.bottom) {
|
|
|
+ if (bSizeBorder.bottom >= csize.bottom) {
|
|
|
this._splitObjectRow($nextRow, $row, $border, csize);
|
|
|
if ($nextRow.childNodes().length > 0) {
|
|
|
if ($nextBorder.childNodes().length > 0) {
|
|
@@ -137,13 +138,24 @@ questionUtils.prototype = {
|
|
|
$item.find('.js-option-column').css('min-height', contentSize.height + 'px');
|
|
|
}
|
|
|
let bSizeBorder = $border.getSizeData();
|
|
|
- if (bSizeBorder.bottom > csize.bottom) {
|
|
|
+ if (bSizeBorder.bottom >= csize.bottom) {
|
|
|
this._splitObjectRow($nextDiv, $item, $border, csize);
|
|
|
}
|
|
|
|
|
|
},
|
|
|
-
|
|
|
-
|
|
|
+ /**
|
|
|
+ * function 插入dom
|
|
|
+ * @param {DomElement} $nextDom 下一栏暂存
|
|
|
+ * @param {DomElement} $dom 需要插入的dom
|
|
|
+ */
|
|
|
+ insertDom($nextDom, $dom) {
|
|
|
+ let firstChild = $nextDom[0].firstChild;
|
|
|
+ if (firstChild) {
|
|
|
+ $dom.insertBefore($(firstChild));
|
|
|
+ } else {
|
|
|
+ $nextDom.append($dom);
|
|
|
+ }
|
|
|
+ },
|
|
|
/**
|
|
|
* function 切分主观题框
|
|
|
* @param {DomElement} $border 题框
|
|
@@ -155,277 +167,129 @@ questionUtils.prototype = {
|
|
|
if ($content.attr('class') === 'border-content') {
|
|
|
let childrens = $content.childNodes();
|
|
|
let $nextContent = $content.clone(false);
|
|
|
- let html = $content.html().toLowerCase();
|
|
|
- if (childrens.length < 1 || html === '' || html === '<p></p>' || html === '<p><br></p>' || html === '<p><br/></p>' || html === '<br/>' || html === '<br>') {
|
|
|
- $nextBorder.append($content);
|
|
|
- } else {
|
|
|
- for (let i =(childrens.length -1); i >= 0; i--) {
|
|
|
- let item = childrens[i];
|
|
|
- let bSize = $border.getSizeData();
|
|
|
- if (bSize.bottom > csize.bottom) {
|
|
|
- if (item.nodeType === 3) {
|
|
|
- this._handerText($nextBorder, item, csize, $border);
|
|
|
- } else {
|
|
|
- let $item = $(item);
|
|
|
- let className = $item.attr('class');
|
|
|
- if (className && className.indexOf('js-lsiten-question') > -1) {
|
|
|
- let type = parseInt($item.attr('data-type'));
|
|
|
- let nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- let $nextChoice = this._handleChoice($item, csize, $border);
|
|
|
- nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextChoice[0].firstChild, nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent[0].appendChild($nextChoice[0].firstChild);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- let $nextJudge = this._handleJudge($item, csize, $border);
|
|
|
- nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextJudge[0].firstChild, nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent[0].appendChild($nextJudge[0].firstChild);
|
|
|
- }
|
|
|
- break;
|
|
|
- case 3:
|
|
|
- let $nextsubject = this._handleSubject($item, csize, $border);
|
|
|
- if ($nextsubject) {
|
|
|
- nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextsubject[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent.append($nextsubject);
|
|
|
- }
|
|
|
- $nextsubject.addClass('js-split-problem');
|
|
|
- $item.addClass('js-split-problem');
|
|
|
- $nextsubject.attr('data-rm', 0);
|
|
|
- if (isEmptyElement($item[0])) {
|
|
|
- $item.remove();
|
|
|
- $nextsubject.attr('data-rm', 1);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case 4:
|
|
|
- let $nextWritting = this._handleWritting($item, csize, $border);
|
|
|
- if ($nextWritting) {
|
|
|
- nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextWritting[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent.append($nextWritting);
|
|
|
- }
|
|
|
- $nextWritting.addClass('js-split-writting-problem');
|
|
|
- $item.addClass('js-split-writting-problem');
|
|
|
- $nextWritting.attr('data-rm', 0);
|
|
|
- if (isEmptyElement($item[0])) {
|
|
|
- $item.remove();
|
|
|
- $nextWritting.attr('data-rm', 1);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- } else if (className && className.indexOf('js-lsiten-options-box') > -1) {
|
|
|
- // 选做题处理
|
|
|
- let $nextOption = this._handleOptions($item, csize, $border);
|
|
|
- let nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextOption[0].firstChild, nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent[0].appendChild($nextOption[0].firstChild);
|
|
|
- }
|
|
|
- } else {
|
|
|
- let $nextDiv = this._handerDiv($item, csize, $border);
|
|
|
- if ($nextDiv.childNodes().length > 0) {
|
|
|
- let nextDivFirstDiv = $nextContent[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextContent[0].insertBefore($nextDiv[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextContent.append($nextDiv)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (isEmptyElement($item[0])) {
|
|
|
- $item.remove();
|
|
|
- }
|
|
|
- }
|
|
|
+ let index = childrens.length - 1;
|
|
|
+ while (index >= 0 ) {
|
|
|
+ let line = childrens[index];
|
|
|
+ if (line && line.className) {
|
|
|
+ if (line.className.indexOf('js-lsiten-line') > -1) {
|
|
|
+ // 普通一行或者是单选题可判断题
|
|
|
+ let $line = $(line);
|
|
|
+ let borderSize = $border.getSizeData();
|
|
|
+ if (borderSize.bottom >= csize.bottom) {
|
|
|
+ this.insertDom($nextContent, $line);
|
|
|
+ }
|
|
|
+ } else if (line.className.indexOf('js-lsiten-question') > -1) {
|
|
|
+ let $line = $(line);
|
|
|
+ let questionType = parseInt($line.attr('data-type'));
|
|
|
+ switch (questionType) {
|
|
|
+ case 1:
|
|
|
+ this._handleChoice($line, $nextContent, csize);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ this._handleJudge($line, $nextContent, csize);
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ this._handleSubject($line, $nextContent, $border, csize);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ this._handleWritting($line, $nextContent, $border, csize);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ line && $content[0].removeChild(line);
|
|
|
}
|
|
|
-
|
|
|
- if ($nextContent.childNodes().length > 0) {
|
|
|
- $nextBorder.append($nextContent);
|
|
|
- }
|
|
|
+ index--;
|
|
|
+ }
|
|
|
+ if ($nextContent.childNodes().length > 0) {
|
|
|
+ $nextBorder.append($nextContent);
|
|
|
+ $border.addClass('js-split-border');
|
|
|
+ $nextBorder.addClass('js-split-border');
|
|
|
+ return $nextBorder;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
}
|
|
|
- return $nextBorder.childNodes().length > 0 ? $nextBorder: false;
|
|
|
} else {
|
|
|
$border.remove();
|
|
|
return false;
|
|
|
}
|
|
|
},
|
|
|
/**
|
|
|
- * function 处理纯文字
|
|
|
- * @param {DomElement} $nextDiv 下一段的div
|
|
|
- * @param {DomElement} item 文本节点
|
|
|
- * @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
- */
|
|
|
- _handerText: function ($nextDiv, item, csize, $border) {
|
|
|
- let text = item.nodeValue;
|
|
|
- let nextNode = item.nextSibling;
|
|
|
- let parentNode = item.parentNode;
|
|
|
- let span = document.createElement('span');
|
|
|
- span.appendChild(item);
|
|
|
- if (nextNode) {
|
|
|
- parentNode.insertBefore(span, nextNode);
|
|
|
- } else {
|
|
|
- parentNode.appendChild(span);
|
|
|
- }
|
|
|
- let $span = $(span);
|
|
|
- let textSize = $span.getSizeData();
|
|
|
- if (textSize.top > csize.bottom) {
|
|
|
- $nextDiv[0].appendChild(item);
|
|
|
- $span.remove();
|
|
|
- } else {
|
|
|
- let textLength = text.length;
|
|
|
- let i = 0;
|
|
|
- while(--textLength) {
|
|
|
- span.innerHTML = text.substr(0, textLength);
|
|
|
- // 要看框的底部是否在栏内
|
|
|
- textSize = $border.getSizeData();
|
|
|
- if (textSize.bottom < csize.bottom) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- let prevText = text.substr(0, textLength);
|
|
|
- let nextText = text.substring(textLength);
|
|
|
- span.outerHTML = prevText;
|
|
|
- $(parentNode).addClass('js-split-item');
|
|
|
- $nextDiv[0].appendChild(document.createTextNode(nextText));
|
|
|
- $nextDiv.addClass('js-split-item');
|
|
|
- }
|
|
|
- },
|
|
|
- /**
|
|
|
- * function 处理纯普通div
|
|
|
- * @param {DomElement} $item 纯普通div
|
|
|
- * @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
- */
|
|
|
- _handerDiv: function ($item, csize, $border) {
|
|
|
- let $nextDiv = $item.clone(false);
|
|
|
- this._splitNormalDiv($nextDiv,$item, csize, $border);
|
|
|
- return $nextDiv;
|
|
|
- },
|
|
|
- /**
|
|
|
- * function 切分纯普通div
|
|
|
- * @param {DomElement} $nextDiv 纯普通div
|
|
|
- * @param {Node} $item 纯普通div
|
|
|
- * @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
- */
|
|
|
- _splitNormalDiv: function ($nextDiv, $item, csize, $border) {
|
|
|
- let bSize = $border.getSizeData();
|
|
|
- if (bSize.bottom < csize.bottom) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- let childs = $item.childNodes();
|
|
|
- let cLength = childs.length;
|
|
|
- if (cLength > 0) {
|
|
|
- for (let i =(cLength -1); i >= 0; i--) {
|
|
|
- let child = childs[i];
|
|
|
- if (child.nodeType === 3) {
|
|
|
- this._handerText($nextDiv, child, csize, $border);
|
|
|
- } else {
|
|
|
- let tName = child.tagName.toLowerCase();
|
|
|
- if (tName === 'br') {
|
|
|
- let nextFirstChild = $nextDiv[0].firstChild;
|
|
|
- if (nextFirstChild) {
|
|
|
- $nextDiv[0].insertBefore(child, nextFirstChild);
|
|
|
- } else {
|
|
|
- $nextDiv[0].appendChild(child);
|
|
|
- }
|
|
|
- } else if (tName === 'img') {
|
|
|
- let nextFirstChild = $nextDiv[0].firstChild;
|
|
|
- if (nextFirstChild) {
|
|
|
- $nextDiv[0].insertBefore(child, nextFirstChild);
|
|
|
- } else {
|
|
|
- $nextDiv[0].appendChild(child);
|
|
|
- }
|
|
|
- } else {
|
|
|
- let $child = $(child);
|
|
|
- let $nextChild = $child.clone(false);
|
|
|
- this._splitNormalDiv($nextChild, $child, csize, $border);
|
|
|
- if ($nextChild.childNodes().length > 0) {
|
|
|
- let nextDivFirstDiv = $nextDiv[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextDiv[0].insertBefore($nextChild[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextDiv.append($nextChild)
|
|
|
- }
|
|
|
- }
|
|
|
- if ($child[0].innerHTML === '') {
|
|
|
- $child.remove();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- /**
|
|
|
- * function 处理选择题div
|
|
|
- * @param {DomElement} $item 纯普通div
|
|
|
- * @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
- */
|
|
|
- _handleOptions: function ($item, csize, $border) {
|
|
|
- let $nextNext = $('<div></div>');
|
|
|
- $nextNext.append($item);
|
|
|
- return $nextNext;
|
|
|
- },
|
|
|
- /**
|
|
|
* function 处理选择题div
|
|
|
* @param {DomElement} $item 纯普通div
|
|
|
+ * @param {DomElement} $nextContent 下一个框暂存
|
|
|
* @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
*/
|
|
|
- _handleChoice: function ($item, csize, $border) {
|
|
|
- let $nextNext = $('<div></div>');
|
|
|
- $nextNext.append($item);
|
|
|
- return $nextNext;
|
|
|
+ _handleChoice: function ($item, $nextContent, csize) {
|
|
|
+ let lineSize = $item.getSizeData();
|
|
|
+ if (lineSize.top >= csize.bottom) {
|
|
|
+ let newLine = gennerOneLine();
|
|
|
+ newLine.append($item);
|
|
|
+ $nextContent.append(newLine);
|
|
|
+ }
|
|
|
},
|
|
|
/**
|
|
|
* function 处理判断题div
|
|
|
* @param {DomElement} $item 纯普通div
|
|
|
+ * @param {DomElement} $nextContent 下一个框暂存
|
|
|
* @param {Object} csize 栏size
|
|
|
- * @param {DomElement} $border 所在框
|
|
|
*/
|
|
|
- _handleJudge: function ($item, csize, $border) {
|
|
|
- let $nextNext = $('<div></div>');
|
|
|
- $nextNext.append($item);
|
|
|
- return $nextNext;
|
|
|
+ _handleJudge: function ($item, $nextContent, csize) {
|
|
|
+ let lineSize = $item.getSizeData();
|
|
|
+ if (lineSize.top >= csize.bottom) {
|
|
|
+ let newLine = gennerOneLine();
|
|
|
+ newLine.append($item);
|
|
|
+ $nextContent.append(newLine);
|
|
|
+ }
|
|
|
},
|
|
|
/**
|
|
|
* function 处理客观题Div
|
|
|
* @param {DomElement} $item 纯普通div
|
|
|
- * @param {Object} csize 栏size
|
|
|
+ * @param {DomElement} $nextContent 下个框暂存
|
|
|
* @param {DomElement} $border 所在框
|
|
|
+ * @param {Object} csize 栏size
|
|
|
*/
|
|
|
- _handleSubject: function ($item, csize, $border) {
|
|
|
+ _handleSubject: function ($item, $nextContent, $border, csize) {
|
|
|
let $nextSubject = $item.clone(false);
|
|
|
- this._splitNormalDiv($nextSubject, $item, csize, $border);
|
|
|
- !$item.childNodes().length && $item.remove();
|
|
|
- return $nextSubject.childNodes().length > 0 ? $nextSubject : false;
|
|
|
+ let childs = $item.childNodes();
|
|
|
+ let index = childs.length - 1;
|
|
|
+ while (index >= 0 ) {
|
|
|
+ let line = childs[index];
|
|
|
+ if (line && line.className) {
|
|
|
+ if (line.className.indexOf('js-lsiten-line') > -1) {
|
|
|
+ // 普通一行
|
|
|
+ let $line = $(line);
|
|
|
+ let bSize = $border.getSizeData();
|
|
|
+ if (bSize.bottom >= csize.bottom) {
|
|
|
+ this.insertDom($nextSubject, $line);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $(line).remove();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ line && $paragraph[0].removeChild(line);
|
|
|
+ }
|
|
|
+ index--;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isEmptyElement($item[0])) {
|
|
|
+ $item.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($nextSubject.childNodes().length > 0) {
|
|
|
+ this.insertDom($nextContent, $nextSubject);
|
|
|
+ $nextSubject.addClass('js-split-question');
|
|
|
+ $item.addClass('js-split-question');
|
|
|
+ }
|
|
|
},
|
|
|
/**
|
|
|
* function 处理作文题Div
|
|
|
* @param {DomElement} $item 纯普通div
|
|
|
- * @param {Object} csize 栏size
|
|
|
+ * @param {DomElement} $nextContent 下个框暂存
|
|
|
* @param {DomElement} $border 所在框
|
|
|
+ * @param {Object} csize 栏size
|
|
|
*/
|
|
|
- _handleWritting: function ($item, csize, $border) {
|
|
|
+ _handleWritting: function ($item, $nextContent, $border, csize) {
|
|
|
let childs = $item.childNodes();
|
|
|
let cLength = childs.length;
|
|
|
let $nextWritting = $item.clone(false);
|
|
@@ -435,20 +299,33 @@ questionUtils.prototype = {
|
|
|
let bSize = $border.getSizeData();
|
|
|
let child = childs[i];
|
|
|
let $child = $(child);
|
|
|
- if (bSize.bottom > csize.bottom) {
|
|
|
+ if (bSize.bottom >= csize.bottom) {
|
|
|
if (child.className.indexOf('lsiten-title') > -1) {
|
|
|
if (!isEmptyElement(child)) {
|
|
|
// 作文标题
|
|
|
let $nextLsitenTitle = $child.clone(false);
|
|
|
- this._splitNormalDiv($nextLsitenTitle, $child, csize, $border);
|
|
|
- if ( $nextLsitenTitle.childNodes().length > 0 ) {
|
|
|
- let nextDivFirstDiv = $nextWritting[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextWritting[0].insertBefore($nextLsitenTitle[0], nextDivFirstDiv);
|
|
|
+ let titleChilds = $child.childNodes();
|
|
|
+ let titleIndex = titleChilds.length - 1;
|
|
|
+ while(titleIndex >= 0) {
|
|
|
+ let line = titleChilds[titleIndex];
|
|
|
+ if (line && line.className) {
|
|
|
+ if (line.className.indexOf('js-lsiten-line') > -1) {
|
|
|
+ // 普通一行
|
|
|
+ let $line = $(line);
|
|
|
+ bSize = $border.getSizeData();
|
|
|
+ if (bSize.bottom >= csize.bottom) {
|
|
|
+ this.insertDom($nextLsitenTitle, $line);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $(line).remove();
|
|
|
+ }
|
|
|
} else {
|
|
|
- $nextWritting.append($nextLsitenTitle);
|
|
|
+ line && $paragraph[0].removeChild(line);
|
|
|
}
|
|
|
- $nextLsitenTitle.addClass('js-split-writting-title');
|
|
|
+ titleIndex--;
|
|
|
+ }
|
|
|
+ if ($nextLsitenTitle.childNodes().length > 0) {
|
|
|
+ this.insertDom($nextWritting, $nextLsitenTitle);
|
|
|
}
|
|
|
} else {
|
|
|
$child.remove();
|
|
@@ -462,14 +339,9 @@ questionUtils.prototype = {
|
|
|
let k = rowLength - 1;
|
|
|
while ( k >= 0 ) {
|
|
|
let bSizeBorder = $border.getSizeData();
|
|
|
- if (bSizeBorder.bottom > csize.bottom) {
|
|
|
+ if (bSizeBorder.bottom >= csize.bottom) {
|
|
|
let $rowItem = $(rows[k]);
|
|
|
- let nextDivFirstDiv = $nextLsitenBorder[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextLsitenBorder[0].insertBefore($rowItem[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextLsitenBorder.append($rowItem);
|
|
|
- }
|
|
|
+ this.insertDom($nextLsitenBorder, $rowItem);
|
|
|
}
|
|
|
k--;
|
|
|
}
|
|
@@ -478,21 +350,23 @@ questionUtils.prototype = {
|
|
|
}
|
|
|
|
|
|
if ($nextLsitenBorder.childNodes().length > 0) {
|
|
|
- let nextDivFirstDiv = $nextWritting[0].firstChild;
|
|
|
- if (nextDivFirstDiv) {
|
|
|
- $nextWritting[0].insertBefore($nextLsitenBorder[0], nextDivFirstDiv);
|
|
|
- } else {
|
|
|
- $nextWritting.append($nextLsitenBorder);
|
|
|
- }
|
|
|
+ this.insertDom($nextWritting, $nextLsitenBorder);
|
|
|
$nextLsitenBorder.addClass('js-split-writting-border');
|
|
|
}
|
|
|
+ } else {
|
|
|
+ $child.remove();
|
|
|
}
|
|
|
}
|
|
|
i--;
|
|
|
isEmptyElement($child[0]) && $child.remove();
|
|
|
}
|
|
|
}
|
|
|
- return $nextWritting.childNodes().length > 0 ? $nextWritting : false;
|
|
|
+
|
|
|
+ if ($nextWritting.childNodes().length > 0) {
|
|
|
+ this.insertDom($nextContent, $nextWritting);
|
|
|
+ $nextWritting.addClass('js-split-question');
|
|
|
+ $item.addClass('js-split-question');
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|