Skip to content

Commit

Permalink
Merge pull request #5 from Klinarr/module4-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Dec 26, 2024
2 parents dd8d9ac + 71d3e01 commit 2bf2b49
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 76 deletions.
59 changes: 41 additions & 18 deletions src/presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import SortView from './view/sort-view.js';
import EventsListView from './view/events-list-view.js';
import EventsEditView from './view/events-edit-view.js';
import EventsItemView from './view/events-item-view.js';
import {render} from './render.js';
import {render, replace, remove} from './framework/render.js';

export default class Presenter {
filtersContainer = null;
sortContainer = null;
eventsContainer = new EventsListView();

constructor(container, model) {
this.container = container;
this.model = model;
this.filtersContainer = this.container.querySelector('.trip-controls');
this.sortContainer = document.querySelector('.trip-events');
}

Expand All @@ -23,7 +21,6 @@ export default class Presenter {
this.renderFilters();
this.renderSort();
this.renderEventsList();
this.renderEventsEdit();
this.renderEventsItems();
}

Expand All @@ -35,7 +32,7 @@ export default class Presenter {
}

renderFilters() {
render(new FiltersView(), this.filtersContainer);
render(new FiltersView(), this.container);
}

renderSort() {
Expand All @@ -46,23 +43,49 @@ export default class Presenter {
render(this.eventsContainer, this.sortContainer, 'beforeend');
}

renderEventsEdit() {
const point = this.model.getPoints()[0];
const destination = this.model.getDestinationById(point.destination);
const offers = this.model.getOffersById(point.type, point.offers);
const allDestinations = this.model.getDestinations();

render(new EventsEditView(point, destination, offers, allDestinations), this.eventsContainer.getElement(), 'beforeend');
}

renderEventsItems() {
for (let i = 0; i < 3; i++) {
const point = this.model.getPoints().shift();
const points = this.model.getPoints().slice(0, 3);
points.forEach((point) => {
const destination = this.model.getDestinationById(point.destination);
const offers = this.model.getOffersById(point.type, point.offers);
const eventItem = new EventsItemView(point, destination, offers);
const eventEdit = new EventsEditView(point, destination, offers, this.model.getDestinations());

render(eventItem, this.eventsContainer.getElement(), 'beforeend');
}
eventItem.setRollupButtonClickHandler(() => {
replace(eventEdit, eventItem);
this.setEditModeHandlers(eventEdit, eventItem);
});

render(eventItem, this.eventsContainer.element, 'beforeend');
});
}

setEditModeHandlers(eventEdit, eventItem) {
this.eventEdit = eventEdit;
this.eventItem = eventItem;

eventEdit.setFormSubmitHandler(() => {
replace(eventItem, eventEdit);
remove(eventEdit);
document.removeEventListener('keydown', this.escKeyDownHandler);
});

eventEdit.setRollupButtonClickHandler(() => {
replace(eventItem, eventEdit);
remove(eventEdit);
document.removeEventListener('keydown', this.escKeyDownHandler);
});

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

escKeyDownHandler = (evt) => {
if (evt.key === 'Escape' || evt.key === 'Esc') {
evt.preventDefault();
replace(this.eventItem, this.eventEdit);
remove(this.eventEdit);
document.removeEventListener('keydown', this.escKeyDownHandler);
}
};

}
25 changes: 15 additions & 10 deletions src/view/events-edit-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';
import { dateModule } from '../utils.js';

function createEventsEditTemplate(point, destination, offers , allDestinations) {
Expand Down Expand Up @@ -47,7 +47,7 @@ function createEventsEditTemplate(point, destination, offers , allDestinations)
</div>
<div class="event__type-item">
<input id="event-type-flight-1" class="event__type-input visually-hidden" type="radio" name="event-type" value="flight" checked>
<input id="event-type-flight-1" class="event__type-input visually-hidden" type="radio" name="event-type" value="flight">
<label class="event__type-label event__type-label--flight" for="event-type-flight-1">Flight</label>
</div>
Expand Down Expand Up @@ -96,7 +96,10 @@ function createEventsEditTemplate(point, destination, offers , allDestinations)
</div>
<button class="event__save-btn btn btn--blue" type="submit">Save</button>
<button class="event__reset-btn" type="reset">Cancel</button>
<button class="event__reset-btn" type="reset">Delete</button>
<button class="event__rollup-btn" type="button">
<span class="visually-hidden">Open event</span>
</button>
</header>
<section class="event__details">
<section class="event__section event__section--offers">
Expand Down Expand Up @@ -131,22 +134,24 @@ function createEventsEditTemplate(point, destination, offers , allDestinations)
);
}

export default class EventsEditView {
export default class EventsEditView extends AbstractView {
constructor(point, destination, offers, allDestinations) {
super();
this.point = point;
this.destination = destination;
this.offers = offers;
this.allDestinations = allDestinations;
}

getTemplate() {
get template() {
return createEventsEditTemplate(this.point, this.destination, this.offers, this.allDestinations);
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
setFormSubmitHandler(callback) {
this.element.querySelector('.event--edit')?.addEventListener('submit', callback);
}

setRollupButtonClickHandler(callback) {
this.element.querySelector('.event__rollup-btn')?.addEventListener('click', callback);
}
}
14 changes: 6 additions & 8 deletions src/view/events-item-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';
import { dateModule } from '../utils.js';

function createEventsItemTemplate(point, destination , offers){
Expand Down Expand Up @@ -50,21 +50,19 @@ function createEventsItemTemplate(point, destination , offers){
);
}

export default class EventsItemView {
export default class EventsItemView extends AbstractView {
constructor(point, destination , offers) {
super();
this.point = point;
this.destination = destination;
this.offers = offers;
}

getTemplate() {
get template() {
return createEventsItemTemplate(this.point, this.destination, this.offers);
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
setRollupButtonClickHandler(callback) {
this.element.querySelector('.event__rollup-btn')?.addEventListener('click', callback);
}
}
13 changes: 3 additions & 10 deletions src/view/events-list-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';

function createTripEventsListTemplate(){

Expand All @@ -9,15 +9,8 @@ function createTripEventsListTemplate(){
);
}

export default class EventsListView {
getTemplate() {
export default class EventsListView extends AbstractView {
get template() {
return createTripEventsListTemplate();
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
}
}
13 changes: 3 additions & 10 deletions src/view/filters-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';

function createFiltersTemplate(){

Expand Down Expand Up @@ -30,15 +30,8 @@ function createFiltersTemplate(){
);
}

export default class FiltersView {
getTemplate() {
export default class FiltersView extends AbstractView {
get template() {
return createFiltersTemplate();
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
}
}
14 changes: 4 additions & 10 deletions src/view/main-info-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';
import { dateModule } from '../utils.js';

function createMainInfoTemplate(point, destination) {
Expand All @@ -21,22 +21,16 @@ function createMainInfoTemplate(point, destination) {
);
}

export default class MainInfoView {
export default class MainInfoView extends AbstractView {
constructor(point, destination) {
super();
this.point = point;
this.destination = destination;
}

getTemplate() {
get template() {
return createMainInfoTemplate(this.point, this.destination);
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
}
}


13 changes: 3 additions & 10 deletions src/view/sort-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from '../render.js';
import AbstractView from '../framework/view/abstract-view.js';

function createSortTemplate(){

Expand Down Expand Up @@ -33,15 +33,8 @@ function createSortTemplate(){
);
}

export default class Sortview {
getTemplate() {
export default class Sortview extends AbstractView {
get template() {
return createSortTemplate();
}

getElement() {
if (!this.element) {
this.element = createElement(this.getTemplate());
}
return this.element;
}
}
4 changes: 4 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module.exports = {
},
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader']
},
],
}
}

0 comments on commit 2bf2b49

Please sign in to comment.