From 5eaf389d3c727ae5b52c5aa0911902387c609e85 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Sat, 19 Dec 2020 13:09:38 -0800 Subject: [PATCH] Added functions for inserting rows and columns --- src/js/editor.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/js/editor.js b/src/js/editor.js index f9ca2bb..3af366e 100644 --- a/src/js/editor.js +++ b/src/js/editor.js @@ -86,6 +86,113 @@ function showTab(tab_name) { $("#"+tab_name.replace("-tab", "-nav")).addClass("active"); } +function insertColumn(table_id, index) { + if (index < 0){return;} + for (let i=0; i < tables_data.length; i++) { + if (tables_data[i]["id"] === table_id) { + let col_ids = getColumnIDs(tables_data[i]["col-def"]); + let col_num = 0; + for (let id of col_ids) { + if (col_ids.includes(`column${col_num}`)) { + col_num++; + } else {break;} + } + let col_id = `column${col_num}` + tables_data[i]["col-def"].splice(index, 0,{ + "field": col_id, + "title": "New Column", + "sortable": false, + "selectable": false, + }) + + let num_cols = $(`#${table_id} > thead > tr`).children().length; + if (index >= num_cols) { + index = num_cols-1; + $.fn.insert = function (data) { + this.after(data); + }; + } else { + $.fn.insert = function (data) { + this.before(data); + }; + } + $(`#${table_id} > thead > tr > th:nth-child(${index + 1})`) + .insert($("") + .attr("id", table_id + "-" + col_id) + .text("New Column")); + + $(`#${table_id} > tbody > tr`).each((j, tr) => { + tr = $(tr); + tr.children(`td:nth-child(${index + 1})`) + .insert($("") + .attr("id", table_id+"-"+col_id+"-"+tables_data[i]["rows"][j]["uid"]) + .text("")) + }); + break; + } + } +} + +function insertRow(table_id, index) { + if (index < 0){return;} + for (let i=0; i < tables_data.length; i++) { + if (tables_data[i]["id"] === table_id) { + let row_ids = getRowIDs(tables_data[i]["rows"]); + let row_num = 0; + for (let id of row_ids) { + if (row_ids.includes(`row${row_num}`)) { + row_num++; + } else {break;} + } + let row_id = `row${row_num}` + let col_ids = getColumnIDs(tables_data[i]["col-def"]); + let row_data = {"uid": row_id}; + for (let id of col_ids) { + row_data[id] = ""; + } + tables_data[i]["rows"].splice(index, 0, row_data); + + let num_rows = $(`#${table_id} > tbody > tr`).length; + if (index >= num_rows) { + index = num_rows-1; + $.fn.insert = function (data) { + this.after(data); + }; + } else { + $.fn.insert = function (data) { + this.before(data); + }; + } + let tr = $("") + .attr("id", table_id+"-"+row_id); + for (let col_id of col_ids) { + tr.append($("") + .attr("id", table_id+"-"+col_id+"-"+row_id) + .text("")) + } + $(`#${table_id} > tbody > tr:nth-child(${index+1})`) + .insert(tr); + break; + } + } +} + +function getColumnIDs(columns) { + let ids = []; + for (let col of columns) { + ids.push(col["field"]); + } + return ids; +} + +function getRowIDs(rows) { + let ids = []; + for (let row of rows) { + ids.push(row["uid"]); + } + return ids; +} + ipcRenderer.on("open-default", (event) => { openFile("default.json"); })