-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant.ts
256 lines (224 loc) · 6.91 KB
/
variant.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
import {DeepObject, autoDeepObject} from '../../utility/deepObject';
import {Field, FieldConfig} from '../field';
import {GlobalConfig, SelectiveEditor} from '../editor';
import {TemplateResult, html} from 'lit-html';
import {DataType} from '../../utility/dataType';
import {FieldsComponent} from '../fields';
import {Types} from '../types';
import {findParentByClassname} from '../../utility/dom';
import merge from 'lodash.merge';
import {repeat} from 'lit-html/directives/repeat.js';
export interface VariantOptionConfig {
/**
* Fields to show when the variant is in use.
*/
fields: Array<FieldConfig>;
/**
* Label for the variant option.
*/
label?: string;
/**
* Help text to explain the variant.
*/
help?: string;
}
export interface VariantFieldConfig extends FieldConfig {
/**
* Label for presenting the variants as options.
*/
variantLabel?: string;
/**
* Variant options the user are allowed to select from.
*/
variants: Record<string, VariantOptionConfig>;
}
export class VariantField extends Field {
config: VariantFieldConfig;
fields?: FieldsComponent;
usingAutoFields: boolean;
variant?: string;
constructor(
types: Types,
config: VariantFieldConfig,
globalConfig: GlobalConfig,
fieldType = 'variant'
) {
super(types, config, globalConfig, fieldType);
this.config = config;
this.usingAutoFields = false;
}
protected createFields(): FieldsComponent | undefined {
const variant: string | undefined =
this.variant || this.originalValue?._variant || this.config.default;
// If we don't know which variant to use, then skip it.
if (!variant) {
return undefined;
}
// Use the found variant if it is undefined.
if (this.variant === undefined) {
this.variant = variant;
}
const variantConfig = this.config.variants[variant] || {};
const fieldConfigs: Array<FieldConfig> = variantConfig.fields || [];
return new this.types.globals.FieldsCls(
this.types,
{
fields: fieldConfigs,
isGuessed: this.usingAutoFields,
parentKey: this.fullKey,
},
this.globalConfig
);
}
protected ensureFields() {
if (!this.fields) {
this.fields = this.createFields();
}
}
handleVariantClick(evt: Event) {
const target = evt.target as HTMLElement;
const variantTarget = findParentByClassname(
target,
'selective__variant__variant'
);
const variant = variantTarget?.dataset.variant;
if (!variant || variant === this.variant) {
return;
}
this.variant = variant;
// Reset the fields so they can be recreated next render.
this.fields = undefined;
this.render();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handleVariantClear(evt: Event) {
this.variant = undefined;
// Reset the fields so they can be recreated next render.
this.fields = undefined;
this.render();
}
get isClean(): boolean {
// If there are no fields, nothing has changed.
if (!this.fields) {
return true;
}
// Check for changes to the variant.
if (this.originalValue && this.originalValue._variant !== this.variant) {
return false;
}
return this.fields.isClean;
}
/**
* Check if the data format is invalid for what the field expects to edit.
*/
get isDataFormatValid(): boolean {
if (this.originalValue === undefined || this.originalValue === null) {
return true;
}
return DataType.isObject(this.originalValue);
}
get isValid(): boolean {
// If there are no fields, nothing has changed.
if (!this.fields) {
return true;
}
return this.fields.isValid;
}
/**
* Template for rendering the field input.
*
* The help text is part of the input template so complex inputs can
* use zones for the help text.
*
* @param editor Selective editor used to render the template.
* @param data Data provided to render the template.
*/
templateInput(editor: SelectiveEditor, data: DeepObject): TemplateResult {
this.ensureFields();
return html`${this.templateHelp(editor, data)}${this.fields?.template(
editor,
autoDeepObject(this.originalValue)
) || ''}`;
}
/**
* Template for rendering the field input structure.
*
* @param editor Selective editor used to render the template.
* @param data Data provided to render the template.
*/
templateInputStructure(
editor: SelectiveEditor,
data: DeepObject
): TemplateResult {
const parts: Array<TemplateResult> = [];
if (!this.isDataFormatValid) {
parts.push(this.templateDataFormatInvalid(editor, data));
} else {
parts.push(this.templateVariants(editor, data));
parts.push(this.templateInput(editor, data));
}
return html`<div class="selective__field__input__structure">${parts}</div>`;
}
/**
* Template for rendering the variant options.
*
* @param editor Selective editor used to render the template.
* @param data Data provided to render the template.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
templateVariants(editor: SelectiveEditor, data: DeepObject): TemplateResult {
const variants = this.config.variants;
const variantKeys = Object.keys(variants).sort();
return html`<div class="selective__variant__variants">
${this.config.variantLabel
? html`<label>${this.config.variantLabel}:</label>`
: ''}
${repeat(
variantKeys,
variantKey => variantKey,
variantKey => html`
<button
?disabled=${this.variant !== variantKey &&
this.fields &&
!this.fields.isClean}
class="selective__variant__variant ${this.variant === variantKey
? 'selective__variant__variant--selected selective__button--primary'
: ''}"
data-variant=${variantKey}
@click=${this.handleVariantClick.bind(this)}
>
<span>${variants[variantKey].label || variantKey}</span>
</button>
`
)}
${this.variant
? html`<div
class="selective__variant__clear"
data-tip="Clear"
@click=${this.handleVariantClear.bind(this)}
>
<span class="material-icons">clear</span>
</div>`
: ''}
</div>`;
}
/**
* Template for rendering the field wrapper.
*
* @param editor Selective editor used to render the template.
* @param data Data provided to render the template.
*/
templateWrapper(editor: SelectiveEditor, data: DeepObject): TemplateResult {
// Need to ensure the fields exists before we check for the clean status.
this.ensureFields();
return super.templateWrapper(editor, data);
}
get value(): Record<string, any> {
if (!this.fields) {
return this.originalValue;
}
return merge({}, this.originalValue, this.fields.value, {
_variant: this.variant,
});
}
}