-
Notifications
You must be signed in to change notification settings - Fork 34
/
field-types.ts
295 lines (248 loc) · 7.89 KB
/
field-types.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
import { FieldContracts } from './field-contracts';
import { FieldModels } from './field-models';
import { FieldType } from './field-type';
import { Link, ILinkResolverResult } from '..';
export namespace Fields {
export class TextField implements FieldContracts.IField {
/**
* Text stored in the field
*/
public text: string;
/**
* Type of the field
*/
public type: FieldType = FieldType.Text;
/**
* Represents text field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
constructor(
public name: string,
public value: any
) {
this.text = this.value;
}
}
export class MultipleChoiceField implements FieldContracts.IField {
/**
* Multiple choice options
*/
public options: FieldModels.MultipleChoiceOption[] = [];
/**
* Type of the field
*/
public type: FieldType = FieldType.MultipleChoice;
/**
* Represents multiple choice field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
constructor(
public name: string,
public value: any
) {
if (this.value && Array.isArray(this.value)) {
this.value.forEach(option => {
const optionTemp = option as FieldContracts.IMultipleChoiceOption;
this.options.push(new FieldModels.MultipleChoiceOption(
optionTemp.name,
optionTemp.codename
));
});
}
}
}
export class DateTimeField implements FieldContracts.IField {
/**
* Date time value
*/
public datetime: Date;
/**
* Type of the field
*/
public type: FieldType = FieldType.DateTime;
/**
* Represents date time field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
constructor(
public name: string,
public value: any
) {
this.datetime = new Date(value);
}
}
export class RichTextField implements FieldContracts.IField {
/**
* Function that is responsible for getting resolved HTML of the field
*/
private resolveHtml: () => string;
/**
* Resolved html in field - store here once the html was resolved to avoid resolving it multiple times
*/
private resolvedHtml: string;
/**
* Type of the field
*/
public type: FieldType = FieldType.RichText;
/**
* Links
*/
public links: Link[] = [];
/**
* Represents rich text field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
* @param {() => string} resolveHtml - Function that resolves HTML
* @param {Link[]} links - Links for this rich text field
*/
constructor(
public name: string,
public value: any,
data: {
resolveHtml: () => string,
links: Link[]
}
) {
Object.assign(this, data);
}
getHtml(): string {
// check if html was already resolved
if (this.resolvedHtml) {
return this.resolvedHtml;
}
this.resolvedHtml = this.resolveHtml();
return this.resolvedHtml;
}
}
export class NumberField implements FieldContracts.IField {
/**
* Type of the field
*/
public type: FieldType = FieldType.Number;
/**
* Number value of this field
*/
public number: number;
/**
* Represents number field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
constructor(
public name: string,
public value: any
) {
this.number = value;
}
}
export class AssetsField implements FieldContracts.IField {
/**
* Type of the field
*/
public type: FieldType = FieldType.Asset;
/**
* List of assets used in this field
*/
public assets: FieldModels.AssetModel[] = [];
/**
* Represents asset field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {any} value - Value of the field
*/
constructor(
public name: string,
public value: any
) {
if (!value) {
throw Error(`Cannot bind assets field because no value was provided`);
}
if (!Array.isArray(value)) {
throw Error(`Cannot bind assets because the provided value is not an array`);
}
this.value.forEach((asset: FieldContracts.IAsset) => {
this.assets.push(new FieldModels.AssetModel(
asset.name,
asset.type,
asset.size,
asset.description,
asset.url
));
});
}
}
export class UrlSlugField implements FieldContracts.IField {
private resolvedUrl?: string;
private resolveLink: () => string;
/**
* Type of the field
*/
public type: FieldType = FieldType.UrlSlug;
/**
* Represents URL slug field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
constructor(
public name: string,
public value: string,
data: {
/**
* Callback for resolving link
*/
resolveLink: () => string | undefined | ILinkResolverResult
}
) {
Object.assign(this, data);
}
getUrl(): string | undefined {
if (this.resolvedUrl) {
return this.resolvedUrl;
}
this.resolvedUrl = this.resolveLink();
return this.resolvedUrl;
}
}
export class TaxonomyField implements FieldContracts.IField {
/**
* Type of the field
*/
public type: FieldType = FieldType.Taxonomy;
/**
* List of assigned taxonomy terms
*/
public taxonomyTerms: FieldModels.TaxonomyTerm[] = [];
/**
* Represents number field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
* @param {string | undefined} taxonomyGroup - Codename of the taxonomy group
*/
constructor(
public name: string,
public value: any,
public taxonomyGroup: string | undefined
) {
if (!value) {
throw Error(`Cannot map taxonomy field because no value was provided`);
}
if (!Array.isArray(value)) {
throw Error(`Cannot get taxonomy field because the provided value is not an array`);
}
const taxonomyList = value as FieldContracts.ITaxonomyTerm[];
taxonomyList.forEach(term => {
this.taxonomyTerms.push(new FieldModels.TaxonomyTerm(term.name, term.codename));
});
}
}
}