-
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 #11 from KseniaTry/module7-task1
- Loading branch information
Showing
23 changed files
with
588 additions
and
313 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 +1,46 @@ | ||
import NewFilters from './view/filters-view'; | ||
import NewTripInfo from './view/trip-info-view'; | ||
import { RenderPosition, render } from './framework/render'; | ||
import MainPresenter from './presenter/main-presenter'; | ||
import PointModel from './model/point-model'; | ||
import { generateFilter } from './mock/filter-mock'; | ||
import FiltersModel from './model/filters-model'; | ||
import FiltersPresenter from './presenter/filters-presenter'; | ||
import AddNewPointButtonView from './view/add-new-point-button-view'; | ||
|
||
const mainContainer = document.querySelector('.trip-main'); | ||
const filtersContainer = document.querySelector('.trip-controls__filters'); | ||
const pointsContainer = document.querySelector('.trip-events'); | ||
|
||
const pointModel = new PointModel(); | ||
const filtersModel = new FiltersModel(); | ||
|
||
const addNewPointButton = new AddNewPointButtonView({ | ||
onClick: onNewPointButtonClick, | ||
}); | ||
|
||
const mainPresenter = new MainPresenter({ | ||
pointsContainer: pointsContainer, | ||
pointModel, | ||
filtersModel, | ||
onNewPointCancel: cancelNewPoint, | ||
}); | ||
|
||
const filtersPresenter = new FiltersPresenter({ | ||
filtersContainer: filtersContainer, | ||
pointModel, | ||
filtersModel, | ||
}); | ||
const filters = generateFilter(pointModel.points); | ||
|
||
render(new NewTripInfo(), mainContainer, RenderPosition.AFTERBEGIN); | ||
render(new NewFilters({filters}), filtersContainer); | ||
render(addNewPointButton, mainContainer); | ||
|
||
function onNewPointButtonClick() { | ||
mainPresenter.createPoint(); | ||
addNewPointButton.element.disabled = true; | ||
} | ||
|
||
function cancelNewPoint() { | ||
addNewPointButton.element.disabled = false; | ||
} | ||
|
||
filtersPresenter.init(); | ||
mainPresenter.init(); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Observable from '../framework/observable'; | ||
import { FilterType } from '../const'; | ||
|
||
export default class FiltersModel extends Observable { | ||
#filter = FilterType.EVERYTHING; | ||
|
||
get filter() { | ||
return this.#filter; | ||
} | ||
|
||
setFilter(updateType, filter) { | ||
this.#filter = filter; | ||
this._notify(updateType, filter); | ||
} | ||
} |
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,21 +1,52 @@ | ||
import { getPoints } from '../mock/point-mock'; | ||
import { getDestinations } from '../mock/destinations-mock'; | ||
import { getOffers } from '../mock/offers-mock'; | ||
import Observable from '../framework/observable'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
export default class PointModel { | ||
export default class PointModel extends Observable { | ||
#points = getPoints(); | ||
#destinations = getDestinations(); | ||
#offers = getOffers(); | ||
#allDestinations = getDestinations(); | ||
#allOffers = getOffers(); | ||
|
||
get points() { | ||
return this.#points; | ||
} | ||
|
||
updatePoint(updateType, updatedPoint) { | ||
const pointIndex = this.#points.findIndex((point) => point.id === updatedPoint.id); | ||
|
||
this.#points = [ | ||
...this.#points.slice(0, pointIndex), | ||
updatedPoint, | ||
...this.#points.slice(pointIndex + 1), | ||
]; | ||
|
||
this._notify(updateType, updatedPoint); | ||
} | ||
|
||
addPoint(updateType, updatedPoint) { | ||
this.#points = [{id: nanoid(), ...updatedPoint}, ...this.#points]; | ||
|
||
this._notify(updateType, updatedPoint); | ||
} | ||
|
||
deletePoint(updateType, updatedPoint) { | ||
const pointIndex = this.#points.findIndex((point) => point.id === updatedPoint.id); | ||
|
||
this.#points = [ | ||
...this.#points.slice(0, pointIndex), | ||
...this.#points.slice(pointIndex + 1), | ||
]; | ||
|
||
this._notify(updateType, updatedPoint); | ||
} | ||
|
||
get destinations() { | ||
return this.#destinations; | ||
return this.#allDestinations; | ||
} | ||
|
||
get offers() { | ||
return this.#offers; | ||
return this.#allOffers; | ||
} | ||
} |
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,60 @@ | ||
import { render, replace, remove } from '../framework/render'; | ||
import FiltersView from '../view/filters-view'; | ||
import { UpdateType } from '../const'; | ||
import { filter } from '../utils/filter-utils'; | ||
|
||
export default class FiltersPresenter { | ||
#filtersModel = null; | ||
#filtersComponent = null; | ||
#filtersContainer = null; | ||
#pointModel = null; | ||
|
||
constructor({ filtersContainer, pointModel, filtersModel }) { | ||
this.#filtersContainer = filtersContainer; | ||
this.#pointModel = pointModel; | ||
this.#filtersModel = filtersModel; | ||
|
||
this.#pointModel.addObserver(this.#handleModelEvent); | ||
this.#filtersModel.addObserver(this.#handleModelEvent); | ||
} | ||
|
||
get filters() { | ||
const points = this.#pointModel.points; | ||
|
||
return Object.entries(filter).map( | ||
([filterType, filterPoints]) => ({ | ||
type: filterType, | ||
count: filterPoints(points).length, | ||
}), | ||
); | ||
} | ||
|
||
init() { | ||
const prevFiltersComponent = this.#filtersComponent; | ||
|
||
this.#filtersComponent = new FiltersView({ | ||
filters: this.filters, | ||
onFiltersChange: this.#handleFiltersChange | ||
}); | ||
|
||
if(prevFiltersComponent === null){ | ||
render(this.#filtersComponent, this.#filtersContainer); | ||
return; | ||
} | ||
|
||
replace(this.#filtersComponent, prevFiltersComponent); | ||
remove(prevFiltersComponent); | ||
} | ||
|
||
#handleModelEvent = () => { | ||
this.init(); | ||
}; | ||
|
||
#handleFiltersChange = (filterType) => { | ||
if (this.#filtersModel.filter === filterType) { | ||
return; | ||
} | ||
|
||
this.#filtersModel.setFilter(UpdateType.MAJOR, filterType); | ||
}; | ||
} |
Oops, something went wrong.