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
|
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { ActiveStore } from '../types.ts'
import type { FileAction, Node, View } from '@nextcloud/files'
import { defineStore } from 'pinia'
import { getNavigation } from '@nextcloud/files'
import { subscribe } from '@nextcloud/event-bus'
import logger from '../logger.ts'
export const useActiveStore = function(...args) {
const store = defineStore('active', {
state: () => ({
_initialized: false,
activeNode: null,
activeView: null,
activeAction: null,
} as ActiveStore),
actions: {
setActiveNode(node: Node) {
if (!node) {
throw new Error('Use clearActiveNode to clear the active node')
}
logger.debug('Setting active node', { node })
this.activeNode = node
},
clearActiveNode() {
this.activeNode = null
},
onDeletedNode(node: Node) {
if (this.activeNode && this.activeNode.source === node.source) {
this.clearActiveNode()
}
},
setActiveAction(action: FileAction) {
this.activeAction = action
},
clearActiveAction() {
this.activeAction = null
},
onChangedView(view: View|null = null) {
logger.debug('Setting active view', { view })
this.activeView = view
this.clearActiveNode()
},
},
})
const activeStore = store(...args)
const navigation = getNavigation()
// Make sure we only register the listeners once
if (!activeStore._initialized) {
subscribe('files:node:deleted', activeStore.onDeletedNode)
activeStore._initialized = true
activeStore.onChangedView(navigation.active)
// Or you can react to changes of the current active view
navigation.addEventListener('updateActive', (event) => {
activeStore.onChangedView(event.detail)
})
}
return activeStore
}
|