1- import AbstractView from '../framework/view/abstract-view.js' ;
1+ import AbstractStatefulView from '../framework/view/abstract-stateful -view.js' ;
22import { humanizeTaskDueDate , DateFormat } from '../utils.js' ;
33
44function createOffferTemplate ( offer , checkedOffer ) {
5- if ( offer . length === 0 ) {
5+ if ( offer . offers . length === 0 ) {
66 return '' ;
77 }
88
@@ -104,20 +104,40 @@ function createPictureTemplate(pictures) {
104104 return `${ pictures . map ( ( picture ) => `<img class="event__photo" src="${ picture . src } " alt="${ picture . description } ">` ) . join ( '' ) } ` ;
105105}
106106
107+ function createDetailsTemplate ( offer , checkedOffer , description , pictures ) {
108+ const offresTemplate = createOffferTemplate ( offer , checkedOffer ) ;
109+ const descriptionTemplate = createDescriptionTemplate ( description ) ;
110+ const pictureTemplate = createPictureTemplate ( pictures ) ;
111+ if ( offer . offers . length || description || pictures . length ) {
112+ return `<section class="event__details">
113+ ${ offresTemplate }
114+ ${ description || pictures . length ? `<section class="event__section event__section--destination">
115+ <h3 class="event__section-title event__section-title--destination">Destination</h3>
116+ ${ description ? descriptionTemplate : '' }
117+ <div class="event__photos-container">
118+ <div class="event__photos-tape">
119+ ${ pictureTemplate }
120+ </div>
121+ </div>
122+ </section>` : '' }
123+ </section>` ;
124+ } else {
125+ return '' ;
126+ }
127+ }
128+
107129function createEditEventTemplate ( point , checkedOffer , offer , allOffers , destination , allDestinations ) {
108130 const { basePrice, dateFrom, dateTo, type} = point ;
109131 const pointId = point . id || 0 ;
110- const description = destination . description ? destination . description : '' ;
132+ const description = destination . description ;
111133 const pictures = destination . pictures . length > 0 ? destination . pictures : [ ] ;
112134
113- const offresTemplate = createOffferTemplate ( offer , checkedOffer ) ;
114135 const buttonsTemplate = createButtonsTemplate ( pointId ) ;
115136 const priceTemplate = createPriceTemplate ( pointId , basePrice ) ;
116137 const timeTemplate = createTimeTemplate ( pointId , dateFrom , dateTo ) ;
117138 const destinationTemplate = createGroupDestinationTemplate ( allDestinations , type , destination , pointId ) ;
118139 const typeTemplate = createTypeTemplate ( allOffers , type , pointId ) ;
119- const descriptionTemplate = createDescriptionTemplate ( description ) ;
120- const pictureTemplate = createPictureTemplate ( pictures ) ;
140+ const detailsTemplate = createDetailsTemplate ( offer , checkedOffer , description , pictures ) ;
121141
122142
123143 return `<li class="trip-events__item">
@@ -133,74 +153,96 @@ function createEditEventTemplate(point, checkedOffer, offer, allOffers, destinat
133153
134154 ${ buttonsTemplate }
135155 </header>
136- <section class="event__details">
137- ${ offresTemplate }
138-
139- <section class="event__section event__section--destination">
140- <h3 class="event__section-title event__section-title--destination">Destination</h3>
141- ${ descriptionTemplate }
142-
143- <div class="event__photos-container">
144- <div class="event__photos-tape">
145- ${ pictureTemplate }
146- </div>
147- </div>
148- </section>
149- </section>
156+ ${ detailsTemplate }
150157 </form>
151158 </li>` ;
152159}
153160
154- export default class EditEventView extends AbstractView {
161+ export default class EditEventView extends AbstractStatefulView {
155162 #pointsModel = null ;
156163 #point = null ;
157- #checkedOffer = null ;
158- #offer = null ;
159- #allOffers = null ;
160- #destination = null ;
161- #allDestinations = null ;
162164 #handleFormSubmit = null ;
163165 #handleFormCloseClick = null ;
166+ #destinations = null ;
164167
165168 constructor ( { pointsModel, point, onFormSubmit, onFormCloseClick} ) {
166169 super ( ) ;
167170 this . #pointsModel = pointsModel ;
168-
169171 this . #point = point ;
170- this . #checkedOffer = [ ...this . #pointsModel. getOfferById ( this . #point. type , this . #point. offers ) ] ;
171- this . #offer = this . #pointsModel. getOfferByType ( this . #point. type ) ;
172- this . #allOffers = this . #pointsModel. getOffer ( ) ;
173- this . #destination = this . #pointsModel. getDestinationById ( this . #point. destination ) ;
174- this . #allDestinations = this . #pointsModel. getDestination ( ) ;
172+ this . _setState ( EditEventView . parseEditToState ( point ) ) ;
175173 this . #handleFormSubmit = onFormSubmit ;
176174 this . #handleFormCloseClick = onFormCloseClick ;
177-
178- this . element . querySelector ( 'form' )
179- . addEventListener ( 'submit' , this . #formFormHandler) ;
180-
181- this . element . querySelector ( '.event__rollup-btn' )
182- . addEventListener ( 'click' , this . #formClickHandler) ;
175+ this . #destinations = this . #pointsModel. getDestination ( ) ;
176+ this . _restoreHandlers ( ) ;
183177 }
184178
185179 get template ( ) {
186180
187181 return createEditEventTemplate (
188- this . #point,
189- this . #checkedOffer,
190- this . #offer,
191- this . #allOffers,
192- this . #destination,
193- this . #allDestinations,
182+ this . _state ,
183+ [ ...this . #pointsModel. getOfferById ( this . _state . type , this . _state . offers ) ] ,
184+ this . #pointsModel. getOfferByType ( this . _state . type ) ,
185+ this . #pointsModel. getOffer ( ) ,
186+ this . #pointsModel. getDestinationById ( this . _state . destination ) ,
187+ this . #pointsModel. getDestination ( ) ,
188+ ) ;
189+ }
190+
191+ reset ( event ) {
192+ this . updateElement (
193+ EditEventView . parseEditToState ( event ) ,
194194 ) ;
195195 }
196196
197+ _restoreHandlers ( ) {
198+ this . element . querySelector ( 'form' ) . addEventListener ( 'submit' , this . #formFormHandler) ;
199+ this . element . querySelector ( '.event__rollup-btn' ) . addEventListener ( 'click' , this . #formClickHandler) ;
200+ this . element . querySelector ( '.event__type-group' ) . addEventListener ( 'change' , this . #typeChangeHandler) ;
201+ this . element . querySelector ( '.event__input--destination' ) . addEventListener ( 'change' , this . #destinationChangeHandler) ;
202+ this . element . querySelector ( '.event__input--price' ) . addEventListener ( 'input' , this . #priceInputHandler) ;
203+ }
204+
197205 #formFormHandler = ( evt ) => {
198206 evt . preventDefault ( ) ;
199- this . #handleFormSubmit( this . #point ) ;
207+ this . #handleFormSubmit( EditEventView . parseStateToEdit ( this . _state ) ) ;
200208 } ;
201209
202210 #formClickHandler = ( evt ) => {
203211 evt . preventDefault ( ) ;
212+ this . reset ( this . #point) ;
204213 this . #handleFormCloseClick( ) ;
205214 } ;
215+
216+ #typeChangeHandler = ( evt ) => {
217+ this . updateElement ( { type : evt . target . value , offers : [ ] } ) ;
218+ } ;
219+
220+ #destinationChangeHandler = ( evt ) => {
221+ evt . preventDefault ( ) ;
222+ const selectedName = evt . target . value . trim ( ) ;
223+ const selectedDest = this . #destinations. find ( ( dest ) => dest . name === selectedName ) ;
224+
225+ if ( selectedDest ) {
226+ this . updateElement ( {
227+ destination : selectedDest . id
228+ } ) ;
229+ } else {
230+
231+ evt . target . value = '' ;
232+ }
233+ } ;
234+
235+ #priceInputHandler = ( evt ) => {
236+ evt . preventDefault ( ) ;
237+ const newPrice = parseInt ( evt . target . value , 10 ) || 0 ;
238+ this . _setState ( { basePrice : newPrice } ) ;
239+ } ;
240+
241+ static parseEditToState ( edit ) {
242+ return { ...edit } ;
243+ }
244+
245+ static parseStateToEdit ( state ) {
246+ return { ...state } ;
247+ }
206248}
0 commit comments