@decaf-ts/ui-decorators 0.4.5 → 0.4.6

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.
Files changed (61) hide show
  1. package/dist/esm/ui-decorators.bundle.min.esm.js +2 -0
  2. package/dist/esm/ui-decorators.bundle.min.esm.js.LICENSE.txt +14 -0
  3. package/dist/ui-decorators.bundle.min.js +2 -0
  4. package/dist/ui-decorators.bundle.min.js.LICENSE.txt +14 -0
  5. package/lib/esm/index.d.ts +12 -0
  6. package/lib/esm/index.js +14 -0
  7. package/lib/esm/model/Renderable.d.ts +3 -0
  8. package/lib/esm/model/Renderable.js +3 -0
  9. package/lib/esm/model/decorators.d.ts +25 -0
  10. package/lib/esm/model/decorators.js +41 -0
  11. package/lib/esm/model/index.d.ts +8 -0
  12. package/lib/esm/model/index.js +10 -0
  13. package/lib/esm/model/model.d.ts +115 -0
  14. package/lib/esm/model/model.js +3 -0
  15. package/lib/esm/model/overrides.d.ts +1 -0
  16. package/lib/esm/model/overrides.js +7 -0
  17. package/lib/esm/ui/Rendering.d.ts +183 -0
  18. package/lib/esm/ui/Rendering.js +298 -0
  19. package/lib/esm/ui/constants.d.ts +64 -0
  20. package/lib/esm/ui/constants.js +88 -0
  21. package/lib/esm/ui/decorators.d.ts +34 -0
  22. package/lib/esm/ui/decorators.js +62 -0
  23. package/lib/esm/ui/errors.d.ts +4 -0
  24. package/lib/esm/ui/errors.js +8 -0
  25. package/lib/esm/ui/index.d.ts +11 -0
  26. package/lib/esm/ui/index.js +13 -0
  27. package/lib/esm/ui/interfaces.d.ts +5 -0
  28. package/lib/esm/ui/interfaces.js +3 -0
  29. package/lib/esm/ui/types.d.ts +46 -0
  30. package/lib/esm/ui/types.js +3 -0
  31. package/lib/esm/ui/utils.d.ts +13 -0
  32. package/lib/esm/ui/utils.js +88 -0
  33. package/lib/index.cjs +29 -0
  34. package/lib/index.d.ts +12 -0
  35. package/lib/model/Renderable.cjs +2 -0
  36. package/lib/model/Renderable.d.ts +3 -0
  37. package/lib/model/decorators.cjs +43 -0
  38. package/lib/model/decorators.d.ts +25 -0
  39. package/lib/model/index.cjs +24 -0
  40. package/lib/model/index.d.ts +8 -0
  41. package/lib/model/model.cjs +2 -0
  42. package/lib/model/model.d.ts +115 -0
  43. package/lib/model/overrides.cjs +7 -0
  44. package/lib/model/overrides.d.ts +1 -0
  45. package/lib/ui/Rendering.cjs +300 -0
  46. package/lib/ui/Rendering.d.ts +183 -0
  47. package/lib/ui/constants.cjs +89 -0
  48. package/lib/ui/constants.d.ts +64 -0
  49. package/lib/ui/decorators.cjs +66 -0
  50. package/lib/ui/decorators.d.ts +34 -0
  51. package/lib/ui/errors.cjs +10 -0
  52. package/lib/ui/errors.d.ts +4 -0
  53. package/lib/ui/index.cjs +27 -0
  54. package/lib/ui/index.d.ts +11 -0
  55. package/lib/ui/interfaces.cjs +2 -0
  56. package/lib/ui/interfaces.d.ts +5 -0
  57. package/lib/ui/types.cjs +2 -0
  58. package/lib/ui/types.d.ts +46 -0
  59. package/lib/ui/utils.cjs +94 -0
  60. package/lib/ui/utils.d.ts +13 -0
  61. package/package.json +1 -1
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hideOn = hideOn;
4
+ exports.hidden = hidden;
5
+ exports.uielement = uielement;
6
+ exports.uiprop = uiprop;
7
+ require("reflect-metadata");
8
+ const constants_1 = require("./constants.cjs");
9
+ const decorator_validation_1 = require("@decaf-ts/decorator-validation");
10
+ const Rendering_1 = require("./Rendering.cjs");
11
+ const db_decorators_1 = require("@decaf-ts/db-decorators");
12
+ /**
13
+ * @namespace ui-decorators.ui.decorators
14
+ * @memberOf ui-decorators.ui
15
+ */
16
+ function hideOn(...operations) {
17
+ return (0, decorator_validation_1.propMetadata)(Rendering_1.RenderingEngine.key(constants_1.UIKeys.HIDDEN), operations);
18
+ }
19
+ function hidden() {
20
+ return hideOn(db_decorators_1.OperationKeys.CREATE, db_decorators_1.OperationKeys.READ, db_decorators_1.OperationKeys.UPDATE, db_decorators_1.OperationKeys.DELETE);
21
+ }
22
+ /**
23
+ * Adds the UIElement definition as metadata to the property, allowing it to be read by any {@link RenderStrategy}
24
+ *
25
+ * @param {string} tag The component/HTML element tag name
26
+ * @param {{}} [props] The properties to pass to that component/HTML Element
27
+ * @param serialize
28
+ *
29
+ * @decorator uielement
30
+ *
31
+ * @category Decorators
32
+ * @subcategory ui-decorators
33
+ */
34
+ function uielement(tag, props, serialize = false) {
35
+ return (original, propertyKey) => {
36
+ const metadata = {
37
+ tag: tag,
38
+ serialize: serialize,
39
+ props: Object.assign({
40
+ name: propertyKey,
41
+ }, props || {}),
42
+ };
43
+ return (0, decorator_validation_1.propMetadata)(Rendering_1.RenderingEngine.key(constants_1.UIKeys.ELEMENT), metadata)(original, propertyKey);
44
+ };
45
+ }
46
+ /**
47
+ * Adds the UIProp definition as metadata to the property, allowing it to be read by any {@link RenderStrategy}
48
+ *
49
+ * this requires a '@uimodel' with a defined tag
50
+ *
51
+ * @param {string} [propName] the property name that will be passed to the component. defaults to the PropertyKey
52
+ *
53
+ * @decorator uiprop
54
+ *
55
+ * @category Decorators
56
+ * @subcategory ui-decorators
57
+ */
58
+ function uiprop(propName = undefined, stringify = false) {
59
+ return (target, propertyKey) => {
60
+ const metadata = {
61
+ name: propName || propertyKey,
62
+ stringify: stringify,
63
+ };
64
+ (0, decorator_validation_1.propMetadata)(Rendering_1.RenderingEngine.key(constants_1.UIKeys.PROP), metadata)(target, propertyKey);
65
+ };
66
+ }
@@ -0,0 +1,34 @@
1
+ import "reflect-metadata";
2
+ import { CrudOperationKeys } from "./types";
3
+ /**
4
+ * @namespace ui-decorators.ui.decorators
5
+ * @memberOf ui-decorators.ui
6
+ */
7
+ export declare function hideOn(...operations: CrudOperationKeys[]): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
8
+ export declare function hidden(): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
9
+ /**
10
+ * Adds the UIElement definition as metadata to the property, allowing it to be read by any {@link RenderStrategy}
11
+ *
12
+ * @param {string} tag The component/HTML element tag name
13
+ * @param {{}} [props] The properties to pass to that component/HTML Element
14
+ * @param serialize
15
+ *
16
+ * @decorator uielement
17
+ *
18
+ * @category Decorators
19
+ * @subcategory ui-decorators
20
+ */
21
+ export declare function uielement(tag: string, props?: Record<string, any>, serialize?: boolean): (original: any, propertyKey?: any) => void;
22
+ /**
23
+ * Adds the UIProp definition as metadata to the property, allowing it to be read by any {@link RenderStrategy}
24
+ *
25
+ * this requires a '@uimodel' with a defined tag
26
+ *
27
+ * @param {string} [propName] the property name that will be passed to the component. defaults to the PropertyKey
28
+ *
29
+ * @decorator uiprop
30
+ *
31
+ * @category Decorators
32
+ * @subcategory ui-decorators
33
+ */
34
+ export declare function uiprop(propName?: string | undefined, stringify?: boolean): (target: any, propertyKey: string) => void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RenderingError = void 0;
4
+ const db_decorators_1 = require("@decaf-ts/db-decorators");
5
+ class RenderingError extends db_decorators_1.BaseError {
6
+ constructor(msg) {
7
+ super(RenderingError.name, msg);
8
+ }
9
+ }
10
+ exports.RenderingError = RenderingError;
@@ -0,0 +1,4 @@
1
+ import { BaseError } from "@decaf-ts/db-decorators";
2
+ export declare class RenderingError extends BaseError {
3
+ constructor(msg: string | Error);
4
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * @namespace ui-decorators.ui
4
+ * @memberOf ui-decorators
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ __exportStar(require("./constants.cjs"), exports);
22
+ __exportStar(require("./decorators.cjs"), exports);
23
+ __exportStar(require("./errors.cjs"), exports);
24
+ __exportStar(require("./interfaces.cjs"), exports);
25
+ __exportStar(require("./Rendering.cjs"), exports);
26
+ __exportStar(require("./types.cjs"), exports);
27
+ __exportStar(require("./utils.cjs"), exports);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @namespace ui-decorators.ui
3
+ * @memberOf ui-decorators
4
+ */
5
+ export * from "./constants";
6
+ export * from "./decorators";
7
+ export * from "./errors";
8
+ export * from "./interfaces";
9
+ export * from "./Rendering";
10
+ export * from "./types";
11
+ export * from "./utils";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { FieldProperties } from "./types";
2
+ import { CrudOperations } from "@decaf-ts/db-decorators";
3
+ export interface CrudFormField extends FieldProperties {
4
+ operation: CrudOperations;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,46 @@
1
+ import { OperationKeys } from "@decaf-ts/db-decorators";
2
+ export interface FieldDefinition<T = void> {
3
+ tag: string;
4
+ rendererId?: string;
5
+ props: T & FieldProperties;
6
+ children?: FieldDefinition<T>[];
7
+ }
8
+ export interface FieldProperties {
9
+ name: string;
10
+ type: string;
11
+ value: string | number | Date;
12
+ hidden?: boolean;
13
+ disabled?: boolean;
14
+ required?: boolean;
15
+ readonly?: boolean;
16
+ maxLength?: number;
17
+ minLength?: number;
18
+ max?: number | Date;
19
+ min?: number | Date;
20
+ pattern?: string;
21
+ step?: number;
22
+ format?: string;
23
+ }
24
+ /**
25
+ * @typedef UIElementMetadata
26
+ * @memberOf ui-decorators.ui.decorators
27
+ */
28
+ export type UIElementMetadata = {
29
+ tag: string;
30
+ props?: Record<string, any>;
31
+ serialize?: boolean;
32
+ };
33
+ /**
34
+ * @typedef UIElementMetadata
35
+ * @memberOf ui-decorators.ui.decorators
36
+ */
37
+ export type UIModelMetadata = Omit<UIElementMetadata, "serialize">;
38
+ /**
39
+ * @typedef UIPropMetadata
40
+ * @memberOf ui-decorators.ui.decorators
41
+ */
42
+ export type UIPropMetadata = {
43
+ name: string;
44
+ stringify: boolean;
45
+ };
46
+ export type CrudOperationKeys = OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE;
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatByType = formatByType;
4
+ exports.parseValueByType = parseValueByType;
5
+ exports.parseToNumber = parseToNumber;
6
+ exports.escapeHtml = escapeHtml;
7
+ exports.revertHtml = revertHtml;
8
+ exports.generateUIModelID = generateUIModelID;
9
+ const decorator_validation_1 = require("@decaf-ts/decorator-validation");
10
+ const constants_1 = require("./constants.cjs");
11
+ const db_decorators_1 = require("@decaf-ts/db-decorators");
12
+ /**
13
+ * @function formatByType
14
+ *
15
+ * @memberOf ui-decorators-web.ui
16
+ */
17
+ function formatByType(type, value, ...args) {
18
+ if (type === constants_1.UIKeys.DATE) {
19
+ const format = args.shift() || constants_1.HTML5DateFormat;
20
+ return (0, decorator_validation_1.formatDate)(new Date(value), format);
21
+ }
22
+ return value;
23
+ }
24
+ function parseValueByType(type, value, fieldProps) {
25
+ let result = undefined;
26
+ switch (type) {
27
+ case constants_1.HTML5InputTypes.NUMBER:
28
+ result = parseToNumber(value);
29
+ break;
30
+ case constants_1.HTML5InputTypes.DATE: {
31
+ const format = fieldProps.format;
32
+ result =
33
+ typeof value === decorator_validation_1.ReservedModels.NUMBER
34
+ ? new Date(value)
35
+ : value
36
+ ? format
37
+ ? (0, decorator_validation_1.parseDate)(format, value)
38
+ : new Date(value)
39
+ : undefined;
40
+ break;
41
+ }
42
+ default:
43
+ result =
44
+ typeof value === decorator_validation_1.ReservedModels.STRING
45
+ ? escapeHtml(value)
46
+ : result;
47
+ }
48
+ if (typeof result === "undefined") {
49
+ throw new db_decorators_1.InternalError(`Failed to parse value of type ${type} from ${typeof value} - ${value}`);
50
+ }
51
+ return result;
52
+ }
53
+ function parseToNumber(value) {
54
+ if (typeof value === "number" && !isNaN(value))
55
+ return value;
56
+ const parsed = Number(value);
57
+ if (!isNaN(parsed))
58
+ return parsed;
59
+ return undefined;
60
+ }
61
+ function escapeHtml(value) {
62
+ if (!value)
63
+ return undefined;
64
+ const tagsToReplace = {
65
+ "&": "&amp;",
66
+ "<": "&lt;",
67
+ ">": "&gt;",
68
+ };
69
+ return `${value}`.replace(/[&<>]/g, (tag) => {
70
+ return tagsToReplace[tag] || tag;
71
+ });
72
+ }
73
+ function revertHtml(value) {
74
+ const tagsToReplace = {
75
+ "&amp;": "&",
76
+ "&lt;": "<",
77
+ "&gt;": ">",
78
+ };
79
+ return `${value}`.replace(/&lt;|&gt;|&amp;/g, (tag) => {
80
+ return tagsToReplace[tag] || tag;
81
+ });
82
+ }
83
+ function generateUIModelID(model) {
84
+ let id;
85
+ try {
86
+ id = (0, db_decorators_1.findModelId)(model);
87
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
88
+ }
89
+ catch (e) {
90
+ id = Date.now();
91
+ }
92
+ const name = model.constructor.name;
93
+ return `${name}-${id}`;
94
+ }
@@ -0,0 +1,13 @@
1
+ import { Model } from "@decaf-ts/decorator-validation";
2
+ import { FieldProperties } from "./types";
3
+ /**
4
+ * @function formatByType
5
+ *
6
+ * @memberOf ui-decorators-web.ui
7
+ */
8
+ export declare function formatByType(type: any, value: any, ...args: unknown[]): string | number;
9
+ export declare function parseValueByType(type: string, value: string | number, fieldProps: FieldProperties): string | number | Date;
10
+ export declare function parseToNumber(value: string | number): number | undefined;
11
+ export declare function escapeHtml(value: string): string | undefined;
12
+ export declare function revertHtml(value: string): string;
13
+ export declare function generateUIModelID<M extends Model>(model: M): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decaf-ts/ui-decorators",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "Extension of decorator validation to ui elements to allow for web integration",
5
5
  "type": "module",
6
6
  "exports": {