From edbd0af3bd28d7b076aca5bcf3d115e482d240db Mon Sep 17 00:00:00 2001 From: Clotaire DUFRESNE <clotaire.dufresne.etu@univ-lille.fr> Date: Wed, 10 Feb 2021 11:48:26 +0100 Subject: [PATCH] =?UTF-8?q?c=20fini=20(retard=20car=20incompr=C3=A9hension?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/Router.js | 13 +++++++++++++ src/main.js | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index c41a110..abdfa61 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack --mode=production", - "watch": "webpack --mode=development --watch" + "watch": "webpack --mode=development --watch" }, "author": "Thomas Fritsch <thomas.fritsch@univ-lille.fr> (https://gitlab.univ-lille.fr/thomas.fritsch)", "homepage": "https://gitlab.univ-lille.fr/js", diff --git a/src/Router.js b/src/Router.js index 7aef071..81e3782 100644 --- a/src/Router.js +++ b/src/Router.js @@ -2,6 +2,7 @@ export default class Router { static titleElement; static contentElement; static routes = []; + static #menuElement; static navigate(path) { const route = this.routes.find(route => route.path === path); @@ -10,4 +11,16 @@ export default class Router { this.contentElement.innerHTML = route.page.render(); } } + + static set menuElement(element) { + this.#menuElement = element; + // au clic sur n'importe quel lien contenu dans "element" + // déclenchez un appel à Router.navigate(path) + // où "path" est la valeur de l'attribut `href=".."` du lien cliqué + element.addEventListener('click', event => { + event.preventDefault(); + const target = event.target; + Router.navigate(target.getAttribute('href')); + }); + } } diff --git a/src/main.js b/src/main.js index 6148d2b..d5b527b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,13 +1,28 @@ import Router from './Router'; import data from './data'; import PizzaList from './pages/PizzaList'; +import Component from './components/Component'; Router.titleElement = document.querySelector('.pageTitle'); Router.contentElement = document.querySelector('.pageContent'); -const pizzaList = new PizzaList([]); -Router.routes = [{ path: '/', page: pizzaList, title: 'La carte' }]; +const pizzaList = new PizzaList([]), + aboutPage = new Component('section', null, 'Ce site est génial'), + pizzaForm = new Component( + 'section', + null, + 'Ici vous pourrez ajouter une pizza' + ); +Router.routes = [ + { path: '/', page: pizzaList, title: 'La carte' }, + { path: '/a-propos', page: aboutPage, title: 'À propos' }, + { path: '/ajouter-pizza', page: pizzaForm, title: 'Ajouter une pizza' }, +]; + +Router.menuElement = document.querySelector('.mainMenu'); Router.navigate('/'); // affiche une page vide pizzaList.pizzas = data; Router.navigate('/'); // affiche la liste des pizzas + +document.querySelector('.newsContainer').setAttribute('style', ''); -- GitLab