Skip to content

Commit

Permalink
Merge pull request #11 from vvoyage/module6-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Oct 10, 2024
2 parents 17c8cd2 + 1d731cb commit 3a97bf4
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/presenter/filters-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export default class FiltersPresenter {
items: this.#filters
}), filtersContainer);
}

}
71 changes: 44 additions & 27 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,31 @@ export default class PointPresenter {
#pointEditorComponent = null;
#point = null;
#onPointChange = null;
#onEditorOpen = null;
#destinationsModel = null;
#offersModel = null;
#mode = PointMode.IDLE;

constructor({ container, destinationsModel, offersModel, onPointChange }) {
constructor({
container,
destinationsModel,
offersModel,
onPointChange,
onEditorOpen,
}) {
this.#container = container;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#onPointChange = onPointChange;
this.#onEditorOpen = onEditorOpen;
}

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

this.#pointComponent = new PointView({
point: this.#point,
destination: this.#destinationsModel.getById(point.destination),
offers: this.#offersModel.getByType(point.type),
onEditClick: this.#pointEditHandler,
});
this.#pointComponent = this.#createPointView();

this.#pointEditorComponent = new PointEditorView({
point: this.#point,
destination: this.#destinationsModel.getById(point.destination),
offers: this.#offersModel.getByType(point.type),
onCloseClick: this.#pointCloseHandler,
onSubmitForm: this.#pointSubmitHandler,
});
this.#pointEditorComponent = this.#createPointEditorView();

if (this.#mode === PointMode.EDITABLE) {
render(this.#pointEditorComponent, this.#container);
Expand All @@ -49,20 +46,9 @@ export default class PointPresenter {
update(point) {
this.#point = point;

const updatedPointComponent = new PointView({
point: this.#point,
destination: this.#destinationsModel.getById(point.destination),
offers: this.#offersModel.getByType(point.type),
onEditClick: this.#pointEditHandler,
});
const updatedPointComponent = this.#createPointView();

const updatedEditorComponent = new PointEditorView({
point: this.#point,
destination: this.#destinationsModel.getById(point.destination),
offers: this.#offersModel.getByType(point.type),
onCloseClick: this.#pointCloseHandler,
onSubmitForm: this.#pointSubmitHandler,
});
const updatedEditorComponent = this.#createPointEditorView();

if (this.#mode === PointMode.EDITABLE) {
replace(updatedEditorComponent, this.#pointEditorComponent);
Expand All @@ -79,6 +65,32 @@ export default class PointPresenter {
remove(this.#pointEditorComponent);
}

resetView() {
if (this.#mode !== PointMode.IDLE) {
this.#replaceEditorByPoint();
}
}

#createPointView() {
return new PointView({
point: this.#point,
destination: this.#destinationsModel.getById(this.#point.destination),
offers: this.#offersModel.getByType(this.#point.type),
onEditClick: this.#pointEditHandler,
onFavoriteToggle: this.#pointFavoriteToggleHandler,
});
}

#createPointEditorView() {
return new PointEditorView({
point: this.#point,
destinations: this.#destinationsModel.get(),
offers: this.#offersModel.get(),
onCloseClick: this.#pointCloseHandler,
onSubmitForm: this.#pointSubmitHandler,
});
}

#replacePointByEditor() {
replace(this.#pointEditorComponent, this.#pointComponent);
document.addEventListener('keydown', this.#escKeyDownHandler);
Expand All @@ -99,6 +111,7 @@ export default class PointPresenter {
};

#pointEditHandler = () => {
this.#onEditorOpen();
this.#replacePointByEditor();
};

Expand All @@ -110,4 +123,8 @@ export default class PointPresenter {
#pointCloseHandler = () => {
this.#replaceEditorByPoint();
};

#pointFavoriteToggleHandler = (isFavorite) => {
this.#onPointChange({ ...this.#point, isFavorite });
};
}
21 changes: 16 additions & 5 deletions src/presenter/route-presenter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { remove, render } from '../framework/render.js';
import {
remove,
render,
} from '../framework/render.js';
import EventsListView from '../view/events-list-view.js';
import EventsListEmptyView from '../view/events-list-empty-view.js';
import { SORTING_COLUMNS, SortType } from '../const.js';
Expand All @@ -17,14 +20,17 @@ export default class RoutePresenter {
#sortingComponent = null;
#pointsPresenters = new Map();

constructor({ container, pointsModel, offersModel, destinationsModel }) {
constructor({
container,
pointsModel,
offersModel,
destinationsModel
}) {
this.#container = container;
this.#pointsModel = pointsModel;
this.#offersModel = offersModel;
this.#destinationsModel = destinationsModel;
this.#points = sortByType[this.#currentSortType]([
...this.#pointsModel.get(),
]);
this.#points = sortByType[this.#currentSortType]([...this.#pointsModel.get()]);
}

init() {
Expand Down Expand Up @@ -75,12 +81,17 @@ export default class RoutePresenter {
destinationsModel: this.#destinationsModel,
offersModel: this.#offersModel,
onPointChange: this.#pointChangeHandler,
onEditorOpen: this.#pointEditHandler,
});

pointPresenter.init(point);
this.#pointsPresenters.set(point.id, pointPresenter);
}

#pointEditHandler = () => {
this.#pointsPresenters.forEach((presenter) => presenter.resetView());
};

#pointChangeHandler = (updatedPoint) => {
this.#points = updateItem(this.#points, updatedPoint);
this.#pointsPresenters.get(updatedPoint.id).update(updatedPoint);
Expand Down
Loading

0 comments on commit 3a97bf4

Please sign in to comment.