Skip to content

Commit

Permalink
Большие перемены (часть 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunianka committed May 27, 2024
1 parent 32684ea commit c946d06
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 207 deletions.
99 changes: 57 additions & 42 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,79 @@ const CITIES = [
];

const OFFERS = [
'Order Uber',
'Add luggage',
'Switch to comfort',
'Rent a car',
'Add breakfast',
'Book tickets',
'Lunch in city',
'Upgrade to a business class'
'Order Uber',

Check failure on line 21 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Add luggage',

Check failure on line 22 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Switch to comfort',

Check failure on line 23 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Rent a car',

Check failure on line 24 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Add breakfast',

Check failure on line 25 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Book tickets',

Check failure on line 26 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Lunch in city',

Check failure on line 27 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
'Upgrade to a business class'

Check failure on line 28 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
];

const DESCRIPTION = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquet varius magna, non porta ligula feugiat eget. Fusce tristique felis at fermentum pharetra. Aliquam id orci ut lectus varius viverra.';

const Price = {
MIN: 1,
MAX: 1000
MIN: 1,

Check failure on line 34 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
MAX: 1000

Check failure on line 35 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
};

const TYPES = [
'taxi',
'bus',
'train',
'ship',
'drive',
'flight',
'check-in',
'sightseeing',
'restaurant'
'taxi',
'bus',
'train',
'ship',
'drive',
'flight',
'check-in',
'sightseeing',
'restaurant'
];

const DEFAULT_TYPE = 'flight';

const POINT_EMPTY = {
basePrice: 0,
dateFrom: null,
dateTo: null,
destination: null,
isFavorite: false,
offers: [],
type: DEFAULT_TYPE
basePrice: 0,
dateFrom: null,
dateTo: null,
destination: null,
isFavorite: false,
offers: [],
type: DEFAULT_TYPE
};

const FilterType = {
EVERYTHING: 'everything',
FUTURE: 'future',
PRESENT: 'present',
PAST: 'past'
EVERYTHING: 'everything',
FUTURE: 'future',
PRESENT: 'present',
PAST: 'past'
}

const Mode = {
DEFAULT: 'default',
EDITING: 'editing',
};

export {
OFFER_COUNT,
DESTINATION_COUNT,
POINT_COUNT,
CITIES,
OFFERS,
DESCRIPTION,
Price,
TYPES,
DEFAULT_TYPE,
POINT_EMPTY,
FilterType
const SortType = {
DAY: 'day',
EVENT: 'event',
TIME: 'time',
PRICE: 'price',
OFFERS: 'offers'
};

export {
OFFER_COUNT,
DESTINATION_COUNT,
POINT_COUNT,
CITIES,
OFFERS,
DESCRIPTION,
Price,
TYPES,
DEFAULT_TYPE,
POINT_EMPTY,
FilterType,
Mode,
SortType
}
117 changes: 117 additions & 0 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import PointView from '../view/point-view.js';
import EditPointView from '../view/edit-point-view.js';
import { remove, render, replace } from '../framework/render.js';
import { Mode } from '../const.js';

export default class PointPresenter {
#container = null;

#destinationsModel = null;
#offersModel = null;

#handleDataChange = null;
#handleModeChange = null;

#pointComponent = null;
#editPointComponent = null;
#point = null;
#mode = Mode.DEFAULT;

constructor({container, destinationsModel, offersModel, onDataChange, onModeChange}) {
this.#container = container;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#handleDataChange = onDataChange;
this.#handleModeChange = onModeChange;
}

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

const prevPointComponent = this.#pointComponent;
const prevEditPointComponent = this.#editPointComponent;

this.#pointComponent = new PointView({
point: this.#point,
pointDestination: this.#destinationsModel.getById(point.destination),
pointOffers: this.#offersModel.getByType(point.type),
onEditClick: this.#pointEditClickHandler,
onFavoriteClick: this.#favoriteClickHandler
});

this.#editPointComponent = new EditPointView({
point: this.#point,
pointDestination: this.#destinationsModel.getById(point.destination),
pointOffers: this.#offersModel.getByType(point.type),
onSubmitClick: this.#formSubmitHandler,
onResetClick: this.#resetButtonClickHandler
})

if (prevPointComponent === null || prevEditPointComponent === null) {
render(this.#pointComponent, this.#container);
return;
}

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

if (this.#mode === Mode.EDITING) {
replace(this.#pointComponent, prevEditPointComponent);
}

remove(prevPointComponent);
remove(prevEditPointComponent);
}

resetView = () => {
if (this.#mode !== Mode.DEFAULT) {
this.#replaceFormToPoint();
}
};

destroy = () => {
remove(this.#pointComponent);
remove(this.#editPointComponent);
}

#replacePointToForm = () => {
replace(this.#editPointComponent, this.#pointComponent);
document.addEventListener('keydown', this.#escKeyDownHandler);
this.#handleModeChange();
this.#mode = Mode.EDITING;
};

#replaceFormToPoint = () => {
replace(this.#pointComponent, this.#editPointComponent);
document.removeEventListener('keydown', this.#escKeyDownHandler);
this.#mode = Mode.DEFAULT;
};

#escKeyDownHandler = (evt) => {
if (evt.key === 'Escape') {
evt.preventDefault();
this.#replaceFormToPoint();
}
};

#pointEditClickHandler = () => {
this.#replacePointToForm();
};

#favoriteClickHandler = () => {
this.#handleDataChange({
...this.#point,
isFavorite: !this.#point.isFavorite
});
};

#formSubmitHandler = (point) => {
this.#handleDataChange(point);
this.#replaceFormToPoint();
}

#resetButtonClickHandler = () => {
this.#replaceFormToPoint();
}
}
122 changes: 57 additions & 65 deletions src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,81 @@
import SortView from '../view/sort-view.js';
import EditPointView from '../view/edit-point-view.js';
import PointView from '../view/point-view.js';
import TripView from '../view/point-list-view.js';
import EmptyListView from '../view/empty-list-view.js';
import PointPresenter from './point-presenter.js';
import { render, replace } from '../framework/render.js';
import { updateItem } from '../utils.js';
import PointListView from '../view/point-list-view.js';

export default class TripPresenter {
#tripContainer = null;
#destinationsModel = null;
#offersModel = null;
#pointsModel = null;
#pointListComponent = new TripView();
#sortComponent = new SortView();
#tripContainer = null;
#destinationsModel = null;
#offersModel = null;
#pointsModel = null;
#pointListComponent = new TripView();
#sortComponent = new SortView();

#points = [];
#points = [];

constructor({ tripContainer, destinationsModel, offersModel, pointsModel }) {
this.#tripContainer = tripContainer;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#pointsModel = pointsModel;
this.#points = [...this.#pointsModel.get()];
}
#pointPresenters = new Map();

init() {
if (this.#points.length === 0) {
render(new EmptyListView(), this.#tripContainer);
return;
constructor({tripContainer, destinationsModel, offersModel, pointsModel}) {
this.#tripContainer = tripContainer;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#pointsModel = pointsModel;
this.#points = [...this.#pointsModel.get()];
}

render(this.#sortComponent, this.#tripContainer);
render(this.#pointListComponent, this.#tripContainer);

this.#points.forEach((point) => {
this.#renderPoint(point);
});
}
init() {
this.#renderBoard();
}

#renderPoint = (point) => {
const pointComponent = new PointView({
point,
pointDestination: this.#destinationsModel.getById(point.destination),
pointOffers: this.#offersModel.getByType(point.type),
onEditClick: pointEditClickHandler
});
#renderPoint = (point) => {
const pointPresenter = new PointPresenter({
container: this.#pointListComponent.element,
destinationsModel: this.#destinationsModel,
offersModel: this.#offersModel,
onDataChange: this.#pointChangeHandler,
onModeChange: this.#modeChangeHandler
});

const editPointComponent = new EditPointView({
point,
pointDestination: this.#destinationsModel.getById(point.destination),
pointOffers: this.#offersModel.getByType(point.type),
onSubmitClick: pointSubmitHandler,
onResetClick: resetButtonClickHandler
});
pointPresenter.init(point);

const replacePointToForm = () => {
replace(editPointComponent, pointComponent);
};

const replaceFormToPoint = () => {
replace(pointComponent, editPointComponent);
};
this.#pointPresenters.set(point.id, pointPresenter);
}

const escKeyDownHandler = (evt) => {
if (evt.key === 'Escape' || evt.key === 'Esc') {
evt.preventDefault();
replaceFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
}
#renderPoints = () => {
this.#points.forEach((point) => {
this.#renderPoint(point);
});
};

function pointEditClickHandler() {
replacePointToForm();
document.addEventListener('keydown', escKeyDownHandler);
#clearPoints = () => {
this.#pointPresenters.forEach((presenter) => presenter.destroy());
this.#pointPresenters.clear();
}

function resetButtonClickHandler() {
replaceFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
#renderPointContainer = () => {
this.#pointListComponent = new PointListView();
render(this.#pointListComponent, this.#tripContainer);
}

function pointSubmitHandler() {
replaceFormToPoint();
document.removeEventListener('keydown', escKeyDownHandler);
#renderBoard = () => {
if (this.#points.length === 0) {
render(new EmptyListView(), this.#tripContainer);
return;
}

this.#renderPointContainer();
this.#renderPoints();
};

#pointChangeHandler = (updatedPoint) => {
this.#points = updateItem(this.#points, updatedPoint);
this.#pointPresenters.get(updatedPoint.id).init(updatedPoint);
}

render(pointComponent, this.#pointListComponent.element);
};
#modeChangeHandler = () => {
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};
}
Loading

0 comments on commit c946d06

Please sign in to comment.