|
32 | 32 | </template> |
33 | 33 |
|
34 | 34 | <script lang="ts"> |
| 35 | + import { defineComponent, PropType } from 'vue'; |
| 36 | +
|
35 | 37 | import { cloneDeep } from 'lodash'; |
36 | | - import Vue from 'vue'; |
37 | | - import Component from 'vue-class-component'; |
38 | | - import { Prop, Watch } from 'vue-property-decorator'; |
39 | 38 |
|
40 | 39 | import { Variable, VariableIndex, VariableOption } from './types'; |
41 | | - import './VariableSubstitution.scss'; |
42 | 40 | import { IdentifierLocation } from 'parse/types'; |
43 | 41 | import DataCatalogEntry, { FieldSourceMeta } from 'catalog/DataCatalogEntry'; |
44 | 42 | import { noop } from 'utils/hueUtils'; |
|
117 | 115 | } catch (err) {} |
118 | 116 | }; |
119 | 117 |
|
120 | | - @Component |
121 | | - export default class VariableSubstitution extends Vue { |
122 | | - @Prop({ required: false, default: {} }) |
123 | | - initialVariables?: { [name: string]: Variable }; |
124 | | - @Prop() |
125 | | - locations?: IdentifierLocation[]; |
| 118 | + export default defineComponent({ |
| 119 | + props: { |
| 120 | + initialVariables: { |
| 121 | + type: Object as PropType<{ [name: string]: Variable }>, |
| 122 | + required: false, |
| 123 | + default: {} |
| 124 | + }, |
| 125 | + locations: { |
| 126 | + type: Object as PropType<IdentifierLocation[]>, |
| 127 | + required: false, |
| 128 | + default: undefined |
| 129 | + } |
| 130 | + }, |
126 | 131 |
|
127 | | - knownVariables: { [name: string]: KnownVariable } = {}; |
| 132 | + emits: ['variables-changed'], |
128 | 133 |
|
129 | | - get activeVariables(): Variable[] { |
130 | | - const active = Object.values(this.knownVariables).filter(variable => variable.active); |
131 | | - active.sort((a, b) => a.index - b.index); |
132 | | - return active; |
133 | | - } |
| 134 | + data(): { |
| 135 | + knownVariables: { [name: string]: KnownVariable }; |
| 136 | + } { |
| 137 | + return { |
| 138 | + knownVariables: {} |
| 139 | + }; |
| 140 | + }, |
134 | 141 |
|
135 | | - mounted(): void { |
136 | | - if (this.initialVariables) { |
137 | | - Object.values(this.initialVariables).forEach((variable, index) => { |
138 | | - const cloned = <KnownVariable>cloneDeep(variable); |
139 | | - cloned.active = true; |
140 | | - cloned.index = index; |
141 | | - this.$set(this.knownVariables, cloned.name, cloned); |
142 | | - }); |
| 142 | + computed: { |
| 143 | + activeVariables(): Variable[] { |
| 144 | + const active = Object.values(this.knownVariables).filter(variable => variable.active); |
| 145 | + active.sort((a, b) => a.index - b.index); |
| 146 | + return active; |
143 | 147 | } |
144 | | - } |
| 148 | + }, |
145 | 149 |
|
146 | | - @Watch('activeVariables') |
147 | | - notifyVariableChange(): void { |
148 | | - this.$emit( |
149 | | - 'variables-changed', |
150 | | - this.activeVariables.reduce((result, variable) => { |
151 | | - result[variable.name] = variable; |
152 | | - return result; |
153 | | - }, <VariableIndex>{}) |
154 | | - ); |
155 | | - } |
| 150 | + watch: { |
| 151 | + activeVariables(): void { |
| 152 | + this.$emit( |
| 153 | + 'variables-changed', |
| 154 | + this.activeVariables.reduce((result, variable) => { |
| 155 | + result[variable.name] = variable; |
| 156 | + return result; |
| 157 | + }, <VariableIndex>{}) |
| 158 | + ); |
| 159 | + }, |
| 160 | + locations(locations?: IdentifierLocation[]): void { |
| 161 | + const toDeactivate = new Set<string>(Object.keys(this.knownVariables)); |
156 | 162 |
|
157 | | - @Watch('locations', { immediate: true }) |
158 | | - updateFromLocations(locations?: IdentifierLocation[]): void { |
159 | | - const toDeactivate = new Set<string>(Object.keys(this.knownVariables)); |
160 | | -
|
161 | | - if (locations) { |
162 | | - locations |
163 | | - .filter(location => location.type === 'variable' && location.value) |
164 | | - .forEach(location => { |
165 | | - const match = location.value && location.value.match(LOCATION_VALUE_REGEX); |
166 | | - if (!match) { |
167 | | - return; |
168 | | - } |
169 | | -
|
170 | | - const name = match[1]; |
171 | | - let variable = this.knownVariables[name]; |
172 | | - if (variable) { |
173 | | - toDeactivate.delete(variable.name); |
174 | | - if (!variable.active) { |
175 | | - this.$set(variable, 'active', true); |
| 163 | + if (locations) { |
| 164 | + locations |
| 165 | + .filter(location => location.type === 'variable' && location.value) |
| 166 | + .forEach(location => { |
| 167 | + const match = location.value && location.value.match(LOCATION_VALUE_REGEX); |
| 168 | + if (!match) { |
| 169 | + return; |
176 | 170 | } |
177 | | - } else { |
178 | | - variable = { |
179 | | - meta: { type: 'text', placeholder: '', options: [] }, |
180 | | - sample: [], |
181 | | - sampleUser: [], |
182 | | - step: '', |
183 | | - type: 'text', |
184 | | - value: '', |
185 | | - name: name, |
186 | | - active: true, |
187 | | - index: Object.keys(this.knownVariables).length + 1 |
188 | | - }; |
189 | | - this.$set(this.knownVariables, name, variable); |
190 | | - } |
191 | | -
|
192 | | - // Case for ${name=1} or ${name=a,b,c} |
193 | | - if (match[2]) { |
194 | | - const optionStrings = match[2].split(','); |
195 | | -
|
196 | | - // When it's just one option it's a placeholder only |
197 | | - if (optionStrings.length === 1) { |
198 | | - const option = parseOption(optionStrings[0]); |
199 | | - if (variable.meta.placeholder !== option.value) { |
200 | | - this.$set(variable.meta, 'placeholder', option.value); |
| 171 | +
|
| 172 | + const name = match[1]; |
| 173 | + let variable = this.knownVariables[name]; |
| 174 | + if (variable) { |
| 175 | + toDeactivate.delete(variable.name); |
| 176 | + if (!variable.active) { |
| 177 | + variable.active = true; |
| 178 | + this.$forceUpdate(); |
201 | 179 | } |
202 | | - variable.meta.options = []; |
203 | 180 | } else { |
204 | | - variable.type = 'select'; |
205 | | - variable.meta.placeholder = ''; |
206 | | - variable.meta.options = optionStrings.map(parseOption); |
| 181 | + variable = { |
| 182 | + meta: { type: 'text', placeholder: '', options: [] }, |
| 183 | + sample: [], |
| 184 | + sampleUser: [], |
| 185 | + step: '', |
| 186 | + type: 'text', |
| 187 | + value: '', |
| 188 | + name: name, |
| 189 | + active: true, |
| 190 | + index: Object.keys(this.knownVariables).length + 1 |
| 191 | + }; |
| 192 | + this.knownVariables[name] = variable; |
| 193 | + this.$forceUpdate(); |
207 | 194 | } |
208 | | - } else { |
209 | | - if (variable.type === 'select') { |
210 | | - // Revert to last known type if options are removed |
211 | | - if (variable.catalogEntry) { |
212 | | - updateFromDataCatalog(variable, variable.catalogEntry); |
| 195 | +
|
| 196 | + // Case for ${name=1} or ${name=a,b,c} |
| 197 | + if (match[2]) { |
| 198 | + const optionStrings = match[2].split(','); |
| 199 | +
|
| 200 | + // When it's just one option it's a placeholder only |
| 201 | + if (optionStrings.length === 1) { |
| 202 | + const option = parseOption(optionStrings[0]); |
| 203 | + if (variable.meta.placeholder !== option.value) { |
| 204 | + variable.meta.placeholder = option.value; |
| 205 | + this.$forceUpdate(); |
| 206 | + } |
| 207 | + variable.meta.options = []; |
213 | 208 | } else { |
214 | | - variable.type = 'text'; |
| 209 | + variable.type = 'select'; |
| 210 | + variable.meta.placeholder = ''; |
| 211 | + variable.meta.options = optionStrings.map(parseOption); |
| 212 | + } |
| 213 | + } else { |
| 214 | + if (variable.type === 'select') { |
| 215 | + // Revert to last known type if options are removed |
| 216 | + if (variable.catalogEntry) { |
| 217 | + updateFromDataCatalog(variable, variable.catalogEntry); |
| 218 | + } else { |
| 219 | + variable.type = 'text'; |
| 220 | + } |
215 | 221 | } |
| 222 | + variable.meta.placeholder = ''; |
| 223 | + variable.meta.options = []; |
216 | 224 | } |
217 | | - variable.meta.placeholder = ''; |
218 | | - variable.meta.options = []; |
219 | | - } |
220 | | -
|
221 | | - if (location.colRef && location.resolveCatalogEntry) { |
222 | | - location |
223 | | - .resolveCatalogEntry() |
224 | | - .then(async entry => { |
225 | | - await updateFromDataCatalog(variable, entry); |
226 | | - }) |
227 | | - .catch(noop); |
228 | | - } |
229 | | - }); |
230 | | - } |
231 | 225 |
|
232 | | - toDeactivate.forEach(name => { |
233 | | - const variable = this.knownVariables[name]; |
234 | | - if (variable) { |
235 | | - this.$set(variable, 'active', false); |
| 226 | + if (location.colRef && location.resolveCatalogEntry) { |
| 227 | + location |
| 228 | + .resolveCatalogEntry() |
| 229 | + .then(async entry => { |
| 230 | + await updateFromDataCatalog(variable, entry); |
| 231 | + }) |
| 232 | + .catch(noop); |
| 233 | + } |
| 234 | + }); |
236 | 235 | } |
237 | | - }); |
| 236 | +
|
| 237 | + toDeactivate.forEach(name => { |
| 238 | + const variable = this.knownVariables[name]; |
| 239 | + if (variable) { |
| 240 | + variable.active = false; |
| 241 | + this.$forceUpdate(); |
| 242 | + } |
| 243 | + }); |
| 244 | + } |
| 245 | + }, |
| 246 | +
|
| 247 | + mounted(): void { |
| 248 | + if (this.initialVariables) { |
| 249 | + Object.values(this.initialVariables).forEach((variable, index) => { |
| 250 | + const cloned = <KnownVariable>cloneDeep(variable); |
| 251 | + cloned.active = true; |
| 252 | + cloned.index = index; |
| 253 | + this.knownVariables[cloned.name] = cloned; |
| 254 | + this.$forceUpdate(); |
| 255 | + }); |
| 256 | + } |
238 | 257 | } |
239 | | - } |
| 258 | + }); |
240 | 259 | </script> |
| 260 | + |
| 261 | +<style lang="scss"> |
| 262 | + @import './VariableSubstitution.scss'; |
| 263 | +</style> |
0 commit comments