-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from KseniiaPan/module5-task1
- Loading branch information
Showing
11 changed files
with
207 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.