-
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.
- Loading branch information
Showing
5 changed files
with
344 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import PointView from '../view/point-view.js'; | ||
import EditPointView from '../view/edit-point-view.js'; | ||
import { remove, render, replace } from '../framework/render.js'; | ||
import { Mode } from '../const.js'; | ||
|
||
export default class PointPresenter { | ||
#container = null; | ||
|
||
#destinationsModel = null; | ||
#offersModel = null; | ||
|
||
#handleDataChange = null; | ||
#handleModeChange = null; | ||
|
||
#pointComponent = null; | ||
#editPointComponent = null; | ||
#point = null; | ||
#mode = Mode.DEFAULT; | ||
|
||
constructor({container, destinationsModel, offersModel, onDataChange, onModeChange}) { | ||
this.#container = container; | ||
this.#destinationsModel = destinationsModel; | ||
this.#offersModel = offersModel; | ||
this.#handleDataChange = onDataChange; | ||
this.#handleModeChange = onModeChange; | ||
} | ||
|
||
init(point) { | ||
this.#point = point; | ||
|
||
const prevPointComponent = this.#pointComponent; | ||
const prevEditPointComponent = this.#editPointComponent; | ||
|
||
this.#pointComponent = new PointView({ | ||
point: this.#point, | ||
pointDestination: this.#destinationsModel.getById(point.destination), | ||
pointOffers: this.#offersModel.getByType(point.type), | ||
onEditClick: this.#pointEditClickHandler, | ||
onFavoriteClick: this.#favoriteClickHandler | ||
}); | ||
|
||
this.#editPointComponent = new EditPointView({ | ||
point: this.#point, | ||
pointDestination: this.#destinationsModel.getById(point.destination), | ||
pointOffers: this.#offersModel.getByType(point.type), | ||
onSubmitClick: this.#formSubmitHandler, | ||
onResetClick: this.#resetButtonClickHandler | ||
}) | ||
|
||
if (prevPointComponent === null || prevEditPointComponent === null) { | ||
render(this.#pointComponent, this.#container); | ||
return; | ||
} | ||
|
||
if (this.#mode === Mode.DEFAULT) { | ||
replace(this.#pointComponent, prevPointComponent); | ||
} | ||
|
||
if (this.#mode === Mode.EDITING) { | ||
replace(this.#pointComponent, prevEditPointComponent); | ||
} | ||
|
||
remove(prevPointComponent); | ||
remove(prevEditPointComponent); | ||
} | ||
|
||
resetView = () => { | ||
if (this.#mode !== Mode.DEFAULT) { | ||
this.#replaceFormToPoint(); | ||
} | ||
}; | ||
|
||
destroy = () => { | ||
remove(this.#pointComponent); | ||
remove(this.#editPointComponent); | ||
} | ||
|
||
#replacePointToForm = () => { | ||
replace(this.#editPointComponent, this.#pointComponent); | ||
document.addEventListener('keydown', this.#escKeyDownHandler); | ||
this.#handleModeChange(); | ||
this.#mode = Mode.EDITING; | ||
}; | ||
|
||
#replaceFormToPoint = () => { | ||
replace(this.#pointComponent, this.#editPointComponent); | ||
document.removeEventListener('keydown', this.#escKeyDownHandler); | ||
this.#mode = Mode.DEFAULT; | ||
}; | ||
|
||
#escKeyDownHandler = (evt) => { | ||
if (evt.key === 'Escape') { | ||
evt.preventDefault(); | ||
this.#replaceFormToPoint(); | ||
} | ||
}; | ||
|
||
#pointEditClickHandler = () => { | ||
this.#replacePointToForm(); | ||
}; | ||
|
||
#favoriteClickHandler = () => { | ||
this.#handleDataChange({ | ||
...this.#point, | ||
isFavorite: !this.#point.isFavorite | ||
}); | ||
}; | ||
|
||
#formSubmitHandler = (point) => { | ||
this.#handleDataChange(point); | ||
this.#replaceFormToPoint(); | ||
} | ||
|
||
#resetButtonClickHandler = () => { | ||
this.#replaceFormToPoint(); | ||
} | ||
} |
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,89 +1,81 @@ | ||
import SortView from '../view/sort-view.js'; | ||
import EditPointView from '../view/edit-point-view.js'; | ||
import PointView from '../view/point-view.js'; | ||
import TripView from '../view/point-list-view.js'; | ||
import EmptyListView from '../view/empty-list-view.js'; | ||
import PointPresenter from './point-presenter.js'; | ||
import { render, replace } from '../framework/render.js'; | ||
import { updateItem } from '../utils.js'; | ||
import PointListView from '../view/point-list-view.js'; | ||
|
||
export default class TripPresenter { | ||
#tripContainer = null; | ||
#destinationsModel = null; | ||
#offersModel = null; | ||
#pointsModel = null; | ||
#pointListComponent = new TripView(); | ||
#sortComponent = new SortView(); | ||
#tripContainer = null; | ||
#destinationsModel = null; | ||
#offersModel = null; | ||
#pointsModel = null; | ||
#pointListComponent = new TripView(); | ||
#sortComponent = new SortView(); | ||
|
||
#points = []; | ||
#points = []; | ||
|
||
constructor({ tripContainer, destinationsModel, offersModel, pointsModel }) { | ||
this.#tripContainer = tripContainer; | ||
this.#destinationsModel = destinationsModel; | ||
this.#offersModel = offersModel; | ||
this.#pointsModel = pointsModel; | ||
this.#points = [...this.#pointsModel.get()]; | ||
} | ||
#pointPresenters = new Map(); | ||
|
||
init() { | ||
if (this.#points.length === 0) { | ||
render(new EmptyListView(), this.#tripContainer); | ||
return; | ||
constructor({tripContainer, destinationsModel, offersModel, pointsModel}) { | ||
this.#tripContainer = tripContainer; | ||
this.#destinationsModel = destinationsModel; | ||
this.#offersModel = offersModel; | ||
this.#pointsModel = pointsModel; | ||
this.#points = [...this.#pointsModel.get()]; | ||
} | ||
|
||
render(this.#sortComponent, this.#tripContainer); | ||
render(this.#pointListComponent, this.#tripContainer); | ||
|
||
this.#points.forEach((point) => { | ||
this.#renderPoint(point); | ||
}); | ||
} | ||
init() { | ||
this.#renderBoard(); | ||
} | ||
|
||
#renderPoint = (point) => { | ||
const pointComponent = new PointView({ | ||
point, | ||
pointDestination: this.#destinationsModel.getById(point.destination), | ||
pointOffers: this.#offersModel.getByType(point.type), | ||
onEditClick: pointEditClickHandler | ||
}); | ||
#renderPoint = (point) => { | ||
const pointPresenter = new PointPresenter({ | ||
container: this.#pointListComponent.element, | ||
destinationsModel: this.#destinationsModel, | ||
offersModel: this.#offersModel, | ||
onDataChange: this.#pointChangeHandler, | ||
onModeChange: this.#modeChangeHandler | ||
}); | ||
|
||
const editPointComponent = new EditPointView({ | ||
point, | ||
pointDestination: this.#destinationsModel.getById(point.destination), | ||
pointOffers: this.#offersModel.getByType(point.type), | ||
onSubmitClick: pointSubmitHandler, | ||
onResetClick: resetButtonClickHandler | ||
}); | ||
pointPresenter.init(point); | ||
|
||
const replacePointToForm = () => { | ||
replace(editPointComponent, pointComponent); | ||
}; | ||
|
||
const replaceFormToPoint = () => { | ||
replace(pointComponent, editPointComponent); | ||
}; | ||
this.#pointPresenters.set(point.id, pointPresenter); | ||
} | ||
|
||
const escKeyDownHandler = (evt) => { | ||
if (evt.key === 'Escape' || evt.key === 'Esc') { | ||
evt.preventDefault(); | ||
replaceFormToPoint(); | ||
document.removeEventListener('keydown', escKeyDownHandler); | ||
} | ||
#renderPoints = () => { | ||
this.#points.forEach((point) => { | ||
this.#renderPoint(point); | ||
}); | ||
}; | ||
|
||
function pointEditClickHandler() { | ||
replacePointToForm(); | ||
document.addEventListener('keydown', escKeyDownHandler); | ||
#clearPoints = () => { | ||
this.#pointPresenters.forEach((presenter) => presenter.destroy()); | ||
this.#pointPresenters.clear(); | ||
} | ||
|
||
function resetButtonClickHandler() { | ||
replaceFormToPoint(); | ||
document.removeEventListener('keydown', escKeyDownHandler); | ||
#renderPointContainer = () => { | ||
this.#pointListComponent = new PointListView(); | ||
render(this.#pointListComponent, this.#tripContainer); | ||
} | ||
|
||
function pointSubmitHandler() { | ||
replaceFormToPoint(); | ||
document.removeEventListener('keydown', escKeyDownHandler); | ||
#renderBoard = () => { | ||
if (this.#points.length === 0) { | ||
render(new EmptyListView(), this.#tripContainer); | ||
return; | ||
} | ||
|
||
this.#renderPointContainer(); | ||
this.#renderPoints(); | ||
}; | ||
|
||
#pointChangeHandler = (updatedPoint) => { | ||
this.#points = updateItem(this.#points, updatedPoint); | ||
this.#pointPresenters.get(updatedPoint.id).init(updatedPoint); | ||
} | ||
|
||
render(pointComponent, this.#pointListComponent.element); | ||
}; | ||
#modeChangeHandler = () => { | ||
this.#pointPresenters.forEach((presenter) => presenter.resetView()); | ||
}; | ||
} |
Oops, something went wrong.