|
@@ -0,0 +1,371 @@
|
|
1
|
+(function() {
|
|
2
|
+ // Schedule Template - by CodyHouse.co
|
|
3
|
+ function ScheduleTemplate( element ) {
|
|
4
|
+ this.element = element;
|
|
5
|
+ this.timelineItems = this.element.getElementsByClassName('cd-schedule__timeline')[0].getElementsByTagName('li');
|
|
6
|
+ this.timelineStart = getScheduleTimestamp(this.timelineItems[0].textContent);
|
|
7
|
+ this.timelineUnitDuration = getScheduleTimestamp(this.timelineItems[1].textContent) - getScheduleTimestamp(this.timelineItems[0].textContent);
|
|
8
|
+
|
|
9
|
+ this.topInfoElement = this.element.getElementsByClassName('cd-schedule__top-info')[0];
|
|
10
|
+ this.singleEvents = this.element.getElementsByClassName('cd-schedule__event');
|
|
11
|
+
|
|
12
|
+ this.modal = this.element.getElementsByClassName('cd-schedule-modal')[0];
|
|
13
|
+ this.modalHeader = this.element.getElementsByClassName('cd-schedule-modal__header')[0];
|
|
14
|
+ this.modalHeaderBg = this.element.getElementsByClassName('cd-schedule-modal__header-bg')[0];
|
|
15
|
+ this.modalBody = this.element.getElementsByClassName('cd-schedule-modal__body')[0];
|
|
16
|
+ this.modalBodyBg = this.element.getElementsByClassName('cd-schedule-modal__body-bg')[0];
|
|
17
|
+ this.modalClose = this.modal.getElementsByClassName('cd-schedule-modal__close')[0];
|
|
18
|
+ this.modalDate = this.modal.getElementsByClassName('cd-schedule-modal__date')[0];
|
|
19
|
+ this.modalEventName = this.modal.getElementsByClassName('cd-schedule-modal__name')[0];
|
|
20
|
+ this.coverLayer = this.element.getElementsByClassName('cd-schedule__cover-layer')[0];
|
|
21
|
+
|
|
22
|
+ this.modalMaxWidth = 800;
|
|
23
|
+ this.modalMaxHeight = 480;
|
|
24
|
+
|
|
25
|
+ this.animating = false;
|
|
26
|
+ this.supportAnimation = Util.cssSupports('transition');
|
|
27
|
+
|
|
28
|
+ this.initSchedule();
|
|
29
|
+ };
|
|
30
|
+
|
|
31
|
+ ScheduleTemplate.prototype.initSchedule = function() {
|
|
32
|
+ this.scheduleReset();
|
|
33
|
+ this.initEvents();
|
|
34
|
+ };
|
|
35
|
+
|
|
36
|
+ ScheduleTemplate.prototype.scheduleReset = function() {
|
|
37
|
+ // according to the mq value, init the style of the template
|
|
38
|
+ var mq = this.mq(),
|
|
39
|
+ loaded = Util.hasClass(this.element, 'js-schedule-loaded'),
|
|
40
|
+ modalOpen = Util.hasClass(this.modal, 'cd-schedule-modal--open');
|
|
41
|
+ if( mq == 'desktop' && !loaded ) {
|
|
42
|
+ Util.addClass(this.element, 'js-schedule-loaded');
|
|
43
|
+ this.placeEvents();
|
|
44
|
+ modalOpen && this.checkEventModal(modalOpen);
|
|
45
|
+ } else if( mq == 'mobile' && loaded) {
|
|
46
|
+ //in this case you are on a mobile version (first load or resize from desktop)
|
|
47
|
+ Util.removeClass(this.element, 'cd-schedule--loading js-schedule-loaded');
|
|
48
|
+ this.resetEventsStyle();
|
|
49
|
+ modalOpen && this.checkEventModal();
|
|
50
|
+ } else if( mq == 'desktop' && modalOpen ) {
|
|
51
|
+ //on a mobile version with modal open - need to resize/move modal window
|
|
52
|
+ this.checkEventModal(modalOpen);
|
|
53
|
+ Util.removeClass(this.element, 'cd-schedule--loading');
|
|
54
|
+ } else {
|
|
55
|
+ Util.removeClass(this.element, 'cd-schedule--loading');
|
|
56
|
+ }
|
|
57
|
+ };
|
|
58
|
+
|
|
59
|
+ ScheduleTemplate.prototype.resetEventsStyle = function() {
|
|
60
|
+ // remove js style applied to the single events
|
|
61
|
+ for(var i = 0; i < this.singleEvents.length; i++) {
|
|
62
|
+ this.singleEvents[i].removeAttribute('style');
|
|
63
|
+ }
|
|
64
|
+ };
|
|
65
|
+
|
|
66
|
+ ScheduleTemplate.prototype.placeEvents = function() {
|
|
67
|
+ // on big devices - place events in the template according to their time/day
|
|
68
|
+ var self = this,
|
|
69
|
+ slotHeight = this.topInfoElement.offsetHeight;
|
|
70
|
+ for(var i = 0; i < this.singleEvents.length; i++) {
|
|
71
|
+ var anchor = this.singleEvents[i].getElementsByTagName('a')[0];
|
|
72
|
+ var start = getScheduleTimestamp(anchor.getAttribute('data-start')),
|
|
73
|
+ duration = getScheduleTimestamp(anchor.getAttribute('data-end')) - start;
|
|
74
|
+
|
|
75
|
+ var eventTop = slotHeight*(start - self.timelineStart)/self.timelineUnitDuration,
|
|
76
|
+ eventHeight = slotHeight*duration/self.timelineUnitDuration;
|
|
77
|
+
|
|
78
|
+ this.singleEvents[i].setAttribute('style', 'top: '+(eventTop-1)+'px; height: '+(eventHeight +1)+'px');
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ Util.removeClass(this.element, 'cd-schedule--loading');
|
|
82
|
+ };
|
|
83
|
+
|
|
84
|
+ ScheduleTemplate.prototype.initEvents = function() {
|
|
85
|
+ var self = this;
|
|
86
|
+ for(var i = 0; i < this.singleEvents.length; i++) {
|
|
87
|
+ // open modal when user selects an event
|
|
88
|
+ this.singleEvents[i].addEventListener('click', function(event){
|
|
89
|
+ event.preventDefault();
|
|
90
|
+ if(!self.animating) self.openModal(this.getElementsByTagName('a')[0]);
|
|
91
|
+ });
|
|
92
|
+ }
|
|
93
|
+ //close modal window
|
|
94
|
+ this.modalClose.addEventListener('click', function(event){
|
|
95
|
+ event.preventDefault();
|
|
96
|
+ if( !self.animating ) self.closeModal();
|
|
97
|
+ });
|
|
98
|
+ this.coverLayer.addEventListener('click', function(event){
|
|
99
|
+ event.preventDefault();
|
|
100
|
+ if( !self.animating ) self.closeModal();
|
|
101
|
+ });
|
|
102
|
+ };
|
|
103
|
+
|
|
104
|
+ ScheduleTemplate.prototype.openModal = function(target) {
|
|
105
|
+ var self = this;
|
|
106
|
+ var mq = self.mq();
|
|
107
|
+ this.animating = true;
|
|
108
|
+
|
|
109
|
+ //update event name and time
|
|
110
|
+ this.modalEventName.textContent = target.getElementsByTagName('em')[0].textContent;
|
|
111
|
+ this.modalDate.textContent = target.getAttribute('data-start')+' - '+target.getAttribute('data-end');
|
|
112
|
+ this.modal.setAttribute('data-event', target.getAttribute('data-event'));
|
|
113
|
+
|
|
114
|
+ //update event content
|
|
115
|
+ this.loadEventContent(target.getAttribute('data-content'));
|
|
116
|
+
|
|
117
|
+ Util.addClass(this.modal, 'cd-schedule-modal--open');
|
|
118
|
+
|
|
119
|
+ setTimeout(function(){
|
|
120
|
+ //fixes a flash when an event is selected - desktop version only
|
|
121
|
+ Util.addClass(target.closest('li'), 'cd-schedule__event--selected');
|
|
122
|
+ }, 10);
|
|
123
|
+
|
|
124
|
+ if( mq == 'mobile' ) {
|
|
125
|
+ self.modal.addEventListener('transitionend', function cb(){
|
|
126
|
+ self.animating = false;
|
|
127
|
+ self.modal.removeEventListener('transitionend', cb);
|
|
128
|
+ });
|
|
129
|
+ } else {
|
|
130
|
+ var eventPosition = target.getBoundingClientRect(),
|
|
131
|
+ eventTop = eventPosition.top,
|
|
132
|
+ eventLeft = eventPosition.left,
|
|
133
|
+ eventHeight = target.offsetHeight,
|
|
134
|
+ eventWidth = target.offsetWidth;
|
|
135
|
+
|
|
136
|
+ var windowWidth = window.innerWidth,
|
|
137
|
+ windowHeight = window.innerHeight;
|
|
138
|
+
|
|
139
|
+ var modalWidth = ( windowWidth*.8 > self.modalMaxWidth ) ? self.modalMaxWidth : windowWidth*.8,
|
|
140
|
+ modalHeight = ( windowHeight*.8 > self.modalMaxHeight ) ? self.modalMaxHeight : windowHeight*.8;
|
|
141
|
+
|
|
142
|
+ var modalTranslateX = parseInt((windowWidth - modalWidth)/2 - eventLeft),
|
|
143
|
+ modalTranslateY = parseInt((windowHeight - modalHeight)/2 - eventTop);
|
|
144
|
+
|
|
145
|
+ var HeaderBgScaleY = modalHeight/eventHeight,
|
|
146
|
+ BodyBgScaleX = (modalWidth - eventWidth);
|
|
147
|
+
|
|
148
|
+ //change modal height/width and translate it
|
|
149
|
+ self.modal.setAttribute('style', 'top:'+eventTop+'px;left:'+eventLeft+'px;height:'+modalHeight+'px;width:'+modalWidth+'px;transform: translateY('+modalTranslateY+'px) translateX('+modalTranslateX+'px)');
|
|
150
|
+ //set modalHeader width
|
|
151
|
+ self.modalHeader.setAttribute('style', 'width:'+eventWidth+'px');
|
|
152
|
+ //set modalBody left margin
|
|
153
|
+ self.modalBody.setAttribute('style', 'margin-left:'+eventWidth+'px');
|
|
154
|
+ //change modalBodyBg height/width ans scale it
|
|
155
|
+ self.modalBodyBg.setAttribute('style', 'height:'+eventHeight+'px; width: 1px; transform: scaleY('+HeaderBgScaleY+') scaleX('+BodyBgScaleX+')');
|
|
156
|
+ //change modal modalHeaderBg height/width and scale it
|
|
157
|
+ self.modalHeaderBg.setAttribute('style', 'height: '+eventHeight+'px; width: '+eventWidth+'px; transform: scaleY('+HeaderBgScaleY+')');
|
|
158
|
+
|
|
159
|
+ self.modalHeaderBg.addEventListener('transitionend', function cb(){
|
|
160
|
+ //wait for the end of the modalHeaderBg transformation and show the modal content
|
|
161
|
+ self.animating = false;
|
|
162
|
+ Util.addClass(self.modal, 'cd-schedule-modal--animation-completed');
|
|
163
|
+ self.modalHeaderBg.removeEventListener('transitionend', cb);
|
|
164
|
+ });
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ //if browser do not support transitions -> no need to wait for the end of it
|
|
168
|
+ this.animationFallback();
|
|
169
|
+ };
|
|
170
|
+
|
|
171
|
+ ScheduleTemplate.prototype.closeModal = function() {
|
|
172
|
+ var self = this;
|
|
173
|
+ var mq = self.mq();
|
|
174
|
+
|
|
175
|
+ var item = self.element.getElementsByClassName('cd-schedule__event--selected')[0],
|
|
176
|
+ target = item.getElementsByTagName('a')[0];
|
|
177
|
+
|
|
178
|
+ this.animating = true;
|
|
179
|
+
|
|
180
|
+ if( mq == 'mobile' ) {
|
|
181
|
+ Util.removeClass(this.modal, 'cd-schedule-modal--open');
|
|
182
|
+ self.modal.addEventListener('transitionend', function cb(){
|
|
183
|
+ Util.removeClass(self.modal, 'cd-schedule-modal--content-loaded');
|
|
184
|
+ Util.removeClass(item, 'cd-schedule__event--selected');
|
|
185
|
+ self.animating = false;
|
|
186
|
+ self.modal.removeEventListener('transitionend', cb);
|
|
187
|
+ });
|
|
188
|
+ } else {
|
|
189
|
+ var eventPosition = target.getBoundingClientRect(),
|
|
190
|
+ eventTop = eventPosition.top,
|
|
191
|
+ eventLeft = eventPosition.left,
|
|
192
|
+ eventHeight = target.offsetHeight,
|
|
193
|
+ eventWidth = target.offsetWidth;
|
|
194
|
+
|
|
195
|
+ var modalStyle = window.getComputedStyle(self.modal),
|
|
196
|
+ modalTop = Number(modalStyle.getPropertyValue('top').replace('px', '')),
|
|
197
|
+ modalLeft = Number(modalStyle.getPropertyValue('left').replace('px', ''));
|
|
198
|
+
|
|
199
|
+ var modalTranslateX = eventLeft - modalLeft,
|
|
200
|
+ modalTranslateY = eventTop - modalTop;
|
|
201
|
+
|
|
202
|
+ Util.removeClass(this.modal, 'cd-schedule-modal--open cd-schedule-modal--animation-completed');
|
|
203
|
+
|
|
204
|
+ //change modal width/height and translate it
|
|
205
|
+ self.modal.style.width = eventWidth+'px';self.modal.style.height = eventHeight+'px';self.modal.style.transform = 'translateX('+modalTranslateX+'px) translateY('+modalTranslateY+'px)';
|
|
206
|
+ //scale down modalBodyBg element
|
|
207
|
+ self.modalBodyBg.style.transform = 'scaleX(0) scaleY(1)';
|
|
208
|
+ //scale down modalHeaderBg element
|
|
209
|
+ // self.modalHeaderBg.setAttribute('style', 'transform: scaleY(1)');
|
|
210
|
+ self.modalHeaderBg.style.transform = 'scaleY(1)';
|
|
211
|
+
|
|
212
|
+ self.modalHeaderBg.addEventListener('transitionend', function cb(){
|
|
213
|
+ //wait for the end of the modalHeaderBg transformation and reset modal style
|
|
214
|
+ Util.addClass(self.modal, 'cd-schedule-modal--no-transition');
|
|
215
|
+ setTimeout(function(){
|
|
216
|
+ self.modal.removeAttribute('style');
|
|
217
|
+ self.modalBody.removeAttribute('style');
|
|
218
|
+ self.modalHeader.removeAttribute('style');
|
|
219
|
+ self.modalHeaderBg.removeAttribute('style');
|
|
220
|
+ self.modalBodyBg.removeAttribute('style');
|
|
221
|
+ }, 10);
|
|
222
|
+ setTimeout(function(){
|
|
223
|
+ Util.removeClass(self.modal, 'cd-schedule-modal--no-transition');
|
|
224
|
+ }, 20);
|
|
225
|
+ self.animating = false;
|
|
226
|
+ Util.removeClass(self.modal, 'cd-schedule-modal--content-loaded');
|
|
227
|
+ Util.removeClass(item, 'cd-schedule__event--selected');
|
|
228
|
+ self.modalHeaderBg.removeEventListener('transitionend', cb);
|
|
229
|
+ });
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ //if browser do not support transitions -> no need to wait for the end of it
|
|
233
|
+ this.animationFallback();
|
|
234
|
+ };
|
|
235
|
+
|
|
236
|
+ ScheduleTemplate.prototype.checkEventModal = function(modalOpen) {
|
|
237
|
+ // this function is used on resize to reset events/modal style
|
|
238
|
+ this.animating = true;
|
|
239
|
+ var self = this;
|
|
240
|
+ var mq = this.mq();
|
|
241
|
+ if( mq == 'mobile' ) {
|
|
242
|
+ //reset modal style on mobile
|
|
243
|
+ self.modal.removeAttribute('style');
|
|
244
|
+ self.modalBody.removeAttribute('style');
|
|
245
|
+ self.modalHeader.removeAttribute('style');
|
|
246
|
+ self.modalHeaderBg.removeAttribute('style');
|
|
247
|
+ self.modalBodyBg.removeAttribute('style');
|
|
248
|
+ Util.removeClass(self.modal, 'cd-schedule-modal--no-transition');
|
|
249
|
+ self.animating = false;
|
|
250
|
+ } else if( mq == 'desktop' && modalOpen) {
|
|
251
|
+ Util.addClass(self.modal, 'cd-schedule-modal--no-transition cd-schedule-modal--animation-completed');
|
|
252
|
+ var item = self.element.getElementsByClassName('cd-schedule__event--selected')[0],
|
|
253
|
+ target = item.getElementsByTagName('a')[0];
|
|
254
|
+
|
|
255
|
+ var eventPosition = target.getBoundingClientRect(),
|
|
256
|
+ eventTop = eventPosition.top,
|
|
257
|
+ eventLeft = eventPosition.left,
|
|
258
|
+ eventHeight = target.offsetHeight,
|
|
259
|
+ eventWidth = target.offsetWidth;
|
|
260
|
+
|
|
261
|
+ var windowWidth = window.innerWidth,
|
|
262
|
+ windowHeight = window.innerHeight;
|
|
263
|
+
|
|
264
|
+ var modalWidth = ( windowWidth*.8 > self.modalMaxWidth ) ? self.modalMaxWidth : windowWidth*.8,
|
|
265
|
+ modalHeight = ( windowHeight*.8 > self.modalMaxHeight ) ? self.modalMaxHeight : windowHeight*.8;
|
|
266
|
+
|
|
267
|
+ var HeaderBgScaleY = modalHeight/eventHeight,
|
|
268
|
+ BodyBgScaleX = (modalWidth - eventWidth);
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+ setTimeout(function(){
|
|
272
|
+ self.modal.setAttribute('style', 'top:'+(windowHeight/2 - modalHeight/2)+'px;left:'+(windowWidth/2 - modalWidth/2)+'px;height:'+modalHeight+'px;width:'+modalWidth+'px;transform: translateY(0) translateX(0)');
|
|
273
|
+ //change modal modalBodyBg height/width
|
|
274
|
+ self.modalBodyBg.style.height = modalHeight+'px';self.modalBodyBg.style.transform = 'scaleY(1) scaleX('+BodyBgScaleX+')';self.modalBodyBg.style.width = '1px';
|
|
275
|
+ //set modalHeader width
|
|
276
|
+ self.modalHeader.setAttribute('style', 'width:'+eventWidth+'px');
|
|
277
|
+ //set modalBody left margin
|
|
278
|
+ self.modalBody.setAttribute('style', 'margin-left:'+eventWidth+'px');
|
|
279
|
+ //change modal modalHeaderBg height/width and scale it
|
|
280
|
+ self.modalHeaderBg.setAttribute('style', 'height: '+eventHeight+'px;width:'+eventWidth+'px; transform:scaleY('+HeaderBgScaleY+');');
|
|
281
|
+ }, 10);
|
|
282
|
+
|
|
283
|
+ setTimeout(function(){
|
|
284
|
+ Util.removeClass(self.modal, 'cd-schedule-modal--no-transition');
|
|
285
|
+ self.animating = false;
|
|
286
|
+ }, 20);
|
|
287
|
+
|
|
288
|
+ }
|
|
289
|
+ };
|
|
290
|
+
|
|
291
|
+ ScheduleTemplate.prototype.loadEventContent = function(content) {
|
|
292
|
+ // load the content of an event when user selects it
|
|
293
|
+ var self = this;
|
|
294
|
+
|
|
295
|
+ httpRequest = new XMLHttpRequest();
|
|
296
|
+ httpRequest.onreadystatechange = function() {
|
|
297
|
+ if (httpRequest.readyState === XMLHttpRequest.DONE) {
|
|
298
|
+ if (httpRequest.status === 200) {
|
|
299
|
+ self.modal.getElementsByClassName('cd-schedule-modal__event-info')[0].innerHTML = self.getEventContent(httpRequest.responseText);
|
|
300
|
+ Util.addClass(self.modal, 'cd-schedule-modal--content-loaded');
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+ };
|
|
304
|
+ httpRequest.open('GET', content+'.html');
|
|
305
|
+ httpRequest.send();
|
|
306
|
+ };
|
|
307
|
+
|
|
308
|
+ ScheduleTemplate.prototype.getEventContent = function(string) {
|
|
309
|
+ // reset the loaded event content so that it can be inserted in the modal
|
|
310
|
+ var div = document.createElement('div');
|
|
311
|
+ div.innerHTML = string.trim();
|
|
312
|
+ return div.getElementsByClassName('cd-schedule-modal__event-info')[0].innerHTML;
|
|
313
|
+ };
|
|
314
|
+
|
|
315
|
+ ScheduleTemplate.prototype.animationFallback = function() {
|
|
316
|
+ if( !this.supportAnimation ) { // fallback for browsers not supporting transitions
|
|
317
|
+ var event = new CustomEvent('transitionend');
|
|
318
|
+ self.modal.dispatchEvent(event);
|
|
319
|
+ self.modalHeaderBg.dispatchEvent(event);
|
|
320
|
+ }
|
|
321
|
+ };
|
|
322
|
+
|
|
323
|
+ ScheduleTemplate.prototype.mq = function(){
|
|
324
|
+ //get MQ value ('desktop' or 'mobile')
|
|
325
|
+ var self = this;
|
|
326
|
+ return window.getComputedStyle(this.element, '::before').getPropertyValue('content').replace(/'|"/g, "");
|
|
327
|
+ };
|
|
328
|
+
|
|
329
|
+ function getScheduleTimestamp(time) {
|
|
330
|
+ //accepts hh:mm format - convert hh:mm to timestamp
|
|
331
|
+ time = time.replace(/ /g,'');
|
|
332
|
+ var timeArray = time.split(':');
|
|
333
|
+ var timeStamp = parseInt(timeArray[0])*60 + parseInt(timeArray[1]);
|
|
334
|
+ return timeStamp;
|
|
335
|
+ };
|
|
336
|
+
|
|
337
|
+ var scheduleTemplate = document.getElementsByClassName('js-cd-schedule'),
|
|
338
|
+ scheduleTemplateArray = [],
|
|
339
|
+ resizing = false;
|
|
340
|
+ if( scheduleTemplate.length > 0 ) { // init ScheduleTemplate objects
|
|
341
|
+ for( var i = 0; i < scheduleTemplate.length; i++) {
|
|
342
|
+ (function(i){
|
|
343
|
+ scheduleTemplateArray.push(new ScheduleTemplate(scheduleTemplate[i]));
|
|
344
|
+ })(i);
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+ window.addEventListener('resize', function(event) {
|
|
348
|
+ // on resize - update events position and modal position (if open)
|
|
349
|
+ if( !resizing ) {
|
|
350
|
+ resizing = true;
|
|
351
|
+ (!window.requestAnimationFrame) ? setTimeout(checkResize, 250) : window.requestAnimationFrame(checkResize);
|
|
352
|
+ }
|
|
353
|
+ });
|
|
354
|
+
|
|
355
|
+ window.addEventListener('keyup', function(event){
|
|
356
|
+ // close event modal when pressing escape key
|
|
357
|
+ if( event.keyCode && event.keyCode == 27 || event.key && event.key.toLowerCase() == 'escape' ) {
|
|
358
|
+ for(var i = 0; i < scheduleTemplateArray.length; i++) {
|
|
359
|
+ scheduleTemplateArray[i].closeModal();
|
|
360
|
+ }
|
|
361
|
+ }
|
|
362
|
+ });
|
|
363
|
+
|
|
364
|
+ function checkResize(){
|
|
365
|
+ for(var i = 0; i < scheduleTemplateArray.length; i++) {
|
|
366
|
+ scheduleTemplateArray[i].scheduleReset();
|
|
367
|
+ }
|
|
368
|
+ resizing = false;
|
|
369
|
+ };
|
|
370
|
+ }
|
|
371
|
+}());
|