-
Notifications
You must be signed in to change notification settings - Fork 2
/
ovingsspeilet.js
448 lines (406 loc) · 17.2 KB
/
ovingsspeilet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* globals window */
((win) => {
'use strict'
if (win.console) {
const m0 = 'Velkommen til Øvingsspeilet'
const s0 =
'font-family: \'Helvetica Neue\', Helvetica, Arial, sans-serif;' +
'text-shadow: -1px -1px hsl(0,100%,50%), 1px 1px hsl(5.4, 100%, 50%), 3px 2px hsl(10.8, 100%, 50%), 5px 3px hsl(16.2, 100%, 50%), 7px 4px hsl(21.6, 100%, 50%), 9px 5px hsl(27, 100%, 50%), 11px 6px hsl(32.4, 100%, 50%), 13px 7px hsl(37.8, 100%, 50%), 14px 8px hsl(43.2, 100%, 50%), 16px 9px hsl(48.6, 100%, 50%), 18px 10px hsl(54, 100%, 50%), 20px 11px hsl(59.4, 100%, 50%), 22px 12px hsl(64.8, 100%, 50%), 23px 13px hsl(70.2, 100%, 50%), 25px 14px hsl(75.6, 100%, 50%), 27px 15px hsl(81, 100%, 50%), 28px 16px hsl(86.4, 100%, 50%), 30px 17px hsl(91.8, 100%, 50%), 32px 18px hsl(97.2, 100%, 50%), 33px 19px hsl(102.6, 100%, 50%), 35px 20px hsl(108, 100%, 50%), 36px 21px hsl(113.4, 100%, 50%), 38px 22px hsl(118.8, 100%, 50%), 39px 23px hsl(124.2, 100%, 50%), 41px 24px hsl(129.6, 100%, 50%), 42px 25px hsl(135, 100%, 50%), 43px 26px hsl(140.4, 100%, 50%), 45px 27px hsl(145.8, 100%, 50%), 46px 28px hsl(151.2, 100%, 50%), 47px 29px hsl(156.6, 100%, 50%), 48px 30px hsl(162, 100%, 50%), 49px 31px hsl(167.4, 100%, 50%), 50px 32px hsl(172.8, 100%, 50%), 51px 33px hsl(178.2, 100%, 50%), 52px 34px hsl(183.6, 100%, 50%), 53px 35px hsl(189, 100%, 50%), 54px 36px hsl(194.4, 100%, 50%), 55px 37px hsl(199.8, 100%, 50%), 55px 38px hsl(205.2, 100%, 50%), 56px 39px hsl(210.6, 100%, 50%), 57px 40px hsl(216, 100%, 50%), 57px 41px hsl(221.4, 100%, 50%), 58px 42px hsl(226.8, 100%, 50%), 58px 43px hsl(232.2, 100%, 50%), 58px 44px hsl(237.6, 100%, 50%), 59px 45px hsl(243, 100%, 50%), 59px 46px hsl(248.4, 100%, 50%), 59px 47px hsl(253.8, 100%, 50%), 59px 48px hsl(259.2, 100%, 50%), 59px 49px hsl(264.6, 100%, 50%), 60px 50px hsl(270, 100%, 50%), 59px 51px hsl(275.4, 100%, 50%), 59px 52px hsl(280.8, 100%, 50%), 59px 53px hsl(286.2, 100%, 50%), 59px 54px hsl(291.6, 100%, 50%), 59px 55px hsl(297, 100%, 50%), 58px 56px hsl(302.4, 100%, 50%), 58px 57px hsl(307.8, 100%, 50%), 58px 58px hsl(313.2, 100%, 50%), 57px 59px hsl(318.6, 100%, 50%), 57px 60px hsl(324, 100%, 50%), 56px 61px hsl(329.4, 100%, 50%), 55px 62px hsl(334.8, 100%, 50%), 55px 63px hsl(340.2, 100%, 50%), 54px 64px hsl(345.6, 100%, 50%), 53px 65px hsl(351, 100%, 50%), 52px 66px hsl(356.4, 100%, 50%), 51px 67px hsl(361.8, 100%, 50%), 50px 68px hsl(367.2, 100%, 50%), 49px 69px hsl(372.6, 100%, 50%), 48px 70px hsl(378, 100%, 50%), 47px 71px hsl(383.4, 100%, 50%), 46px 72px hsl(388.8, 100%, 50%), 45px 73px hsl(394.2, 100%, 50%), 43px 74px hsl(399.6, 100%, 50%), 42px 75px hsl(405, 100%, 50%), 41px 76px hsl(410.4, 100%, 50%), 39px 77px hsl(415.8, 100%, 50%), 38px 78px hsl(421.2, 100%, 50%), 36px 79px hsl(426.6, 100%, 50%), 35px 80px hsl(432, 100%, 50%), 33px 81px hsl(437.4, 100%, 50%), 32px 82px hsl(442.8, 100%, 50%), 30px 83px hsl(448.2, 100%, 50%), 28px 84px hsl(453.6, 100%, 50%), 27px 85px hsl(459, 100%, 50%), 25px 86px hsl(464.4, 100%, 50%), 23px 87px hsl(469.8, 100%, 50%);' + // eslint-disable-line max-len
'font-size: 40px;' +
'line-height: 80px;'
win.console.log('%c' + m0, s0)
const m1 =
'Er du intressert i å vite mer om hvordan øvingsspeilet er bygget opp? ' +
'Sjekk det ut på Github. Start med å åpne et nytt issue :)'
const s1 =
'font-family: \'Helvetica Neue\', Helvetica, Arial, sans-serif;' +
'color: #fff;' +
'font-size: 16px;' +
'padding: 12px 16px;' +
'background: #444;' +
'border-radius: 4px;' +
'line-height: 80px;' +
'text-shadow: 0 2px #000;'
win.console.log('%c' + m1, s1)
const m2 = 'https://github.com/lafte/ovingsspeilet'
const s2 =
'font-family: \'Helvetica Neue\', Helvetica, Arial, sans-serif;' +
'font-size: 30px;' +
'padding: 15px 30px;' +
'background: rgba(255, 255, 255, 0.8);' +
'border-radius: 4px;' +
'line-height: 50px;' +
'text-shadow: 0 0 #000;'
win.console.log('%c' + m2, s2)
}
win.jQuery(win.document).ready(() => {
const GOOGLE_API_KEY = 'AIzaSyDvx6ilj_CXqxxtFSOGeUxAJso5hlRfa0s'
const TV1000_ID = '1Byw2CTdG-gnRI-V_QCYGlB4VpMDABsHH67im8GHS_AA'
const FIXED =
[ { start: 2550 // 1 * DAY + 18 * HOUR + 30 MIN
, end: 2760 // 1 * DAY + 22 * HOUR
, title: 'S. Møller'
}
, { start: 3990 // 2 * DAY + 18 * HOUR + 30 MIN
, end: 4200 // 2 * DAY + 22 * HOUR
, title: 'Leisure Suit Lovers'
}
, { start: 5430 // 3 * DAY + 18 * HOUR + 30 MIN
, end: 5640 // 3 * DAY + 22 * HOUR
, title: 'Kjellerbandet'
}
, { start: 6840 // 4 * DAY + 18 * HOUR
, end: 7080 // 4 * DAY + 22 * HOUR
, title: 'Snaustrinda Spelemanslag'
}
, { start: 2460 // 1 * DAY + 17 * HOUR
, end: 2550 // 1 * DAY + 18 * HOUR + 30 MIN
, title: 'Orkesterstyremøte'
}
]
const $ = win.jQuery
const moment = win.moment
const storage = win.localStorage
const VASKEVAKT_START = 11 // 11 o'clock
const VASKEVAKT_END = 12 // 12 o'clock
const BARVAKT_EVENT_START = 21 // 21 o'clock
const BARVAKT_EVENT_END = 9 // 9 o'clock (next day)
const COLOR_BG_BOOKING = '#FAA43A'
const COLOR_TEXT_BOOKING = 'white'
const COLOR_BG_SAMF_INTERNAL = '#eee'
const COLOR_TEXT_SAMF_INTERNAL = '#888'
const COLOR_BG_LAAFTE_INTERNAL = '#eee'
const COLOR_TEXT_LAAFTE_INTERNAL = '#888'
const COLOR_BG_FIXED = '#44bb99'
const COLOR_TEXT_FIXED = 'white'
const COLOR_BG_TV1000 = '#5DA5DA'
const COLOR_TEXT_TV1000 = 'white'
let currentEvent = null
const calendarParams =
{ eventSources:
// Normal booking
[ { url: '?action=events'
, etype: 'booking'
, backgroundColor: COLOR_BG_BOOKING
, textColor: COLOR_TEXT_BOOKING
}
// Interne datoer, Studentersamfundet i Trondhjem
, { googleCalendarId:
, googleCalendarApiKey: GOOGLE_API_KEY
, etype: 'gcal'
, backgroundColor: COLOR_BG_SAMF_INTERNAL
, textColor: COLOR_TEXT_SAMF_INTERNAL
}
// Musikerlåfte kalender
, { googleCalendarId: '[email protected]'
, googleCalendarApiKey: GOOGLE_API_KEY
, etype: 'gcal'
, backgroundColor: COLOR_BG_LAAFTE_INTERNAL
, textColor: COLOR_TEXT_LAAFTE_INTERNAL
}
// Fixed events
, { events: (pstart, pend, tz, cb) => {
const startofweek =
moment(pstart)
.tz('Europe/Oslo')
.startOf('day')
.subtract(pstart.day(), 'days')
const startofweekdst = startofweek.isDST()
const events = []
FIXED.forEach((row) => {
const start = startofweek.clone().add(row.start, 'minutes')
if (start.isDST() !== startofweekdst) {
if (startofweekdst) start.add(1, 'hour')
else start.subtract(1, 'hour')
}
while (start < pstart) start.add(7, 'days')
while (start < pend) {
const event =
{ start: start.toDate()
, end:
start.clone()
.add(row.end - row.start, 'minutes')
.toDate()
, title: row.title
}
events.push(event)
start.add(7, 'days')
}
})
cb(events)
}
, editable: false
, backgroundColor: COLOR_BG_FIXED
, textColor: COLOR_TEXT_FIXED
, etype: 'fixed'
}
]
, defaultView: 'agendaWeek'
, header:
{ left: 'create prev,next today'
, center: 'title'
, right: 'month,agendaWeek,agendaDay,listWeek'
}
, weekNumbers: true
, allDaySlot: true
, allDayText: ''
, scrollTime: '12:00:00'
, height: 'parent'
, nextDayThreshold: '12:00:00'
, fixedWeekCount: false
, lazyFetching: false
, navLinks: true
, displayEventEnd: true
, customButtons:
{ create:
{ text: 'Ny øvelse'
, click: () => showCreate()
}
}
, dayClick: (date) => showCreate(date)
, eventClick: (event) => {
switch (event.source.etype) {
default:
case 'fixed':
case 'gcal':
// Noop
break
case 'booking':
currentEvent = event
$('#view-when').text(event.start.twix(event.end).format())
$('#view-contact_name').text(event.contact_name)
$('#view-contact_phone').text(event.contact_phone)
$('#view-title').text(event.title)
$('#view-details').text(event.details)
$('#view-editbtn')[event.end < Date.now() ? 'hide' : 'show']()
$('#view-deletebtn')[event.id ? 'show' : 'hide']()
$('#view-modal').modal('show')
break
case 'tv1000':
$('#tv1000-header').text(event.header || '')
$('#tv1000-styrevakt').text(event.styrevakt || '')
$('#tv1000-styrevakt-tlf').text(event.styrevaktTlf || '')
$('#tv1000-name1').text(event.name1 || '')
$('#tv1000-phone1').text(event.phone1 || '')
$('#tv1000-name2').text(event.name2 || '')
$('#tv1000-phone2').text(event.phone2 || '')
$('#tv1000-note').text(event.note || '')
$('#tv1000-modal').find('.only-barvakt, .only-vaskevakt').hide()
$('#tv1000-modal .only-' + event.type).show()
$('#tv1000-modal').modal('show')
break
}
}
}
$('#calendar').fullCalendar(calendarParams)
function fillForm(event) {
currentEvent = event
$('#form-id').val(event.id || '')
$('#form-start').val(
event.start && event.start.format('YYYY-MM-DD[T]HH:mm') || ''
)
$('#form-end').val(
event.end && event.end.format('YYYY-MM-DD[T]HH:mm') || ''
)
$('#form-contact_name').val(event.contact_name || '')
$('#form-contact_phone').val(event.contact_phone || '')
$('#form-title').val(event.title || '')
$('#form-details').val(event.details || '')
}
function showCreate(start) {
const event = {}
if (start) {
event.start = start
event.end = start.clone().add(2, 'hours')
}
event.contact_name = localGet('contact_name')
event.contact_phone = localGet('contact_phone')
event.title = localGet('title')
event.details = localGet('details')
$('#form-modaltitle').text('Ny øvelse')
$('#form-submitbtn').text('Opprett')
fillForm(event)
$('#form-modal').modal('show')
}
function localGet(field) {
return storage && storage.getItem('ovingsspeilet-' + field) || ''
}
function localSet(field, value) {
if (storage)
storage.setItem('ovingsspeilet-' + field, value || '')
}
const pickerOptions =
{ format: 'YYYY-MM-DD[T]HH:mm'
, minDate: new Date()
, clearButton: false
, nowButton: false
, switchOnClick: true
, lang: 'nb'
, weekStart: 1
}
$('#form-start').bootstrapMaterialDatePicker(pickerOptions)
.on('change', (event, date) => {
$('#form-end').bootstrapMaterialDatePicker('setMinDate', date)
if (!$('#form-end').val()) {
const value =
date.clone()
.add(2, 'hours')
.format('YYYY-MM-DD[T]HH:mm')
$('#form-end').val(value)
}
})
$('#form-end').bootstrapMaterialDatePicker(pickerOptions)
.on('change', (event, date) => {
$('#form-start').bootstrapMaterialDatePicker('setMaxDate', date)
if (!$('#form-start').val()) {
const value =
date.clone()
.subtract(2, 'hours')
.format('YYYY-MM-DD[T]HH:mm')
$('#form-start').val(value)
}
})
$('#form-modal').on('submit', (event) => {
event.preventDefault()
currentEvent.start = moment($('#form-start').val())
currentEvent.end = moment($('#form-end').val())
currentEvent.contact_name = $('#form-contact_name').val()
currentEvent.contact_phone = $('#form-contact_phone').val()
currentEvent.title = $('#form-title').val()
currentEvent.details = $('#form-details').val()
localSet('contact_name', currentEvent.contact_name)
localSet('contact_phone', currentEvent.contact_phone)
localSet('title', currentEvent.title)
localSet('details', currentEvent.details)
const savedEvent = currentEvent
const params =
{ method: 'POST'
, url: '?action=save'
, data: $('#form-modal').serialize()
, success: (id) => {
if (savedEvent.id) {
$('#calendar').fullCalendar('updateEvent', savedEvent)
} else {
savedEvent.id = id
savedEvent.backgroundColor = COLOR_BG_BOOKING
savedEvent.textColor = COLOR_TEXT_BOOKING
const source =
{ events: [ savedEvent ]
, etype: 'booking'
}
$('#calendar').fullCalendar('addEventSource', source)
}
}
}
$.ajax(params)
$('#form-modal').modal('hide')
})
$('#view-editbtn').on('click', () => {
$('#view-modal').modal('hide')
$('#form-modaltitle').text('Rediger øvelse')
$('#form-submitbtn').text('Endre')
fillForm(currentEvent)
$('#form-modal').modal('show')
})
$('#view-deletebtn').on('click', () => {
if (!currentEvent.id)
return
if (!window.confirm('Er du sikker på at du vil slette denne øvelsen?'))
return
const params =
{ method: 'POST'
, url: '?action=delete'
, data: 'id=' + currentEvent.id
}
$.ajax(params)
$('#calendar').fullCalendar('removeEvents', currentEvent.id)
$('#view-modal').modal('hide')
})
// Bar og vaskevakter
const TV1000_COLUMN_BAR_DATE = 0
const TV1000_COLUMN_BAR_STYREVAKT = 1
const TV1000_COLUMN_BAR_GROUP = 2
const TV1000_COLUMN_BAR_NAME_1 = 3
const TV1000_COLUMN_BAR_PHONE_1 = 4
const TV1000_COLUMN_BAR_NAME_2 = 5
const TV1000_COLUMN_BAR_PHONE_2 = 6
const TV1000_COLUMN_BAR_NOTE = 7
const TV1000_COLUMN_BAR_STYREVAKT_TLF = 8
const TV1000_COLUMN_WASH_DATE = 9
const TV1000_COLUMN_WASH_GROUP = 10
const TV1000_COLUMN_WASH_NAME_1 = 11
const TV1000_COLUMN_WASH_PHONE_1 = 12
const TV1000_COLUMN_WASH_NAME_2 = 13
const TV1000_COLUMN_WASH_PHONE_2 = 14
const TV1000_COLUMN_WASH_NOTE = 15
const tv1000Url =
'https://sheets.googleapis.com/v4/spreadsheets/' +
TV1000_ID +
'/values/Øvingsspeilet-hjelpeark!A2:P100?key=' +
GOOGLE_API_KEY
const tv1000Success = (data) => {
const events = []
data.values.forEach((row) => {
// Barvakt
const bdate = moment(row[TV1000_COLUMN_BAR_DATE], 'YYYY-MM-DD')
let title = ''
switch (bdate.day()) {
case 5:
title = 'Klubbaften'
break
case 6:
title = 'Lørdagskveld'
break
default:
title = 'Barvakt'
break
}
title += ' ' + row[TV1000_COLUMN_BAR_GROUP]
events.push(
{ start: bdate.clone().hour(BARVAKT_EVENT_START).toDate()
, end: bdate.clone().add(1, 'day').hour(BARVAKT_EVENT_END).toDate()
, title: title + ' - Barvakt oppmøte 22:00'
, header: title + ' ' + bdate.format('LL')
, styrevakt: row[TV1000_COLUMN_BAR_STYREVAKT]
, styrevaktTlf:
row[TV1000_COLUMN_BAR_STYREVAKT_TLF]
.replace(/[^0-9]/g, '')
, group: row[TV1000_COLUMN_BAR_GROUP]
, name1: row[TV1000_COLUMN_BAR_NAME_1]
, phone1: row[TV1000_COLUMN_BAR_PHONE_1]
, name2: row[TV1000_COLUMN_BAR_NAME_2]
, phone2: row[TV1000_COLUMN_BAR_PHONE_2]
, note: row[TV1000_COLUMN_BAR_NOTE]
, type: 'barvakt'
})
// Vaskevakt
const vdate = moment(row[TV1000_COLUMN_WASH_DATE], 'YYYY-MM-DD')
events.push(
{ start: vdate.clone().hour(VASKEVAKT_START).toDate()
, end: vdate.clone().hour(VASKEVAKT_END).toDate()
, title: 'Vaskevakt ' + row[TV1000_COLUMN_WASH_GROUP]
, header:
'Vaskevakt ' +
row[TV1000_COLUMN_WASH_GROUP] +
' ' +
vdate.format('LL')
, name1: row[TV1000_COLUMN_WASH_NAME_1]
, phone1: row[TV1000_COLUMN_WASH_PHONE_1]
, name2: row[TV1000_COLUMN_WASH_NAME_2]
, phone2: row[TV1000_COLUMN_WASH_PHONE_2]
, note: row[TV1000_COLUMN_WASH_NOTE]
, type: 'vaskevakt'
})
})
const source =
{ events: events
, backgroundColor: COLOR_BG_TV1000
, textColor: COLOR_TEXT_TV1000
, etype: 'tv1000'
}
$('#calendar').fullCalendar('addEventSource', source)
}
$.ajax(tv1000Url, { success: tv1000Success })
})
})(window)