/
EntityOptions.ts
108 lines (93 loc) · 3.63 KB
/
EntityOptions.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
import { Observable } from 'rxjs/Observable';
import { of as observableOf } from 'rxjs/observable/of';
import { Observer } from 'rxjs/Observer';
import { combineLatest } from 'rxjs/operator/combineLatest';
import { auditTime } from 'rxjs/operator/auditTime';
import { is, can } from '../utils';
export type Primitive = number | string | boolean | string[] | Object;
export interface SerializedOptions {
[key: string]: any;
id?: number;
fields?: string[];
layout?: string;
meta?: string;
showEditable?: boolean;
showReadOnly?: boolean;
executeFormTriggers?: boolean;
params?: Object;
}
export interface EntityOptions {
[key: string]: any;
id?: number | Observable<number>;
fields?: string[] | Observable<string[]>;
layout?: string | Observable<string>;
meta?: string | Observable<string>;
showEditable?: boolean | Observable<boolean>;
showReadOnly?: boolean | Observable<boolean>;
executeFormTriggers?: boolean | Observable<boolean>;
params?: Object | Observable<Object>;
}
// tslint:disable-next-line:only-arrow-functions
export function observeOptions(options: EntityOptions, audit: boolean = true): Observable<SerializedOptions | null> {
if (!is(options).defined) {
return observableOf(null);
}
return Observable.create((observer: Observer<SerializedOptions>) => {
let combined = combineLatest.call(
getOrCreateObservable('id', options),
getOrCreateObservable('fields', options),
getOrCreateObservable('layout', options),
getOrCreateObservable('meta', options),
getOrCreateObservable('showEditable', options),
getOrCreateObservable('showReadOnly', options),
getOrCreateObservable('executeFormTriggers', options),
getOrCreateObservable('params', options)
);
if (audit) {
combined = auditTime.call(combined, 0);
}
combined
.subscribe(([id, fields, layout, meta, editable, readOnly, triggers, params]: [number, string[], string, string, boolean, boolean, boolean, Object]) => {
const serializedOptions: SerializedOptions = {};
if (id !== undefined) {
serializedOptions.id = id;
}
if (fields !== undefined) {
serializedOptions.fields = fields;
}
if (layout !== undefined) {
serializedOptions.layout = layout;
}
if (meta !== undefined) {
serializedOptions.meta = meta;
}
if (editable !== undefined) {
serializedOptions.showEditable = editable;
}
if (readOnly !== undefined) {
serializedOptions.showReadOnly = readOnly;
}
if (triggers !== undefined) {
serializedOptions.executeFormTriggers = triggers;
}
if (params !== undefined) {
serializedOptions.params = params;
}
observer.next(serializedOptions);
});
});
}
// tslint:disable-next-line:only-arrow-functions
export function getOrCreateObservable(key: string, options: EntityOptions): Observable<Primitive> {
if (options[key] instanceof Observable) {
return options[key];
}
if (can(options).have(key)) {
return new Observable<Primitive>(subscriber => {
subscriber.next(options[key]);
});
}
return new Observable<Primitive>(subscriber => {
subscriber.next(undefined);
});
}