-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathNcAppNavigationItem.vue
775 lines (700 loc) · 17.5 KB
/
NcAppNavigationItem.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
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
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<docs>
### Usage
#### Simple element
* With an icon:
```vue
<template>
<ul>
<NcAppNavigationItem name="My name">
<template #icon>
<Check :size="20" />
</template>
</NcAppNavigationItem>
</ul>
</template>
<script>
import Check from 'vue-material-design-icons/Check.vue'
export default {
components: {
Check,
},
}
</script>
```
* With a spinning loader instead of the icon:
```vue
<ul>
<NcAppNavigationItem name="Loading Item" :loading="true" />
</ul>
```
* With an active state (only needed when not using `vue-router` and the `to` property, otherwise this is set automatically)
```vue
<ul>
<NcAppNavigationItem name="Current page" :active="true" />
</ul>
```
#### Element with actions
Wrap the children in a template. If you have more than 2 actions, a popover menu and a menu
button will be automatically created.
```vue
<template>
<div id="app-navigation-vue"><!-- Just a wrapper necessary in the docs. Not needed when NcAppNavigation is correctly used as parent. -->
<ul>
<NcAppNavigationItem name="Item with actions">
<template #icon>
<Check :size="20" />
</template>
<template #actions>
<NcActionButton @click="alert('Edit')">
<template #icon>
<Pencil :size="20" />
</template>
Edit
</NcActionButton>
<NcActionButton @click="alert('Delete')">
<template #icon>
<Delete :size="20" />
</template>
Delete
</NcActionButton>
<NcActionLink name="Link" href="https://nextcloud.com">
<template #icon>
<OpenInNew :size="20" />
</template>
</NcActionLink>
</template>
</NcAppNavigationItem>
</ul>
</div>
</template>
<script>
import Check from 'vue-material-design-icons/Check.vue'
import Delete from 'vue-material-design-icons/Delete.vue'
import OpenInNew from 'vue-material-design-icons/OpenInNew.vue'
import Pencil from 'vue-material-design-icons/Pencil.vue'
export default {
components: {
Check,
Delete,
OpenInNew,
Pencil,
},
methods: {
alert(msg) {
alert(msg)
},
},
}
</script>
```
#### Element with counter
Just nest the counter in a template within `<NcAppNavigationItem>` and add `#counter` to it.
```vue
<template>
<ul>
<NcAppNavigationItem name="Item with counter">
<template #icon>
<Folder :size="20" />
</template>
<template #counter>
<NcCounterBubble :count="90" />
</template>
</NcAppNavigationItem>
</ul>
</template>
<script>
import Folder from 'vue-material-design-icons/Folder.vue'
export default {
components: {
Folder,
},
}
</script>
```
#### Element with children
Wrap the children in a template with the `slot` property and use the prop `allowCollapse` to choose wether to allow or
prevent the user from collapsing the items.
```vue
<template>
<ul>
<NcAppNavigationItem name="Item with children" :allowCollapse="true" :open="true">
<template #icon>
<Folder :size="20" />
</template>
<template #counter>
<NcCounterBubble :count="90" />
</template>
<template #actions>
<NcActionButton @click="alert('Edit')">
<template #icon>
<Pencil :size="20" />
</template>
Edit
</NcActionButton>
<NcActionButton @click="alert('Delete')">
<template #icon>
<Delete :size="20" />
</template>
Delete
</NcActionButton>
<NcActionLink name="Link" href="https://nextcloud.com">
<template #icon>
<OpenInNew :size="20" />
</template>
</NcActionLink>
</template>
<template #default>
<NcAppNavigationItem name="AppNavigationItemChild1" />
<NcAppNavigationItem name="AppNavigationItemChild2" />
<NcAppNavigationItem name="AppNavigationItemChild3" />
<NcAppNavigationItem name="AppNavigationItemChild4" />
</template>
</NcAppNavigationItem>
</ul>
</template>
<script>
import Folder from 'vue-material-design-icons/Folder.vue'
import Delete from 'vue-material-design-icons/Delete.vue'
import OpenInNew from 'vue-material-design-icons/OpenInNew.vue'
import Pencil from 'vue-material-design-icons/Pencil.vue'
export default {
components: {
Folder,
Delete,
OpenInNew,
Pencil,
},
methods: {
alert(msg) {
alert(msg)
},
},
}
</script>
```
#### Editable element
Add the prop `:editable=true` and an edit placeholder if you need it. By default
the placeholder is the previous name of the element.
```vue
<template>
<ul>
<NcAppNavigationItem name="Editable Item" :editable="true"
editPlaceholder="your_placeholder_here" @update:name="function(value){alert(value)}">
<template #icon>
<Folder :size="20" />
</template>
</NcAppNavigationItem>
</ul>
</template>
<script>
import Folder from 'vue-material-design-icons/Folder.vue'
export default {
components: {
Folder,
},
methods: {
alert(msg) {
alert(msg)
},
},
}
</script>
```
#### Undo element
Just set the `undo` and `name` props. When clicking the undo button, an `undo` event is emitted.
```vue
<template>
<ul>
<NcAppNavigationItem :undo="true" name="Deleted important entry" @undo="alert('undo delete')" />
</ul>
</template>
<script>
export default {
methods: {
alert(msg) {
alert(msg)
},
},
}
</script>
```
#### Link element
Href that start by http will be treated as external and opened in a new tab
```
<div>
<ul>
<NcAppNavigationItem name="Files" href="/index.php/apps/files" />
<NcAppNavigationItem name="Nextcloud" href="https://nextcloud.com" />
</ul>
</div>
```
#### Custom title
```
<ul>
<NcAppNavigationItem name="Nextcloud" title="Open the Nextcloud website" href="https://nextcloud.com" />
</ul>
```
#### Pinned element
Just set the `pinned` prop.
```
<ul>
<NcAppNavigationItem name="Pinned item" :pinned="true" />
</ul>
```
</docs>
<template>
<li :id="id"
:class="{
'app-navigation-entry--opened': opened,
'app-navigation-entry--pinned': pinned,
'app-navigation-entry--collapsible': allowCollapse && !!$slots.default,
}"
class="app-navigation-entry-wrapper">
<component :is="isRouterLink ? 'router-link' : 'NcVNodes'"
v-slot="{ href: routerLinkHref, navigate, isActive }"
v-bind="{ ...isRouterLink && { custom: true, to } }">
<div :class="{
'app-navigation-entry--editing': editingActive,
'app-navigation-entry--deleted': undo,
'active': (isActive && to) || active,
}"
class="app-navigation-entry">
<!-- Icon and name -->
<a v-if="!undo"
class="app-navigation-entry-link"
:aria-current="active || (isActive && to) ? 'page' : undefined"
:aria-description="ariaDescription"
:aria-expanded="!!$slots.default ? opened.toString() : undefined"
:href="href || routerLinkHref || '#'"
:target="isExternal(href) ? '_blank' : undefined"
:title="title || name"
@blur="handleBlur"
@click="onClick($event, navigate, routerLinkHref)"
@focus="handleFocus"
@keydown.tab.exact="handleTab">
<!-- icon if not collapsible -->
<!-- never show the icon over the collapsible if mobile -->
<div :class="{ [icon]: icon }"
class="app-navigation-entry-icon">
<NcLoadingIcon v-if="loading" />
<!-- @slot Slot for the optional leading icon -->
<slot v-else name="icon" />
</div>
<span class="app-navigation-entry__name" :class="{ 'hidden-visually': editingActive }">
{{ name }}
</span>
<div v-if="editingActive" class="editingContainer">
<NcInputConfirmCancel ref="editingInput"
v-model="editingValue"
:placeholder="editPlaceholder !== '' ? editPlaceholder : name"
:primary="(isActive && to) || active"
@cancel="cancelEditing"
@confirm="handleEditingDone" />
</div>
</a>
<!-- undo entry -->
<div v-if="undo" class="app-navigation-entry__deleted">
<div class="app-navigation-entry__deleted-description">
{{ name }}
</div>
</div>
<!-- Counter and Actions -->
<div v-if="(!!$slots.actions || !!$slots.counter || editable || undo) && !editingActive"
class="app-navigation-entry__utils"
:class="{'app-navigation-entry__utils--display-actions': forceDisplayActions || menuOpenLocalValue || menuOpen }">
<div v-if="!!$slots.counter"
class="app-navigation-entry__counter-wrapper">
<!-- @slot Slot for the `NcCounterBubble` -->
<slot name="counter" />
</div>
<NcActions v-if="!!$slots.actions || (editable && !editingActive) || undo"
ref="actions"
:inline="inlineActions"
class="app-navigation-entry__actions"
container="#app-navigation-vue"
:boundaries-element="actionsBoundariesElement"
:placement="menuPlacement"
:open="menuOpen"
:force-menu="forceMenu"
:default-icon="menuIcon"
:variant="(isActive && to) || active ? 'primary' : null"
@update:open="onMenuToggle">
<template #icon>
<!-- @slot Slot for the custom menu icon -->
<slot name="menu-icon" />
</template>
<NcActionButton v-if="editable && !editingActive"
:aria-label="editButtonAriaLabel"
@click="handleEdit">
<template #icon>
<Pencil :size="20" />
</template>
{{ editLabel }}
</NcActionButton>
<NcActionButton v-if="undo"
:aria-label="undoButtonAriaLabel"
@click="handleUndo">
<template #icon>
<Undo :size="20" />
</template>
</NcActionButton>
<!-- @slot Slot for additional `NcAction*` -->
<slot name="actions" />
</NcActions>
</div>
<NcAppNavigationIconCollapsible v-if="allowCollapse && !!$slots.default" :open="opened" @click.prevent.stop="toggleCollapse" />
<!-- @slot Slot for anything (virtual) that should be mounted in the component, like a related modal -->
<slot name="extra" />
</div>
</component>
<!-- Children elements -->
<ul v-if="canHaveChildren && !!$slots.default" class="app-navigation-entry__children">
<!-- @slot Slot for children -->
<slot />
</ul>
</li>
</template>
<script>
import { useIsMobile } from '../../composables/useIsMobile/index.js'
import { createElementId } from '../../utils/createElementId.ts'
import { t } from '../../l10n.js'
import Pencil from 'vue-material-design-icons/Pencil.vue'
import Undo from 'vue-material-design-icons/Undo.vue'
import NcActions from '../NcActions/index.js'
import NcActionButton from '../NcActionButton/index.js'
import NcAppNavigationIconCollapsible from './NcAppNavigationIconCollapsible.vue'
import NcInputConfirmCancel from './NcInputConfirmCancel.vue'
import NcLoadingIcon from '../NcLoadingIcon/index.js'
import NcVNodes from '../NcVNodes/index.ts'
export default {
name: 'NcAppNavigationItem',
components: {
NcActions,
NcActionButton,
NcAppNavigationIconCollapsible,
NcInputConfirmCancel,
NcLoadingIcon,
NcVNodes,
Pencil,
Undo,
},
props: {
/**
* If you are not using vue-router you can use the property to set this item as the active navigation entry.
* When using vue-router and the `to` property this is set automatically.
*/
active: {
type: Boolean,
default: false,
},
/**
* The main text content of the entry.
*/
name: {
type: String,
required: true,
},
/**
* The title attribute of the element.
*/
title: {
type: String,
default: null,
},
/**
* id attribute of the list item element
*/
id: {
type: String,
default: () => createElementId(),
validator: id => id.trim() !== '',
},
/**
* Refers to the icon on the left, this prop accepts a class
* like 'icon-category-enabled'.
*/
icon: {
type: String,
default: '',
},
/**
* Displays a loading animated icon on the left of the element
* instead of the icon.
*/
loading: {
type: Boolean,
default: false,
},
/**
* Passing in a route will make the root element of this
* component a `<router-link />` that points to that route.
* By leaving this blank, the root element will be a `<li>`.
*/
to: {
type: [String, Object],
default: null,
},
/**
* A direct link. This will be used as the `href` attribute.
* This will ignore any `to` prop being defined.
*/
href: {
type: String,
default: null,
},
/**
* Gives the possibility to collapse the children elements into the
* parent element (true) or expands the children elements (false).
*/
allowCollapse: {
type: Boolean,
default: false,
},
/**
* Makes the name of the item editable by providing an `ActionButton`
* component that toggles a form
*/
editable: {
type: Boolean,
default: false,
},
/**
* Only for 'editable' items, sets label for the edit action button.
*/
editLabel: {
type: String,
default: '',
},
/**
* Only for items in 'editable' mode, sets the placeholder text for the editing form.
*/
editPlaceholder: {
type: String,
default: '',
},
/**
* Pins the item to the bottom left area, above the settings. Do not
* place 'non-pinned' `AppnavigationItem` components below `pinned`
* ones.
*/
pinned: {
type: Boolean,
default: false,
},
/**
* Puts the item in the 'undo' state.
*/
undo: {
type: Boolean,
default: false,
},
/**
* The navigation collapsible state (synced)
*/
open: {
type: Boolean,
default: false,
},
/**
* The actions menu open state (synced)
*/
menuOpen: {
type: Boolean,
default: false,
},
/**
* Force the actions to display in a three dot menu
*/
forceMenu: {
type: Boolean,
default: false,
},
/**
* The action's menu default icon
*/
menuIcon: {
type: String,
default: undefined,
},
/**
* The action's menu direction
*/
menuPlacement: {
type: String,
default: 'bottom',
},
/**
* Entry aria details
*/
ariaDescription: {
type: String,
default: null,
},
/**
* To be used only when the elements in the actions menu are very important
*/
forceDisplayActions: {
type: Boolean,
default: false,
},
/**
* Number of action items outside the menu
*/
inlineActions: {
type: Number,
default: 0,
},
},
emits: [
'update:menuOpen',
'update:open',
'update:name',
'click',
'undo',
],
setup() {
return {
isMobile: useIsMobile(),
}
},
data() {
return {
actionsBoundariesElement: undefined,
editingValue: '',
opened: this.open, // Collapsible state
editingActive: false,
/**
* Tracks the open state of the actions menu
*/
menuOpenLocalValue: false,
focused: false,
}
},
computed: {
isRouterLink() {
return this.to && !this.href
},
// Checks if the component is already a children of another
// instance of AppNavigationItem
canHaveChildren() {
if (this.$parent.$options._componentTag === 'AppNavigationItem') {
return false
} else {
return true
}
},
editButtonAriaLabel() {
return this.editLabel ? this.editLabel : t('Edit item')
},
undoButtonAriaLabel() {
return t('Undo changes')
},
},
watch: {
open(newVal) {
this.opened = newVal
},
},
mounted() {
this.actionsBoundariesElement = document.querySelector('#content-vue') || undefined
},
methods: {
// sync opened menu state with prop
onMenuToggle(state) {
this.$emit('update:menuOpen', state)
this.menuOpenLocalValue = state
},
// toggle the collapsible state
toggleCollapse() {
this.opened = !this.opened
this.$emit('update:open', this.opened)
},
/**
* Handle link click
*
* @param {PointerEvent} event - Native click event
* @param {Function} [navigate] - VueRouter link's navigate if any
* @param {string} [routerLinkHref] - VueRouter link's href
*/
onClick(event, navigate, routerLinkHref) {
// Always forward native event
this.$emit('click', event)
// Do not navigate with control keys - it is opening in a new tab
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) {
return
}
// Prevent default link behaviour if it's a router-link and navigate manually
if (routerLinkHref) {
navigate?.(event)
event.preventDefault()
}
},
// Edition methods
handleEdit() {
this.editingValue = this.name
this.editingActive = true
this.onMenuToggle(false)
this.$nextTick(() => {
this.$refs.editingInput.focusInput()
})
},
cancelEditing() {
this.editingActive = false
},
handleEditingDone() {
this.$emit('update:name', this.editingValue)
this.editingValue = ''
this.editingActive = false
},
// Undo methods
handleUndo() {
this.$emit('undo')
},
/**
* Show actions upon focus
*/
handleFocus() {
this.focused = true
},
handleBlur() {
this.focused = false
},
/**
* This method checks if the root element of the component is focused and
* if that's the case it focuses the actions button if available
*
* @param {Event} e the keydown event
*/
handleTab(e) {
// If there is no actions menu, do nothing.
if (!this.$refs.actions) {
return
}
if (this.focused) {
e.preventDefault()
this.$refs.actions.$refs.menuButton.$el.focus()
this.focused = false
} else {
this.$refs.actions.$refs.menuButton.$el.blur()
}
},
/**
* Is this an external link
*
* @param {string} href The link to check
* @return {boolean} Whether it is external or not
*/
isExternal(href) {
// Match any protocol
return href && href.match(/[a-z]+:\/\//i)
},
},
}
</script>
<style scoped lang="scss">
@use '../../assets/NcAppNavigationItem.scss';
</style>