diff --git a/src/js/script.js b/src/js/script.js index aa4b170..1ab49c2 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -32,34 +32,35 @@ function makeTables() { let table_index = data.indexOf(table); // create nav element - $("nav").append(``) + $("nav").append(``); let table_elm = $("
"); table_elm.attr("id", name+"-table"); let cols = table["col-def"] let tr = $(""); - let td = $("") - td.css("border", "none") + let td = $(""); + td.css("border", "none"); if (data.length > 1) { - td.html(``) + td.html(``); } - tr.append(td) + tr.append(td); let index = 0; for (let col of cols) { - let th = $(``); - th.attr("id", name+"-"+index.toString()) + let id = name+"-"+index.toString(); + let th = $(``); + th.attr("id", id); th.append(`${col.title}`); - th.append(`×`) + th.append(`×`); tr.append(th); index++; } - td = $("") - td.css("border", "none") - td.html(``) - tr.append(td) + td = $(""); + td.css("border", "none"); + td.html(``); + tr.append(td); table_elm.append(tr); for (let row of table["rows"]) { let index = table["rows"].indexOf(row); @@ -68,7 +69,7 @@ function makeTables() { td = $(""); td.css("border", "none"); td.append(``); - td.append(`
`) + td.append(`
`); tr.append(td); for (let col of cols) { @@ -97,7 +98,7 @@ function makeTables() { } function removeTables() { - $("nav").empty(); + $("#nav").empty(); $("#tables").empty(); } @@ -216,9 +217,17 @@ function addTable() { function selectCell(table_name, title, field, cell_id) { let elm = $("#"+cell_id); + if (elm.is("th")) { + $("#cell-text").prop("disabled", true); + $("#cell-text").val(""); + } + else { + $("#cell-text").prop("disabled", false); + } selected = {table_name:table_name,title:title,field:field, cell:cell_id}; current_col = {"title":title, "field":field}; $("td").removeClass("w3-green"); + $("th").removeClass("w3-green"); elm.addClass("w3-green"); for (let table of data) { if (table["name"] == table_name) { @@ -228,7 +237,7 @@ function selectCell(table_name, title, field, cell_id) { for (let col of table["col-def"]) { if (col["title"] == title) { $("#col-sortable").prop("checked", col["sortable"]); - $("#col-selectable").prop("checked", col["selectable"]) + $("#col-selectable").prop("checked", col["selectable"]); } } for (let row of table["rows"]) { @@ -259,15 +268,15 @@ function save() { } } if (!table_exsits && table_name.length > 0) { - selected["cell"] = selected["cell"].replace(sanitizeName(table["name"]), sanitizeName(table_name)) + selected["cell"] = selected["cell"].replace(sanitizeName(table["name"]), sanitizeName(table_name)); table["name"] = table_name; for (let row of table["rows"]) { let match = row["uid"].match(uid_regex); - row["uid"] = `${sanitizeName(table_name)}-row${match[2]}` + row["uid"] = `${sanitizeName(table_name)}-row${match[2]}`; } - $("#table-name").removeClass("w3-red") + $("#table-name").removeClass("w3-red"); } else { - $("#table-name").addClass("w3-red") + $("#table-name").addClass("w3-red"); return; } } @@ -292,9 +301,9 @@ function save() { selected["cell"] = replaceLast(selected["cell"], col["field"], field); col["title"] = title; col["field"] = field; - $("#col-name").removeClass("w3-red") + $("#col-name").removeClass("w3-red"); } else { - $("#col-name").addClass("w3-red") + $("#col-name").addClass("w3-red"); return; } } @@ -410,7 +419,7 @@ function tableDrop(event, new_index) { } function showTable(table_name) { - let reset = false + let reset = false; if (current_table != table_name) { current_table = table_name; reset = true; @@ -428,7 +437,7 @@ function showTable(table_name) { let child = $($($("#"+table_name+"-table").children()[1]).children()[1]); selectCell(table["name"], table["col-def"][0]["title"], table["col-def"][0]["field"], child.attr("id")); } else { - let elm = $(`#${selected["cell"]}`) + let elm = $(`#${selected["cell"]}`); selectCell(table["name"], current_col["title"], current_col["field"], selected["cell"]); } } @@ -438,26 +447,67 @@ function showTable(table_name) { function startEditCell(cell_id) { let cell = $(`#${cell_id}`); let cell_text = $("#cell-text").val(); - let text_area = $(``); - cell.parent().css("height", `${cell.outerHeight()}px`) - cell.replaceWith(text_area); - text_area.children()[0].focus(); + let td = $(``); + let text_area = $(``); + text_area.focusout(function (event){finishEditCell(event, cell_id)}); + td.append(text_area); + cell.parent().css("height", `${cell.outerHeight()}px`); + cell.replaceWith(td); + text_area.focus(); +} + +function startEditHeader(cell_id) { + let cell = $(`#${cell_id}`); + let cell_text = $("#col-name").val(); + let th = $(``); + let text_area = $(``); + text_area.focusout(function (event){finishEditHeader(event, cell_id)}); + th.append(text_area); + cell.parent().css("height", `${cell.outerHeight()}px`); + cell.replaceWith(th); + text_area.focus(); } function finishEditCell(event, cell_id) { - if (event.ctrlKey && event.key == "Enter" || event.type == "focusout") { - let text_area_val = $(`#${cell_id}-edit`).val(); + let cell = $(`#${cell_id}-edit`); + if (event.ctrlKey && event.key == "Enter") { + cell.off("focusout"); + let text_area_val = cell.val(); $("#cell-text").val(text_area_val); save(); } else if (event.key == "Escape") { + cell.off("focusout"); let cell_val = $("#cell-text").val(); + cell.val(cell_val); + save(); + } else if (event.type == "focusout") { + let text_area_val = cell.val(); + $("#cell-text").val(text_area_val); + save(); + } +} + +function finishEditHeader(event, cell_id) { + let cell = $(`#${cell_id}-edit`); + if (event.ctrlKey && event.key == "Enter") { + cell.off("focusout"); + let text_area_val = $(`#${cell_id}-edit`).val(); + $("#col-name").val(text_area_val); + save(); + } else if (event.key == "Escape") { + cell.off("focusout"); + let cell_val = $("#col-name").val(); $(`#${cell_id}-edit`).val(cell_val); save(); + } else if (event.type == "focusout") { + let text_area_val = $(`#${cell_id}-edit`).val(); + $("#col-name").val(text_area_val); + save(); } } function saveFile(new_file) { - ipcRenderer.send("save", data, new_file) + ipcRenderer.send("save", data, new_file); } function openFile(file_path) {