Moved generating the nav bar and tab content to the website

Fixed issue with loading local files within the packaged app
Fixed issue with 1.0.0 schema tab name
This commit is contained in:
Matthew Welch 2021-01-12 20:51:15 -08:00
parent f6630ff659
commit 7d444006d1
4 changed files with 19 additions and 109 deletions

View File

@ -1,7 +1,7 @@
{
"name": "webeditor",
"productName": "webeditor",
"version": "1.0.0",
"version": "1.1.0",
"description": "My Electron application description",
"main": "src/index.js",
"scripts": {

View File

@ -1,13 +1,14 @@
const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron');
const Store = require("electron-store");
const fs = require("fs");
const path = require("path");
const util = require('util')
let Validator = require("jsonschema").Validator;
let validator = new Validator();
let all_schemas = [
"src/schemas/current_schema.json",
"src/schemas/1.0.0_schema.json",
path.join(app.getAppPath(), "src/schemas/current_schema.json"),
path.join(app.getAppPath(), "src/schemas/1.0.0_schema.json"),
]
let schema_updaters = {
"1.0.0_schema.json": update_1_0_0_schema
@ -103,14 +104,13 @@ function createSplashScreen() {
store.set("recent_files", []);
}
splash_screen.loadFile('src/splash.html');
splash_screen.loadFile(path.join(app.getAppPath(), "src/splash.html"));
// Open the DevTools.
// splash_screen.webContents.openDevTools();
// store.openInEditor();
}
function createEditorWindow() {
function createEditorWindow(json) {
let main_window = new BrowserWindow({
width: 1000,
height: 600,
@ -118,11 +118,14 @@ function createEditorWindow() {
webPreferences: {
nodeIntegration: true
}
});
})
main_window.maximize();
updateMenuBar();
main_window.loadFile("src/editor.html");
main_window.loadFile(path.join(app.getAppPath(), "src/editor.html"))
.then(() => {
main_window.webContents.send("open", json);
});
// Open the DevTools.
// main_window.webContents.openDevTools();
return main_window;
@ -174,10 +177,7 @@ function openFile(file_path="", new_file=false) {
current_file = file_path
if (new_file) {
let old_window = BrowserWindow.getFocusedWindow();
let window = createEditorWindow();
window.once("ready-to-show", () => {
window.webContents.send("open", json);
})
createEditorWindow(json);
old_window.close();
} else {
let window = BrowserWindow.getFocusedWindow();
@ -196,99 +196,7 @@ function saveFile(json_data, create_new_file) {
}
}
if (current_file) {
let tables_data = json_data["tables"]
fs.writeFileSync(current_file, JSON.stringify(json_data, null, 4));
let nav_content_path = current_file.replace(".json", "-nav-content.html");
let tab_content_path = current_file.replace(".json", "-tab-content.html");
let nav = "";
let tabs = "";
let table_tabs = {"": []};
let tables_added = [];
for (let table_data of tables_data) {
if (table_data["tab_name"] === "") {
table_tabs[""].push(table_data["id"]);
} else {
if (!Object.keys(table_tabs).includes(table_data["tab_name"])) {
table_tabs[table_data["tab_name"]] = [table_data];
} else {
table_tabs[table_data["tab_name"]].push(table_data);
}
}
}
for (let table_data of tables_data) {
let table_id = table_data["id"];
if (table_data["hidden"]) {continue;}
if (tables_added.includes(table_id)) {continue;}
let li;
if (table_tabs[""].includes(table_id)) {
li = $("<li>").addClass("nav-item");
let tab_link = $("<a>")
.attr({"id": table_id+"-tab",
"class": "nav-link",
"data-bs-toggle": "tab",
"href": "#"+table_id,
"role": "tab",
"aria-controls": table_id,
"aria-selected": false})
.html(table_data["name"]);
li.append(tab_link);
let div = $("<div>")
.attr({"id": table_id,
"class": "tab-pane fade",
"role": "tabpanel",
"aria-labelledby": table_id+"-tab"})
let h3 = $("<h3>")
.attr("id", table_id+"-header")
.html(`${table_data["name"]} (<span id="${table_id}-completed"></span>/<span id="${table_id}-total"></span>)`);
div.append(h3)
.append(`<p>${table_data["description"]}</p>`)
.append(`<table id="${table_data["name"]}-table" class="table table-hover"></table>`);
tabs += div.prop("outerHTML");
tables_added.push(table_id)
} else {
li = $("<li>").addClass("nav-item dropdown");
let a = $("<a>")
.attr({"class": "nav-link dropdown-toggle",
"href": "#",
"data-bs-toggle": "dropdown",
"role": "button",
"aria-expanded": false})
.html(table_data["tab_name"]);
li.append(a)
let ul = $("<ul>").addClass("dropdown-menu");
for (let table of table_tabs[table_data["tab_name"]]) {
let nav_link = $("<li>").append($("<a>")
.attr({"id": table["id"]+"-tab",
"class": "dropdown-item",
"data-bs-toggle": "tab",
"href": "#"+table["id"],
"role": "tab",
"aria-controls": table["id"],
"aria-selected": false})
.html(table["name"]));
ul.append(nav_link);
let div = $("<div>")
.attr({"id": table["id"],
"class": "tab-pane fade",
"role": "tabpanel",
"aria-labelledby": table["id"]+"-tab"})
let h3 = $("<h3>")
.attr("id", table["id"]+"-header")
.html(`${table["name"]} (<span id="${table["id"]}-completed"></span>/<span id="${table["id"]}-total"></span>)`);
div.append(h3)
.append(`<p>${table["description"]}</p>`)
.append(`<table id="${table["name"]}-table" class="table table-hover"></table>`);
tabs += div.prop("outerHTML");
tables_added.push(table["id"]);
}
li.append(ul);
}
nav += li.prop("outerHTML");
}
fs.writeFileSync(nav_content_path, nav);
fs.writeFileSync(tab_content_path, tabs);
}
}
@ -336,7 +244,7 @@ function checkFile(file_path) {
let schema = loadJson(schema_path);
result = validator.validate(json, schema);
if (result.valid) {
let split_schema = schema_path.split("/");
let split_schema = schema_path.split(/[\\/]/);
result = {"schema": split_schema[split_schema.length-1], "result": result};
valid_result = true;
break;
@ -359,6 +267,8 @@ function getUpdatedJson(validation_result) {
function update_1_0_0_schema(json) {
$.each(json, (i, table_data) => {
table_data["id"] = "table"+i;
table_data["tab_name"] = table_data["tab-name"];
delete table_data["tab-name"];
let col_dict = {};
$.each(table_data["col-def"], (j, col_data) => {
col_dict[col_data["field"]] = "column"+j;

View File

@ -60,10 +60,10 @@ function refreshPage() {
function generateTable(table_data, table_index) {
const table_id = table_data["id"];
let a = $("<a></a>")
let a = $("<div>")
.attr("id", table_id + "-nav")
.addClass("nav-link table-nav")
.attr("href", "#" + table_id)
.addClass("btn nav-link table-nav")
// .attr("href", "#" + table_id)
.text(table_data["name"])
.on("click", () => {
showTab(table_id);

View File

@ -6,7 +6,7 @@
"name": {"type": "string"},
"filter": {"type": "boolean"},
"hidden": {"type": "boolean"},
"tab_name": {"type": "string"},
"tab-name": {"type": "string"},
"description": {"type": "string"},
"col-def": {
"type": "array",