/
definedValueFieldComponents.ts
343 lines (291 loc) · 14.4 KB
/
definedValueFieldComponents.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
import { computed, defineComponent, inject, PropType, ref, watch } from "vue";
import CheckBox from "@Obsidian/Controls/checkBox.obs";
import CheckBoxList from "@Obsidian/Controls/checkBoxList.obs";
import DropDownList from "@Obsidian/Controls/dropDownList.obs";
import NumberBox from "@Obsidian/Controls/numberBox.obs";
import { asBoolean, asTrueFalseOrNull } from "@Obsidian/Utility/booleanUtils";
import { toNumber, toNumberOrNull } from "@Obsidian/Utility/numberUtils";
import { useVModelPassthrough } from "@Obsidian/Utility/component";
import { ListItemBag } from "@Obsidian/ViewModels/Utility/listItemBag";
import { ClientValue, ConfigurationPropertyKey, ConfigurationValueKey, ValueItem } from "./definedValueField.partial";
import { getFieldEditorProps } from "./utils";
function parseModelValue(modelValue: string | undefined): string {
try {
const clientValue = JSON.parse(modelValue ?? "") as ClientValue;
return clientValue.value;
}
catch {
return "";
}
}
function getClientValue(value: string | string[], valueOptions: ValueItem[]): ClientValue {
const values = Array.isArray(value) ? value : [value];
const selectedValues = valueOptions.filter(v => values.includes(v.value));
if (selectedValues.length >= 1) {
return {
value: selectedValues.map(v => v.value).join(","),
text: selectedValues.map(v => v.text).join(", "),
description: selectedValues.map(v => v.description).join(", ")
};
}
else {
return {
value: "",
text: "",
description: ""
};
}
}
export const EditComponent = defineComponent({
name: "DefinedValueField.Edit",
components: {
DropDownList,
CheckBoxList
},
props: getFieldEditorProps(),
setup(props, { emit }) {
const internalValue = ref(parseModelValue(props.modelValue));
const internalValues = ref(parseModelValue(props.modelValue).split(",").filter(v => v !== ""));
const valueOptions = computed((): ValueItem[] => {
try {
return JSON.parse(props.configurationValues[ConfigurationValueKey.Values] ?? "[]") as ValueItem[];
}
catch {
return [];
}
});
const displayDescription = computed((): boolean => asBoolean(props.configurationValues[ConfigurationValueKey.DisplayDescription]));
/** The options to choose from */
const options = computed((): ListItemBag[] => {
return valueOptions.value.map(v => {
return {
text: displayDescription.value ? (v.description || v.text) : v.text,
value: v.value
};
});
});
const isMultiple = computed((): boolean => asBoolean(props.configurationValues[ConfigurationValueKey.AllowMultiple]));
const configAttributes = computed((): Record<string, unknown> => {
const attributes: Record<string, unknown> = {};
const enhancedConfig = props.configurationValues[ConfigurationValueKey.EnhancedSelection];
if (enhancedConfig) {
attributes.enhanceForLongLists = asBoolean(enhancedConfig);
}
return attributes;
});
/** The number of columns wide the checkbox list will be. */
const repeatColumns = computed((): number => toNumber(props.configurationValues[ConfigurationValueKey.RepeatColumns]));
watch(() => props.modelValue, () => {
internalValue.value = parseModelValue(props.modelValue);
internalValues.value = parseModelValue(props.modelValue).split(",").filter(v => v !== "");
});
watch(() => internalValue.value, () => {
if (!isMultiple.value) {
const clientValue = getClientValue(internalValue.value, valueOptions.value);
emit("update:modelValue", JSON.stringify(clientValue));
}
});
watch(() => internalValues.value, () => {
if (isMultiple.value) {
const clientValue = getClientValue(internalValues.value, valueOptions.value);
emit("update:modelValue", JSON.stringify(clientValue));
}
});
return {
configAttributes,
internalValue,
internalValues,
isMultiple,
isRequired: inject("isRequired") as boolean,
options,
repeatColumns
};
},
template: `
<DropDownList v-if="!isMultiple || configAttributes.enhanceForLongLists" v-model="internalValue" v-bind="configAttributes" :items="options" />
<CheckBoxList v-else v-model="internalValues" :items="options" horizontal :repeatColumns="repeatColumns" />
`
});
export const FilterComponent = defineComponent({
name: "DefinedValueField.Filter",
components: {
EditComponent
},
props: getFieldEditorProps(),
setup(props, { emit }) {
const internalValue = useVModelPassthrough(props, "modelValue", emit);
const configurationValues = ref({ ...props.configurationValues });
configurationValues.value[ConfigurationValueKey.AllowMultiple] = "True";
watch(() => props.configurationValues, () => {
configurationValues.value = { ...props.configurationValues };
configurationValues.value[ConfigurationValueKey.AllowMultiple] = "True";
});
return {
internalValue,
configurationValues
};
},
template: `
<EditComponent v-model="internalValue" :configurationValues="configurationValues" />
`
});
export const ConfigurationComponent = defineComponent({
name: "DefinedValueField.Configuration",
components: {
DropDownList,
CheckBoxList,
CheckBox,
NumberBox
},
props: {
modelValue: {
type: Object as PropType<Record<string, string>>,
required: true
},
configurationProperties: {
type: Object as PropType<Record<string, string>>,
required: true
}
},
setup(props, { emit }) {
// Define the properties that will hold the current selections.
const definedTypeValue = ref("");
const allowMultipleValues = ref(false);
const displayDescriptions = ref(false);
const enhanceForLongLists = ref(false);
const includeInactive = ref(false);
const repeatColumns = ref<number | null>(null);
const selectableValues = ref<string[]>([]);
/** The defined types that are available to be selected from. */
const definedTypeItems = ref<ListItemBag[]>([]);
/** The defined values that are available to be selected from. */
const definedValueItems = ref<ListItemBag[]>([]);
/** The options to show in the defined type picker. */
const definedTypeOptions = computed((): ListItemBag[] => {
return definedTypeItems.value;
});
/** The options to show in the selectable values picker. */
const definedValueOptions = computed((): ListItemBag[] => definedValueItems.value);
/** Determines if we have any defined values to show. */
const hasValues = computed((): boolean => {
return definedValueItems.value.length > 0;
});
/**
* Update the modelValue property if any value of the dictionary has
* actually changed. This helps prevent unwanted postbacks if the value
* didn't really change - which can happen if multiple values get updated
* at the same time.
*
* @returns true if a new modelValue was emitted to the parent component.
*/
const maybeUpdateModelValue = (): boolean => {
const newValue: Record<string, string> = {
...props.modelValue
};
// Construct the new value that will be emitted if it is different
// than the current value.
newValue[ConfigurationValueKey.DefinedType] = definedTypeValue.value;
newValue[ConfigurationValueKey.SelectableValues] = selectableValues.value.join(",");
newValue[ConfigurationValueKey.AllowMultiple] = asTrueFalseOrNull(allowMultipleValues.value) ?? "False";
newValue[ConfigurationValueKey.DisplayDescription] = asTrueFalseOrNull(displayDescriptions.value) ?? "False";
newValue[ConfigurationValueKey.EnhancedSelection] = asTrueFalseOrNull(enhanceForLongLists.value) ?? "False";
newValue[ConfigurationValueKey.IncludeInactive] = asTrueFalseOrNull(includeInactive.value) ?? "False";
newValue[ConfigurationValueKey.RepeatColumns] = repeatColumns.value?.toString() ?? "";
// Compare the new value and the old value.
const anyValueChanged = newValue[ConfigurationValueKey.DefinedType] !== props.modelValue[ConfigurationValueKey.DefinedType]
|| newValue[ConfigurationValueKey.SelectableValues] !== (props.modelValue[ConfigurationValueKey.SelectableValues] ?? "")
|| newValue[ConfigurationValueKey.AllowMultiple] !== (props.modelValue[ConfigurationValueKey.AllowMultiple] ?? "False")
|| newValue[ConfigurationValueKey.DisplayDescription] !== (props.modelValue[ConfigurationValueKey.DisplayDescription] ?? "False")
|| newValue[ConfigurationValueKey.EnhancedSelection] !== (props.modelValue[ConfigurationValueKey.EnhancedSelection] ?? "False")
|| newValue[ConfigurationValueKey.IncludeInactive] !== (props.modelValue[ConfigurationValueKey.IncludeInactive] ?? "False")
|| newValue[ConfigurationValueKey.RepeatColumns] !== (props.modelValue[ConfigurationValueKey.RepeatColumns] ?? "");
// If any value changed then emit the new model value.
if (anyValueChanged) {
emit("update:modelValue", newValue);
return true;
}
else {
return false;
}
};
/**
* Emits the updateConfigurationValue if the value has actually changed.
*
* @param key The key that was possibly modified.
* @param value The new value.
*/
const maybeUpdateConfiguration = (key: string, value: string): void => {
if (maybeUpdateModelValue()) {
emit("updateConfigurationValue", key, value);
}
};
// Watch for changes coming in from the parent component and update our
// data to match the new information.
watch(() => [props.modelValue, props.configurationProperties], () => {
const definedTypes = props.configurationProperties[ConfigurationPropertyKey.DefinedTypes];
const definedValues = props.configurationProperties[ConfigurationPropertyKey.DefinedValues];
definedTypeItems.value = definedTypes ? JSON.parse(props.configurationProperties.definedTypes) as ListItemBag[] : [];
definedValueItems.value = definedValues ? JSON.parse(props.configurationProperties.definedValues) as ListItemBag[] : [];
definedTypeValue.value = props.modelValue.definedtype;
allowMultipleValues.value = asBoolean(props.modelValue[ConfigurationValueKey.AllowMultiple]);
displayDescriptions.value = asBoolean(props.modelValue[ConfigurationValueKey.DisplayDescription]);
enhanceForLongLists.value = asBoolean(props.modelValue[ConfigurationValueKey.EnhancedSelection]);
includeInactive.value = asBoolean(props.modelValue[ConfigurationValueKey.IncludeInactive]);
repeatColumns.value = toNumberOrNull(props.modelValue[ConfigurationValueKey.RepeatColumns]);
selectableValues.value = (props.modelValue[ConfigurationValueKey.SelectableValues]?.split(",") ?? []).filter(s => s !== "");
}, {
immediate: true
});
// Watch for changes in properties that require new configuration
// properties to be retrieved from the server.
watch([definedTypeValue, selectableValues, displayDescriptions, includeInactive], () => {
if (maybeUpdateModelValue()) {
emit("updateConfiguration");
}
});
// Watch for changes in properties that only require a local UI update.
watch(allowMultipleValues, () => maybeUpdateConfiguration(ConfigurationValueKey.AllowMultiple, asTrueFalseOrNull(allowMultipleValues.value) ?? "False"));
watch(enhanceForLongLists, () => maybeUpdateConfiguration(ConfigurationValueKey.EnhancedSelection, asTrueFalseOrNull(enhanceForLongLists.value) ?? "False"));
watch(repeatColumns, () => maybeUpdateConfiguration(ConfigurationValueKey.RepeatColumns, repeatColumns.value?.toString() ?? ""));
return {
allowMultipleValues,
definedTypeValue,
definedTypeOptions,
definedTypeItems,
definedValueOptions,
displayDescriptions,
enhanceForLongLists,
hasValues,
includeInactive,
repeatColumns,
selectableValues
};
},
template: `
<div>
<DropDownList v-model="definedTypeValue" label="Defined Type" :items="definedTypeOptions" :showBlankItem="false" />
<CheckBox v-model="allowMultipleValues" label="Allow Multiple Values" text="Yes" help="When set, allows multiple defined type values to be selected." />
<CheckBox v-model="displayDescriptions" label="Display Descriptions" text="Yes" help="When set, the defined value descriptions will be displayed instead of the values." />
<CheckBox v-model="enhanceForLongLists" label="Enhance For Long Lists" text="Yes" />
<CheckBox v-model="includeInactive" label="Include Inactive" text="Yes" />
<NumberBox v-model="repeatColumns" label="Repeat Columns" />
<CheckBoxList v-if="hasValues" v-model="selectableValues" label="Selectable Values" :items="definedValueOptions" :horizontal="true" />
</div>
`
});