diff --git a/package.json b/package.json index c41a110ca19262f50af831518d2d2dc6098b0615..abdfa619e9426f7c982feea585be7add72473e18 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 7aef0711884fbc5af076fc2d62abd4dc61ad0d00..e046d998fb49908dddc6ad5bb746b7c89f868405 100644 --- a/src/Router.js +++ b/src/Router.js @@ -1,13 +1,36 @@ -export default class Router { +export default class Router { static titleElement; static contentElement; static routes = []; + static #menuElement; + + static set menuElement(element) { + this.#menuElement = element; + document.querySelectorAll('.mainMenu a').forEach(element => { + element.addEventListener('click', event => { + event.preventDefault(); + this.navigate(element.getAttribute('href')); + }) + }) + // 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é + } + static navigate(path) { const route = this.routes.find(route => route.path === path); if (route) { + document.querySelector('.active')?.removeAttribute('class'); + document.querySelectorAll('.mainMenu a').forEach(element => { + if(element.getAttribute('href') === path) { + element.setAttribute('class', 'active'); + } + }) + window.history.pushState(null,null,path); this.titleElement.innerHTML = `<h1>${route.title}</h1>`; this.contentElement.innerHTML = route.page.render(); + route.page.mount?.(this.contentElement); } } } diff --git a/src/main.js b/src/main.js index 6148d2ba698b9b4314ca2f4afde288b429439f04..61c1548aef7d21ae292add1ac6d17ddba3d26054 100644 --- a/src/main.js +++ b/src/main.js @@ -1,13 +1,36 @@ import Router from './Router'; import data from './data'; import PizzaList from './pages/PizzaList'; +import Component from './components/Component'; +import PizzaForm from './pages/PizzaForm'; Router.titleElement = document.querySelector('.pageTitle'); Router.contentElement = document.querySelector('.pageContent'); -const pizzaList = new PizzaList([]); +const pizzaList = new PizzaList([]), + aboutPage = new Component('p', null, 'ce site est génial'), + pizzaForm = new PizzaForm(); + +//const pizzaList = new PizzaList([]); Router.routes = [{ path: '/', page: pizzaList, title: 'La carte' }]; Router.navigate('/'); // affiche une page vide pizzaList.pizzas = data; Router.navigate('/'); // affiche la liste des pizzas + +document.querySelector('.newsContainer').setAttribute('style',''); + +document.querySelector('.closeButton').addEventListener('click', event => { + event.preventDefault(); + document.querySelector('.newsContainer').setAttribute('style','display:none'); +}) + + +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'); + \ No newline at end of file diff --git a/src/pages/Page.js b/src/pages/Page.js new file mode 100644 index 0000000000000000000000000000000000000000..8ccf3ed8dc481d6f0e94cc52d421f76a533a0ae1 --- /dev/null +++ b/src/pages/Page.js @@ -0,0 +1,12 @@ +import Component from '../components/Component'; + +export default class Page extends Component { + element; + + constructor(className, children) { + super('section', { name: 'class', value: className }, children); + } + mount(element) { + this.element = element; + } +} diff --git a/src/pages/PizzaForm.js b/src/pages/PizzaForm.js new file mode 100644 index 0000000000000000000000000000000000000000..6a6d74b011a7d2bcbb7f0c96b502e6d90f5b0652 --- /dev/null +++ b/src/pages/PizzaForm.js @@ -0,0 +1,38 @@ +import Page from './Page.js'; + +export default class PizzaForm extends Page { + render() { + return /*html*/ ` + <form class="pizzaForm"> + <label> + Nom : + <input type="text" name="name"> + </label> + <button type="submit">Ajouter</button> + </form>`; + } + + mount(element) { + super.mount(element); + const form = document.querySelector('form'); + + form.addEventListener('submit', function(event) { + event.preventDefault(); + PizzaForm.submit(event); + }); + + } + + static submit(event) { + const form = document.querySelector('form'), + input = form.querySelector('input[name=name]'); + + if(input.value) { + alert(`La pizza ${input.value} a été ajouté !`); + input.value = ''; + } else { + alert('Champ vide !'); + } + + } +} diff --git a/src/pages/PizzaList.js b/src/pages/PizzaList.js index fcaedaf6bc1e55e9595774b5bd9ba88be2346d94..fe637606536e2f01023d33bf9bfe19638bf4d4d1 100644 --- a/src/pages/PizzaList.js +++ b/src/pages/PizzaList.js @@ -1,11 +1,11 @@ -import Component from '../components/Component.js'; import PizzaThumbnail from '../components/PizzaThumbnail.js'; +import Page from './Page'; -export default class PizzaList extends Component { +export default class PizzaList extends Page { #pizzas; constructor(pizzas) { - super('section', { name: 'class', value: 'pizzaList' }); + super('pizzaList'); this.pizzas = pizzas; }