Skip to content

Commit 1f6fd08

Browse files
authored
Wildcard Select: Support wildcard only values (#696)
* Support for value is wildcard only (*) * changes as discussed
1 parent 961b1b2 commit 1f6fd08

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

frontend/src/components/WildcardSelect.vue

+66-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
<div class="d-flex flex-row">
1919
<v-select
2020
class="selectClass"
21+
:class="selectClass"
2122
color="cyan darken-2"
2223
item-color="cyan darken-2"
2324
:label="wildcardSelectLabel"
@@ -30,12 +31,21 @@ limitations under the License.
3031
:hint="wildcardSelectHint"
3132
persistent-hint
3233
>
34+
<template v-slot:selection="{ item }">
35+
<span v-if="item.customWildcard">&lt;Custom&gt;</span>
36+
<span v-else>{{item.value}}</span>
37+
</template>
3338
<template v-slot:item="{ item }">
3439
<v-list-item-content>
3540
<v-list-item-title>
36-
<span v-if="item.startsWithWildcard">&lt;prefix&gt;</span>
37-
<span>{{item.value}}</span>
38-
<span v-if="item.endsWithWildcard">&lt;suffix&gt;</span>
41+
<template v-if="item.value.length">
42+
<span v-if="item.startsWithWildcard">&lt;prefix&gt;</span>
43+
<span>{{item.value}}</span>
44+
<span v-if="item.endsWithWildcard">&lt;suffix&gt;</span>
45+
</template>
46+
<template v-else>
47+
<span v-if="item.isWildcard">Custom {{wildcardSelectLabel}}</span>
48+
</template>
3949
</v-list-item-title>
4050
</v-list-item-content>
4151
</template>
@@ -108,41 +118,63 @@ export default {
108118
textFieldClass () {
109119
return this.wildcardSelectedValue.startsWithWildcard ? 'textFieldStartClass' : 'textFieldEndClass'
110120
},
121+
selectClass () {
122+
if (this.wildcardSelectedValue.customWildcard) {
123+
return 'selectSmallClass'
124+
}
125+
return undefined
126+
},
111127
wildcardSelectItemObjects () {
112128
return map(this.wildcardSelectItems, item => {
113129
let startsWithWildcard = false
114130
let endsWithWildcard = false
131+
let customWildcard = false
115132
const value = trim(item, '*')
116133
let pattern = value
117134

118-
if (startsWith(item, '*')) {
135+
if (item === '*') {
136+
customWildcard = true
137+
pattern = '.*'
138+
} else if (startsWith(item, '*')) {
119139
startsWithWildcard = true
120-
pattern = '.+' + pattern
121-
}
122-
if (endsWith(item, '*')) {
140+
pattern = '.*' + pattern
141+
} else if (endsWith(item, '*')) {
123142
endsWithWildcard = true
124-
pattern = pattern + '.+'
143+
pattern = pattern + '.*'
125144
}
126145

127146
return {
128-
text: value,
129147
value,
130148
startsWithWildcard,
131149
endsWithWildcard,
132-
isWildcard: startsWithWildcard || endsWithWildcard,
150+
customWildcard,
151+
isWildcard: startsWithWildcard || endsWithWildcard || customWildcard,
133152
regex: new RegExp('^' + pattern + '$')
134153
}
135154
})
136155
},
137156
wildcardTextFieldLabel () {
138-
return this.wildcardSelectedValue.startsWithWildcard ? 'Prefix' : 'Suffix'
157+
if (this.wildcardSelectedValue.startsWithWildcard) {
158+
return 'Prefix'
159+
}
160+
if (this.wildcardSelectedValue.endsWithWildcard) {
161+
return 'Suffix'
162+
}
163+
if (this.wildcardSelectedValue.customWildcard) {
164+
return `Custom ${this.wildcardSelectLabel}`
165+
}
166+
return undefined
139167
},
140168
wildcardSelectHint () {
141169
if (!this.wildcardSelectedValue.isWildcard) {
142170
return undefined
143171
}
144-
const label = this.wildcardSelectedValue.startsWithWildcard ? 'prefix' : 'suffix'
145-
return `Selected wildcard value requires a ${label} which needs to be specified`
172+
if (this.wildcardSelectedValue.customWildcard) {
173+
return `Specify custom ${this.wildcardSelectLabel}`
174+
} else {
175+
const label = this.wildcardSelectedValue.startsWithWildcard ? 'prefix' : 'suffix'
176+
return `Selected wildcard value requires a ${label} which needs to be specified`
177+
}
146178
},
147179
internalValue () {
148180
if (this.wildcardSelectedValue.startsWithWildcard) {
@@ -151,6 +183,9 @@ export default {
151183
if (this.wildcardSelectedValue.endsWithWildcard) {
152184
return `${this.wildcardSelectedValue.value}${this.wildcardVariablePart}`
153185
}
186+
if (this.wildcardSelectedValue.customWildcard) {
187+
return this.wildcardVariablePart
188+
}
154189
return this.wildcardSelectedValue.value
155190
}
156191
},
@@ -174,6 +209,21 @@ export default {
174209
matches.sort(function (a, b) {
175210
return b.value.length - a.value.length
176211
})
212+
matches.sort(function (a, b) {
213+
if (a.isWildcard && !b.isWildcard) {
214+
return 1
215+
}
216+
if (b.isWildcard && !a.isWildcard) {
217+
return -1
218+
}
219+
if (a.customWildcard && !b.customWildcard) {
220+
return 1
221+
}
222+
if (b.customWildcard && !a.customWildcard) {
223+
return -1
224+
}
225+
return 0
226+
})
177227

178228
const bestMatch = head(matches)
179229
if (!bestMatch) {
@@ -215,4 +265,7 @@ export default {
215265
order: 2;
216266
margin-left: 5px;
217267
}
268+
.selectSmallClass {
269+
max-width: 120px;
270+
}
218271
</style>

frontend/tests/unit/WildcardSelect.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Vue.use(Vuelidate)
2727
const sampleWildcardItems = [
2828
'*Foo',
2929
'Foo',
30+
'*',
3031
'Bar*',
3132
'BarBla'
3233
]
@@ -83,4 +84,31 @@ describe('WildcardSelect.vue', function () {
8384
expect(wildcardSelectedValue.isWildcard).to.be.false
8485
expect(wildcardVariablePart).to.equal('')
8586
})
87+
88+
it('should select wildcard if inital value is wildcard', function () {
89+
const wildcardSelect = createWildcardSelecteComponent('Bar*')
90+
const wildcardSelectedValue = wildcardSelect.wildcardSelectedValue
91+
const wildcardVariablePart = wildcardSelect.wildcardVariablePart
92+
expect(wildcardSelectedValue.value).to.equal('Bar')
93+
expect(wildcardSelectedValue.endsWithWildcard).to.be.true
94+
expect(wildcardVariablePart).to.equal('')
95+
})
96+
97+
it('Should select initial custom wildcard value', function () {
98+
const wildcardSelect = createWildcardSelecteComponent('*')
99+
const wildcardSelectedValue = wildcardSelect.wildcardSelectedValue
100+
const wildcardVariablePart = wildcardSelect.wildcardVariablePart
101+
expect(wildcardSelectedValue.value).to.equal('')
102+
expect(wildcardSelectedValue.customWildcard).to.be.true
103+
expect(wildcardVariablePart).to.equal('')
104+
})
105+
106+
it('Should select custom wildcard', function () {
107+
const wildcardSelect = createWildcardSelecteComponent('RandomValue')
108+
const wildcardSelectedValue = wildcardSelect.wildcardSelectedValue
109+
const wildcardVariablePart = wildcardSelect.wildcardVariablePart
110+
expect(wildcardSelectedValue.value).to.equal('')
111+
expect(wildcardSelectedValue.customWildcard).to.be.true
112+
expect(wildcardVariablePart).to.equal('RandomValue')
113+
})
86114
})

0 commit comments

Comments
 (0)