-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathBaseList.vue
More file actions
163 lines (157 loc) · 4.66 KB
/
BaseList.vue
File metadata and controls
163 lines (157 loc) · 4.66 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
<template>
<div>
<BAlert :show="messageVisible" :variant="messageVariant"> {{ messageText }} </BAlert>
<div v-if="itemsVisible" class="card-header">
There are {{ itemsLength }}
<GButton
size="small"
tooltip
tooltip-placement="bottom"
:disabled="busy"
:title="tooltipAll"
@click.prevent="executeAll()">
<span :class="icon" />
</GButton>
{{ plural }} available.
</div>
<GTable v-if="itemsVisible" striped no-sort-reset :fields="fields" :items="items">
<template v-slot:cell(execute)="data">
<GButton
size="small"
tooltip
tooltip-placement="bottom"
:disabled="busy"
:title="tooltip"
@click.prevent="execute([data.item.id])">
<span :class="icon" />
</GButton>
</template>
<template v-slot:cell(links)="data">
<li v-for="link in data.item.links" :key="link.name">
{{ link.name }}
</li>
</template>
</GTable>
</div>
</template>
<script>
import { BAlert } from "bootstrap-vue";
import GButton from "@/components/BaseComponents/GButton.vue";
import GTable from "@/components/Common/GTable.vue";
export default {
components: {
BAlert,
GButton,
GTable,
},
props: {
icon: {
type: String,
},
tooltip: {
type: String,
},
plural: {
type: String,
},
success: {
type: String,
},
fields: {
type: Array,
},
getter: {
type: Function,
},
setter: {
type: Function,
},
},
data() {
return {
items: [],
busy: false,
messageText: null,
messageVariant: null,
};
},
computed: {
tooltipAll: function () {
return `${this.tooltip} all`;
},
itemsIndex: function () {
return this.items.reduce((r, v) => {
r[v.id] = v;
return r;
}, {});
},
itemsAll: function () {
return this.items.map((v) => v.id);
},
itemsVisible: function () {
return this.items.length > 0;
},
itemsLength: function () {
return this.items.length;
},
messageVisible: function () {
return this.messageText != null;
},
},
created() {
this.messageText = null;
this.getter()
.then((response) => {
this.items = response.data;
if (!this.itemsVisible) {
this.messageVariant = "warning";
this.messageText = `No ${this.plural} available.`;
}
})
.catch((e) => {
this._errorMessage(e);
});
},
methods: {
executeAll: function () {
this.execute(this.itemsAll);
},
execute: function (ids) {
this.busy = true;
this.messageVariant = "warning";
this.messageText = "Executing request. Please wait...";
this._highlightRows(this.itemsAll, "default");
this.setter(ids)
.then((response) => {
const data = response.data;
if (data) {
this.messageVariant = "info";
this.messageText = data.message;
this._highlightRows(data.failed, "danger");
this._highlightRows(data[this.success], "success");
}
this.busy = false;
})
.catch((e) => {
this._errorMessage(e);
this.busy = false;
});
},
_highlightRows: function (ids, status) {
if (ids) {
for (const id of ids) {
const item = this.itemsIndex[id];
if (item) {
item._rowVariant = status;
}
}
}
},
_errorMessage: function (e) {
const message = e && e.response && e.response.data && e.response.data.err_msg;
this.messageText = message || "Request failed for an unknown reason.";
this.messageVariant = "danger";
},
},
};
</script>