-
Notifications
You must be signed in to change notification settings - Fork 56
/
Action.vue
153 lines (137 loc) Β· 2.97 KB
/
Action.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
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcButton v-if="isWebLink"
type="primary"
class="action-button pull-right"
:href="link"
@click="onClickActionButtonWeb">
{{ label }}
</NcButton>
<NcButton v-else-if="!isWebLink"
:type="buttonType"
class="action-button pull-right"
@click="onClickActionButton">
{{ label }}
</NcButton>
</template>
<script>
import axios from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import { emit } from '@nextcloud/event-bus'
export default {
name: 'Action',
components: {
NcButton,
},
props: {
label: {
type: String,
default: '',
required: true,
},
link: {
type: String,
default: '',
required: true,
},
type: {
type: String,
default: '',
required: true,
},
primary: {
type: Boolean,
default: false,
required: true,
},
notificationIndex: {
type: Number,
required: true,
},
},
data() {
return {
tabbed: false,
}
},
computed: {
isWebLink() {
return this.typeWithDefault === 'WEB'
},
typeWithDefault() {
return this.type || 'GET'
},
buttonType() {
return this.primary ? 'primary' : 'secondary'
},
},
methods: {
async onClickActionButtonWeb(e) {
try {
const event = {
cancelAction: false,
notification: this.$parent.$props,
action: {
url: this.link,
type: this.typeWithDefault,
},
}
await emit('notifications:action:execute', event)
if (event.cancelAction) {
// Action cancelled by event
e.preventDefault()
}
} catch (error) {
console.error('Failed to perform action', error)
showError(t('notifications', 'Failed to perform action'))
}
},
async onClickActionButton() {
try {
const event = {
cancelAction: false,
notification: this.$parent.$props,
action: {
url: this.link,
type: this.typeWithDefault,
},
}
await emit('notifications:action:execute', event)
if (event.cancelAction) {
// Action cancelled by event
return
}
// execute action
await axios({
method: this.typeWithDefault,
url: this.link,
})
// emit event to current app
this.$parent._$el.fadeOut(OC.menuSpeed)
this.$parent.$emit('remove', this.notificationIndex)
emit('notifications:action:executed', event)
try {
$('body').trigger(new $.Event('OCA.Notification.Action', {
notification: this.$parent,
action: {
url: this.link,
type: this.typeWithDefault,
},
}))
// do not do anything but log, the action went fine
// only the event bus listener failed, this is not our problem
} catch (error) {
console.error(error)
}
} catch (error) {
console.error('Failed to perform action', error)
showError(t('notifications', 'Failed to perform action'))
}
},
},
}
</script>