@rws-framework/client 2.20.2 → 2.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/_attrs/json-attr.ts +62 -0
- package/src/index.ts +3 -0
package/package.json
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DecoratorAttributeConfiguration,
|
|
3
|
+
AttributeConfiguration,
|
|
4
|
+
Observable,
|
|
5
|
+
} from "@microsoft/fast-element";
|
|
6
|
+
import { handleExternalChange } from "./_external_handler";
|
|
7
|
+
import RWSViewComponent, { IWithCompose } from "../_component";
|
|
8
|
+
|
|
9
|
+
type ExAttrOpts = { converter?: (val: any) => string }
|
|
10
|
+
const _default_opts = {
|
|
11
|
+
converter: (val: any) => {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(val);
|
|
14
|
+
}catch(e){
|
|
15
|
+
console.error(e);
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function jsonAttr(
|
|
23
|
+
config?: DecoratorAttributeConfiguration,
|
|
24
|
+
): (target: {}, property: string) => void;
|
|
25
|
+
|
|
26
|
+
export function jsonAttr(target: {}, property: string, opts?: ExAttrOpts): void;
|
|
27
|
+
export function jsonAttr(
|
|
28
|
+
configOrTarget?: DecoratorAttributeConfiguration | {},
|
|
29
|
+
property?: string,
|
|
30
|
+
opts: ExAttrOpts = _default_opts
|
|
31
|
+
): void | ((target: {}, property: string) => void) {
|
|
32
|
+
let config: AttributeConfiguration;
|
|
33
|
+
|
|
34
|
+
function decorator($target: {}, $prop: string): void {
|
|
35
|
+
if (arguments.length > 1) {
|
|
36
|
+
config.property = $prop;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
config.mode = 'fromView';
|
|
40
|
+
config.converter = { fromView: opts.converter, toView: null };
|
|
41
|
+
|
|
42
|
+
const attrs = AttributeConfiguration.locate($target.constructor);
|
|
43
|
+
|
|
44
|
+
RWSViewComponent.setExternalAttr(($target.constructor as IWithCompose<any>).name, $prop);
|
|
45
|
+
|
|
46
|
+
attrs.push(config);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (arguments.length > 1) {
|
|
50
|
+
// Non invocation:
|
|
51
|
+
// - @attr
|
|
52
|
+
config = {} as any;
|
|
53
|
+
decorator(configOrTarget!, property!);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Invocation with or w/o opts:
|
|
58
|
+
// - @attr()
|
|
59
|
+
// - @attr({...opts})
|
|
60
|
+
config = configOrTarget === void 0 ? ({} as any) : configOrTarget;
|
|
61
|
+
return decorator;
|
|
62
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,8 @@ import ServiceWorkerService, { ServiceWorkerServiceInstance } from './services/S
|
|
|
10
10
|
import { ngAttr } from './components/_attrs/angular-attr';
|
|
11
11
|
import { externalObservable } from './components/_attrs/external-observable';
|
|
12
12
|
import { externalAttr } from './components/_attrs/external-attr';
|
|
13
|
+
import { jsonAttr } from './components/_attrs/json-attr';
|
|
14
|
+
|
|
13
15
|
import { RWSPlugin } from './plugins/_plugin';
|
|
14
16
|
import RWSClient, { RWSClientInstance } from './client';
|
|
15
17
|
import RWSViewComponent from './components/_component';
|
|
@@ -62,6 +64,7 @@ export {
|
|
|
62
64
|
observable,
|
|
63
65
|
externalObservable,
|
|
64
66
|
externalAttr,
|
|
67
|
+
jsonAttr,
|
|
65
68
|
attr,
|
|
66
69
|
ngAttr,
|
|
67
70
|
|