first commit
This commit is contained in:
412
module/Optimize.js
Normal file
412
module/Optimize.js
Normal file
@@ -0,0 +1,412 @@
|
||||
|
||||
|
||||
window.kon = window.kon || {}
|
||||
|
||||
kon.op = (function () {
|
||||
html = ""
|
||||
thistarget = ""
|
||||
vp = ""
|
||||
cp = ""
|
||||
variable_array = []
|
||||
|
||||
function setTarget(target) {
|
||||
thistarget = target;
|
||||
}
|
||||
|
||||
function setPattern(type, reg) {
|
||||
var pattern = global.pattern;
|
||||
if (!type) {
|
||||
kon.op.vp = new RegExp(pattern.variable.map(function (value) { return value }).join("|"), "g");
|
||||
if (isBrowerCheck() !== "IE")
|
||||
kon.op.cp = new RegExp(pattern.code.map(function (value) { return value }).join("|") + "|" + pattern.unicode.source, "gum");
|
||||
} else if (type === "code") {
|
||||
if (isBrowerCheck() !== "IE")
|
||||
kon.op.cp = new RegExp(reg + "|" + pattern.unicode.source, "gum");
|
||||
} else if (type === "variable") {
|
||||
kon.op.vp = new RegExp(reg, "g");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function removeTag() {
|
||||
|
||||
var text = "";
|
||||
thistarget.find("img").remove();
|
||||
|
||||
// p 안에 없는 span들 전부 꺼내서 p 로 감싸기.
|
||||
thistarget.children("span").each(function (_index, element) {
|
||||
if ($(element).next().prop("tagName") != "SPAN") {
|
||||
text += element.innerText;
|
||||
element.insertAdjacentHTML("beforebegin", "<p>" + text + "</p>");
|
||||
element.remove();
|
||||
text = ""
|
||||
} else {
|
||||
text += element.innerText.replace(/\t| /g, " ")
|
||||
element.remove();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function createSeperation() {
|
||||
var seperate = global.pattern.seperate,
|
||||
indexArr = [],
|
||||
html,
|
||||
tt
|
||||
|
||||
html = thistarget.html().replace(/ /g, " "); //   => ? 나와서 ' ' 띄어쓰기로 변경시킴.
|
||||
html = html.replace(/\t/g, " "); // 탭 => ? 띄어쓰기 ' ' 띄어쓰기로 변경시킴.
|
||||
|
||||
indexArr = regex_match(seperate, html)
|
||||
thistarget.html("");
|
||||
|
||||
indexArr.forEach(function (value, index) {
|
||||
index == 0 ? thistarget.append(html.slice(0, value.index)) : thistarget.append("<div class='question'>" + html.slice(tt, value.index) + "</div>")
|
||||
tt = value.index
|
||||
})
|
||||
|
||||
thistarget.append("<div class='question'>" + html.slice(tt, html.length) + "</div>")
|
||||
}
|
||||
|
||||
function optimize_html() {
|
||||
thistarget.find("p").each(function (_index, ele) {
|
||||
|
||||
var text = "";
|
||||
ele.removeAttribute("class");
|
||||
|
||||
Array.from(ele.childNodes).forEach(function (value) {
|
||||
var thistext = !(value.innerText) ? value.textContent.replace(/\t/g, " ") : value.innerText.replace(/\t/g, " ");
|
||||
thistext = to_blank(thistext)
|
||||
if (value.nodeType !== 3 && value.nodeType !== 1) thistext = ""
|
||||
text += thistext;
|
||||
});
|
||||
|
||||
text = text.trim().replace(kon.op.vp, function (e) { return "<mark class='variable_mark'>" + e + "</mark>" })
|
||||
text = text.trim().replace(kon.op.cp, function (e) { return "<mark class='code_mark'>" + e + "</mark>" })
|
||||
|
||||
ele.innerHTML = text;
|
||||
ele.style.all = "unset"
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
function each_analysis() {
|
||||
kon.op.variable_array = []
|
||||
thistarget.find(".question").each(function (_index, element) {
|
||||
kon.op.variable_array.push(analysis_html(element))
|
||||
})
|
||||
return kon.op.variable_array
|
||||
}
|
||||
|
||||
function analysis_html(element) {
|
||||
var multi_pattern = global.pattern.multi,
|
||||
rank_pattern = global.pattern.rank,
|
||||
data = {},
|
||||
codetarget,
|
||||
codeindex = 1;
|
||||
|
||||
attr_flag = false;
|
||||
|
||||
data.variable = ""
|
||||
data.title = ""
|
||||
data.stype = ""
|
||||
data.description = ""
|
||||
data.stype = ""
|
||||
data.qtype = ""
|
||||
|
||||
data["dataset"] = {
|
||||
"attr": {},
|
||||
"scale": {},
|
||||
"multi": {},
|
||||
"single": {},
|
||||
"rank": {}
|
||||
};
|
||||
|
||||
data["datagroup"] = {
|
||||
"attr": {
|
||||
"header": {},
|
||||
"prev": {},
|
||||
"after": {}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
$(element).find("p:not(table p)").each(function (index, elem) {
|
||||
var text = elem.innerText
|
||||
var code_arr = [];
|
||||
var rankLength;
|
||||
var codevalue = [];
|
||||
//보기 문항 인지.
|
||||
if (regex_test(kon.op.cp, text)) {
|
||||
code_arr = regex_match(kon.op.cp, text);
|
||||
|
||||
if (regex_test(kon.op.vp, text)) {
|
||||
var variable_name = text.match(kon.op.vp)[0].replace(/[\_\-]/, "0")
|
||||
|
||||
variable_name = variable_name.replace(/[^a-zA-Z0-9]/g, "")
|
||||
if ($.isNumeric(variable_name)) {
|
||||
data.variable = "BQ" + variable_name
|
||||
} else {
|
||||
data.variable = variable_name
|
||||
}
|
||||
//해당 태그안에 보기가 있으니
|
||||
data.title = text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (regex_test(multi_pattern, data.title || ""))
|
||||
codetarget = data.dataset.multi;
|
||||
else
|
||||
codetarget = data.dataset.single;
|
||||
|
||||
codevalue = code_arr.map(function (st, index) {
|
||||
var a = code_arr.length == index + 1 ? text.slice((st.index + st.value.length), text.length) : text.slice((st.index + st.value.length), code_arr[index + 1].index)
|
||||
return a;
|
||||
})
|
||||
|
||||
codevalue.forEach(function (value) {
|
||||
codetarget[codeindex] = { "label": value.replace(/[\n\t]/g, " "), "etc": String(+regex_test(global.pattern.etc, value)), "value": String(codeindex) }
|
||||
codeindex++;
|
||||
})
|
||||
|
||||
} else {
|
||||
if (regex_test(kon.op.vp, text)) {
|
||||
var variable_name = text.match(kon.op.vp)[0].replace(/[\_\-]/, "0")
|
||||
|
||||
variable_name = variable_name.replace(/[^a-zA-Z0-9]/g, "")
|
||||
if ($.isNumeric(variable_name)) {
|
||||
data.variable = "BQ" + variable_name
|
||||
} else {
|
||||
data.variable = variable_name
|
||||
}
|
||||
//해당 태그안에 보기가 있으니
|
||||
data.title = text;
|
||||
}
|
||||
}
|
||||
|
||||
if (regex_test(rank_pattern, text)) {
|
||||
rankLength = regex_match(rank_pattern, text).length;
|
||||
|
||||
data["dataset"]["rank"].val = rankLength;
|
||||
data["dataset"]["rank"].max = rankLength;
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// 속성, 척도 문항 찾기.
|
||||
$(element).find("table").each(function (_tableid, ttable) {
|
||||
|
||||
var tr = $(ttable).find("tr"),
|
||||
innerText_tr = [],
|
||||
scale = [],
|
||||
group_tr = {
|
||||
true: [],
|
||||
false: []
|
||||
}
|
||||
|
||||
if (!tr.length) {
|
||||
if ($(ttable).text().trim() === "") $(ttable).remove();
|
||||
return true;
|
||||
}
|
||||
|
||||
//scale 처리
|
||||
var attr_value = 0; /* 속성 코드 */
|
||||
tr.each(function (_trid, ttr) {
|
||||
var thisStat;
|
||||
var v_chk = false // 코드 가지고 있는 지 체크
|
||||
if (ttr.innerText.trim() == "") {
|
||||
ttr.remove();
|
||||
} else {
|
||||
//각 tr의 td.innerText => 배열로 반환
|
||||
|
||||
innerText_tr = $.map($(ttr).find("td"), function (value, id) {
|
||||
var tableObj = {}
|
||||
var text = value.innerText.replace(/[\n\t]/g, " ");
|
||||
tableObj["col"] = value.colSpan || 1;
|
||||
tableObj["row"] = value.rowSpan || 1;
|
||||
tableObj["label"] = text;
|
||||
|
||||
return tableObj
|
||||
});
|
||||
|
||||
// 각 tr의 td.innerText 배열에 보기 코드가 있는 지 확인.
|
||||
innerText_tr.some(function (element) {
|
||||
return regex_test(kon.op.cp, element.label)
|
||||
});
|
||||
thisStat = innerText_tr.some(function (element) { return regex_test(kon.op.cp, element.label) });
|
||||
scale.push(thisStat)
|
||||
/* 보기와 코드를 가지고 있는 tr */
|
||||
if (thisStat) {
|
||||
innerText_tr.some(function (ar, id) {
|
||||
/* 코드가 아닌 td 찾고 해당 td에 attr 속성 값 부여 */
|
||||
if (!regex_test(kon.op.cp, ar.label)) {
|
||||
if(!v_chk){
|
||||
attr_value++;
|
||||
v_chk = true;
|
||||
}
|
||||
ar.value = attr_value;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
group_tr[thisStat].push(innerText_tr);
|
||||
}
|
||||
})
|
||||
|
||||
prev = scale.pop();
|
||||
|
||||
// 보기인 tr과 보기가 아닌 tr이 같이 있을 경우
|
||||
if (scale.some(function (element) { return prev ^ element })) {
|
||||
var attr_f;
|
||||
var codenum = 0;
|
||||
attr_flag = true;
|
||||
//보기가 있는 TR에서 검색 every는 모든 요소가 주어진 판별 함수를 통과하는 지 테스트
|
||||
attr_f = group_tr[true].some(function (ele, index) {
|
||||
// 보기가 아닌 td가 하나라도 있다면 attr 간주
|
||||
return ele.some(function (ele_child) {
|
||||
if (!regex_test(kon.op.cp, ele_child.label))
|
||||
if (!index) codenum += ele_child.col
|
||||
else return true;
|
||||
})
|
||||
})
|
||||
|
||||
var ii = 1;
|
||||
if (attr_f) {
|
||||
data["datagroup"]["attr"]["header"] = { "1": [] }
|
||||
group_tr[false].forEach(function (tr, index) {
|
||||
tr.forEach(function (value) {
|
||||
if (codenum > 0) {
|
||||
data["datagroup"]["attr"]["header"]["1"].push({ "label": value.label, "cols": value.col, "rows": value.row })
|
||||
} else {
|
||||
data["dataset"]["scale"][ii] = { "label": value.label, "cols": value.col, "rows": value.row, "value": ii }
|
||||
ii++;
|
||||
}
|
||||
codenum -= value.col;
|
||||
})
|
||||
})
|
||||
|
||||
group_tr[true].forEach(function (td, id) {
|
||||
var a = td.filter(function (value) {
|
||||
return !regex_test(kon.op.cp, value.label)
|
||||
});
|
||||
data["dataset"]["attr"][id + 1] = a.pop();
|
||||
|
||||
a.forEach(function (ae, index2) {
|
||||
if (!data["datagroup"]["attr"]["prev"][id]) data["datagroup"]["attr"]["prev"][id] = []
|
||||
data["datagroup"]["attr"]["prev"][id].push({ "label": ae.label, "cols": ae.col, "rows": ae.row });
|
||||
})
|
||||
})
|
||||
|
||||
} else {
|
||||
group_tr[false].forEach(function (tr, index) {
|
||||
tr.forEach(function (value) {
|
||||
data["dataset"]["scale"][ii] = { "label": value.label, "cols": value.col, "rows": value.row, "value": ii }
|
||||
ii++;
|
||||
})
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// 모두 보기를 가진 td이거나 모두 보기가 아닌 td 일 경우
|
||||
var index_arr = []
|
||||
var hv
|
||||
if (prev) {
|
||||
text = group_tr[true].map(function (element) { return element.map(function (text) { return text.label }).join("") }).join("");
|
||||
index_arr = regex_match(kon.op.cp, text)
|
||||
|
||||
if (regex_test(kon.op.vp, text)) {
|
||||
var variable_name = text.match(kon.op.vp)[0].replace(/[\_\-]/, "0")
|
||||
variable_name = variable_name.replace(/[^a-zA-Z0-9]/g, "")
|
||||
|
||||
if ($.isNumeric(variable_name)) {
|
||||
data.variable = "BQ" + variable_name
|
||||
} else {
|
||||
data.variable = variable_name
|
||||
}
|
||||
//해당 태그안에 보기가 있으니
|
||||
data.title = text;
|
||||
}
|
||||
|
||||
var codevalue
|
||||
|
||||
codevalue = index_arr.map(function (st, index) {
|
||||
var a = index_arr.length == index + 1 ? text.slice((st.index + st.value.length), text.length) : text.slice((st.index + st.value.length), index_arr[index + 1].index)
|
||||
return a;
|
||||
})
|
||||
if (regex_test(multi_pattern, data.title))
|
||||
codetarget = data.dataset.multi;
|
||||
else
|
||||
codetarget = data.dataset.single;
|
||||
|
||||
codevalue.forEach(function (value, index) {
|
||||
codetarget[codeindex] = { "label": value.replace(/[\n\t]/g, " "), "etc": String(+regex_test(global.pattern.etc, value)), "value": String(codeindex) }
|
||||
codeindex++;
|
||||
});
|
||||
|
||||
} else {
|
||||
text = group_tr[false].map(function (element) { return element.map(function (text) { return text.label }).join("") }).join("");
|
||||
if (regex_test(rank_pattern, text)) {
|
||||
rankLength = regex_match(rank_pattern, text).length;
|
||||
data["dataset"]["rank"].val = rankLength;
|
||||
data["dataset"]["rank"].max = rankLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
if (Object.keys(data["dataset"]["attr"]).length > 0) {
|
||||
data.qtype = "attr"
|
||||
data.stype = "AM1"
|
||||
} else if (Object.keys(data["dataset"]["rank"]).length > 0) {
|
||||
data.qtype = "rank"
|
||||
data.stype = "RM1"
|
||||
data.dataset.multi = Object.assign({}, data.dataset.single);
|
||||
data.dataset.single = {}
|
||||
} else {
|
||||
be_scale = Object.keys(data["dataset"]["scale"]).length > 0 ? 1 : 0
|
||||
be_multi = Object.keys(data["dataset"]["multi"]).length > 0 ? 1 : 0
|
||||
be_single = Object.keys(data["dataset"]["single"]).length > 0 ? 1 : 0
|
||||
if (be_scale + be_multi + be_single > 1) {
|
||||
data.qtype = "com"
|
||||
data.stype = "CM1"
|
||||
} else {
|
||||
if (be_single) {
|
||||
data.qtype = "single"
|
||||
data.stype = "SM2"
|
||||
} else if (be_multi) {
|
||||
data.qtype = "multi"
|
||||
data.stype = "MM2"
|
||||
} else if (be_scale) {
|
||||
data.qtype = "scale"
|
||||
data.stype = "SCM1"
|
||||
} else {
|
||||
data.qtype = "open"
|
||||
data.stype = "OM1"
|
||||
data["dataset"]["open"] = { "1": { "label": "", "must": "1" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
var module = {
|
||||
setTarget: setTarget,
|
||||
setPattern: setPattern,
|
||||
removeTag: removeTag, // 불필요한 태그 삭제
|
||||
createSeperation: createSeperation, // 문항 분할
|
||||
optimize_html: optimize_html,
|
||||
each_analysis: each_analysis,
|
||||
analysis_html: analysis_html, // html 분석
|
||||
variable_array: variable_array, // 출력된 변수
|
||||
vp: vp,
|
||||
cp: cp
|
||||
}
|
||||
|
||||
return module
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user