-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathFreeBusy.vue
575 lines (547 loc) Β· 15.5 KB
/
FreeBusy.vue
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcDialog size="large"
:name="dialogName || $t('calendar', 'Availability of attendees, resources and rooms')"
@closing="$emit('close')">
<div class="modal__content modal--scheduler">
<div v-if="loadingIndicator" class="loading-indicator">
<div class="icon-loading" />
</div>
<div class="modal__content__header">
<h2>{{ $t('calendar', 'Find a time') }}</h2>
<h3>{{ eventTitle }}</h3>
<div class="modal__content__header__attendees">
{{ t('calendar', 'with') }}
<NcUserBubble size="24" :display-name="organizer.commonName" />
<NcUserBubble v-for="attendee in attendees"
:key="attendee.id"
size="24"
class="modal__content__header__attendees__user-bubble"
:display-name="attendee.commonName">
<template #name>
<a href="#"
title="Remove user"
class="icon-close"
@click="removeAttendee(attendee)" />
</template>
</NcUserBubble>
</div>
</div>
<div class="modal__content__actions">
<InviteesListSearch class="modal__content__actions__select"
:already-invited-emails="alreadyInvitedEmails"
:organizer="organizer"
@add-attendee="addAttendee" />
<div class="modal__content__actions__date">
<NcButton type="secondary"
@click="handleActions('today')">
{{ $t('calendar', 'Today') }}
</NcButton>
<NcButton type="secondary"
@click="handleActions('left')">
<template #icon>
<ChevronLeftIcon :size="20" />
</template>
</NcButton>
<NcButton type="secondary"
@click="handleActions('right')">
<template #icon>
<ChevronRightIcon :size="20" />
</template>
</NcButton>
<NcDateTimePickerNative :hide-label="true"
:value="currentDate"
@input="(date)=>handleActions('picker', date)" />
<NcPopover :focus-trap="false">
<template #trigger>
<NcButton type="tertiary-no-background">
<template #icon>
<HelpCircleIcon :size="20" />
</template>
</NcButton>
</template>
<template>
<div class="freebusy-caption">
<div class="freebusy-caption__calendar-user-types" />
<div class="freebusy-caption__colors">
<div v-for="color in colorCaption" :key="color.color" class="freebusy-caption-item">
<div class="freebusy-caption-item__color" :style="{ 'background-color': color.color }" />
<div class="freebusy-caption-item__label">
{{ color.label }}
</div>
</div>
</div>
</div>
</template>
</NcPopover>
</div>
</div>
<FullCalendar ref="freeBusyFullCalendar"
:options="options" />
<div v-if="!disableFindTime" class="modal__content__footer">
<div class="modal__content__footer__title">
<p v-if="freeSlots">
{{ $t('calendar', 'Available times:') }}
<NcSelect class="available-slots__multiselect"
:options="freeSlots"
:placeholder="placeholder"
:clearable="false"
input-id="slot"
label="displayStart"
:label-outside="true"
:value="selectedSlot"
@option:selected="setSlotSuggestion">
<template #selected-option="{}">
{{ $t('calendar', 'Suggestion accepted') }}
</template>
</NcSelect>
</p>
<h3>
{{ formattedCurrentStart }}
</h3>
<p>{{ formattedCurrentTime }}<span class="modal__content__footer__title__timezone">{{ formattedTimeZone }}</span></p>
</div>
<NcButton type="primary"
@click="save">
{{ $t('calendar', 'Done') }}
<template #icon>
<CheckIcon :size="20" />
</template>
</NcButton>
</div>
</div>
</NcDialog>
</template>
<script>
// Import FullCalendar itself
import FullCalendar from '@fullcalendar/vue'
import resourceTimelinePlugin from '@fullcalendar/resource-timeline'
import interactionPlugin from '@fullcalendar/interaction'
import { NcDateTimePickerNative, NcButton, NcPopover, NcUserBubble, NcDialog, NcSelect } from '@nextcloud/vue'
// Import event sources
import freeBusyBlockedForAllEventSource from '../../../fullcalendar/eventSources/freeBusyBlockedForAllEventSource.js'
import freeBusyFakeBlockingEventSource from '../../../fullcalendar/eventSources/freeBusyFakeBlockingEventSource.js'
import freeBusyResourceEventSource from '../../../fullcalendar/eventSources/freeBusyResourceEventSource.js'
// Import localization plugins
import { getDateFormattingConfig } from '../../../fullcalendar/localization/dateFormattingConfig.js'
import { getFullCalendarLocale } from '../../../fullcalendar/localization/localeProvider.js'
import momentPluginFactory from '../../../fullcalendar/localization/momentPlugin.js'
// Import timezone plugins
import VTimezoneNamedTimezone from '../../../fullcalendar/timezones/vtimezoneNamedTimezoneImpl.js'
import ChevronRightIcon from 'vue-material-design-icons/ChevronRight.vue'
import ChevronLeftIcon from 'vue-material-design-icons/ChevronLeft.vue'
import CheckIcon from 'vue-material-design-icons/Check.vue'
import HelpCircleIcon from 'vue-material-design-icons/HelpCircle.vue'
import InviteesListSearch from '../Invitees/InviteesListSearch.vue'
import { getColorForFBType } from '../../../utils/freebusy.js'
import { getFirstFreeSlot, getBusySlots } from '../../../services/freeBusySlotService.js'
import dateFormat from '../../../filters/dateFormat.js'
import { mapState } from 'pinia'
import useSettingsStore from '../../../store/settings.js'
export default {
name: 'FreeBusy',
components: {
NcSelect,
FullCalendar,
InviteesListSearch,
NcDateTimePickerNative,
NcDialog,
NcButton,
NcPopover,
NcUserBubble,
ChevronRightIcon,
ChevronLeftIcon,
CheckIcon,
HelpCircleIcon,
},
props: {
/**
* The organizer object.
* See /src/models/attendee.js for details
*/
organizer: {
type: Object,
required: true,
},
/**
* The attendee objects.
* See /src/models/attendee.js for details
*/
attendees: {
type: Array,
required: true,
},
/**
* The start-date to query free-busy information from
*/
startDate: {
type: Date,
required: true,
},
/**
* The end-date to query free-busy information from
*/
endDate: {
type: Date,
required: true,
},
eventTitle: {
type: String,
default: '',
},
alreadyInvitedEmails: {
type: Array,
default: () => [],
},
dialogName: {
type: String,
required: false,
},
disableFindTime: {
type: Boolean,
default: false,
},
},
data() {
return {
loadingIndicator: true,
currentDate: this.startDate,
currentStart: this.startDate,
currentEnd: this.endDate,
lang: getFullCalendarLocale().locale,
formattingOptions: { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' },
freeSlots: [],
selectedSlot: null,
}
},
mounted() {
const calendar = this.$refs.freeBusyFullCalendar.getApi()
calendar.scrollToTime(this.scrollTime)
this.findFreeSlots()
},
computed: {
...mapState(useSettingsStore, {
timezoneId: 'getResolvedTimezone',
}),
...mapState(useSettingsStore, ['showWeekends', 'showWeekNumbers', 'timezone']),
placeholder() {
return this.$t('calendar', 'Select automatic slot')
},
/**
* FullCalendar Plugins
*
* @return {(PluginDef)[]}
*/
plugins() {
return [
resourceTimelinePlugin,
momentPluginFactory(),
VTimezoneNamedTimezone,
interactionPlugin,
]
},
formattedCurrentStart() {
return this.currentDate.toLocaleDateString(this.lang, this.formattingOptions)
},
formattedCurrentTime() {
const options = { hour: '2-digit', minute: '2-digit', hour12: true }
const startTime = this.currentStart.toLocaleTimeString(this.lang, options)
const endTime = this.currentEnd.toLocaleTimeString(this.lang, options)
return `${startTime} - ${endTime} `
},
scrollTime() {
const options = { hour: '2-digit', minute: '2-digit', seconds: '2-digit', hour12: false }
return this.currentDate.getHours() > 0 ? new Date(this.currentDate.getTime() - 60 * 60 * 1000).toLocaleTimeString(this.lang, options) : '10:00:00'
},
formattedTimeZone() {
return this.timezoneId.replace('/', '-')
},
eventSources() {
return [
freeBusyResourceEventSource(
this._uid,
this.organizer.attendeeProperty,
this.attendees.map((a) => a.attendeeProperty),
),
freeBusyFakeBlockingEventSource(
this._uid,
this.resources,
this.currentStart,
this.currentEnd,
),
freeBusyBlockedForAllEventSource(
this.organizer.attendeeProperty,
this.attendees.map((a) => a.attendeeProperty),
this.resources,
),
]
},
resources() {
const resources = []
const roles = {
CHAIR: this.$t('calendar', 'chairperson'),
'REQ-PARTICIPANT': this.$t('calendar', 'required participant'),
'NON-PARTICIPANT': this.$t('calendar', 'non-participant'),
'OPT-PARTICIPANT': this.$t('calendar', 'optional participant'),
}
for (const attendee of [this.organizer, ...this.attendees]) {
let title = attendee.commonName || attendee.uri.slice(7)
if (attendee === this.organizer) {
title = this.$t('calendar', '{organizer} (organizer)', {
organizer: title,
})
} else {
title = this.$t('calendar', '{attendee} ({role})', {
attendee: title,
role: roles[attendee.role],
})
}
resources.push({
id: attendee.attendeeProperty.email,
title,
})
}
// Sort the resources by ID, just like fullcalendar does. This ensures that
// the fake blocking event can know the first and last resource reliably
// ref https://fullcalendar.io/docs/resourceOrder
resources.sort((a, b) => (a.id > b.id) - (a.id < b.id))
return resources
},
/**
* List of possible Free-Busy values.
* This is used as legend.
*
* @return {({color: string, label: string})[]}
*/
colorCaption() {
return [{
// TRANSLATORS: free as in available
label: this.$t('calendar', 'Free'),
color: getColorForFBType('FREE'),
}, {
label: this.$t('calendar', 'Busy (tentative)'),
color: getColorForFBType('BUSY-TENTATIVE'),
}, {
label: this.$t('calendar', 'Busy'),
color: getColorForFBType('BUSY'),
}, {
label: this.$t('calendar', 'Out of office'),
color: getColorForFBType('BUSY-UNAVAILABLE'),
}, {
label: this.$t('calendar', 'Unknown'),
color: getColorForFBType('UNKNOWN'),
}]
},
/**
* Configuration options for FullCalendar
* Please see https://fullcalendar.io/docs#toc for details
*
* @return {object}
*/
options() {
return {
// Initialization:
initialView: 'resourceTimelineDay',
initialDate: this.currentStart,
schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
// Data
eventSources: this.eventSources,
resources: this.resources,
// Plugins
plugins: this.plugins,
// Interaction:
editable: false,
selectable: true,
select: this.handleSelect,
// Localization:
...getDateFormattingConfig(),
...getFullCalendarLocale(),
// Rendering
height: 'auto',
loading: this.loading,
headerToolbar: false,
resourceAreaColumns: [
{
field: 'title',
headerContent: 'Attendees',
},
],
// Timezones:
timeZone: this.timezoneId,
// Formatting of the title
// will produce something like "Tuesday, September 18, 2018"
// ref https://fullcalendar.io/docs/date-formatting
titleFormat: {
month: 'long',
year: 'numeric',
day: 'numeric',
weekday: 'long',
},
dateClick: this.findFreeSlots(),
}
},
},
methods: {
handleSelect(arg) {
this.currentStart = arg.start
this.currentEnd = arg.end
},
save() {
this.$emit('update-dates', { start: this.currentStart, end: this.currentEnd })
},
addAttendee(attendee) {
this.$emit('add-attendee', attendee)
this.findFreeSlots()
},
removeAttendee(attendee) {
this.$emit('remove-attendee', attendee)
this.findFreeSlots()
},
loading(isLoading) {
this.loadingIndicator = isLoading
},
handleActions(action, date = null) {
const calendar = this.$refs.freeBusyFullCalendar.getApi()
switch (action) {
case 'today':
calendar.today()
break
case 'left':
calendar.prev()
break
case 'right':
calendar.next()
break
case 'picker':
calendar.gotoDate(date)
break
}
this.currentDate = calendar.getDate()
calendar.scrollToTime(this.scrollTime)
this.findFreeSlots()
},
async findFreeSlots() {
// Doesn't make sense for multiple days
if (this.currentStart.getDate() !== this.currentEnd.getDate()) {
return
}
// Needed to update with full calendar widget changes
const startSearch = new Date(this.currentStart)
startSearch.setDate(this.currentDate.getDate())
startSearch.setMonth(this.currentDate.getMonth())
startSearch.setYear(this.currentDate.getFullYear())
const endSearch = new Date(this.currentEnd)
endSearch.setDate(this.currentDate.getDate())
endSearch.setMonth(this.currentDate.getMonth())
endSearch.setYear(this.currentDate.getFullYear())
try {
// for now search slots only in the first week days
const endSearchDate = new Date(startSearch)
endSearchDate.setDate(startSearch.getDate() + 8)
const eventResults = await getBusySlots(
this.organizer.attendeeProperty,
this.attendees.map((a) => a.attendeeProperty),
startSearch,
endSearchDate,
this.timezoneId,
)
const freeSlots = getFirstFreeSlot(
startSearch,
endSearch,
eventResults.events,
)
freeSlots.forEach((slot) => {
slot.displayStart = dateFormat(slot.start, false, getFullCalendarLocale().locale)
})
this.freeSlots = freeSlots
} catch (error) {
// Handle error here
console.error('Error occurred while finding free slots:', error)
throw error // Re-throwing the error to handle it in the caller
}
},
setSlotSuggestion(slot) {
this.selectedSlot = slot
const calendar = this.$refs.freeBusyFullCalendar.getApi()
calendar.gotoDate(slot.start)
calendar.scrollToTime(this.scrollTime)
// have to make these "selected" version of the props seeing as they can't be modified directly, and they aren't updated reactively when vuex is
this.currentStart = slot.start
this.currentEnd = slot.end
const clonedDate = new Date(slot.start) // so as not to modify slot.start
this.currentDate = new Date(clonedDate.setHours(0, 0, 0, 0))
},
},
}
</script>
<style lang='scss' scoped>
.icon-close {
display: block;
height: 100%;
}
.modal__content {
padding: 50px;
//when the calendar is open, it's cut at the bottom, adding a margin fixes it
margin-bottom: 95px;
&__actions{
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
&__select{
width: 260px;
}
&__date{
display: flex;
justify-content: space-between;
align-items: center;
& > *{
margin-left: 5px;
}
}
}
&__header{
margin-bottom: 20px;
h3{
font-weight: 500;
}
&__attendees{
&__user-bubble{
margin-right: 5px;
}
}
}
&__footer{
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;
&__title{
h3{
font-weight: 500;
}
&__timezone{
color: var(--color-text-lighter);
}
}
}
}
:deep(.vs__search ) {
text-overflow: ellipsis;
}
:deep(.mx-input) {
height: 38px !important;
}
</style>
<style lang="scss">
.blocking-event-free-busy {
// Show the blocking event above any other blocks, especially the *blocked for all* one
z-index: 3 !important;
}
.free-busy-block {
opacity: 0.7 !important;
}
</style>