Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Новожилова Екатерина #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 14 additions & 55 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,23 @@
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<form>
<fieldset class="beverage">
<h4 class="beverage-count">Напиток №1</h4>
<label class="field">
<span class="label-text">Я буду</span>
<select>
<option value="espresso">Эспрессо</option>
<option value="capuccino" selected>Капучино</option>
<option value="cacao">Какао</option>
</select>
</label>
<div class="field">
<span class="checkbox-label">Сделайте напиток на</span>
<label class="checkbox-field">
<input type="radio" name="milk" value="usual" checked />
<span>обычном молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="no-fat" />
<span>обезжиренном молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="soy" />
<span>соевом молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="coconut" />
<span>кокосовом молоке</span>
</label>
<div>
<button type="button" class="add-button">+ Добавить напиток</button>
</div>
<div style="margin-top: 30px">
<button type="submit" class="submit-button">Готово</button>
</div>
<div class="modal-container">
<div class="modal">
<div class="modal-header">
<h1>Ваш заказ</h1>
<div class="remove-button" id="modal-close">X</div>
</div>
<div class="field">
<span class="checkbox-label">Добавьте к напитку:</span>
<label class="checkbox-field">
<input type="checkbox" name="options" value="whipped cream" />
<span>взбитых сливок</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="marshmallow" />
<span>зефирок</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="chocolate" />
<span>шоколад</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="cinnamon" />
<span>корицу</span>
</label>
<div class="modal-body">
<h3 class="modal-text">Ваш заказ</h3>
</div>
</fieldset>
<div>
<button type="button" class="add-button">+ Добавить напиток</button>
</div>
<div style="margin-top: 30px">
<button type="submit" class="submit-button">Готово</button>
</div>
</form>

</div>
<script src="index.js"></script>
</body>
</html>
179 changes: 179 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
let countOnMenu = 0
const submitButton = document.querySelector(".submit-button");
const addButton = document.querySelector(".add-button");

function createForm() {
const form = document.createElement("form")
form.classList.add("beverage-form")

form.innerHTML =
`<fieldset class="beverage">
<div class="beverage-header">
<h4 class="beverage-count">Напиток №${countOnMenu + 1}</h4>
</div>
<label class="field">
<span class="label-text">Я буду</span>
<select name="coffee">
<option value="espresso">Эспрессо</option>
<option value="capuccino" selected>Капучино</option>
<option value="cacao">Какао</option>
</select>
</label>
<div class="field">
<span class="checkbox-label">Сделайте напиток на</span>
<label class="checkbox-field">
<input type="radio" name="milk" value="usual" checked />
<span>обычном молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="no-fat" />
<span>обезжиренном молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="soy" />
<span>соевом молоке</span>
</label>
<label class="checkbox-field">
<input type="radio" name="milk" value="coconut" />
<span>кокосовом молоке</span>
</label>
</div>
<div class="field">
<span class="checkbox-label">Добавьте к напитку:</span>
<label class="checkbox-field">
<input type="checkbox" name="options" value="whipped cream" />
<span>взбитых сливок</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="marshmallow" />
<span>зефирок</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="chocolate" />
<span>шоколад</span>
</label>
<label class="checkbox-field">
<input type="checkbox" name="options" value="cinnamon" />
<span>корицу</span>
</label>
</div>
</fieldset>`;

return form;
}

submitButton.onclick = () => {
const mod = new ModalWindow();
mod.show()
}

addButton.onclick = () => {
const form = createForm();
addForm(form);
}

function addForm(form) {
document.body.insertBefore(form, document.getElementById("add-button-container"));
countOnMenu++

const fieldSetHeader = form.querySelector(".beverage-header");
fieldSetHeader.appendChild(createReButton(form));
}

function createReButton(form) {
const reButton = document.createElement("div");

reButton.classList.add("remove-button");
reButton.textContent = "X";

reButton.onclick = function () {
if (countOnMenu <= 1) return;
document.body.removeChild(form);
countOnMenu--;
updateDrinkNumbers();
};

return reButton;
}

function updateDrinkNumbers() {
const numberDrink = document.querySelectorAll(".beverage-count");
for (let i = 0; i < countOnMenu; i++)
numberDrink[i].textContent = `Напиток №${i + 1}`;
}

class ModalWindow {

constructor() {
this.modalContainer = document.querySelector(".mod-container");

const modalClose = document.getElementById("mod-close");
modalClose.onclick = () => this.hide();

const modalText = this.modalContainer.querySelector(".mod-text");
const ending = countOnMenu % 10 === 1 ? "ок" :
countOnMenu % 10 >= 2 && countOnMenu % 10 <= 4 ? "ка" : "ков";
modalText.textContent = `Вы заказали ${countOnMenu} напит${ending}`;

this.modalContainer.querySelector(".mod-body").appendChild(this.createOrderTable());
};

createOrderTable() {
const table = document.createElement("table");
table.classList.add("order-table");
table.innerHTML = `
<thead><tr>
<th>Напиток</th>
<th>Молоко</th>
<th>Дополнительно</th>
</tr></thead>
`;

for (let form of document.querySelectorAll(".beverage-form")) {
const formData = new FormData(form);
const dataKeys = ["coffee", "milk", "options"];

const tableRow = document.createElement("tr");

for (let key of dataKeys) {
const column = document.createElement("td");

const values = formData.getAll(key).map(e => ModalWindow.#translate(e));
column.textContent = values.join(", ");

tableRow.appendChild(column);
}

table.appendChild(tableRow);
}

return table;
}

hide() {
this.modalContainer.querySelector(".order-table").remove();
this.modalContainer.style.visibility = "hidden";
};

show() {
this.modalContainer.style.visibility = "visible";
};

static #translate(word) {
switch(word) {
case "espresso": return "Эспрессо";
case "capuccino": return "Капучино";
case "cacao": return "Какао";

case "usual": return "Обычное молоко";
case "no-fat": return "Обезжиренное молоко";
case "soy": return "Соевое молоко";
case "coconut": return "Кокосовое молоко";

case "whipped-cream": return "Взбитые сливки";
case "marshmallow": return "Зефирки";
case "chocolate": return "Шоколад";
case "cinnamon": return "Корица";
}
}
}
72 changes: 72 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
border: 1px solid #eee;
margin: 15px 0;
padding: 15px;
display: inline-block;
}

.beverage-header {
display: flex;
justify-content: space-between;
}

.beverage-count {
Expand Down Expand Up @@ -43,5 +49,71 @@
padding: 7px 15px;
border: 2px solid orange;
border-radius: 5px;
cursor: pointer;
}

.remove-button {
cursor: pointer;
font-weight: bold;
padding: 4px 4px;
margin: 4px;
height: 100%;
border: 2px solid black;
border-radius: 5px;
transition: 0.3s;
}

.remove-button:hover {
background-color: #ff6767;
transition: 0.1s;
}

.order-table{
table-layout: fixed;
width: 100%;
border-collapse: collapse;
}

.order-table th, .order-table {
border: 1px solid black;
}

.order-table td {
border:1px solid grey;
}

.modal-container {
visibility: hidden;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
position: fixed;
top: 0;
left: 0;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}

.modal {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
width: 750px;
position: fixed;
background-color: white;
padding: 15px;
}

.modal-header {
display: flex;
justify-content: space-between;
}

.modal-footer {
margin-top: 15px;
}

#modal-close {
cursor: pointer;
}