353 lines
12 KiB
JavaScript
353 lines
12 KiB
JavaScript
|
|
window.kon = window.kon || {}
|
|
|
|
|
|
function contextmenu() {
|
|
id = event.target.id
|
|
switch (id) {
|
|
case "modifybtn":
|
|
kon.exceltable.modifyContext();
|
|
break;
|
|
case "savebtn":
|
|
kon.exceltable.saveContext();
|
|
break;
|
|
case "mergebtn":
|
|
kon.exceltable.mergeContext();
|
|
break;
|
|
case "copybtn":
|
|
kon.exceltable.selectedCopy();
|
|
break;
|
|
case "clearbtn":
|
|
kon.exceltable.mergeClear();
|
|
break;
|
|
}
|
|
kon.exceltable.context_off()
|
|
}
|
|
|
|
kon.exceltable = (function () {
|
|
var target,
|
|
st, ot,
|
|
sx, sy,
|
|
ox, oy,
|
|
td,
|
|
context, context_x, context_y
|
|
|
|
function setTable(table) {
|
|
if (target) clearexcel();
|
|
target = table;
|
|
td = target.querySelectorAll("td,th");
|
|
|
|
var tr = target.querySelectorAll("tr");
|
|
var tc = 0;
|
|
|
|
$(tr[0]).find("td,th").each(function (id, el) { tc += el.colSpan });
|
|
|
|
target.setAttribute("oncontextmenu", "return false;")
|
|
var cc = Array(tr.length).fill().map(function () { return new Array(tc).fill(0) })
|
|
|
|
tr.forEach(function (r, index) {
|
|
var coln = 1;
|
|
|
|
cc[index].some(function (num) {
|
|
if (num === 0)
|
|
return true;
|
|
else
|
|
coln += num;
|
|
})
|
|
|
|
r.querySelectorAll("td,th").forEach(function (t, _id) {
|
|
while (cc[index][coln]) {
|
|
coln += cc[index][coln];
|
|
}
|
|
|
|
if (t.getAttribute("data-cell") !== "none") {
|
|
setEventListener(t)
|
|
}
|
|
t.setAttribute("data-col", "tdcol-" + coln)
|
|
|
|
if (t.rowSpan > 1) {
|
|
for (var ix = index; ix < (index + t.rowSpan); ix++)
|
|
cc[ix][coln] = t.colSpan;
|
|
|
|
coln += t.colSpan
|
|
} else {
|
|
if (cc[index][coln] === 0) coln += t.colSpan;
|
|
}
|
|
})
|
|
r.setAttribute("data-row", "trow-" + index)
|
|
})
|
|
|
|
var csshtml = document.getElementsByTagName('style')[0].innerHTML
|
|
|
|
if (csshtml.indexOf(".selectedcell") === -1) {
|
|
csshtml += ".selectedcell {background-color:#d7e9fa;}"
|
|
|
|
document.getElementsByTagName('style')[0].innerHTML = csshtml;
|
|
}
|
|
}
|
|
|
|
function selectedCopy() {
|
|
var copytext = ""
|
|
var t = createnode("textarea", "", { "style": "position:absolute;left:-99999px" });
|
|
document.body.appendChild(t);
|
|
document.execCommand("copy");
|
|
$(target).find("tr:has(.selectedcell)").each(function (id, ele) {
|
|
copytext += "<tr>"
|
|
$(ele).find(".selectedcell").each(function (idx, element) {
|
|
copytext += element.outerHTML
|
|
});
|
|
copytext += "</tr>"
|
|
})
|
|
copytext = "<table>" + copytext + "</table>"
|
|
t.value = copytext
|
|
t.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(t);
|
|
|
|
var item = $('.item');
|
|
item.addClass('is-visible');
|
|
setTimeout(function () {
|
|
item.addClass('is-open');
|
|
}, 20);
|
|
item.removeClass('is-open');
|
|
item.one('transitionend', function (e) {
|
|
item.removeClass('is-visible');
|
|
});
|
|
}
|
|
|
|
function setEventListener(t) {
|
|
t.addEventListener("selectstart", function (e) { e.preventDefault() });
|
|
t.addEventListener("mousedown", listenfirst, true)
|
|
t.addEventListener("mousedown", context_off, true)
|
|
t.addEventListener("mousedown", showmenu, true)
|
|
t.addEventListener("mouseup", clearEventover, true)
|
|
}
|
|
|
|
function removeEventListener(t) {
|
|
t.removeEventListener("mousedown", listenfirst, true)
|
|
t.removeEventListener("mousedown", context_off, true)
|
|
t.removeEventListener("mousedown", showmenu, true)
|
|
t.removeEventListener("mouseup", clearEventover, true)
|
|
}
|
|
|
|
function SelectorRemove(s) {
|
|
s.each(function (id, el) {
|
|
removeEventListener(el);
|
|
})
|
|
}
|
|
|
|
function removeExcel() {
|
|
target = "";
|
|
td = "";
|
|
context.remove();
|
|
}
|
|
|
|
function clearEventover() { $(td).off("mouseover") }
|
|
|
|
function getTable() { return target }
|
|
|
|
function modifyContext() {
|
|
$(".selectedcell").each(function (_index, ele) {
|
|
//2022-02-16 박준성
|
|
var text = ele.innerHTML
|
|
//var text = ele.innerText
|
|
//ele.removeEventListener("mousedown",listenfirst,true);
|
|
if (_index === 0) {
|
|
var offset = ele.getBoundingClientRect()
|
|
_x = offset.left + "px"; //
|
|
_y = offset.top + "px"; //
|
|
var a = createnode("div",
|
|
[
|
|
{
|
|
"element": "button",
|
|
"text": "저장",
|
|
"attr": {
|
|
"onclick": "contextmenu()",
|
|
"id": "savebtn",
|
|
"style": "width:200px",
|
|
"class": "btn btn-sm btn-primary",
|
|
"type": "button"
|
|
}
|
|
}
|
|
],
|
|
{ "style": "position:fixed;top:" + _y + ";right:" + _x + "" }
|
|
)
|
|
$("body").append(a)
|
|
}
|
|
|
|
$(ele)
|
|
.removeClass("selectedcell")
|
|
.addClass("editabled")
|
|
.text("")
|
|
.append(createnode("input", "", { "type": "text", "value": text, "style": "width:100%" }))
|
|
})
|
|
$(".editabled:first input[type=text]").focus();
|
|
}
|
|
|
|
function saveContext() {
|
|
$(target).find("tr:has(.editabled)").find(".editabled").each(function (index, element) {
|
|
var a = $(element)
|
|
.removeClass("editabled")
|
|
.find("input")
|
|
var text = a.val();
|
|
a.remove();
|
|
$(element).text(text);
|
|
})
|
|
event.target.remove();
|
|
}
|
|
|
|
function mergeContext() {
|
|
var col = 0, row = 0
|
|
if ($(".selectedcell").length === 1) {
|
|
alert("하나의 셀은 병합하실 수 없습니다.")
|
|
return false;
|
|
}
|
|
$(".selectedcell:first").closest("tr").find(".selectedcell").each(function (id, ele) {
|
|
col += ele.colSpan
|
|
})
|
|
$("tr:has(.selectedcell)").find(".selectedcell:first").each(function (id, ele) {
|
|
row += ele.rowSpan;
|
|
})
|
|
|
|
|
|
var sf = $(".selectedcell:first").attr("colspan", col).attr("rowspan", row);
|
|
$(".selectedcell:gt(0)")
|
|
.each(function (_index, element) { sf.text(sf.text() + element.innerText) })
|
|
.remove();
|
|
|
|
td = target.querySelectorAll("td,th")
|
|
}
|
|
|
|
function mergeClear() {
|
|
var t = $(".selectedcell");
|
|
var text = t.text();
|
|
var first = "";
|
|
var cols = parseInt(t.attr("colspan")) || 1;
|
|
var rows = parseInt(t.attr("rowspan")) || 1;
|
|
var col = parseInt(t.attr("data-col").split("-")[1]);
|
|
var tr = t.closest("tr")
|
|
var prevflag;
|
|
t.removeAttr("colspan").removeAttr("rowspan").remove();
|
|
|
|
for (var ii = 1; ii <= rows; ii++) {
|
|
prevflag = false;
|
|
for (var i = col; i >= 1; i--) {
|
|
if (tr.find("td[data-col=tdcol-" + i + "]").length === 0)
|
|
continue;
|
|
else {
|
|
prevflag = true;
|
|
t = tr.find("td[data-col=tdcol-" + i + "]");
|
|
break;
|
|
}
|
|
}
|
|
for (var i = cols; i >= 1; i--) {
|
|
var cd = createnode("td", "", { "data-col": "tdcol-" + parseInt(i - 1 + col) });
|
|
if (!first) first = cd
|
|
setEventListener(cd);
|
|
prevflag ? t.after(cd) : tr.prepend(cd)
|
|
}
|
|
tr = tr.next();
|
|
if (!tr.length) break;
|
|
}
|
|
td = target.querySelectorAll("td,th")
|
|
first.innerText = text;
|
|
}
|
|
|
|
|
|
function makeContext(id, selBtn) {
|
|
/* E - 수정, C - 복사, M - 병합, U - 병합 해제 */
|
|
selBtn = selBtn || "ECMU"
|
|
context = document.createElement("div");
|
|
context.id = id;
|
|
context.style.dispaly = "none"
|
|
context.style.display = "none"
|
|
context.style.position = "fixed"
|
|
context.style.textAlign = "center"
|
|
context.style.background = "#fff"
|
|
context.style.boxShadow = "1px 1px 5px 0 rgba(0, 0, 0, 0.54)"
|
|
|
|
var btn = []
|
|
|
|
|
|
if(selBtn.indexOf("E") != -1 || selBtn === "" ) btn.push(createnode("Button", "수정", { "class": "btn btn-sm btn-outline-primary m-1", "type": "button", "onclick": "contextmenu()", "id": "modifybtn", "style": "display:block;width:75px" }))
|
|
if(selBtn.indexOf("C") != -1 || selBtn === "" ) btn.push(createnode("Button", "복사", { "class": "btn btn-sm btn-outline-primary m-1", "type": "button", "onclick": "contextmenu()", "id": "copybtn", "style": "display:block;width:75px" }))
|
|
if(selBtn.indexOf("M") != -1 || selBtn === "" ) btn.push(createnode("Button", "병합", { "class": "btn btn-sm btn-outline-success m-1", "type": "button", "onclick": "contextmenu()", "id": "mergebtn", "style": "display:block;width:75px" }))
|
|
if(selBtn.indexOf("U") != -1 || selBtn === "" ) btn.push(createnode("Button", "병합해제", { "class": "btn btn-sm btn-outline-danger m-1", "type": "button", "onclick": "contextmenu()", "id": "clearbtn", "style": "display:block;width:75px" }))
|
|
appendChild_m(context, btn)
|
|
document.body.appendChild(context);
|
|
}
|
|
|
|
function showmenu() {
|
|
if (event.button !== 2) return false;
|
|
var offset = event.target.getBoundingClientRect()
|
|
context_x = offset.right + "px"; //
|
|
context_y = offset.bottom + "px"; //
|
|
context.style.display = "block";
|
|
context.style.left = context_x;
|
|
context.style.top = context_y;
|
|
}
|
|
|
|
function clearexcel() {
|
|
target.querySelectorAll("td,th").forEach(function (t) {
|
|
removeEventListener(t);
|
|
})
|
|
}
|
|
|
|
function context_off() {
|
|
if (context.style.display === "block")
|
|
context.style.display = "none"
|
|
}
|
|
|
|
function listenfirst() {
|
|
context_off();
|
|
if (event.button) return false;
|
|
sx = 0
|
|
sy = 0
|
|
$(td).on("mouseover", listenmove);
|
|
$(".selectedcell").removeClass("selectedcell");
|
|
st = event.target;
|
|
sx = parseInt(st.getAttribute("data-col").split("-")[1]);
|
|
sy = parseInt(st.closest("tr").getAttribute("data-row").split("-")[1]);
|
|
st.classList.add("selectedcell");
|
|
}
|
|
|
|
function listenmove() {
|
|
ot = event.target;
|
|
ox = 0
|
|
oy = 0
|
|
$(".selectedcell").removeClass("selectedcell");
|
|
oy = parseInt(ot.closest("tr").getAttribute("data-row").split("-")[1]);
|
|
ox = parseInt(ot.getAttribute("data-col").split("-")[1]);
|
|
for (var fy = Math.min(oy, sy); fy <= Math.max(oy, sy); fy++) {
|
|
var tt = target.querySelector("[data-row=trow-" + fy + "]");
|
|
if (!tt) continue;
|
|
for (var fx = Math.min(ox, sx); fx <= Math.max(ox, sx); fx++) {
|
|
var td = tt.querySelector("[data-col=tdcol-" + fx + "]")
|
|
if (td)
|
|
td.classList.add("selectedcell")
|
|
}
|
|
}
|
|
}
|
|
|
|
var module = {
|
|
setTable: setTable,
|
|
getTable: getTable,
|
|
makeContext: makeContext,
|
|
showmenu: showmenu,
|
|
clearexcel: clearexcel,
|
|
context_off: context_off,
|
|
listenfirst: listenfirst,
|
|
listenmove: listenmove,
|
|
modifyContext: modifyContext,
|
|
saveContext: saveContext,
|
|
mergeContext: mergeContext,
|
|
mergeClear: mergeClear,
|
|
SelectorRemove: SelectorRemove,
|
|
removeExcel: removeExcel,
|
|
selectedCopy: selectedCopy
|
|
}
|
|
return module;
|
|
|
|
})();
|
|
|