Skip to content
Snippets Groups Projects
Commit a7b3887f authored by Baptiste Lantoine's avatar Baptiste Lantoine
Browse files

tp3 D3

parent f98a536c
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode=production", "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)", "author": "Thomas Fritsch <thomas.fritsch@univ-lille.fr> (https://gitlab.univ-lille.fr/thomas.fritsch)",
"homepage": "https://gitlab.univ-lille.fr/js", "homepage": "https://gitlab.univ-lille.fr/js",
......
...@@ -2,12 +2,30 @@ export default class Router { ...@@ -2,12 +2,30 @@ export default class Router {
static titleElement; static titleElement;
static contentElement; static contentElement;
static routes = []; static routes = [];
static #menuElement;
static set menuElement(element) {
this.#menuElement = element;
const value = element.querySelectorAll('a');
value.forEach(elem => {
console.log(elem.getAttribute('href'));
//console.log(elem);
elem.addEventListener('click', event => {
event.preventDefault();
Router.navigate(elem.getAttribute('href'));
console.log('event');
});
});
// 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) { static navigate(path) {
const route = this.routes.find(route => route.path === path); const route = this.routes.find(route => route.path === path);
if (route) { if (route) {
this.titleElement.innerHTML = `<h1>${route.title}</h1>`; this.titleElement.innerHTML = `<h1>${route.title}</h1>`;
this.contentElement.innerHTML = route.page.render(); this.contentElement.innerHTML = route.page.render();
route.page.mount?.(this.contentElement);
} }
} }
} }
import Router from './Router'; import Router from './Router';
import data from './data'; import data from './data';
import PizzaList from './pages/PizzaList'; import PizzaList from './pages/PizzaList';
import PizzaForm from './pages/PizzaForm';
import Component from './components/Component';
Router.titleElement = document.querySelector('.pageTitle'); Router.titleElement = document.querySelector('.pageTitle');
Router.contentElement = document.querySelector('.pageContent'); Router.contentElement = document.querySelector('.pageContent');
const pizzaList = new PizzaList([]); const pizzaList = new PizzaList([]),
Router.routes = [{ path: '/', page: pizzaList, title: 'La carte' }]; aboutPage = new Component('p', null, 'ce site est génial'),
pizzaForm = new PizzaForm();
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.navigate('/'); // affiche une page vide Router.navigate('/'); // affiche une page vide
pizzaList.pizzas = data; pizzaList.pizzas = data;
Router.navigate('/'); // affiche la liste des pizzas Router.navigate('/'); // affiche la liste des pizzas
const display = document.querySelector('.newsContainer');
display.setAttribute('style', '');
const displayBtn = document.querySelector('.closeButton');
displayBtn.addEventListener('click', onclick);
function onclick(event) {
display.setAttribute('style', 'display:none');
}
Router.menuElement = document.querySelector('.mainMenu');
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;
}
}
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);
}
submit(event) {}
}
import Component from '../components/Component.js'; import Component from '../components/Component.js';
import PizzaThumbnail from '../components/PizzaThumbnail.js'; import PizzaThumbnail from '../components/PizzaThumbnail.js';
import Page from './Page.js';
export default class PizzaList extends Component { export default class PizzaList extends Page {
#pizzas; #pizzas;
constructor(pizzas) { constructor(pizzas) {
super('section', { name: 'class', value: 'pizzaList' }); super('pizzaList');
this.pizzas = pizzas; this.pizzas = pizzas;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment