-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathQueryBuilderChild.vue
134 lines (115 loc) · 2.81 KB
/
QueryBuilderChild.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
<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';
import { Component as VueComponent } from 'vue';
import {
RuleSet, Rule, QueryBuilderConfig, RuleDefinition,
} from '@/types';
import { isRule, isRuleSet, isQueryBuilderConfig } from '@/guards';
import QueryBuilderGroup from '@/QueryBuilderGroup.vue';
import QueryBuilderRule from '@/QueryBuilderRule.vue';
@Component({
components: {
QueryBuilderGroup,
QueryBuilderRule,
},
})
export default class QueryBuilderChild extends Vue {
@Prop({
required: true,
validator: param => isQueryBuilderConfig(param),
}) readonly config!: QueryBuilderConfig
@Prop({
required: true,
validator: query => isRule(query) || isRuleSet(query),
}) readonly query!: RuleSet | Rule
@Prop() readonly depth!: number
get isRule(): boolean {
return isRule(this.query);
}
get isRuleSet(): boolean {
return isRuleSet(this.query);
}
get ruleDefinition(): RuleDefinition | null {
if (!this.isRule) {
return null;
}
const ruleDefinition = this.config
.rules
.find(definition => definition.identifier === (this.query as Rule).identifier);
return ruleDefinition || null;
}
get component(): VueComponent {
if (this.isRule && this.ruleDefinition) {
return QueryBuilderRule;
}
if (this.isRuleSet) {
return QueryBuilderGroup;
}
throw new Error('No component definition available.');
}
get definition(): RuleDefinition | null {
if (this.isRule && this.ruleDefinition) {
return this.ruleDefinition;
}
if (this.isRuleSet) {
return null;
}
throw new Error('No component definition available.');
}
}
</script>
<template>
<div class="query-builder-child">
<component
:is="component"
:config="config"
:query="query"
:depth="depth"
@query-update="$emit('query-update', $event)"
class="query-builder-child__component"
>
<template
v-for="(_, slotName) in $scopedSlots"
v-slot:[slotName]="props"
>
<slot
:name="slotName"
v-bind="props"
/>
</template>
</component>
<button
aria-label="Close"
class="query-builder-child__delete-child"
@click="$emit('delete-child')"
>
<span aria-hidden="true">×</span>
</button>
</div>
</template>
<style lang="scss" scoped>
.query-builder-child {
display: flex;
flex-flow: row;
position: relative;
}
.query-builder-child__component {
flex: 1;
}
.query-builder-child__delete-child {
position: absolute;
top: 16px;
right: 8px;
font-size: 1.5rem;
font-weight: 700;
line-height: 1;
color: #000;
text-shadow: 0 1px 0 #fff;
opacity: .5;
padding: 0;
background-color: transparent;
border: 0;
appearance: none;
cursor: pointer;
}
</style>