-
Notifications
You must be signed in to change notification settings - Fork 100
/
AppNavigation.vue
493 lines (467 loc) · 13.1 KB
/
AppNavigation.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
<!--
Nextcloud - Tasks
@author Raimund Schlüßler
@copyright 2018 Raimund Schlüßler <raimund.schluessler@mailbox.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.
This library 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 library. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<NcAppNavigation>
<template #list>
<NcAppNavigationItem v-for="collection in collections"
v-show="!hideCollection(collection)"
:id="'collection_' + collection.id"
:key="collection.id"
:collection-id="collection.id"
:to="{ name: 'collections', params: { collectionId: collection.id } }"
:name="collection.displayName"
class="collection reactive"
draggable="false"
@dragstart="dragStart"
@drop="(e) => dropTaskOnCollection(e, collection)"
@dragover="dragOver"
@dragenter="(e) => dragEnter(e, collection)"
@dragleave="dragLeave"
@click="setInitialRoute(`/collections/${collection.id}`)">
<template #icon>
<component :is="collection.icon"
:size="20" />
</template>
<template #counter>
<NcCounterBubble v-show="collectionCount(collection.id)">
{{ counterFormatter(collectionCount(collection.id)) }}
</NcCounterBubble>
</template>
</NcAppNavigationItem>
<Sortable class="draggable-container"
:list="calendars"
item-key="id"
:options="{swapThreshold: 0.30, delay: 500, delayOnTouchOnly: true, touchStartThreshold: 3}"
@dragstart="setData($event)"
@update="update">
<template #item="{element}">
<ListItemCalendar :calendar="element"
@click="setInitialRoute(`/calendars/${element.id}`)" />
</template>
</Sortable>
<NcAppNavigationItem v-click-outside="() => {creating = false}"
:name="t('tasks', 'Add List…')"
:class="{'collection--edit': creating}"
class="collection reactive"
@click="startCreate($event)">
<template #icon>
<Plus :size="20" />
</template>
<li>
<div class="app-navigation-entry-edit">
<NcTextField ref="newListInput"
v-model="newCalendarName"
v-tooltip="{
content: tooltipMessage,
shown: showTooltip('list_new'),
trigger: 'manual'
}"
type="text"
:show-trailing-button="newCalendarName !== ''"
trailing-button-icon="arrowRight"
:error="nameError"
:label="t('tasks', 'New list')"
@trailing-button-click="create()"
@keyup="checkName($event)">
<Plus :size="16" />
</NcTextField>
<Colorpicker :selected-color="selectedColor" @color-selected="setColor" />
</div>
</li>
</NcAppNavigationItem>
<Trashbin v-if="hasTrashBin" />
</template>
<template #footer>
<AppNavigationSettings />
</template>
</NcAppNavigation>
</template>
<script>
import ListItemCalendar from '../components/AppNavigation/ListItemCalendar.vue'
import Colorpicker from '../components/AppNavigation/Colorpicker.vue'
import AppNavigationSettings from '../components/AppNavigation/AppNavigationSettings.vue'
import Trashbin from '../components/AppNavigation/Trashbin.vue'
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import NcAppNavigation from '@nextcloud/vue/dist/Components/NcAppNavigation.js'
import NcAppNavigationItem from '@nextcloud/vue/dist/Components/NcAppNavigationItem.js'
import NcCounterBubble from '@nextcloud/vue/dist/Components/NcCounterBubble.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip.js'
import CalendarToday from 'vue-material-design-icons/CalendarToday.vue'
import CalendarWeek from 'vue-material-design-icons/CalendarWeek.vue'
import Check from 'vue-material-design-icons/Check.vue'
import CircleOutline from 'vue-material-design-icons/CircleOutline.vue'
import Plus from 'vue-material-design-icons/Plus.vue'
import Star from 'vue-material-design-icons/Star.vue'
import TrendingUp from 'vue-material-design-icons/TrendingUp.vue'
import { Sortable } from 'sortablejs-vue3'
import { vOnClickOutside as ClickOutside } from '@vueuse/components'
import { mapState, mapGetters, mapActions } from 'vuex'
export default {
components: {
ListItemCalendar,
Colorpicker,
Trashbin,
NcAppNavigation,
NcAppNavigationItem,
NcCounterBubble,
NcTextField,
AppNavigationSettings,
Sortable,
CalendarToday,
CalendarWeek,
Check,
CircleOutline,
Plus,
TrendingUp,
Star,
},
directives: {
ClickOutside,
Tooltip,
},
inject: ['$OCA'],
data() {
return {
editing: '',
shareOpen: '',
copySuccess: false,
copied: false,
creating: false,
nameError: false,
newCalendarName: '',
selectedColor: '',
tooltipMessage: '',
tooltipTarget: '',
}
},
computed: {
...mapState({
collections: state => state.collections.collections,
}),
...mapGetters({
calendars: 'getSortedCalendars',
collectionCount: 'getCollectionCount',
calendarCount: 'getCalendarCount',
isCalendarNameUsed: 'isCalendarNameUsed',
getTask: 'getTaskByUri',
initialRoute: 'initialRoute',
hasTrashBin: 'hasTrashBin',
}),
},
methods: {
t,
...mapActions([
'appendCalendar',
'setPriority',
'setPercentComplete',
'setDate',
'setSetting',
'setCalendarOrder',
]),
/**
* Format the task counter
*
* @param {number} count The number of tasks
*/
counterFormatter(count) {
switch (false) {
case count !== 0:
return ''
case count < 999:
return '999+'
default:
return count
}
},
/**
* Indicate that we drag a calendar item
*
* @param {object} e The event object
*/
setData(e) {
e.dataTransfer.setData('text/calendar', 'calendar')
},
/**
* Called when the calendar list order is changed.
*
* @param {object} $event The event which caused the sorting
*/
update($event) {
const newIndex = $event.newIndex
const oldIndex = $event.oldIndex
// If the calendars array has no entry, we don't need to sort.
if (this.calendars.length === 0) {
return
}
// If the calendar is inserted at its current position, don't sort.
if (newIndex === oldIndex) {
return
}
// Get a copy of the sorted calendars array
const calendars = [...this.calendars]
// In case the task to move is already in the array, move it to the new position
if (oldIndex > -1) {
calendars.splice(newIndex, 0, calendars.splice(oldIndex, 1)[0])
} else {
return
}
// Get the new sort order for the moved calendar and apply it.
// We just do that to minimize the number of other calendars to be changed.
let newSortOrder
if (newIndex + 1 < calendars.length
&& (newIndex < 1 || calendars[newIndex + 1].order - 1 > calendars[newIndex - 1].order)) {
newSortOrder = calendars[newIndex + 1].order - 1
} else {
newSortOrder = calendars[newIndex - 1].order + 1
}
if (newSortOrder < 0) {
newSortOrder = 0
}
// Apply the new sort order for the moved calendar
this.setCalendarOrder({ calendar: calendars[newIndex], order: newSortOrder })
// Check the sort orders to be strictly monotonous
let currentIndex = 1
while (currentIndex < calendars.length) {
if (calendars[currentIndex].order <= calendars[currentIndex - 1].order) {
const order = { calendar: calendars[currentIndex], order: calendars[currentIndex - 1].order + 1 }
this.setCalendarOrder(order)
}
currentIndex++
}
},
/**
* Handle the drag start
*
* @param {object} e The event object
* @return {boolean}
*/
dragStart(e) {
e.stopPropagation()
e.preventDefault()
return false
},
/**
* Handle the drag over
*
* @param {object} e The event object
* @return {boolean}
*/
dragOver(e) {
if (e.preventDefault) {
e.preventDefault()
}
return false
},
/**
* Set the appropriate class on hovering
*
* @param {object} e The event object
* @param {object} collection The collection on which the task was dropped
*/
dragEnter(e, collection) {
// Check if dropping here is allowed
if (!['starred', 'completed', 'today', 'week'].includes(collection.id)) {
return
}
// Dragging calendars has no effect
if (e.dataTransfer.getData('text/calendar') === 'calendar') {
return
}
// Get the correct element, in case we hover a child.
if (e.target.closest) {
const target = e.target.closest('li.collection')
if (target) {
const collections = document.querySelectorAll('li.collection')
collections.forEach((f) => { f.classList.remove('dnd-hover') })
target.classList.add('dnd-hover')
}
}
},
/**
* Remove the hovering class after leaving
*
* @param {object} e The event object
*/
dragLeave(e) {
// Don't do anything if we leave towards a child element.
if (e.target.contains(e.relatedTarget)) {
return
}
// Get the correct element, in case we leave directly from a child.
if (e.target.closest) {
const target = e.target.closest('li.collection')
if (!target || target.contains(e.relatedTarget)) {
return
}
target.classList.remove('dnd-hover')
}
},
/**
* Drop a task on a collection
*
* @param {object} e The event object
* @param {object} collection The collection
*/
dropTaskOnCollection(e, collection) {
// Remove all hover classes
const collections = document.querySelectorAll('li.collection')
collections.forEach((f) => { f.classList.remove('dnd-hover') })
// Check if dropping here is allowed
if (!['starred', 'completed', 'today', 'week'].includes(collection.id)) {
return
}
const taskUri = e.dataTransfer.getData('text/uri')
if (taskUri) {
const task = this.getTask(taskUri)
switch (collection.id) {
case 'starred':
this.setPriority({ task, priority: 1 })
break
case 'completed':
this.setPercentComplete({ task, complete: 100 })
break
case 'today':
this.setDate({ task, day: 0 })
break
case 'week':
this.setDate({ task, day: 6 })
break
}
}
},
hideCollection(collection) {
switch (collection.show) {
case 0:
return true
case 1:
return false
case 2:
return this.collectionCount(collection.id) < 1
}
},
showTooltip(target) {
return this.tooltipTarget === target
},
startCreate(e) {
if (this.$OCA.Theming) {
this.selectedColor = this.$OCA.Theming.color
} else {
this.selectedColor = '#0082C9'
}
this.newCalendarName = ''
this.creating = true
this.$nextTick(
() => this.$refs.newListInput.$refs.inputField.$refs.input.focus(),
)
e.stopPropagation()
},
async create() {
if (!this.isNameAllowed(this.newCalendarName).allowed) {
return
}
try {
const { id: calendarId } = await this.appendCalendar({ displayName: this.newCalendarName, color: this.selectedColor })
if (calendarId) {
await this.$router.push({ name: 'calendars', params: { calendarId } })
}
} catch (error) {
console.debug(error)
showError(t('tasks', 'An error occurred, unable to create the list.'))
} finally {
this.creating = false
}
},
checkName(event) {
const check = this.isNameAllowed(this.newCalendarName)
this.tooltipMessage = check.msg
if (!check.allowed) {
this.tooltipTarget = 'list_new'
this.nameError = true
} else {
this.tooltipTarget = ''
this.nameError = false
}
if (event.keyCode === 13) {
this.create()
}
if (event.keyCode === 27) {
event.preventDefault()
this.tooltipTarget = ''
this.creating = false
this.editing = false
this.nameError = false
}
},
isNameAllowed(name) {
const check = {
allowed: false,
msg: '',
}
if (this.isCalendarNameUsed(name)) {
check.msg = t('tasks', 'The name "{calendar}" is already used.', { calendar: name }, undefined, { sanitize: false, escape: false })
} else if (!name) {
check.msg = t('tasks', 'An empty name is not allowed.')
} else {
check.allowed = true
}
return check
},
setColor(color) {
this.selectedColor = color
},
/**
* Saves the current route as new initial route
*
* @param {string} route The new initial route
*/
setInitialRoute(route) {
if (route === this.initialRoute) {
return
}
this.setSetting({ type: 'initialRoute', value: route })
},
},
}
</script>
<style lang="scss" scoped>
$color-error: #e9322d;
.app-navigation li {
&.dnd-hover,
&.sortable-ghost {
background-color: var(--color-primary-element-light);
box-shadow: 0 0 3px var(--color-primary-element);
z-index: 1000;
}
}
:deep(.collection) {
&.collection--edit {
.app-navigation-entry {
display: none;
}
.app-navigation-entry-edit {
display: inline-block;
width: 100%;
}
}
.app-navigation-entry-edit {
padding-left: 5px !important;
display: none;
position: relative;
}
}
</style>