1
- function dynamicFilter ( inputId ) {
1
+ const dynamicFilter = inputId => {
2
2
// Declare variables
3
3
var input , filter , items , i , txtValue ;
4
4
input = document . getElementById ( inputId ) ;
@@ -13,7 +13,7 @@ function dynamicFilter(inputId) {
13
13
items [ i ] . style . display = 'none' ;
14
14
}
15
15
}
16
- }
16
+ } ;
17
17
18
18
const toDescription = component => {
19
19
const description = component . getFirstPropertyValue ( 'description' ) ;
@@ -64,23 +64,117 @@ const filterForPeriod = (minDate, maxDate) => event => event.startDate >= minDat
64
64
65
65
const listVEventComponents = raw => new ICAL . Component ( ICAL . parse ( raw ) ) . getAllSubcomponents ( 'vevent' ) ;
66
66
67
- const fetchEvents = ( patterns , minDate , maxDate ) => fetch ( 'https://www.lyontechhub.org/Lyon-Tech-Hub-Calendar/calendar.ics' ) . then ( ( response ) => response . text ( ) ) . then ( ( raw ) =>
67
+ const calendarICSUrl = 'https://www.lyontechhub.org/Lyon-Tech-Hub-Calendar/calendar.ics' ;
68
+
69
+ const fetchEvents = ( patterns , minDate , maxDate ) => fetch ( calendarICSUrl ) . then ( ( response ) => response . text ( ) ) . then ( ( raw ) =>
68
70
listVEventComponents ( raw )
69
71
. map ( toEvent )
70
72
. filter ( filterForPeriod ( minDate , maxDate ) )
71
73
. filter ( matchForPatterns ( patterns ) ) ) ;
72
74
73
- function displayEvents ( template , element , events ) {
75
+ const displayEvents = ( template , element , events ) => {
74
76
if ( element ) {
75
77
element . innerHTML = template ( {
76
- paneTitle : element . getAttribute ( 'data-pane-title' )
77
- , noEventCaption : element . getAttribute ( 'data-no-event-caption' )
78
- , events : events
79
- , hasEvents : events . length > 0
80
- , noEvent : events . length == 0
78
+ paneTitle : element . getAttribute ( 'data-pane-title' ) ,
79
+ noEventCaption : element . getAttribute ( 'data-no-event-caption' ) ,
80
+ events : events ,
81
+ hasEvents : events . length > 0 ,
82
+ noEvent : events . length == 0
81
83
} ) ;
82
84
}
83
- }
85
+ } ;
86
+
87
+ const loadCommunities = ( ) =>
88
+ fetch ( '/communities.json' )
89
+ . then ( ( response ) => response . text ( ) )
90
+ . then ( ( body ) => JSON . parse ( body ) ) ;
91
+
92
+ const loadCalendar = async ( ) => {
93
+ const communities = await loadCommunities ( ) ;
94
+ const communitiesCalendars =
95
+ communities
96
+ . map ( ( community ) => {
97
+ var color = '#' ;
98
+ for ( var i = 0 ; i < 6 ; i ++ ) {
99
+ color += ( Math . floor ( Math . random ( ) * 100 ) % 16 ) . toString ( 16 ) ;
100
+ }
101
+ console . log ( community . key , color ) ;
102
+ return { id : community . key , name : community . name , backgroundColor : color } ;
103
+ } ) ;
104
+
105
+ const Calendar = tui . Calendar ;
106
+ const calendar = new Calendar ( '#calendar' , {
107
+ usageStatistics : false ,
108
+ defaultView : 'month' ,
109
+ isReadOnly : true ,
110
+ useDetailPopup : true ,
111
+ month : {
112
+ dayNames : [ 'Dim' , 'Lun' , 'Mar' , 'Mer' , 'Jeu' , 'Ven' , 'Sam' ] ,
113
+ startDayOfWeek : 1 ,
114
+ } ,
115
+ timezone : {
116
+ zones : [
117
+ {
118
+ timezoneName : 'Europe/Paris' ,
119
+ } ,
120
+ ] ,
121
+ } ,
122
+ calendars : [
123
+ {
124
+ id : 'default' ,
125
+ name : 'Default' ,
126
+ // backgroundColor: '#03bd9e',
127
+ } ,
128
+ ...communitiesCalendars
129
+ ] ,
130
+ } ) ;
131
+
132
+ fetch ( calendarICSUrl )
133
+ . then ( ( response ) => response . text ( ) )
134
+ . then ( ( raw ) => listVEventComponents ( raw ) . map ( toEvent ) )
135
+ . then ( ( items ) => {
136
+ calendar . createEvents (
137
+ items . map ( ( item ) => {
138
+ var title = item . title ;
139
+ var calendarId = 'default' ;
140
+ const match = title . match ( / ^ \[ ( .* ?) \] ( .+ ) $ / ) ;
141
+ if ( match ) {
142
+ for ( var i = 0 ; i < communities . length ; i ++ ) {
143
+ const patterns = communities [ i ] . patternsGoogleCalendar ;
144
+ if ( patterns ) {
145
+ for ( var j = 0 ; j < patterns . length ; j ++ ) {
146
+ if ( match [ 1 ] . localeCompare ( patterns [ j ] , 'en' , { sensitivity : 'base' } ) === 0 ) {
147
+ // console.log(match[2], communities[i].key);
148
+ title = match [ 2 ] ;
149
+ calendarId = communities [ i ] . key ;
150
+ break ;
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ return {
158
+ calendarId : calendarId ,
159
+ id : item . id ,
160
+ title : title ,
161
+ body : item . description ,
162
+ start : item . startDate ,
163
+ end : item . endDate ,
164
+ location : item . location ,
165
+ raw : { url : item . url } ,
166
+ }
167
+ } )
168
+ ) ;
169
+ } )
170
+ ;
171
+
172
+ document . querySelector ( '#calendar' ) . style . height = '800px' ;
173
+ document . querySelector ( '#calendarToday' ) . onclick = ( ) => { calendar . today ( ) ; } ;
174
+ document . querySelector ( '#calendarNext' ) . onclick = ( ) => { calendar . next ( ) ; } ;
175
+ document . querySelector ( '#calendarPrevious' ) . onclick = ( ) => { calendar . prev ( ) ; } ;
176
+
177
+ } ;
84
178
85
179
window . onload = ( ) => {
86
180
var communityDetailsEventsElement = document . getElementById ( 'communityDetails' ) ;
@@ -116,4 +210,9 @@ window.onload = () => {
116
210
} ) ;
117
211
} ) ;
118
212
}
213
+
214
+ var calendarElement = document . getElementById ( 'calendar' ) ;
215
+ if ( calendar ) {
216
+ loadCalendar ( ) ;
217
+ }
119
218
}
0 commit comments