Skip to content

Commit

Permalink
Merge pull request #11 from dashuulka/module7-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 26, 2024
2 parents 696249d + 8ea8659 commit 48f1512
Show file tree
Hide file tree
Showing 24 changed files with 530 additions and 135 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"dayjs": "1.11.10",
"flatpickr": "^4.6.13",
"he": "^1.2.0",
"nanoid": "^5.0.7"
}
}
20 changes: 17 additions & 3 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ const OFFERS = [
'Travel by train',
];

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

const Mode = {
DEFAULT: 'DEFAULT',
Expand All @@ -75,6 +75,18 @@ const SortType = [{
PRICE: 'price'
}];

const UserAction = {
UPDATE_POINT: 'UPDATE_POINT',
ADD_POINT: 'ADD_POINT',
DELETE_POINT: 'DELETE_POINT',
};

const UpdateType = {
PATCH: 'PATCH',
MINOR: 'MINOR',
MAJOR: 'MAJOR',
};

export {POINT_EMPTY,
POINT_TYPE,
DESTINATION,
Expand All @@ -85,4 +97,6 @@ export {POINT_EMPTY,
POINTS_COUNT,
FilterType,
Mode,
SortType};
SortType,
UserAction,
UpdateType};
47 changes: 40 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,67 @@
import FilterPointsView from './view/filter-points-view.js';
//import FilterPointsView from './view/filter-points-view.js';
import TripInfoView from './view/trip-info-view.js';
import TripPresenter from './presenter/trip-presenter.js';
import FilterPresenter from './presenter/filter-presenter.js';
import NewPointButtonView from './view/new-point-button-view.js';

import { render, RenderPosition } from './framework/render.js';

import PointsModel from './model/point-model.js';
import DestinationModel from './model/destination-model.js';
import OffersModel from './model/offer-model.js';
import FilterModel from './model/filter-model.js';

import { generateFilters } from './mock/filter.js';
//import { generateFilters } from './mock/filter.js';

const filterHeaderElement = document.querySelector('.trip-controls');
const siteFilterElement = filterHeaderElement.querySelector('.trip-controls__filters');
/* const filters = [
{
type: 'everything',
name: 'EVERYTHING',
count: 0,
}
] */

//const filterHeaderElement = document.querySelector('.trip-controls');
//const siteFilterElement = filterHeaderElement.querySelector('.trip-controls__filters');
const siteMainElement = document.querySelector('.page-main');
const tripInfoElement = document.querySelector('.trip-main');

const pointsModel = new PointsModel();
const destinationModel = new DestinationModel();
const offersModel = new OffersModel();
const filterModel = new FilterModel();

const tripPresenter = new TripPresenter({
listContainer: siteMainElement,
pointsModel,
destinationsModel: destinationModel,
offersModel
offersModel,
filterModel,
onNewPointDestroy: handleNewPointFormClose
});

//const filters = generateFilters(pointsModel.points);
const filterPresenter = new FilterPresenter({
filterContainer: siteMainElement,
filterModel,
pointsModel
});

const filters = generateFilters(pointsModel.points);
const newPointButtonComponent = new NewPointButtonView({
onClick: handleNewPointButtonClick
});

function handleNewPointFormClose() {
newPointButtonComponent.element.disabled = false;
}

function handleNewPointButtonClick() {
tripPresenter.createPoint();
newPointButtonComponent.element.disabled = true;
}

render(new FilterPointsView({ filters }), siteFilterElement);
render(new TripInfoView(), tripInfoElement, RenderPosition.AFTERBEGIN);
render(newPointButtonComponent, tripInfoElement);

filterPresenter.init();
tripPresenter.init();
12 changes: 0 additions & 12 deletions src/mock/filter.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/model/filter-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Observable from '../framework/observable.js';
import {FilterType} from '../const.js';

export default class FilterModel extends Observable {
#filter = FilterType.EVERYTHING;

get filter() {
return this.#filter;
}

setFilter(updateType, filter) {
this.#filter = filter;
this._notify(updateType, filter);
}
}
43 changes: 42 additions & 1 deletion src/model/point-model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
import Observable from '../framework/observable';
import { getRandomPoint } from '../mock/point';
import { POINTS_COUNT } from '../const';

export default class PointsModel {
export default class PointsModel extends Observable {
#points = Array.from({length: POINTS_COUNT }, () => getRandomPoint());

get points() {
return this.#points;
}

updatePoint = (updateType, update) => {
const index = this.#points.findIndex((point) => point.id === update.id);

if (index === -1) {
throw new Error ('Can\'t update unexisting point');
}

this.#points = [
...this.#points.slice(0, index),
update,
...this.#points.slice(index + 1),
];

this._notify(updateType, update);
};

addPoint = (updateType, update) => {
this.#points = [
update,
...this.#points,
];

this._notify(updateType, update);
};

deletePoint = (updateType, update) => {
const index = this.#points.findIndex((point) => point.id === update.id);

if (index === -1) {
throw new Error('Can\'t delete unexisting point');
}

this.#points = [
...this.#points.slice(0, index),
...this.#points.slice(index + 1),
];

this._notify(updateType);
};
}
58 changes: 58 additions & 0 deletions src/presenter/filter-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {render, replace, remove} from '../framework/render.js';
import FilterPointsView from '../view/filter-points-view.js';
//import {filter} from '../utils/filter.js';
import {FilterType, UpdateType} from '../const.js';

export default class FilterPresenter {
#filterContainer = null;
#filterModel = null;
#pointsModel = null;

#filterComponent = null;

constructor({filterContainer, filterModel, pointsModel}) {
this.#filterContainer = filterContainer;
this.#filterModel = filterModel;
this.#pointsModel = pointsModel;

this.#pointsModel.addObserver(this.#handleModelEvent);
this.#filterModel.addObserver(this.#handleModelEvent);
}

get filters() {
return Object.values(FilterType).map((type) => ({
type
}));
}

init() {
const filters = this.filters;
const prevFilterComponent = this.#filterComponent;

this.#filterComponent = new FilterPointsView({
filters,
currentFilterType: this.#filterModel.filter,
onFilterTypeChange: this.#handleFilterTypeChange
});

if (prevFilterComponent === null) {
render(this.#filterComponent, this.#filterContainer);
return;
}

replace(this.#filterComponent, prevFilterComponent);
remove(prevFilterComponent);
}

#handleModelEvent = () => {
this.init();
};

#handleFilterTypeChange = (filterType) => {
if (this.#filterModel.filter === filterType) {
return;
}

this.#filterModel.setFilter(UpdateType.MAJOR, filterType);
};
}
66 changes: 66 additions & 0 deletions src/presenter/new-point-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {remove, render, RenderPosition} from '../framework/render.js';
import EditPointView from '../view/editing-form-view.js';
import {nanoid} from 'nanoid';
import {UserAction, UpdateType} from '../const.js';

export default class NewPointPresenter {
#pointListContainer = null;
#handleDataChange = null;
#handleDestroy = null;

#pointEditComponent = null;

constructor({pointListContainer, onDataChange, onDestroy}) {
this.#pointListContainer = pointListContainer;
this.#handleDataChange = onDataChange;
this.#handleDestroy = onDestroy;
}

init() {
if (this.#pointEditComponent !== null) {
return;
}

this.#pointEditComponent = new EditPointView({
onFormSubmit: this.#handleFormSubmit,
onDeleteClick: this.#handleDeleteClick
});

render(this.#pointEditComponent, this.#pointListContainer, RenderPosition.AFTERBEGIN);

document.addEventListener('keydown', this.#escKeyDownHandler);
}

destroy() {
if (this.#pointEditComponent === null) {
return;
}

this.#handleDestroy();

remove(this.#pointEditComponent);
this.#pointEditComponent = null;

document.removeEventListener('keydown', this.#escKeyDownHandler);
}

#handleFormSubmit = (point) => {
this.#handleDataChange(
UserAction.ADD_POINT,
UpdateType.MINOR,
{id: nanoid(), ...point},
);
this.destroy();
};

#handleDeleteClick = () => {
this.destroy();
};

#escKeyDownHandler = (evt) => {
if (evt.key === 'Escape' || evt.key === 'Esc') {
evt.preventDefault();
this.destroy();
}
};
}
Loading

0 comments on commit 48f1512

Please sign in to comment.