From 243621f3b2b8597e5ea2f39b1c538884fd6f957a Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Thu, 9 Feb 2023 00:45:54 +0300 Subject: [PATCH] feat(web): add TFM settings page Only setting TFM database name by now --- web/public/css/general.css | 4 ++ web/public/js/tfm-database.js | 9 ++- web/public/js/tfm-files.js | 2 +- web/public/js/tfm-management.js | 34 +++++----- web/public/js/tfm-settings.js | 67 ++++++++++++++++++++ web/public/js/tfm-tags.js | 2 +- web/public/tfm/index.html | 5 +- web/public/tfm/settings.html | 108 ++++++++++++++++++++++++++++++++ web/server/web-server.go | 2 +- 9 files changed, 211 insertions(+), 22 deletions(-) create mode 100644 web/public/js/tfm-settings.js create mode 100644 web/public/tfm/settings.html diff --git a/web/public/css/general.css b/web/public/css/general.css index 9944e49..dd0b39a 100644 --- a/web/public/css/general.css +++ b/web/public/css/general.css @@ -121,6 +121,10 @@ form { color: #bbb !important; } +td { + vertical-align: top; +} + .button-flex { display: flex; flex-direction: row; diff --git a/web/public/js/tfm-database.js b/web/public/js/tfm-database.js index 33a3ef6..1685ccb 100644 --- a/web/public/js/tfm-database.js +++ b/web/public/js/tfm-database.js @@ -1,6 +1,11 @@ +var db_name = localStorage["tfm_db_name"]; +if (db_name == null) { + location.href = "/tfm/settings"; +} + $(document).on("click", "#btn-save", function (e) { e.preventDefault(); - let resp = tdb_query("$TFM", 4, ""); + let resp = tdb_query(db_name, 4, ""); if (resp.status) { alert("Successfully saved!"); } else { @@ -13,7 +18,7 @@ $(document).on("click", "#btn-discard", function (e) { if (!confirm("All unsaved changes will be lost permanently. Are you sure?")) { return; } - let resp = tdb_query("$TFM", 2, ""); + let resp = tdb_query(db_name, 2, ""); if (resp.status) { alert("Successfully reloaded database!"); } else { diff --git a/web/public/js/tfm-files.js b/web/public/js/tfm-files.js index d375468..8f0672a 100644 --- a/web/public/js/tfm-files.js +++ b/web/public/js/tfm-files.js @@ -20,7 +20,7 @@ $(window).on("load", function () { $(document).on("submit", "#menu-add form", function (e) { e.preventDefault(); - let resp = tdb_query("$TFM", 18, $("#new-name").val()); + let resp = tdb_query(db_name, 18, $("#new-name").val()); if (!resp.status) { alert("Something went wrong!"); return; diff --git a/web/public/js/tfm-management.js b/web/public/js/tfm-management.js index 0dddc32..270bbce 100644 --- a/web/public/js/tfm-management.js +++ b/web/public/js/tfm-management.js @@ -1,3 +1,7 @@ +var db_name = localStorage["tfm_db_name"]; +if (db_name == null) { + location.href = "/tfm/settings"; +} var sasahyou = null, sappyou = null, shoppyou = null; if (localStorage["sasahyou"] != null) { sasahyou = JSON.parse(localStorage["sasahyou"]); @@ -23,13 +27,13 @@ var current_sasa_index = -1; var menu_count = 0; function sasahyou_load() { - let db_info = tdb_query("$TFM", 0, ""); + let db_info = tdb_query(db_name, 0, ""); if (db_info == null || !db_info.status) { alert("Failed to fetch TFM database"); throw new Error("Failed to fetch TFM database"); } if (sasahyou == null || sasahyou_mts !== db_info.data[0].sasahyou.mts) { - let resp = tdb_query("$TFM", 16, ""); + let resp = tdb_query(db_name, 16, ""); if (resp == null || !resp.status) { alert("Failed to get sasahyou"); throw new Error("Failed to get sasahyou"); @@ -42,13 +46,13 @@ function sasahyou_load() { } function sappyou_load() { - let db_info = tdb_query("$TFM", 0, ""); + let db_info = tdb_query(db_name, 0, ""); if (db_info == null || !db_info.status) { alert("Failed to fetch TFM database"); throw new Error("Failed to fetch TFM database"); } if (sappyou == null || sappyou_mts !== db_info.data[0].sappyou.mts) { - let resp = tdb_query("$TFM", 32, ""); + let resp = tdb_query(db_name, 32, ""); if (resp == null || !resp.status) { alert("Failed to get sappyou"); throw new Error("Failed to get sappyou"); @@ -61,13 +65,13 @@ function sappyou_load() { } function shoppyou_load() { - let db_info = tdb_query("$TFM", 0, ""); + let db_info = tdb_query(db_name, 0, ""); if (db_info == null || !db_info.status) { alert("Failed to fetch TFM database"); throw new Error("Failed to fetch TFM database"); } if (shoppyou == null || shoppyou_mts !== db_info.data[0].shoppyou.mts) { - let resp = tdb_query("$TFM", 8, ""); + let resp = tdb_query(db_name, 8, ""); if (resp == null || !resp.status) { alert("Failed to get shoppyou"); throw new Error("Failed to get shoppyou"); @@ -90,7 +94,7 @@ function menu_view_file_open() { $("#file-name").val(decodeURI(current_sasa.path)); $("#menu-file-view .list-item").css("display", ""); $("#btn-full").attr("href", "/files/" + current_sasa.path); - let resp = tdb_query("$TFM", 24, '' + current_sasa.id); + let resp = tdb_query(db_name, 24, '' + current_sasa.id); if (!resp.status) { alert("Something went wrong!"); return; @@ -122,7 +126,7 @@ function menu_view_tag_open() { $("#menu-tag-view").css("display", "flex"); $("#menu-tag-view .list-item").css("display", ""); $("#tag-name").val(decodeURI(current_tanzaku.name)); - let resp = tdb_query("$TFM", 40, '' + current_tanzaku.id); + let resp = tdb_query(db_name, 40, '' + current_tanzaku.id); if (!resp.status) { alert("Something went wrong!"); return; @@ -324,7 +328,7 @@ $(document).on("reset", "#menu-add form", function (e) { $(document).on("submit", "#menu-file-view form", function (e) { e.preventDefault(); - let resp = tdb_query("$TFM", 24, '' + current_sasa.id); + let resp = tdb_query(db_name, 24, '' + current_sasa.id); if (!resp.status) { alert("Something went wrong!"); return; @@ -332,7 +336,7 @@ $(document).on("submit", "#menu-file-view form", function (e) { resp.data.forEach(tanzaku => { let current = $(`.list-item[tid="${tanzaku.id}"]`); if (!current.hasClass("selected") && - !tdb_query("$TFM", 9, '' + current_sasa.id + ' ' + tanzaku.id).status) { + !tdb_query(db_name, 9, '' + current_sasa.id + ' ' + tanzaku.id).status) { console.log("ERROR: failed to remove kazari: " + current_sasa.id + '-' + tanzaku.id); } }); @@ -341,7 +345,7 @@ $(document).on("submit", "#menu-file-view form", function (e) { if (resp.data.find(t => t.id === tid) != null) { return; } - if (!tdb_query("$TFM", 10, '' + current_sasa.id + ' ' + tid)) { + if (!tdb_query(db_name, 10, '' + current_sasa.id + ' ' + tid)) { console.log("ERROR: failed to add kazari: " + current_sasa.id + '-' + tid); } }); @@ -350,7 +354,7 @@ $(document).on("submit", "#menu-file-view form", function (e) { $(document).on("submit", "#menu-tag-view form", function (e) { e.preventDefault(); - let resp = tdb_query("$TFM", 40, '' + current_tanzaku.id); + let resp = tdb_query(db_name, 40, '' + current_tanzaku.id); if (!resp.status) { alert("Something went wrong!"); return; @@ -358,7 +362,7 @@ $(document).on("submit", "#menu-tag-view form", function (e) { resp.data.forEach(sasa => { let current = $(`.list-item[sid="${sasa.id}"]`); if (!current.hasClass("selected") && - !tdb_query("$TFM", 9, '' + sasa.id + ' ' + current_tanzaku.id).status) { + !tdb_query(db_name, 9, '' + sasa.id + ' ' + current_tanzaku.id).status) { console.log("ERROR: failed to remove kazari: " + sasa.id + '-' + current_tanzaku.id); } }); @@ -367,7 +371,7 @@ $(document).on("submit", "#menu-tag-view form", function (e) { if (resp.data.find(s => s.id === sid) != null) { return; } - if (!tdb_query("$TFM", 10, '' + sid + ' ' + current_tanzaku.id)) { + if (!tdb_query(db_name, 10, '' + sid + ' ' + current_tanzaku.id)) { console.log("ERROR: failed to add kazari: " + sid + '-' + current_tanzaku.id); } }); @@ -379,7 +383,7 @@ $(document).on("click", "#btn-remove", function (e) { if (!confirm("This tag will be removed permanently. Are you sure?")) { return; } - let resp = tdb_query("$TFM", 33, '' + current_tanzaku.id); + let resp = tdb_query(db_name, 33, '' + current_tanzaku.id); if (!resp.status) { alert("Something went wrong!"); return; diff --git a/web/public/js/tfm-settings.js b/web/public/js/tfm-settings.js new file mode 100644 index 0000000..d5d3338 --- /dev/null +++ b/web/public/js/tfm-settings.js @@ -0,0 +1,67 @@ +var db_name = localStorage["tfm_db_name"], + sort_files = localStorage["sort_files"], + sort_tags = localStorage["sort_tags"]; +if (sort_files == null) { + sort_files = "id"; +} +if (sort_tags == null) { + sort_tags = "id"; +} + +function settings_load() { + if (db_name != null) { + $("#db_name").val(db_name); + } + if (sort_files != null) { + if (sort_files[0] === '-') { + $("#files-reverse").prop("checked", true); + sort_files = sort_files.slice(1); + } + $(`#files-by-${sort_files}`).prop("checked", true); + } + if (sort_tags != null) { + if (sort_tags[0] === '-') { + $("#tags-reverse").prop("checked", true); + sort_tags = sort_tags.slice(1); + } + $(`#tags-by-${sort_tags}`).prop("checked", true); + } +} + +$(window).on("load", function () { + settings_load(); +}); + +$(document).on("reset", "#settings", function (e) { + e.preventDefault(); + settings_load(); +}); + +$(document).on("submit", "#settings", function (e) { + e.preventDefault(); + let db_name_input = $("#db_name"); + let db_name_val = db_name_input.val(); + if (db_name_val !== db_name) { + let resp = tdb_query("", 0, ""); + if (!resp.status) { + alert("Failed to fetch databases"); + return; + } + let found = false; + resp.data.every(db => { + if (db.name === db_name_val) { + db_name = db_name_val; + localStorage["tfm_db_name"] = db_name; + found = true; + db_name_input.removeClass("is-invalid"); + return false; + } + return true; + }); + if (!found) { + db_name_input.addClass("is-invalid"); + return; + } + } + alert("Successfully updated settings!"); +}); diff --git a/web/public/js/tfm-tags.js b/web/public/js/tfm-tags.js index 66618a1..1e7008c 100644 --- a/web/public/js/tfm-tags.js +++ b/web/public/js/tfm-tags.js @@ -29,7 +29,7 @@ $(document).on("input", "#text-filter-all", function (e) { $(document).on("submit", "#menu-add form", function (e) { e.preventDefault(); - let resp = tdb_query("$TFM", 34, $("#new-name").val() + '\n' + $("#new-description").val()); + let resp = tdb_query(db_name, 34, $("#new-name").val() + '\n' + $("#new-description").val()); if (!resp.status) { alert("Something went wrong!"); return; diff --git a/web/public/tfm/index.html b/web/public/tfm/index.html index f2b847d..ca818d3 100644 --- a/web/public/tfm/index.html +++ b/web/public/tfm/index.html @@ -36,8 +36,9 @@ Home
- - + + Settings +
diff --git a/web/public/tfm/settings.html b/web/public/tfm/settings.html new file mode 100644 index 0000000..0010732 --- /dev/null +++ b/web/public/tfm/settings.html @@ -0,0 +1,108 @@ + + + + + Settings | Tanabata File Manager + + + + + + + + + + + + + + + + + + + + + + + +

TFM: Settings

+
+
+
+
+ + +
Database not found
+
+ + + + + + + + + +
+
+ Files sorting +
+ + +
+
+ + +
+
+ + +
+
+
+
+ Tags sorting +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+
+ + TFM home + +
+
+
+
+ + + \ No newline at end of file diff --git a/web/server/web-server.go b/web/server/web-server.go index 5e2ebd2..46b3256 100644 --- a/web/server/web-server.go +++ b/web/server/web-server.go @@ -159,7 +159,7 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) { } } log.Println("Got TDBMS response") - if request.TRDB == "$TFM" && (request.TRC == 0b10000 || request.TRC == 0b101000) { + if strings.HasPrefix(strings.ToLower(request.TRDB), "tfm") && (request.TRC == 0b10000 || request.TRC == 0b101000) { var json_response JSON err = json.Unmarshal(response, &json_response) if err != nil {