class Controller { constructor(presenter) { this.presenter = presenter; }…
- User
class Controller { constructor(presenter) { this.presenter = presenter; }
handleEvent(element) { this.presenter.setLayout(element); }}
class Presenter { constructor(containerId) { this.container = document.getElementById(containerId); this.divs = this.container.querySelectorAll('.inner-div'); }
setLayout(layoutType) { // Wyczyść wszystkie style dla divs this.divs.forEach(div => { div.style.removeProperty('left'); div.style.removeProperty('top'); div.style.removeProperty('right'); div.style.removeProperty('bottom'); div.textContent = ''; }); // Stosuj nowe style w zależności od układu switch (layoutType) { case 'A': // Układ X this.divs[0].style.left = '0'; this.divs[0].style.top = '0'; this.divs[0].textContent = 'Jan'; this.divs[1].style.left = '45%'; this.divs[1].style.top = '45%'; this.divs[1].textContent = 'Kowalski'; this.divs[2].style.right = '0'; this.divs[2].style.bottom = '0'; break; case 'B': // Układ Y this.divs.forEach((div, index) => { div.style.left = `${index * 33.3}%`; div.style.top = '45%'; }); this.divs[0].textContent = 'Jan'; this.divs[1].textContent = 'Kowalski'; break; case 'C': // Układ Z this.divs[0].style.left = '0'; this.divs[0].style.top = '0'; this.divs[1].style.left = '12%'; this.divs[1].style.top = '0'; this.divs[2].style.right = '0'; this.divs[2].style.bottom = '0'; this.divs[2].textContent = 'Jan'; this.divs[3].style.right = '12%'; this.divs[3].style.bottom = '0'; this.divs[3].textContent = 'Kowalski'; break; } }}
document.addEventListener('DOMContentLoaded', () => { const presenter = new Presenter('layoutContainer'); const controller = new Controller(presenter);
document.getElementById('btnA').addEventListener('click', () => controller.handleEvent('A')); document.getElementById('btnB').addEventListener('click', () => controller.handleEvent('B')); document.getElementById('btnC').addEventListener('click', () => controller.handleEvent('C'));}); w case a i case b maja byc 3 divy
- Assistant
Jeśli w układach 'A' i 'B' powinny być użyte trzy
divy, a także w układzie 'A' powinny być rozmieszczone w szczególny sposób, możemy odpowiednio zmodyfikować metodęsetLayoutw klasiePresenter. Poniżej przedstawiam zmienioną implementację dla przypadków 'A' i 'B', zaktualizowaną do użytku trzemadivami:setLayout(layoutType) { // Wyczyść wszystkie style dla divs this.divs.forEach(div => { div.style.removeProperty('left'); div.style.removeProperty('top'); div.style.removeProperty('right'); div.style.removeProperty('bottom'); div.textContent = ''; }); // Stosuj nowe style w zależności od układu switch (layoutType) { case 'A': // Nowy układ X, z trzema divami this.divs[0].style.left = '0'; this.divs[0].style.top = '0'; this.divs[0].textContent = 'Jan'; this.divs[1].style.left = '45%'; this.divs[1].style.top = '45%'; this.divs[1].textContent = 'Kowalski'; this.divs[2].style.left = '90%'; this.divs[2].style.bottom = '45%'; this.divs[2].textContent = 'Nowak'; // dodatkowa zawartość dla trzeciego diva break; case 'B': // Nowy układ Y, z trzema divami, rozciągając je równomiernie this.divs.forEach((div, index) => { div.style.left = `${index * 33.3}%`; div.style.top = '45%'; }); this.divs[0].textContent = 'Jan'; this.divs[1].textContent = 'Kowalski'; this.divs[2].textContent = 'Nowak'; // dodatkowa zawartość dla trzeciego diva break; case 'C': this.divs[0].style.left = '0'; this.divs[0].style.top = '0'; this.divs[1].style.left = '12%'; this.divs[1].style.top = '0'; this.divs[2].style.right = '0'; this.divs[2].style.bottom = '0'; this.divs[2].textContent = 'Jan'; this.divs[3].style.right = '12%'; this.divs[3].style.bottom = '0'; this.divs[3].textContent = 'Kowalski'; break; } }Wprowadzone modyfikacje pozwalają na korzystanie z trzech
divów w układach 'A' i 'B', umożliwiając przy tym różne ułożenie i teksty w zależności od wybranego układu. Tworzą one interesujące rozłożenie prezentowanych elementów.