-
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 Borisod/module5-task2
- Loading branch information
Showing
9 changed files
with
193 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const SortType = { | ||
DAY: 'day', | ||
PRICE: 'price', | ||
TIME: 'time', | ||
}; | ||
|
||
export const SORT_OPTIONS = [ | ||
{ id: 'sort-day', value: 'day', label: 'Day', isDisabled: false, isChecked: true }, | ||
{ id: 'sort-event', value: 'event', label: 'Event', isDisabled: true, isChecked: false }, | ||
{ id: 'sort-time', value: 'time', label: 'Time', isDisabled: false, isChecked: false }, | ||
{ id: 'sort-price', value: 'price', label: 'Price', isDisabled: false, isChecked: false }, | ||
{ id: 'sort-offer', value: 'offer', label: 'Offers', isDisabled: true, isChecked: false }, | ||
]; |
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,11 +1,11 @@ | ||
import GeneralPresenter from './presenter/general-presenter'; | ||
import BoardPresenter from './presenter/board-presenter'; | ||
import PointModel from './model/model'; | ||
|
||
const filtersContainer = document.querySelector('.trip-controls__filters'); | ||
const eventsContainer = document.querySelector('.trip-events'); | ||
const model = new PointModel(); | ||
model.init(); | ||
const generalPresenter = new GeneralPresenter({filtersContainer, eventsContainer, model}); | ||
const boardPresenter = new BoardPresenter({filtersContainer, eventsContainer, model}); | ||
|
||
generalPresenter.init(); | ||
boardPresenter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import FiltersView from '../view/filters-view.js'; | ||
import SortView from '../view/sorts-view.js'; | ||
import NoEventItemView from '../view/no-events-item-view.js'; | ||
import EventListView from '../view/events-list-view.js'; | ||
import { RenderPosition } from '../render.js'; | ||
import { render } from '../framework/render.js'; | ||
import EventPresenter from './event-presenter.js'; | ||
import {SortType} from '../const.js'; | ||
|
||
export default class BoardPresenter { | ||
#filtersContainer = null; | ||
#eventsContainer = null; | ||
#eventsListView = new EventListView(); | ||
#model = null; | ||
#eventPresenters = new Map(); | ||
#currentSortType = 'day'; | ||
|
||
constructor({ filtersContainer, eventsContainer, model }) { | ||
this.#filtersContainer = filtersContainer; | ||
this.#eventsContainer = eventsContainer; | ||
this.#model = model; | ||
} | ||
|
||
init() { | ||
render(new FiltersView(), this.#filtersContainer, RenderPosition.BEFOREEND); | ||
|
||
if (this.#model.events.length === 0) { | ||
render(new NoEventItemView(), this.#eventsContainer); | ||
return; | ||
} | ||
|
||
const sortView = new SortView({ | ||
onSortTypeChange: this.#handleSortTypeChange, | ||
}); | ||
render(sortView, this.#eventsContainer); | ||
|
||
render(this.#eventsListView, this.#eventsContainer); | ||
|
||
this.#renderTripEvents(); | ||
} | ||
|
||
#renderTripEvents() { | ||
this.#clearTripEvents(); | ||
|
||
const sortedEvents = this.#getSortedEvents(); | ||
sortedEvents.forEach((event) => { | ||
this.#renderEvent(event); | ||
}); | ||
} | ||
|
||
#renderEvent(event) { | ||
const eventPresenter = new EventPresenter({ | ||
eventsContainer: this.#eventsListView.element, | ||
model: this.#model, | ||
onEventUpdate: this.#handleEventUpdate, | ||
onEdit: this.#handleEditEvent, | ||
}); | ||
eventPresenter.init(event); | ||
this.#eventPresenters.set(event.id, eventPresenter); | ||
} | ||
|
||
#clearTripEvents() { | ||
this.#eventPresenters.forEach((presenter) => presenter.resetView()); | ||
this.#eventPresenters.clear(); | ||
this.#eventsListView.element.replaceChildren(); | ||
} | ||
|
||
#getSortedEvents() { | ||
const events = [...this.#model.events]; | ||
|
||
switch (this.#currentSortType) { | ||
case SortType.TIME: | ||
return events.sort( | ||
(a, b) => | ||
(new Date(b.dateTo) - new Date(b.dateFrom)) - | ||
(new Date(a.dateTo) - new Date(a.dateFrom)) | ||
); | ||
case SortType.PRICE: | ||
return events.sort((a, b) => b.basePrice - a.basePrice); | ||
case SortType.DAY: | ||
default: | ||
return events.sort((a, b) => new Date(a.dateFrom) - new Date(b.dateFrom)); | ||
} | ||
} | ||
|
||
#handleSortTypeChange = (sortType) => { | ||
if (this.#currentSortType === sortType) { | ||
return; | ||
} | ||
|
||
this.#currentSortType = sortType; | ||
this.#renderTripEvents(); | ||
}; | ||
|
||
#handleEditEvent = () => { | ||
this.#eventPresenters.forEach((presenter) => presenter.resetView()); | ||
}; | ||
|
||
#handleEventUpdate = (updatedEvent) => { | ||
this.#model.updateEvent(updatedEvent); | ||
|
||
if (this.#eventPresenters.has(updatedEvent.id)) { | ||
const presenter = this.#eventPresenters.get(updatedEvent.id); | ||
presenter.init(updatedEvent); | ||
} | ||
}; | ||
} |
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 was deleted.
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
Oops, something went wrong.