This repository was archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathQueryBuilderRule.vue
129 lines (107 loc) · 4.18 KB
/
QueryBuilderRule.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
<template>
<div class="vqb-rule" :class="{ 'panel panel-default form-inline': styled }">
<div :class="{ 'form-group': styled }">
<label>{{ rule.label }}</label>
<select v-if="typeof rule.operands !== 'undefined'" v-model="query.selectedOperand" :class="{ 'form-control': styled }">
<option v-for="operand in rule.operands">{{ operand }}</option>
</select>
<select v-if="typeof rule.operators !== 'undefined' && rule.operators.length > 1" v-model="query.selectedOperator" :class="{ 'form-control': styled }">
<option v-for="operator in rule.operators" v-bind:value="operator">
{{ operator }}
</option>
</select>
<input :class="{ 'form-control': styled }" v-if="rule.inputType === 'text'" type="text" v-model="query.value" :placeholder="labels.textInputPlaceholder">
<input :class="{ 'form-control': styled }" v-if="rule.inputType === 'number'" type="number" v-model="query.value">
<input :class="{ 'form-control': styled }" v-if="rule.inputType === 'date'" type="date" v-model="query.value">
<template v-if="isCustomComponent">
<component :value="query.value" @input="updateQuery" :is="rule.component" v-bind="rule.customComponentProps"></component>
</template>
<div class="checkbox" v-if="rule.inputType === 'checkbox'">
<label v-for="choice in rule.choices">
<input type="checkbox" :value="choice.value" v-model="query.value"> {{ choice.label }}
</label>
</div>
<div class="radio" v-if="rule.inputType === 'radio'">
<label v-for="choice in rule.choices">
<input type="radio" :value="choice.value" v-model="query.value"> {{ choice.label }}
</label>
</div>
<select
v-if="rule.inputType === 'select'"
:class="{ 'form-control': styled }"
:multiple="rule.type === 'multi-select'"
v-model="query.value">
<template v-for="(option, option_key) in selectOptions">
<option v-if="!Array.isArray(option)" :value="option.value">
{{ option.label }}
</option>
<optgroup v-if="Array.isArray(option)" :label="option_key">
<option v-for="sub_option in option" :value="sub_option.value">{{ sub_option.label }}</option>
</optgroup>
</template>
</select>
<button type="button" :class="{ 'close pull-right': styled }" @click="remove" v-html="labels.removeRule"></button>
</div>
</div>
</template>
<script>
import deepClone from '../utilities.js';
export default {
name: "query-builder-rule",
props: ['query', 'index', 'rule', 'styled', 'labels'],
beforeMount () {
if (this.rule.type === 'custom-component') {
this.$options.components[this.id] = this.rule.component;
}
},
methods: {
remove: function() {
this.$emit('child-deletion-requested', this.index);
},
updateQuery(value) {
let updated_query = deepClone(this.query);
updated_query.value = value;
this.$emit('update:query', updated_query);
},
},
computed: {
isCustomComponent () {
return this.rule.type === 'custom-component';
},
selectOptions () {
if (typeof this.rule.choices === 'undefined') {
return {};
}
return this.rule.choices.reduce(function(groups, item, index) {
let key = item['group'];
if (typeof key !== 'undefined') {
groups[key] = groups[key] || [];
groups[key].push(item);
} else {
groups[index] = item;
}
return groups;
}, {});
},
},
mounted () {
let updated_query = deepClone(this.query);
// Set a default value for these types if one isn't provided already
if(this.query.value === null){
if (this.rule.inputType === 'checkbox') {
updated_query.value = [];
}
if (this.rule.type === 'select') {
updated_query.value = this.rule.choices[0].value;
}
if (this.rule.type === 'custom-component') {
updated_query.value = null;
if(typeof this.rule.default !== 'undefined') {
updated_query.value = deepClone(this.rule.default)
}
}
this.$emit('update:query', updated_query);
}
}
}
</script>