forked from nextcloud/calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorMixin.js
More file actions
789 lines (743 loc) Β· 20.7 KB
/
Copy pathEditorMixin.js
File metadata and controls
789 lines (743 loc) Β· 20.7 KB
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/**
* @copyright Copyright (c) 2019 Georg Ehrke
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { getRFCProperties } from '../models/rfcProps'
import logger from '../utils/logger.js'
import { getIllustrationForTitle } from '../utils/illustration.js'
import { getPrefixedRoute } from '../utils/router.js'
import { dateFactory } from '../utils/date.js'
import { uidToHexColor } from '../utils/color.js'
import { mapState } from 'vuex'
/**
* This is a mixin for the editor. It contains common Vue stuff, that is
* required both in the popover as well as the sidebar.
*
* See inline for more documentation
*/
export default {
data() {
return {
// Indicator whether or not the event is currently loading
isLoading: true,
// Indicator whether the editor is visible
isVisible: false,
// Indicator whether sidebar has been fully expanded
isExpanded: false,
// Stores error if any occurred
error: false,
// The calendar-id of the selected calendar
calendarId: null,
// Whether or not an action is required on leave
requiresActionOnRouteLeave: true,
// Whether or not the this and all future option will be forced
// This is the case when editing the recurrence-rule of an existing recurring event
forceThisAndAllFuture: false,
// Whether or not the master item is being edited
isEditingMasterItem: false,
// Whether or not it is a recurrence-exception
isRecurrenceException: false,
}
},
computed: {
...mapState({
calendarObject: (state) => state.calendarObjectInstance.calendarObject || null,
calendarObjectInstance: (state) => state.calendarObjectInstance.calendarObjectInstance || null,
}),
eventComponent() {
return this.calendarObjectInstance ? this.calendarObjectInstance.eventComponent : null
},
/**
* Returns the events title or an empty string if the event is still loading
*
* @returns {string}
*/
title() {
if (!this.eventComponent) {
return ''
}
return this.calendarObjectInstance.title || ''
},
/**
* Returns the location or null if the event is still loading
*
* @returns {string|null}
*/
location() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.location
},
/**
* Returns the description or null if the event is still loading
*
* @returns {string|null}
*/
description() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.description
},
/**
* Returns the start-date (without timezone) or null if the event is still loading
*
* @returns {Date|null}
*/
startDate() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.startDate
},
/**
* Returns the timezone of the event's start-date or null if the event is still loading
*
* @returns {string|null}
*/
startTimezone() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.startTimezoneId
},
/**
* Returns the end-date (without timezone) or null if the event is still loading
*
* @returns {Date|null}
*/
endDate() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.endDate
},
/**
* Returns the timezone of the event's end-date or null if the event is still loading
*
* @returns {string|null}
*/
endTimezone() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.endTimezoneId
},
/**
* Returns whether or not the event is all-day or null if the event is still loading
*
* @returns {boolean|null}
*/
isAllDay() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.isAllDay
},
/**
* Returns whether or not the user is allowed to modify the all-day setting
*
* @returns {boolean}
*/
canModifyAllDay() {
if (!this.calendarObjectInstance) {
return false
}
return this.calendarObjectInstance.canModifyAllDay
},
/**
* Returns an illustration matching this event's title
*
* @returns {string}
*/
backgroundImage() {
return getIllustrationForTitle(this.title)
},
/**
* Returns the color the illustration should be colored in
*
* @returns {String}
*/
illustrationColor() {
return this.color || this.selectedCalendarColor
},
/**
* Returns the color of the calendar selected by the user
* This is used to color illustration
*
* @returns {string|*}
*/
selectedCalendarColor() {
if (!this.selectedCalendar) {
const calendars = this.$store.getters.sortedCalendars
if (calendars.length > 0) {
return calendars[0].color
}
return uidToHexColor('')
}
return this.selectedCalendar.color
},
/**
* Returns the custom color of this event
*
* @returns {null|String}
*/
color() {
if (!this.calendarObjectInstance) {
return null
}
return this.calendarObjectInstance.customColor
},
/**
* Returns whether or not to display event details
*
* @returns {boolean}
*/
displayDetails() {
return this.isLoading === false
&& this.error === false
&& this.isVisible === true
},
showSaveButtons() {
return this.displayDetails === true
&& this.isReadOnly === false
},
/**
* Returns whether or not to allow editing the event
*
* @returns {boolean}
*/
isReadOnly() {
if (!this.calendarObject) {
return true
}
const calendar = this.$store.getters.getCalendarById(this.calendarObject.calendarId)
if (!calendar) {
return true
}
return calendar.readOnly
},
/**
* Returns all calendars selectable by the user
*
* @returns {Object[]}
*/
calendars() {
if (this.isReadOnly && this.calendarObject) {
return [
this.$store.getters.getCalendarById(this.calendarObject.calendarId),
]
}
return this.$store.getters.sortedCalendars
},
/**
* Returns the object of the selected calendar
*
* @returns {Object}
*/
selectedCalendar() {
return this.$store.getters.getCalendarById(this.calendarId)
},
/**
* Returns whether or not to display the calendar-picker
*
* @returns {boolean}
*/
showCalendarPicker() {
// Always show the calendar's name when we are in a read-only calendar
if (this.isReadOnly) {
return true
}
return this.$store.getters.sortedCalendars.length > 1
},
/**
* Returns the preferred timezone of the user.
* If the timezone is set to automatic, it returns the detected one
*
* @returns {string}
*/
currentUserTimezone() {
return this.$store.getters.getResolvedTimezone
},
/**
* Returns whether or not the user is allowed to delete this event
*
* @returns {boolean}
*/
canDelete() {
if (!this.calendarObject) {
return false
}
if (this.isReadOnly) {
return false
}
if (this.isLoading) {
return false
}
return this.calendarObject.existsOnServer
},
/**
* Returns whether or not the user is allowed to create recurrence exceptions for this event
*
* @returns {boolean}
*/
canCreateRecurrenceException() {
if (!this.eventComponent) {
return false
}
return this.eventComponent.canCreateRecurrenceExceptions()
},
/**
* Returns a an object with properties from RFCs including
* their displayName, a description, options, etc.
*
* @returns {{geo, color, timeTransparency, description, resources, location, categories, accessClass, priority, status}}
*/
rfcProps() {
return getRFCProperties()
},
/**
* Returns whether or not this event can be downloaded from the server
*
* @returns {boolean}
*/
hasDownloadURL() {
if (!this.calendarObject) {
return false
}
if (this.isLoading) {
return false
}
return this.calendarObject.existsOnServer
},
/**
* Returns the download url as a string or null if event is loading or does not exist on the server (yet)
*
* @returns {string|null}
*/
downloadURL() {
if (!this.calendarObject) {
return null
}
if (!this.calendarObject.dav) {
return null
}
return this.calendarObject.dav.url + '?export'
},
/**
* Returns whether or not this is a new event
*
* @returns {boolean}
*/
isNew() {
if (!this.calendarObject) {
return true
}
if (!this.calendarObject.dav) {
return true
}
return false
},
},
mounted() {
this.$nextTick(() => {
// The animation does not work with v-if only.
// So we mount it with display:none (aka v-show=false)
// and set it to display:block once it's mounted
this.isVisible = true
// Certain elements in the sidebar like autosize textareas
// require to the sidebar to be fully opened the calculate
// their height. The workaround of setting an absolute width
// on the textareas does not work, because the sidebar itself
// does not have an absolute width.
window.setTimeout(() => {
this.isExpanded = true
}, 500)
})
},
methods: {
/**
* Changes the selected calendar
* Does not move the calendar-object yet, that's done in save
*
* @param {Object} selectedCalendar The new calendar selected by the user
*/
changeCalendar(selectedCalendar) {
this.calendarId = selectedCalendar.id
// If this is a new event that does not exist on the server yet,
// override the internally stored calendarId. If we did not do this,
// it would create the event in the default calendar first and move it
// to the desired calendar as a second step.
if (this.calendarObject && !this.calendarObject.existsOnServer) {
this.calendarObject.calendarId = selectedCalendar.id
}
},
/**
* This will forge the user to update this and all future occurrences when saving
*/
forceModifyingFuture() {
this.forceThisAndAllFuture = true
},
/**
* Closes the editor and returns to normal calendar-view
*/
closeEditor() {
const params = Object.assign({}, this.$store.state.route.params)
delete params.object
delete params.recurrenceId
this.$router.push({
name: getPrefixedRoute(this.$store.state.route.name, 'CalendarView'),
params,
})
this.$store.commit('resetCalendarObjectInstanceObjectIdAndRecurrenceId')
},
/**
* Resets the calendar-object back to it's original state and closes the editor
*/
async cancel() {
if (this.isLoading) {
return
}
if (!this.calendarObject) {
logger.error('Calendar-object not found')
this.closeEditor()
return
}
this.$store.commit('resetCalendarObjectToDav', {
calendarObject: this.calendarObject,
})
this.requiresActionOnRouteLeave = false
this.closeEditor()
},
/**
* Saves a calendar-object
*
* @param {Boolean} thisAndAllFuture Whether to modify only this or this and all future occurrences
* @returns {Promise<void>}
*/
async save(thisAndAllFuture = false) {
if (!this.calendarObject) {
logger.error('Calendar-object not found')
return
}
if (this.isReadOnly) {
return
}
if (this.forceThisAndAllFuture) {
thisAndAllFuture = true
}
this.isLoading = true
await this.$store.dispatch('saveCalendarObjectInstance', {
thisAndAllFuture,
calendarId: this.calendarId,
})
this.isLoading = false
},
/**
* Saves a calendar-object and closes the editor
*
* @param {Boolean} thisAndAllFuture Whether to modify only this or this and all future occurrences
* @returns {Promise<void>}
*/
async saveAndLeave(thisAndAllFuture = false) {
await this.save(thisAndAllFuture)
this.requiresActionOnRouteLeave = false
this.closeEditor()
},
/**
* Deletes a calendar-object
*
* @param {Boolean} thisAndAllFuture Whether to delete only this or this and all future occurrences
* @returns {Promise<void>}
*/
async delete(thisAndAllFuture = false) {
if (!this.calendarObject) {
logger.error('Calendar-object not found')
return
}
if (this.isReadOnly) {
return
}
this.isLoading = true
await this.$store.dispatch('deleteCalendarObjectInstance', { thisAndAllFuture })
this.isLoading = false
},
/**
* Deletes a calendar-object and closes the editor
*
* @param {Boolean} thisAndAllFuture Whether to delete only this or this and all future occurrences
* @returns {Promise<void>}
*/
async deleteAndLeave(thisAndAllFuture = false) {
await this.delete(thisAndAllFuture)
this.requiresActionOnRouteLeave = false
this.closeEditor()
},
/**
* Updates the title of this event
*
* @param {String} title New title
*/
updateTitle(title) {
if (title.trim() === '') {
title = null
}
this.$store.commit('changeTitle', {
calendarObjectInstance: this.calendarObjectInstance,
title,
})
},
/**
* Updates the description of this event
*
* @param {String} description New description
*/
updateDescription(description) {
this.$store.commit('changeDescription', {
calendarObjectInstance: this.calendarObjectInstance,
description,
})
},
/**
* Updates the location of this event
*
* @param {String} location New location
*/
updateLocation(location) {
this.$store.commit('changeLocation', {
calendarObjectInstance: this.calendarObjectInstance,
location,
})
},
/**
* Updates the start date of this event
*
* @param {Date} startDate New start date
*/
updateStartDate(startDate) {
this.$store.dispatch('changeStartDate', {
calendarObjectInstance: this.calendarObjectInstance,
startDate,
})
},
/**
* Updates the timezone of this event's start date
*
* @param {String} startTimezone New start timezone
*/
updateStartTimezone(startTimezone) {
if (!startTimezone) {
return
}
this.$store.dispatch('changeStartTimezone', {
calendarObjectInstance: this.calendarObjectInstance,
startTimezone,
})
},
/**
* Updates the end date of this event
*
* @param {Date} endDate New end date
*/
updateEndDate(endDate) {
this.$store.commit('changeEndDate', {
calendarObjectInstance: this.calendarObjectInstance,
endDate,
})
},
/**
* Updates the timezone of this event's end date
*
* @param {String} endTimezone New end timezone
*/
updateEndTimezone(endTimezone) {
if (!endTimezone) {
return
}
this.$store.dispatch('changeEndTimezone', {
calendarObjectInstance: this.calendarObjectInstance,
endTimezone,
})
},
/**
* Toggles the event between all-day and timed
*/
toggleAllDay() {
this.$store.dispatch('toggleAllDay', {
calendarObjectInstance: this.calendarObjectInstance,
})
},
/**
* Resets the internal state after changing the viewed calendar-object
*/
resetState() {
this.isLoading = true
this.error = false
this.calendarId = null
this.requiresActionOnRouteLeave = true
this.forceThisAndAllFuture = false
this.isEditingMasterItem = false
this.isRecurrenceException = false
},
},
/**
* This is executed before entering the Editor routes
*
* @param {Object} to The route to navigate to
* @param {Object} from The route coming from
* @param {Function} next Function to be called when ready to load the next view
*/
async beforeRouteEnter(to, from, next) {
if (to.name === 'NewSidebarView' || to.name === 'NewPopoverView') {
next(async vm => {
vm.resetState()
const isAllDay = (to.params.allDay === '1')
const start = parseInt(to.params.dtstart, 10)
const end = parseInt(to.params.dtend, 10)
const timezoneId = vm.$store.getters.getResolvedTimezone
const calendarId = vm.$store.state.settings.defaultCalendarId
try {
await vm.$store.dispatch('getCalendarObjectInstanceForNewEvent', { isAllDay, start, end, timezoneId, calendarId })
vm.calendarId = vm.calendarObject.calendarId
} catch (error) {
console.debug(error)
vm.error = true
} finally {
vm.isLoading = false
}
})
} else {
next(async vm => {
vm.resetState()
const objectId = to.params.object
const recurrenceId = to.params.recurrenceId
if (recurrenceId === 'next') {
const closeToDate = dateFactory()
// TODO: can we replace this by simply returning the new route since we are inside next()
// Probably not though, because it's async
const recurrenceId = await vm.$store.dispatch('resolveClosestRecurrenceIdForCalendarObject', { objectId, closeToDate })
const params = Object.assign({}, vm.$route.params, { recurrenceId })
vm.$router.replace({ name: vm.$route.name, params })
}
try {
await vm.$store.dispatch('getCalendarObjectInstanceByObjectIdAndRecurrenceId', { objectId, recurrenceId })
vm.calendarId = vm.calendarObject.calendarId
vm.isEditingMasterItem = vm.eventComponent.isMasterItem()
vm.isRecurrenceException = vm.eventComponent.isRecurrenceException()
} catch (error) {
console.debug(error)
vm.error = true
} finally {
vm.isLoading = false
}
})
}
},
/**
* This function is called when the route changes. This can be caused by various actions:
* - Change of selected time-range when creating new event
* - Navigating through the calendar-view
*
* @param {Object} to The route to navigate to
* @param {Object} from The route coming from
* @param {Function} next Function to be called when ready to load the next view
*/
async beforeRouteUpdate(to, from, next) {
// If we are in the New Event dialog, we want to update the selected time
if (to.name === 'NewSidebarView' || to.name === 'NewPopoverView') {
// If allDay, dtstart and dtend are the same there is no need to update.
// This is usally the case when navigating through the calendar while the editor is open
if (to.params.allDay === from.params.allDay
&& to.params.dtstart === from.params.dtstart
&& to.params.dtend === from.params.dtend) {
next()
return
}
const isAllDay = (to.params.allDay === '1')
const start = to.params.dtstart
const end = to.params.dtend
const timezoneId = this.$store.getters.getResolvedTimezone
await this.$store.dispatch('updateCalendarObjectInstanceForNewEvent', { isAllDay, start, end, timezoneId })
next()
} else {
// If both the objectId and recurrenceId remained the same
// there is no need to update. This is usally the case when navigating
// through the calendar while the editor is open
if (to.params.object === from.params.object
&& to.params.recurrenceId === from.params.recurrenceId) {
next()
return
}
this.isLoading = true
try {
await this.save()
} catch (error) {
console.debug(error)
next(false)
return
}
this.resetState()
const objectId = to.params.object
const recurrenceId = to.params.recurrenceId
if (recurrenceId === 'next') {
const closeToDate = dateFactory()
const recurrenceId = await this.$store.dispatch('resolveClosestRecurrenceIdForCalendarObject', { objectId, closeToDate })
const params = Object.assign({}, this.$route.params, { recurrenceId })
next({ name: this.$route.name, params })
return
}
try {
await this.$store.dispatch('getCalendarObjectInstanceByObjectIdAndRecurrenceId', { objectId, recurrenceId })
this.calendarId = this.calendarObject.calendarId
this.isEditingMasterItem = this.eventComponent.isMasterItem()
this.isRecurrenceException = this.eventComponent.isRecurrenceException()
} catch (error) {
console.debug(error)
this.error = true
} finally {
this.isLoading = false
next()
}
}
},
/**
* This route is called when the user leaves the editor
*
* @param {Object} to The route to navigate to
* @param {Object} from The route coming from
* @param {Function} next Function to be called when ready to load the next view
*/
async beforeRouteLeave(to, from, next) {
// requiresActionOnRouteLeave is false when an action like deleting / saving / cancelling was already taken.
// The responsibility of this method is to automatically save the event when the user clicks outside the editor
if (!this.requiresActionOnRouteLeave) {
next()
return
}
try {
await this.save()
next()
} catch (error) {
console.debug(error)
next(false)
}
},
}