-
Notifications
You must be signed in to change notification settings - Fork 19
/
SearchInput.vue
285 lines (269 loc) · 7.03 KB
/
SearchInput.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
<template>
<div id="search-bar">
<Multiselect
v-model="search"
class="searchInput"
:placeholder="placeholder"
:options="searchResults"
:user-select="true"
label="displayName"
track-by="multiselectKey"
:internal-search="false"
open-direction="below"
@search-change="makeSearchRequest">
<template #option="{option}">
<div class="searchList">
<div class="searchList__filterWorkPackage">
<div class="filterProjectTypeStatus">
<div class="filterProjectTypeStatus__project">
{{ option.project }}
</div>
<div class="filterProjectTypeStatus__type" :style="{'color':option.typeCol}">
{{ option.typeTitle }}
</div>
<div class="filterProjectTypeStatus__status"
:style="{'background-color':option.statusCol}">
<div class="filterProjectTypeStatus__status__title">
{{ option.statusTitle }}
</div>
</div>
</div>
<div v-if="option.assignee" class="filterAssignee">
<div class="filterAssignee__avatar">
<Avatar
class="item-avatar"
:size="25"
:url="option.picture"
:user="option.assignee"
:display-name="option.assignee" />
</div>
<div class="filterAssignee__assignee">
{{ option.assignee }}
</div>
</div>
</div>
<div class="filterWorkpackageSubject">
<div class="filterWorkpackageSubject__subject">
{{ option.subject }}
</div>
</div>
</div>
</template>
<template #noOptions>
{{ t('integration_openproject', 'Start typing to search') }}
</template>
</Multiselect>
<div v-if="state !== 'ok'"
class="stateMsg text-center">
{{ stateMessages }}
</div>
</div>
</template>
<script>
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { translate as t } from '@nextcloud/l10n'
import Avatar from '@nextcloud/vue/dist/Components/Avatar'
import Multiselect from '@nextcloud/vue/dist/Components/Multiselect'
export default {
name: 'SearchInput',
components: {
Avatar,
Multiselect,
},
data: () => ({
search: null,
state: 'ok',
searchResults: [],
}),
computed: {
placeholder() {
return t('integration_openproject', 'Search for a work package to create a relation')
},
stateMessages() {
if (this.state === 'no-token') {
return t('integration_openproject', 'No OpenProject account connected')
} else if (this.state === 'error') {
return t('integration_openproject', 'Error connecting to OpenProject')
} else if (this.state === 'loading') {
return t('integration_openproject', 'Wait while we fetch work packages')
}
return ''
},
},
watch: {
search(value, oldValue) {
// if the value in the search input field changes we need to reset the searchResults
if (oldValue !== null) {
if (value.length < oldValue.length && value.length <= 3) {
this.searchResults = []
this.state = 'ok'
}
}
},
},
methods: {
async makeSearchRequest(search) {
this.search = search
if (search <= 3) {
this.searchResults = []
return
}
const url = generateUrl('/apps/integration_openproject/work-packages')
if (this.search.length > 3) {
const req = {}
req.params = {
searchQuery: this.search,
}
this.state = 'loading'
const response = await axios.get(url, req)
if (response.status === 200) {
this.state = 'ok'
if (response.data.length === 0) {
this.searchResults = []
} else {
this.processWorkPackages(response.data)
}
} else {
this.checkStatusCode(response.status)
}
} else {
this.state = 'ok'
}
},
checkStatusCode(statusCode) {
if (statusCode === 200) {
this.state = 'ok'
} else if (statusCode === 401) {
this.state = 'no-token'
} else {
this.state = 'error'
}
},
replaceHrefToGetId(href) {
// this is a helper method replaces the string like this "/api/v3/types/3" to get id
return href
? href.replace(/.*\//, '')
: null
},
async processWorkPackages(workPackages) {
for (let i = 0; i < workPackages.length; i++) {
const statusId = this.replaceHrefToGetId(workPackages[i]._links.status.href)
const typeId = this.replaceHrefToGetId(workPackages[i]._links.type.href)
const userId = this.replaceHrefToGetId(workPackages[i]._links.assignee.href)
const userName = workPackages[i]._links.assignee.title
const avatar = await this.getUserAvatar(userId, userName)
const statusColor = await this.getWorkPackageColorAttributes('/apps/integration_openproject/statuses/', statusId)
const typeColor = await this.getWorkPackageColorAttributes('/apps/integration_openproject/types/', typeId)
const found = this.searchResults.some(el => el.id === workPackages[i].id)
if (!found) {
this.searchResults.push({
id: workPackages[i].id,
subject: workPackages[i].subject,
project: workPackages[i]._links.project.title,
statusTitle: workPackages[i]._links.status.title,
typeTitle: workPackages[i]._links.type.title,
assignee: userName,
statusCol: statusColor,
typeCol: typeColor,
picture: avatar,
})
}
}
},
async getWorkPackageColorAttributes(path, id) {
const url = generateUrl(path + id)
const req = {}
this.state = 'loading'
const response = await axios.get(url, req)
if (response.status === 200) {
this.state = 'ok'
return response.data.color
}
this.checkStatusCode(response.status)
return ''
},
async getUserAvatar(userId, userName) {
const url = generateUrl('/apps/integration_openproject/avatar?')
+ encodeURIComponent('userId') + '=' + userId + '&' + encodeURIComponent('userName') + '=' + userName
return url
},
},
}
</script>
<style scoped lang="scss">
.searchInput {
width: 100%;
}
.stateMsg {
padding: 30px;
text-align: center;
color: #6d6d6d;
}
.searchList {
display: flex;
flex-direction: column;
justify-content: space-between;
&__filterWorkPackage {
margin-top: 8px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
.filterProjectTypeStatus {
display: flex;
justify-content: space-between;
&__project {
padding: 6px 6px 6px 12px;
color: #6d6d6d;
font-size: 0.87rem;
}
&__type {
padding: 6px;
text-transform: uppercase;
font-size: 0.87rem;
}
&__status {
margin: 6px;
width: 90px;
height: 25px;
text-align: center;
font-size: 0.75rem;
border-radius: 3px;
&__title {
mix-blend-mode: multiply;
}
}
}
.filterAssignee {
display: flex;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
position: absolute;
right: 0px;
&__avatar {
padding: 6px;
}
&__assignee {
padding: 6px;
padding-right: 12px;
font-size: 0.81rem;
color: #0096FF;
text-align: center;
}
}
}
.filterWorkpackageSubject {
margin: 12px;
text-align: justify;
&__subject {
font-weight: bold;
font-size: 14px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
}
</style>