Skip to content

Commit

Permalink
Merge pull request #7 from MaxSiryatov/module5-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 27, 2024
2 parents 08e8d32 + ab3c449 commit b99ed8c
Show file tree
Hide file tree
Showing 35 changed files with 214 additions and 95 deletions.
7 changes: 6 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ for (let i = 0; i < IMAGE_COUNT; i++) {
IMAGES.push(`${IMAGE_URL}${getRandomValue()}`);
}

export {POINTS_COUNT, CITIES_COUNT, OFFERS_COUNT, IMAGE_COUNT, POINTS_TYPE, DESTINATIONS, OFFERS, DESCRIPTION, IMAGE_URL, IMAGES, FilterType};
const Mode = {
DEFAULT: 'DEFAULT',
EDITING: 'EDITING'
};

export { POINTS_COUNT, CITIES_COUNT, OFFERS_COUNT, IMAGE_COUNT, POINTS_TYPE, DESTINATIONS, OFFERS, DESCRIPTION, IMAGE_URL, IMAGES, FilterType, Mode };
2 changes: 1 addition & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const filter = {
[FilterType.PRESENT] : (point) => dayjs().isAfter(dayjs(point.dateFrom)) && dayjs().isBefore(dayjs(point.dateTo))
};

export { filter };
export { filter };
1 change: 1 addition & 0 deletions src/framework/ui-blocker/ui-blocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ export default class UiBlocker {
this.#element.classList.remove('ui-blocker--on');
};
}

2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const tripPresenter = new TripPresenter({
container: tripInfoContainer,
pointModel
});
tripPresenter.init();
tripPresenter.init();
2 changes: 1 addition & 1 deletion src/mock/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ function getRandomDestination() {
});
}

export {getRandomDestination};
export {getRandomDestination};
2 changes: 1 addition & 1 deletion src/mock/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ function getRandomOffer() {
});
}

export {getRandomOffer};
export {getRandomOffer};
2 changes: 1 addition & 1 deletion src/mock/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ function getRandomPoint(destinationID, offersID) {
});
}

export {getRandomPoint};
export {getRandomPoint};
2 changes: 1 addition & 1 deletion src/model/destination-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export default class DestinationModel {
});
return temp;
}
}
}
2 changes: 1 addition & 1 deletion src/model/offer-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export default class OfferModel {
this.offersIds = offersArr.map((offer) => offer.id);
return this.offersIds;
}
}
}
2 changes: 1 addition & 1 deletion src/model/point-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export default class PointModel {
getPoint() {
return this.points[0];
}
}
}
116 changes: 116 additions & 0 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import PointEditView from '../view/point-edit-view.js';
import PointView from '../view/point-view.js';
import OpenFormBtnView from '../view/open-form-button-view.js';
import CloseFormBtnView from '../view/close-form-button-view.js';
import SaveFormBtnView from '../view/save-form-button-view.js';
import { RenderPosition, remove, render, replace } from '../framework/render.js';
import { Mode } from '../const.js';

export default class PointPresenter {

Check failure on line 10 in src/presenter/point-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Trailing spaces not allowed
#pointsListView;
#handlePointChange;
#handleModeChange;

#point;
#mode = Mode.DEFAULT;

#pointComponent;
#pointEditComponent;

constructor ({pointListContainer, onDataChange, onModeChange}) {
this.#pointsListView = pointListContainer;
this.#handlePointChange = onDataChange;
this.#handleModeChange = onModeChange;
}

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

const prevPointComponent = this.#pointComponent;
const prevEditFormComponent = this.#pointEditComponent;

const escKeyDownHandler = (evt) => {
if (evt.key === 'Escape') {
this.#replacePointToEditForm();
document.removeEventListener('keydown', escKeyDownHandler);
}
};

this.#pointComponent = new PointView({
data: this.#point,
onFavouriteClick: this.#onFavouriteClick,
});

this.#pointEditComponent = new PointEditView({
data: this.#point,
onSubmit: () => {
this.#replaceEditFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
}
});

const deleteBtn = this.#pointEditComponent.element.querySelector('.event__reset-btn');

const openBtn = new OpenFormBtnView({
onClick: () => {
this.#replacePointToEditForm();
document.addEventListener('keydown', escKeyDownHandler);
}});

const closeBtn = new CloseFormBtnView({
onClick: () => {
this.#replaceEditFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
}});

const saveBtn = new SaveFormBtnView();

render(openBtn, this.#pointComponent.element, RenderPosition.BEFOREEND);
render(saveBtn, deleteBtn, RenderPosition.BEFOREBEGIN);
render(closeBtn, deleteBtn, RenderPosition.AFTEREND);

if (!prevPointComponent || !prevEditFormComponent) {
render(this.#pointComponent, this.#pointsListView);
return;
}

if (this.#mode === Mode.DEFAULT) {
replace(this.#pointComponent, prevPointComponent);
}

if (this.#mode === Mode.EDITING) {
replace(this.#pointEditComponent, prevEditFormComponent);
}

remove(prevEditFormComponent);
remove(prevPointComponent);
}

resetView() {
if (this.#mode === Mode.EDITING) {
this.#replaceEditFormToPoint();
}
}

destroy() {
remove(this.#pointComponent);
remove(this.#pointEditComponent);
}

#replaceEditFormToPoint() {
replace(this.#pointComponent, this.#pointEditComponent);
this.#mode = Mode.DEFAULT;
}

#replacePointToEditForm() {
replace(this.#pointEditComponent, this.#pointComponent);
this.#handleModeChange();
this.#mode = Mode.EDITING;
}

#onFavouriteClick = () => {
this.#handlePointChange({...this.#point, isFavorite: !this.#point.isFavorite});
};

}
72 changes: 27 additions & 45 deletions src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import EventListView from '../view/event-list-view.js';
import PointEditView from '../view/point-edit-view.js';
import SortView from '../view/sort-view.js';
import PointView from '../view/point-view.js';
import FilterFormView from '../view/filter-form-view.js';
import FilterItemView from '../view/filter-item-view.js';
import OpenFormBtnView from '../view/open-form-button-view.js';
import CloseFormBtnView from '../view/close-form-button-view.js';
import SaveFormBtnView from '../view/save-form-button-view.js';
import TripInfoView from '../view/trip-info-view.js';
import EmptyPointListView from '../view/empty-point-list-view.js';
import { RenderPosition, render, replace } from '../framework/render.js';
import { generateFilter } from '../mock/filters.js';
import PointPresenter from './point-presenter.js';
import { updateItem } from '../utils.js';
import { RenderPosition, render } from '../framework/render.js';

export default class TripPresenter {
#eventListView = new EventListView();
#emptyPointListView = new EmptyPointListView();
#tripInfoView = new TripInfoView();
#filterFormView = new FilterFormView();
#sortView = new SortView();
#container = null;
#pointModel = null;
#pointPresenters = new Map();
#container;
#pointModel;
#tripPoint = [];

constructor ({container, pointModel}) {
Expand All @@ -29,10 +27,13 @@ export default class TripPresenter {

init() {
this.#tripPoint = [...this.#pointModel.getPoints()];

const tripControls = document.querySelector('.trip-main__trip-controls');
const filterFormContainer = document.querySelector('.trip-controls__filters');
const filters = generateFilter(this.#tripPoint);

render(this.#filterFormView, filterFormContainer);

for (let i = 0; i < filters.length; i++) {
render(new FilterItemView(filters[i]), this.#filterFormView.element);
}
Expand All @@ -41,53 +42,34 @@ export default class TripPresenter {
render(this.#tripInfoView, tripControls, RenderPosition.BEFOREBEGIN);
render(this.#sortView, this.#container);
render(this.#eventListView, this.#container);

for (let i = 0; i < this.#tripPoint.length; i++) {
this.#renderPoint(this.#tripPoint[i]);
}
}

else {
render(this.#emptyPointListView, this.#container);
}
}

#renderPoint (point) {
const pointComponent = new PointView({data: point});
const escKeyDownBtnHandler = (evt) => {
if (evt.key === 'Escape') {
replacePointToForm();
document.removeEventListener('keydown', escKeyDownBtnHandler);
}
};
const formComponent = new PointEditView({
data: point,
onSubmit: () => {
replacePointToForm();
document.removeEventListener('keydown', escKeyDownBtnHandler);
}});
const deleteBtn = formComponent.element.querySelector('.event__reset-btn');
const openBtn = new OpenFormBtnView({
onClick: () => {
replaceFormToPoint();
document.addEventListener('keydown', escKeyDownBtnHandler);
}});
const closeBtn = new CloseFormBtnView({
onClick: () => {
replacePointToForm();
document.removeEventListener('keydown', escKeyDownBtnHandler);
}});
const saveBtn = new SaveFormBtnView();
const pointPresenter = new PointPresenter({
pointListContainer: this.#eventListView.element,
onDataChange: this.#handlePointChange,
onModeChange: this.#handleModeChange
});

render(pointComponent, this.#eventListView.element);
render(openBtn, pointComponent.element, RenderPosition.BEFOREEND);
render(saveBtn, deleteBtn, RenderPosition.BEFOREBEGIN);
render(closeBtn, deleteBtn, RenderPosition.AFTEREND);
pointPresenter.init(point);
this.#pointPresenters.set(point.id, pointPresenter);
}

function replacePointToForm() {
replace(pointComponent, formComponent);
}
#handlePointChange = (updatedPoint) => {
this.#tripPoint = updateItem(this.#tripPoint, updatedPoint);
this.#pointPresenters.get(updatedPoint.id).init(updatedPoint);
};

function replaceFormToPoint() {
replace(formComponent, pointComponent);
}
}
}
#handleModeChange = () => {
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};
}
6 changes: 6 additions & 0 deletions src/templates/close-form-button-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function createCloseFormBtn() {
return `<button class="event__rollup-btn" type="button">

Check failure on line 2 in src/templates/close-form-button-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
<span class="visually-hidden">Open event</span>
</button>`;
}

Check failure on line 5 in src/templates/close-form-button-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 0 spaces but found 2

Check failure on line 6 in src/templates/close-form-button-template.js

View workflow job for this annotation

GitHub Actions / Check

Trailing spaces not allowed

Check failure on line 6 in src/templates/close-form-button-template.js

View workflow job for this annotation

GitHub Actions / Check

Newline required at end of file but not found
3 changes: 3 additions & 0 deletions src/templates/empty-point-list-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function createEmptyPointsListTemplate() {
return '<p class="trip-events__msg">Click New Event to create your first point</p>';

Check failure on line 2 in src/templates/empty-point-list-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
}

Check failure on line 3 in src/templates/empty-point-list-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 0 spaces but found 2
3 changes: 2 additions & 1 deletion src/templates/event-list-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ function createEventListTemplate () {
return '<ul class="trip-events__list"></ul>';

Check failure on line 2 in src/templates/event-list-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
}

Check failure on line 3 in src/templates/event-list-template.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 0 spaces but found 2

export { createEventListTemplate };
export { createEventListTemplate };

2 changes: 1 addition & 1 deletion src/templates/filter-form-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ function createFilterFormTemplate () {
</form>`;
}

export {createFilterFormTemplate};
export {createFilterFormTemplate};
2 changes: 1 addition & 1 deletion src/templates/filter-item-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ function isAvailible(filter) {
return filter.exists ? '' : 'disabled';
}

export {createFilterItemTemplate};
export {createFilterItemTemplate};
5 changes: 5 additions & 0 deletions src/templates/open-form-button-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function createOpenFormBtn() {
return `<button class="event__rollup-btn" type="button">
<span class="visually-hidden">Open event</span>
</button>`;
}
2 changes: 1 addition & 1 deletion src/templates/point-edit-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ function isChecked(offers, title) {
});
return check ? 'checked' : '';
}
export {createPointEditTemplate};
export {createPointEditTemplate};
2 changes: 1 addition & 1 deletion src/templates/point-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ function isFavorite(check) {
return check ? 'event__favorite-btn--active' : '';
}

export {createPointTemplate};
export {createPointTemplate};
3 changes: 3 additions & 0 deletions src/templates/save-form-button-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function createSaveFormBtn() {
return '<button class="event__save-btn btn btn--blue" type="submit">Save</button>';
}
2 changes: 1 addition & 1 deletion src/templates/sort-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ const createSortTemplate = () => `
</form>
`;

export {createSortTemplate};
export {createSortTemplate};
3 changes: 2 additions & 1 deletion src/templates/trip-info-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ function createTripInfoTemplate () {
</section>`;
}

export {createTripInfoTemplate};
export {createTripInfoTemplate};

6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ function getDate({next}) {
return date;
}

export { getDateDiff, getTime, getMonthAndDay, getFullDate, getRandomArrayElement, getDate, getRandomValue };
function updateItem (items, update) {
return items.map((item) => item.id === update.id ? update : item);
}

export { getDateDiff, getTime, getMonthAndDay, getFullDate, getRandomArrayElement, getDate, getRandomValue, updateItem };
Loading

0 comments on commit b99ed8c

Please sign in to comment.