@@ -25,6 +25,12 @@ function init() {
2525 nextMonth ( ) ;
2626 } ) ;
2727
28+ window . addEventListener ( 'hashchange' , function ( ) {
29+ loadStartDate ( ) ;
30+ build ( ) ;
31+ } ) ;
32+
33+
2834 $ ( '#js-events-form' ) . on ( 'submit' , function ( event ) {
2935 $ ( '#form-errors' ) . html ( '' ) . hide ( ) . removeClass ( 'alert--success' ) . removeClass ( 'alert--error' ) ;
3036
@@ -35,12 +41,8 @@ function init() {
3541 submission . location = { text : $ ( '#event-location' ) . val ( ) } ;
3642 submission . type = $ ( '#event-type' ) . val ( ) ;
3743 submission . website = $ ( '#event-website' ) . val ( ) ;
38- submission . submission = {
39- name : $ ( '#event-name' ) . val ( ) ,
40- email : $ ( '#event-email' ) . val ( )
41- } ;
42- submission . startDate = moment . utc ( $ ( '#event-start-date' ) . val ( ) , 'YYYY-MM-DD' ) . toDate ( ) ;
43- submission . endDate = moment . utc ( $ ( '#event-end-date' ) . val ( ) , 'YYYY-MM-DD' ) . toDate ( ) ;
44+ submission . startDate = Date . parse ( $ ( '#event-start-date' ) . val ( ) ) ;
45+ submission . endDate = Date . parse ( $ ( '#event-end-date' ) . val ( ) ) ;
4446 submission . isAllDay = true ;
4547 if ( ! dateRegex . test ( $ ( '#event-start-date' ) . val ( ) ) ) {
4648 return setError ( 'Start date is not valid' , 'Please use the format YYYY-MM-DD' , 'alert--error' ) ;
@@ -54,33 +56,60 @@ function init() {
5456 if ( submission . endDate < new Date ( ) ) {
5557 return setError ( 'End date is not valid' , 'Please enter a end date that is in the future' , 'alert--error' ) ;
5658 }
57- $ . post ( '{{ site.external_server }}/events/submit.json' , submission , function ( res ) {
58- if ( res && res . message ) {
59- setError ( res . message , 'We will review the details of your event and contact you when it has been approved for inclusion on our site.' , 'alert--success' ) ;
60- $ ( '#js-events-form' ) . get ( 0 ) . reset ( ) ;
61- }
62- else {
63- console . log ( res ) ;
64- }
65- } ) . fail ( function ( res ) {
66- console . log ( res . responseJSON ) ;
67- if ( res . responseJSON && res . responseJSON . error && res . responseJSON . error . name == 'ValidationError' ) {
68- $ ( '#form-errors' ) . append ( $ ( '<p/>' ) . text ( res . responseJSON . error . details [ 0 ] . message ) ) ;
69- }
70- else if ( res . responseJSON && res . responseJSON . error && res . responseJSON . error . details ) {
71- $ ( '#form-errors' ) . text ( res . responseJSON . error . details . messsage ) . show ( ) ;
72- }
73- else {
74- $ ( '#form-errors' ) . append ( $ ( '<p/>' ) . text ( 'An unknown error has occurred!' ) ) ;
75- }
76- $ ( '#form-errors' ) . addClass ( 'alert--error' ) . show ( ) ;
59+ fetchLongLat ( submission ) . finally ( updated => {
60+ if ( ! updated )
61+ updated = submission ;
62+ var output = "" ;
63+ Object . keys ( updated ) . forEach ( ( key , index ) => {
64+ if ( updated [ key ] === null || updated [ key ] === '' )
65+ return
66+ if ( index === 0 )
67+ output += '- '
68+ else
69+ output += ' '
70+ output += key + ': '
71+ if ( typeof updated [ key ] === 'object' )
72+ output += `"${ updated [ key ] [ 'text' ] } "` ;
73+ else if ( typeof updated [ key ] === 'number' )
74+ output += new moment ( updated [ key ] ) . format ( 'YYYY-MM-DD' ) ;
75+ else if ( typeof updated [ key ] === 'boolean' || key === 'longitude' || key === 'latitude' )
76+ output += updated [ key ] ;
77+ else if ( key === 'description' ) {
78+ output += '|'
79+ updated [ key ] . split ( "\n" ) . forEach ( ( line ) => {
80+ output += "\n"
81+ output += ' '
82+ output += line
83+ } ) ;
84+ } else
85+ output += `"${ updated [ key ] } "` ;
86+ output += "\n"
87+ } ) ;
88+ $ ( '#js-events-form-output' ) . html ( output ) ;
7789 } ) ;
7890 } ) ;
7991
80- window . addEventListener ( 'hashchange' , function ( ) {
81- loadStartDate ( ) ;
82- build ( ) ;
83- } ) ;
92+ async function fetchLongLat ( submission ) {
93+ if ( ! submission . location || ! submission . location . text ) return submission ;
94+
95+ try {
96+ const res = await fetch (
97+ 'https://nominatim.openstreetmap.org/search?format=jsonv2&q=' + encodeURIComponent ( submission . location )
98+ ) ;
99+
100+ const data = await res . json ( ) ;
101+
102+ if ( data . length > 0 ) {
103+ const result = data [ 0 ] ;
104+ submission . longitude = result . lon ;
105+ submission . latitude = result . lat ;
106+ }
107+ } catch ( err ) {
108+ console . error ( 'Location Lookup failed:' , err ) ;
109+ }
110+
111+ return submission ;
112+ }
84113
85114 function setError ( title , body , cssClass ) {
86115 $ ( '#form-errors' ) . append ( $ ( '<h4/>' ) . text ( title ) ) ;
@@ -178,7 +207,7 @@ function init() {
178207 }
179208
180209 function loadLocations ( ) {
181- var locationsUrl = '{{ site.external_server }}/events/ locations' ;
210+ var locationsUrl = '{{ site.external_server }}/locations.json ' ;
182211 $ . getJSON ( locationsUrl , function ( locations ) {
183212 if ( locations && locations . length > 0 ) {
184213 buildMap ( locations ) ;
@@ -189,54 +218,57 @@ function init() {
189218 function loadEvents ( ) {
190219 $ ( '.js-events__list' ) . html ( '<span class="spinner spinner--padded spinner--large spinner--center"></span>' ) ;
191220 var tplInfo = Handlebars . templates [ 'events-info' ] ;
192- var eventsUrl = '{{ site.external_server }}/events/upcoming.json?limit=60&start=' +
193- startDate . format ( 'YYYY/MM/DD' ) + '&end=' + endDate . format ( 'YYYY/MM/DD' ) ;
221+ var eventsUrl = `{{ site.external_server }}/events-${ startDate . format ( 'YYYY-MM' ) } .json`
222+
223+ fetch ( eventsUrl )
224+ . then ( response => {
225+ return response . json ( ) ;
226+ } )
227+ . then ( events => {
228+ if ( events . length === 0 ) {
229+ $ ( '.js-events__list' ) . html ( Handlebars . templates [ 'events-info-none' ] ( { month : startDate . format ( 'MMMM YYYY' ) } ) ) ;
230+ }
231+ else {
232+ $ ( '.js-events__list' ) . html ( '' ) ;
233+ var today = new Date ( ) ; today . setHours ( 0 , 0 , 0 , 0 ) ;
194234
195- $ . getJSON ( eventsUrl , function ( events ) {
196- if ( events . length === 0 ) {
197- $ ( '.js-events__list' ) . html ( Handlebars . templates [ 'events-info-none' ] ( { month : startDate . format ( 'MMMM YYYY' ) } ) ) ;
198- }
199- else {
200- $ ( '.js-events__list' ) . html ( '' ) ;
201- var today = new Date ( ) ; today . setHours ( 0 , 0 , 0 , 0 ) ;
235+ events . forEach ( function ( event , index ) {
236+ var eventDate = new moment ( event . startDate ) ;
202237
203- events . forEach ( function ( event , index ) {
204- var eventDate = new moment ( event . startDate ) ;
238+ event . number = index + 1 ;
239+ event . description = event . description . replace ( / \n \n \n / g, '<br><br>' ) ;
240+ event . description = event . description . replace ( / \n / g, '<br>' ) ;
241+ event . dateString = eventDate . format ( "YYYY-MM-DD" ) ;
205242
206- event . number = index + 1 ;
207- event . description = event . description . replace ( / \n \n \n / g, '<br><br>' ) ;
208- event . description = event . description . replace ( / \n / g, '<br>' ) ;
209- event . dateString = eventDate . format ( "YYYY-MM-DD" ) ;
243+ var $event = $ ( tplInfo ( event ) ) ;
210244
211- var $event = $ ( tplInfo ( event ) ) ;
245+ if ( index % 2 === 0 && index > 0 ) $event . addClass ( 'reset-m' ) ;
246+ if ( eventDate < today ) $event . children ( 'article' ) . addClass ( "event--past" ) ;
212247
213- if ( index % 2 === 0 && index > 0 ) $event . addClass ( 'reset-m' ) ;
214- if ( eventDate < today ) $event . children ( 'article' ) . addClass ( "event--past" ) ;
248+ var descToggle = $event . find ( ".event__toggle" ) ;
249+ descToggle . click ( function ( e ) {
250+ e . preventDefault ( ) ;
215251
216- var descToggle = $event . find ( ".event__toggle" ) ;
217- descToggle . click ( function ( e ) {
218- e . preventDefault ( ) ;
252+ var desc = $event . find ( ".event__description" ) ;
253+ if ( desc . attr ( "hidden" ) ) {
254+ desc . removeAttr ( "hidden" ) ;
255+ desc . removeClass ( "hidden" ) ;
219256
220- var desc = $event . find ( ".event__description" ) ;
221- if ( desc . attr ( "hidden" ) ) {
222- desc . removeAttr ( "hidden" ) ;
223- desc . removeClass ( "hidden" ) ;
257+ this . innerText = "Less.." ;
258+ } else {
259+ desc . attr ( "hidden" , "true ") ;
260+ desc . addClass ( "hidden" ) ;
224261
225- this . innerText = "Less.." ;
226- } else {
227- desc . attr ( "hidden" , "true" ) ;
228- desc . addClass ( "hidden" ) ;
262+ this . innerText = "More.." ;
263+ }
264+ } ) ;
229265
230- this . innerText = "More.." ;
231- }
266+ $ ( '.js-events__list' ) . append ( $event ) ;
267+ addEventToCalendar ( event ) ;
232268 } ) ;
233-
234- $ ( '.js-events__list' ) . append ( $event ) ;
235- addEventToCalendar ( event ) ;
236- } ) ;
237- }
238- buildMap ( events ) ;
239- } ) ;
269+ }
270+ buildMap ( events ) ;
271+ } ) ;
240272 }
241273
242274 function buildMap ( events ) {
@@ -267,7 +299,7 @@ function init() {
267299
268300 popup_content = `<h5>${ event . location } </h5>`
269301 popup_content += `<b>${ event . title } </b>`
270- popup_content += `<p>${ event . startDate } - ${ event . endDate } </p>`
302+ popup_content += `<p>${ new moment ( event . startDate ) . format ( 'YYYY-MM-DD' ) } - ${ new moment ( event . endDate ) . format ( 'YYYY-MM-DD' ) } </p>`
271303 popup_content += `<p><a href="${ event . website } ">Website</a></p>`
272304 var popup = L . popup ( { content : popup_content , className : `${ event . number } ` } ) ;
273305 marker . bindPopup ( popup ) ;
0 commit comments