Skip to content

Commit

Permalink
Merge pull request #7 from KseniiaPan/module5-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Sep 30, 2024
2 parents 027f1ae + 25524e9 commit 95a794a
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 67 deletions.
7 changes: 6 additions & 1 deletion src/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ const FilterType = {
PAST: 'PAST',
};

const Mode = {
DEFAULT: 'DEFAULT',
EDITING: 'EDITING',
};

const HOURS = 24;
const MINUTES = 60;

export {TIME_FORMAT, HOURS, MINUTES, EVENT_TYPES, FilterType};
export {TIME_FORMAT, HOURS, MINUTES, EVENT_TYPES, FilterType, Mode};
7 changes: 1 addition & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import FiltersPresenter from './presenter/filters-presenter.js';
import EventsPresenter from './presenter/events-presenter.js';

import PointsModel from './model/points-model.js';
import DestinationsModel from './model/destinations-model.js';
import OffersModel from './model/offers-model.js';

const tripFiltersElement = document.querySelector(
'.trip-controls__filters');
const tripEventsElement = document.querySelector('.trip-events');

const pointsModel = new PointsModel();
const destinationsModel = new DestinationsModel();
const offersModel = new OffersModel();


const filtersPresenter = new FiltersPresenter({
filtersContainer: tripFiltersElement,
Expand All @@ -21,8 +18,6 @@ const filtersPresenter = new FiltersPresenter({
const eventsPresenter = new EventsPresenter({
eventsContainer: tripEventsElement,
pointsModel,
destinationsModel,
offersModel,
});

filtersPresenter.init();
Expand Down
5 changes: 2 additions & 3 deletions src/mock/filter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {filter} from '../utils/filter.js';

function generateFilter(points) {
function generateFilter() {
return Object.entries(filter).map(
([filterType, filterPoints]) => ({
([filterType]) => ({
type: filterType,
count: filterPoints(points).length,
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/points-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getRandomPoint} from '../mock/points';

const POINTS_COUNT = 15;
const POINTS_COUNT = 6;

export default class PointsModel {
points = Array.from({length: POINTS_COUNT}, getRandomPoint);
Expand Down
97 changes: 47 additions & 50 deletions src/presenter/events-presenter.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,82 @@
import SortView from '../view/sort-view.js';
import EventsListView from '../view/events-list-view.js';
import EventEditView from '../view/event-edit-view.js';
import EventPointView from '../view/event-point-view.js';
import NoEventView from '../view/no-event-view.js';
import {isEscapeKey} from '../utils/utils.js';
import PointPresenter from '../presenter/point-presenter.js';
import DestinationsModel from '../model/destinations-model.js';
import OffersModel from '../model/offers-model.js';


import {render, replace,} from '../framework/render.js';
import {render} from '../framework/render.js';
import {updateItem} from '../utils/utils.js';

export default class EventsPresenter {
#eventsContainer = null;
#pointsModel = null;
#destinationsModel = null;
#offersModel = null;

#eventsListComponent = new EventsListView();

#eventsPoints = [];
#pointPresenters = new Map ();

constructor({ eventsContainer, pointsModel, destinationsModel, offersModel }) {
constructor({ eventsContainer, pointsModel }) {
this.#eventsContainer = eventsContainer;
this.#pointsModel = pointsModel;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
}

init() {
this.#eventsPoints = [...this.#pointsModel.getPoints()];

if (this.#eventsPoints.length === 0) {
render(new NoEventView(), this.#eventsContainer);
this.#renderEmptyList();
return;
}
render(new SortView(), this.#eventsContainer);
render(this.#eventsListComponent, this.#eventsContainer);
this.#renderSort();

this.#renderPointsList();

this.#eventsPoints.forEach((point) => {
this.#renderEventPoint(point);
});
}

#handlePointChange = (updatedPoint) => {
this.#eventsPoints = updateItem(this.#eventsPoints, updatedPoint);
this.#pointPresenters.get(updatedPoint.id).init(updatedPoint);
};

#handleModeChange = () => {
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};

#renderSort() {
render(new SortView(), this.#eventsContainer);
}

#renderPointsList() {
render(this.#eventsListComponent, this.#eventsContainer);
}

#clearPointsList() {
this.#pointPresenters.forEach((presenter) => presenter.destroy());
this.#pointPresenters.clear();
}

#renderEmptyList() {
render(new NoEventView(), this.#eventsContainer);
}

#renderEventPoint(point) {
const escKeyDownHandler = (evt) => {
if (isEscapeKey) {
evt.preventDefault();
replaceEditFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
}
};
const onEditOpenButtonClick = () => {
replacePointToEditForm();
document.addEventListener('keydown', escKeyDownHandler);
};
const onEditCloseButtonClick = () => {
replaceEditFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
};

const eventPointComponent = new EventPointView ({
point,
destination: this.#destinationsModel.getDestinationsById(point.destination),
offers: [...this.#offersModel.getOffersById(point.type, point.offers)],
onEditOpenButtonClick,
});
const destinationsModel = new DestinationsModel();
const offersModel = new OffersModel();

const eventPointEditingComponent = new EventEditView ({
point,
chosenDestination: this.#destinationsModel.getDestinationsById(point.destination),
chosenOffers: [...this.#offersModel.getOffersById(point.type, point.offers)],
allDestinations: this.#destinationsModel.getDestinations(),
allOffers: this.#offersModel.getOffersByType(point.type),
onEditCloseButtonClick,
const pointPresenter = new PointPresenter({
pointListContainer: this.#eventsListComponent.element,
onDataChange: this.#handlePointChange,
onModeChange: this.#handleModeChange,
destinationsModel,
offersModel,
});

function replaceEditFormToPoint() {
replace(eventPointComponent, eventPointEditingComponent);
}
function replacePointToEditForm() {
replace(eventPointEditingComponent, eventPointComponent);
}

render(eventPointComponent, this.#eventsListComponent.element);
pointPresenter.init(point);
this.#pointPresenters.set(point.id, pointPresenter);
}
}
2 changes: 1 addition & 1 deletion src/presenter/filters-presenter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FiltersView from '../view/filtes-view.js';
import FiltersView from '../view/filters-view.js';
import {generateFilter} from '../mock/filter.js';
import { render } from '../framework/render.js';

Expand Down
117 changes: 117 additions & 0 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import EventEditView from '../view/event-edit-view.js';
import EventPointView from '../view/event-point-view.js';
import {isEscapeKey} from '../utils/utils.js';
import {replace, render, remove} from '../framework/render.js';
import {Mode} from '../consts.js';


export default class PointPresenter {
#pointListContainer = null;
#handleDataChange = null;
#handleModeChange = null;

#pointComponent = null;
#pointEditComponent = null;
#destinationsModel = null;
#offersModel = null;
#point = null;
#mode = Mode.DEFAULT;

constructor({pointListContainer, onDataChange, destinationsModel, offersModel, onModeChange}) {
this.#pointListContainer = pointListContainer;
this.#handleDataChange = onDataChange;
this.#handleModeChange = onModeChange;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
}

init(point) {
this.#point = point;

const prevPointComponent = this.#pointComponent;
const prevPointEditComponent = this.#pointEditComponent;

this.#pointComponent = new EventPointView ({
point: this.#point,
destination: this.#destinationsModel.getDestinationsById(point.destination),
offers: [...this.#offersModel.getOffersById(point.type, point.offers)],
onEditOpenButtonClick: this.#handleEditOpenClick,
onFavoriteClick: this.#handleFavoriteClick,
});

this.#pointEditComponent = new EventEditView ({
point: this.#point,
chosenDestination: this.#destinationsModel.getDestinationsById(point.destination),
chosenOffers: [...this.#offersModel.getOffersById(point.type, point.offers)],
allDestinations: this.#destinationsModel.getDestinations(),
allOffers: this.#offersModel.getOffersByType(point.type),
onEditCloseButtonClick: this.#handleEditCloseClick,
});

if (!prevPointComponent || !prevPointEditComponent) {
render(this.#pointComponent, this.#pointListContainer);
return;
}

if (this.#mode === Mode.DEFAULT) {
replace(this.#pointComponent, prevPointComponent);
}

if (this.#mode === Mode.EDITING) {
replace(this.#pointEditComponent, prevPointEditComponent);
}

remove(prevPointComponent);
remove(prevPointEditComponent);
}

destroy() {
remove(this.#pointComponent);
remove(this.#pointEditComponent);
}

resetView() {
if (this.#mode !== Mode.DEFAULT) {
this.#replaceEditFormToPoint();
}
}

#replaceEditFormToPoint() {
replace(this.#pointComponent, this.#pointEditComponent);
this.#mode = Mode.DEFAULT;
}

#replacePointToEditForm() {
replace(this.#pointEditComponent, this.#pointComponent);
this.#handleModeChange();
this.#mode = Mode.EDITING;
}

#escKeyDownHandler = (evt) => {
if (isEscapeKey) {
evt.preventDefault();
this.#replaceEditFormToPoint();
document.removeEventListener('keydown', this.#escKeyDownHandler);
}
};

#handleEditOpenClick = () => {
this.#replacePointToEditForm();
document.addEventListener('keydown', this.#escKeyDownHandler);
};

#handleEditCloseClick = () => {
this.#replaceEditFormToPoint();
document.removeEventListener('keydown', this.#escKeyDownHandler);
};

#handleFormSubmit = (point) => {
this.#handleDataChange(point);
this.#replaceEditFormToPoint();
};

#handleFavoriteClick = () => {
this.#handleDataChange({...this.#point, isFavorite: !this.#point.isFavorite});
};
}

7 changes: 6 additions & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ function padToTwoDigits(number) {
return number.toString().padStart(2, '0');
}

export {getRandomArrayElement, padToTwoDigits, capitalize, isEscapeKey};
function updateItem(items, update) {
return items.map((item) => item.id === update.id ? update : item);
}

export {getRandomArrayElement, padToTwoDigits, capitalize, isEscapeKey, updateItem};

16 changes: 15 additions & 1 deletion src/view/event-edit-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {createOffersTemplate, createTypeTemplate, humanizeEventDate} from '../ut

function createEventEditingTemplate(event, chosenDestination, chosenOffers, allDestinations, allOffers) {
const { basePrice, dateFrom, dateTo, type } = event;
const { name, description } = chosenDestination;
const { name, description, pictures } = chosenDestination;

const typeTemplate = createTypeTemplate(EVENT_TYPES, type);
const offersTemplate = createOffersTemplate(allOffers.offers, chosenOffers, type);
Expand Down Expand Up @@ -71,6 +71,13 @@ function createEventEditingTemplate(event, chosenDestination, chosenOffers, allD
<section class="event__section event__section--destination">
<h3 class="event__section-title event__section-title--destination">Destination</h3>
<p class="event__destination-description">${description}</p>
<div class="event__photos-container">
<div class="event__photos-tape">
${pictures.map((picture) => `
<img class="event__photo" src = ${picture.src} alt=${picture.description}>
`).join('')}
</div>
</div>
</section>
</section>
</form>`;
Expand All @@ -83,6 +90,7 @@ export default class EventEditView extends AbstractView {
#allDestinations = null;
#allOffers = null;
#handleEditCloseButton = null;
#handleFormSubmit;

constructor({point, chosenDestination, chosenOffers, allDestinations, allOffers, onEditCloseButtonClick}) {
super();
Expand All @@ -104,4 +112,10 @@ export default class EventEditView extends AbstractView {
evt.preventDefault();
this.#handleEditCloseButton();
};

#formSubmitHandler = (evt) => {
evt.preventDefault();
this.#handleFormSubmit();
this.#handleFormSubmit(this.#point);
};
}
Loading

0 comments on commit 95a794a

Please sign in to comment.