From b3a3cf0808b47195195e56b81abdbf0dbf9c6074 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Fri, 22 Dec 2023 13:28:15 +0100 Subject: [PATCH] Segmented functions for rendering pages in there own file --- html/templates/login_modal.html | 2 +- html/templates/reg_modal.html | 2 +- js/index.js | 2 +- js/scripts/home.js | 9 ++ js/scripts/login.js | 54 +++++++++++ js/scripts/main.js | 154 -------------------------------- js/scripts/menu_mod.js | 42 +++++++++ js/scripts/reg.js | 64 +++++++++++++ 8 files changed, 172 insertions(+), 157 deletions(-) create mode 100644 js/scripts/home.js create mode 100644 js/scripts/login.js delete mode 100644 js/scripts/main.js create mode 100644 js/scripts/menu_mod.js create mode 100644 js/scripts/reg.js diff --git a/html/templates/login_modal.html b/html/templates/login_modal.html index 0090f82..9a83636 100644 --- a/html/templates/login_modal.html +++ b/html/templates/login_modal.html @@ -3,7 +3,7 @@
- +
diff --git a/html/templates/reg_modal.html b/html/templates/reg_modal.html index 09aadfa..eea3f8e 100644 --- a/html/templates/reg_modal.html +++ b/html/templates/reg_modal.html @@ -4,7 +4,7 @@

- +
diff --git a/js/index.js b/js/index.js index bb6f4ba..9858a8b 100644 --- a/js/index.js +++ b/js/index.js @@ -1,7 +1,7 @@ // Copyright © 2023 Entreprise SkamKraft 'use strict'; import { TemplateEngine } from "./skama_code/ui/templeting_engine.js"; -import { home } from "./scripts/main.js"; +import home from "./scripts/home.js"; let temp_engine = new TemplateEngine("html"); home(temp_engine); diff --git a/js/scripts/home.js b/js/scripts/home.js new file mode 100644 index 0000000..9bafb7f --- /dev/null +++ b/js/scripts/home.js @@ -0,0 +1,9 @@ +import menu_mod from "./menu_mod.js"; + +export default function home(temp_engine) { + temp_engine.after_render(menu_mod); + temp_engine.render("templates/home.html"); +} + + + diff --git a/js/scripts/login.js b/js/scripts/login.js new file mode 100644 index 0000000..5a8d730 --- /dev/null +++ b/js/scripts/login.js @@ -0,0 +1,54 @@ +import { Modal } from "../skama_code/ui/modal.js"; +import { Auth } from "../skama_code/auth/auth.js"; +import { My } from "../skama_code/api/agent.js"; +import home from "./home.js"; +import menu_mod from "./menu_mod.js"; + +export default function login(temp_engine) { + let auth = new Auth(true); + let modal = new Modal("login-modal", temp_engine); + + function render_login() { + temp_engine.render(`templates/login.html`); + modal.load("templates/login_modal.html") + } + + modal.add_class("ext-modal"); + temp_engine.after_render(menu_mod); + + render_login(); + + temp_engine.add_event("#ok", "click", () => { + home(temp_engine); + }); + + temp_engine.add_event("#forget", "click", () => { + My.agent = null; + auth.unload_token(); + modal.close(); + render_login(); + }); + + temp_engine.add_event("#val", "click", () => { + let token = $("#in-token").val(); + auth.login(token); + }); + + temp_engine.add_event("#cancel", "click", () => { + $("#in-token").val(""); + }); + + auth.done((agent) => { + modal.show(); + My.agent = agent; + }).fail((errs) => { + $(".errors").html(""); + errs.forEach(err => { + $(".errors").append(` +

${err}

+ `); + }); + }); + + auth.relog(); +} \ No newline at end of file diff --git a/js/scripts/main.js b/js/scripts/main.js deleted file mode 100644 index 77ba801..0000000 --- a/js/scripts/main.js +++ /dev/null @@ -1,154 +0,0 @@ -import { Modal } from "../skama_code/ui/modal.js"; -import { Auth } from "../skama_code/auth/auth.js"; - -let my_agent = null; - -function init_menu(temp_engine) { - temp_engine.add_event("#reg-link", "click", () => { - reg(temp_engine); - }); - temp_engine.add_event("#login-link", "click", () => { - login(temp_engine); - }); - temp_engine.add_event(".nav-brand", "click", () => { - home(temp_engine); - }); -} - -function loged_links() { - $(".nav-links").prepend(` - - - `); -} - -function show_stats() { - $(".stats").html(` -

Agent name : ${my_agent.name}

-

Credits : ${my_agent.credits}

-

Ships : ${my_agent.ships_cpt}

-

Faction : ${my_agent.faction}

-

HQ : ${my_agent.hq}

- `); -} - -function menu_mod(temp_engine) { - init_menu(temp_engine); - if(my_agent) { - show_stats(); - loged_links(); - } -} - -export function home(temp_engine) { - temp_engine.after_render(menu_mod); - temp_engine.render("templates/home.html"); -} - -export function reg(temp_engine) { - let active = false; - let auth = new Auth(true); - let modal = new Modal("reg-modal", temp_engine); - - function render_reg() { - temp_engine.render(`templates/reg.html`); - modal.load("templates/reg_modal.html") - } - - modal.add_class("ext-modal"); - temp_engine.after_render(menu_mod); - - render_reg(); - - temp_engine.add_event("#ok", "click", () => { - home(temp_engine); - }); - - temp_engine.add_event("#forget", "click", () => { - my_agent = null; - auth.unload_token(); - modal.close(); - render_reg(); - }); - - temp_engine.add_event("#val", "click", () => { - if (!active) { - active = true; - let name = $("#in-name").val(); - let faction = $("#in-faction").val(); - auth.register({ - name: name, - faction: faction - }); - } - }); - - temp_engine.add_event("#cancel", "click", () => { - $("#in-name").val(""); - $("#in-faction").val(""); - }); - - auth.done((agent) => { - $(".show-token").text(agent.token); - modal.show(); - my_agent = agent; - active = false; - }).fail((errs) => { - $(".errors").html(""); - errs.forEach(err => { - $(".errors").append(` -

${err}

- `); - }); - active = false; - }); -} - -export function login(temp_engine) { - let auth = new Auth(true); - let modal = new Modal("login-modal", temp_engine); - - function render_login() { - temp_engine.render(`templates/login.html`); - modal.load("templates/login_modal.html") - } - - modal.add_class("ext-modal"); - temp_engine.after_render(menu_mod); - - render_login(); - - temp_engine.add_event("#ok", "click", () => { - home(temp_engine); - }); - - temp_engine.add_event("#forget", "click", () => { - my_agent = null; - auth.unload_token(); - modal.close(); - render_login(); - }); - - temp_engine.add_event("#val", "click", () => { - let token = $("#in-token").val(); - auth.login(token); - }); - - temp_engine.add_event("#cancel", "click", () => { - $("#in-token").val(""); - }); - - auth.done((agent) => { - modal.show(); - my_agent = agent; - }).fail((errs) => { - $(".errors").html(""); - errs.forEach(err => { - $(".errors").append(` -

${err}

- `); - }); - }); - - auth.relog(); -} \ No newline at end of file diff --git a/js/scripts/menu_mod.js b/js/scripts/menu_mod.js new file mode 100644 index 0000000..591123a --- /dev/null +++ b/js/scripts/menu_mod.js @@ -0,0 +1,42 @@ +import { My } from "../skama_code/api/agent.js"; +import login from "./login.js"; +import reg from "./reg.js"; +import home from "./home.js"; + +function init_menu(temp_engine) { + temp_engine.add_event("#reg-link", "click", () => { + reg(temp_engine); + }); + temp_engine.add_event("#login-link", "click", () => { + login(temp_engine); + }); + temp_engine.add_event(".nav-brand", "click", () => { + home(temp_engine); + }); +} + +function loged_links() { + $(".nav-links").prepend(` + + + `); +} + +function show_stats() { + $(".stats").html(` +

Agent name : ${My.agent.name}

+

Credits : ${My.agent.credits}

+

Ships : ${My.agent.ships_cpt}

+

Faction : ${My.agent.faction}

+

HQ : ${My.agent.hq}

+ `); +} + +export default function menu_mod(temp_engine) { + init_menu(temp_engine); + console.log(My.agent) + if(My.agent) { + show_stats(); + loged_links(); + } +} \ No newline at end of file diff --git a/js/scripts/reg.js b/js/scripts/reg.js new file mode 100644 index 0000000..af94dac --- /dev/null +++ b/js/scripts/reg.js @@ -0,0 +1,64 @@ +import { Modal } from "../skama_code/ui/modal.js"; +import { Auth } from "../skama_code/auth/auth.js"; +import { My } from "../skama_code/api/agent.js"; +import home from "./home.js"; +import menu_mod from "./menu_mod.js"; + +export default function reg(temp_engine) { + let active = false; + let auth = new Auth(true); + let modal = new Modal("reg-modal", temp_engine); + + function render_reg() { + temp_engine.render(`templates/reg.html`); + modal.load("templates/reg_modal.html") + } + + modal.add_class("ext-modal"); + temp_engine.after_render(menu_mod); + + render_reg(); + + temp_engine.add_event("#ok", "click", () => { + home(temp_engine); + }); + + temp_engine.add_event("#forget", "click", () => { + My.agent = null; + auth.unload_token(); + modal.close(); + render_reg(); + }); + + temp_engine.add_event("#val", "click", () => { + if (!active) { + active = true; + let name = $("#in-name").val(); + let faction = $("#in-faction").val(); + auth.register({ + name: name, + faction: faction + }); + } + }); + + temp_engine.add_event("#cancel", "click", () => { + $("#in-name").val(""); + $("#in-faction").val(""); + }); + + auth.done((agent) => { + $(".show-token").text(agent.token); + modal.show(); + My.agent = agent; + active = false; + }).fail((errs) => { + $(".errors").html(""); + errs.forEach(err => { + $(".errors").append(` +

${err}

+ `); + }); + active = false; + }); +} \ No newline at end of file