From 0ee20773bbbeedf0b495c49ff43c6fef852c9788 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sun, 23 Jan 2022 00:24:23 -0300 Subject: [PATCH] Add frontend support --- src/main.rs | 7 ++-- static/index.html | 6 +-- static/index.js | 43 +++++++++++++++++---- static/schemas/{polish.json => Polish.json} | 0 4 files changed, 42 insertions(+), 14 deletions(-) rename static/schemas/{polish.json => Polish.json} (100%) diff --git a/src/main.rs b/src/main.rs index c64ca01..70d6b2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,8 @@ use database::{WordDb, DbError}; const DB_DIR: &str = "/usr/share/inflectived"; const CACHE_DIR: &str = "/var/cache/inflectived"; -const FRONTEND_DIR: &str = "/opt/inflectived"; +//const FRONTEND_DIR: &str = "/opt/inflectived"; +const FRONTEND_DIR: &str = "static"; const MAJOR: i32 = 0; const MINOR: i32 = 1; @@ -84,7 +85,7 @@ async fn main() { } } }, - ("run", _) => { + ("run", matches) => { let figment = rocket::Config::figment() .merge(("address", "0.0.0.0")); @@ -95,7 +96,7 @@ async fn main() { views::get_langs, views::frontend]); - if let Ok(true) = fs::try_exists(FRONTEND_DIR) { + if let Ok(_) = fs::try_exists(FRONTEND_DIR) { app = app.mount("/static", FileServer::from(FRONTEND_DIR)); } diff --git a/static/index.html b/static/index.html index 2c73c74..e3f8b49 100644 --- a/static/index.html +++ b/static/index.html @@ -19,9 +19,9 @@
- + +
diff --git a/static/index.js b/static/index.js index 754db57..cc046a0 100644 --- a/static/index.js +++ b/static/index.js @@ -1,16 +1,43 @@ $(document).ready(() => { - let polishSchemas = null; + let selectedLang = null; + let schema = null; + let langs = null; $.ajax({ - url: '/static/schemas/polish.json', + url: `/langs?installed`, success: data => { - polishSchemas = data - if(window.location.hash) { - getWord(); - } + langs = data; + + $('#langs').html(data.map(lang => ``)); + + setLang($('#langs').val()); } }); + $('#lang').on('change', e => { + console.log(e.target.value); + + let langCode = e.target.value; + + setLang(code); + }); + + function setLang(code) { + let lang = langs.find(lang => lang.code == code); + + selectedLang = lang; + + $.ajax({ + url: `/static/schemas/${lang.name}.json`, + success: data => { + polishSchemas = data + if(window.location.hash) { + getWord(); + } + } + }); + } + const searchBar = $('#search-bar'); const searchForm = $('#search-form'); const ajaxContent = $('#ajax-content'); @@ -23,7 +50,7 @@ $(document).ready(() => { appendTo: '#search-form', source: (request, response) => { $.ajax({ - url: '/langs/pl/words?like=' + request.term + '&limit=20&offset=0', + url: `/langs/${selectedLang.code}/words?like=${request.term}&limit=20&offset=0`, success: data => response(data) }) }, @@ -49,7 +76,7 @@ $(document).ready(() => { document.title = `Inflective - ${decodeURIComponent(word)}`; $.ajax({ - url: '/langs/pl/words/' + word, + url: `/langs/${selectedLang.code}/words/${word}`, success: (data) => { ajaxContent.html(generateHtml(word, data)); diff --git a/static/schemas/polish.json b/static/schemas/Polish.json similarity index 100% rename from static/schemas/polish.json rename to static/schemas/Polish.json