-
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
136 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SortType } from '../const.js'; | ||
import { getPointsDateDifference, getPointsDurationDifference, getPointsPriceDifference } from '../utils.js'; | ||
|
||
const sort = { | ||
[SortType.DAY]: (points) => points.sort(getPointsDateDifference), | ||
[SortType.PRICE]: (points) => points.sort(getPointsPriceDifference), | ||
[SortType.TIME]: (points) => points.sort(getPointsDurationDifference), | ||
[SortType.EVENT]: () => { | ||
throw new Error(`Sort by ${SortType.EVENT} is not implemented`); | ||
}, | ||
[SortType.OFFERS]: () => { | ||
throw new Error(`Sort by ${SortType.OFFERS} is not implemented`); | ||
} | ||
}; | ||
|
||
export { sort }; |
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,35 +1,64 @@ | ||
import AbstractView from '../framework/view/abstract-view.js'; | ||
import { EnabledSortType, SortType } from '../const.js'; | ||
|
||
const createSortTemplate = () => { | ||
const createSortItemsTemplate = ({ items }) => { | ||
const sortItems = items.map(sortItem => { | ||
return ( | ||
`<div class="trip-sort__item trip-sort__item--${sortItem.type}"> | ||
<input | ||
id="sort-${sortItem.type}" | ||
class="trip-sort__input visually-hidden" | ||
type="radio" | ||
name="trip-sort" | ||
value="sort-${sortItem.type}" | ||
data-sort-type="${sortItem.type}" | ||
${(sortItem.isChecked) ? 'checked' : ''} | ||
${(sortItem.isDisabled) ? 'disabled' : ''} | ||
> | ||
<label | ||
class="trip-sort__btn" | ||
for="sort-${sortItem.type}">${sortItem.type}</label> | ||
</div>` | ||
) | ||
}).join(''); | ||
|
||
return sortItems; | ||
} | ||
|
||
const createSortTemplate = ({ items }) => { | ||
return ( | ||
`<form class="trip-events__trip-sort trip-sort" action="#" method="get"> | ||
<div class="trip-sort__item trip-sort__item--day"> | ||
<input id="sort-day" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-day"> | ||
<label class="trip-sort__btn" for="sort-day">Day</label> | ||
</div> | ||
<div class="trip-sort__item trip-sort__item--event"> | ||
<input id="sort-event" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-event" disabled> | ||
<label class="trip-sort__btn" for="sort-event">Event</label> | ||
</div> | ||
<div class="trip-sort__item trip-sort__item--time"> | ||
<input id="sort-time" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-time"> | ||
<label class="trip-sort__btn" for="sort-time">Time</label> | ||
</div> | ||
<div class="trip-sort__item trip-sort__item--price"> | ||
<input id="sort-price" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-price" checked> | ||
<label class="trip-sort__btn" for="sort-price">Price</label> | ||
</div> | ||
<div class="trip-sort__item trip-sort__item--offer"> | ||
<input id="sort-offer" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-offer" disabled> | ||
<label class="trip-sort__btn" for="sort-offer">Offers</label> | ||
</div> | ||
</form>` | ||
${createSortItemsTemplate({ items })} | ||
</form>` | ||
); | ||
}; | ||
} | ||
|
||
export default class SortView extends AbstractView { | ||
#items = null; | ||
#onItemChange = null; | ||
|
||
constructor({ sortType, onItemChange }) { | ||
super(); | ||
|
||
this.#items = Object.values(SortType).map((type) => ({ | ||
type, | ||
isChecked: (type === sortType), | ||
isDisabled: !EnabledSortType[type] | ||
})); | ||
|
||
this.#onItemChange = onItemChange; | ||
|
||
this.element.addEventListener('change', this.#itemChangeHandler); | ||
} | ||
|
||
get template() { | ||
return createSortTemplate(); | ||
return createSortTemplate({ | ||
items: this.#items | ||
}); | ||
} | ||
|
||
#itemChangeHandler = (evt) => { | ||
evt.preventDefault(); | ||
this.#onItemChange(evt.target.dataset.sortType); | ||
}; | ||
} |