@mgdis/core-ui-helpers 1.0.0

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 (146) hide show
  1. package/.turbo/turbo-build.log +52 -0
  2. package/CHANGELOG.md +12 -0
  3. package/LICENSE +29 -0
  4. package/README.md +95 -0
  5. package/dist/angular/index.cjs +27 -0
  6. package/dist/angular/index.cjs.map +1 -0
  7. package/dist/angular/index.d.ts +21 -0
  8. package/dist/angular/index.js +25 -0
  9. package/dist/angular/index.js.map +1 -0
  10. package/dist/angular/index.umd.js +33 -0
  11. package/dist/angular/index.umd.js.map +1 -0
  12. package/dist/locale/index.cjs +164 -0
  13. package/dist/locale/index.cjs.map +1 -0
  14. package/dist/locale/index.d.ts +103 -0
  15. package/dist/locale/index.js +156 -0
  16. package/dist/locale/index.js.map +1 -0
  17. package/dist/locale/index.umd.js +170 -0
  18. package/dist/locale/index.umd.js.map +1 -0
  19. package/dist/playwright/index.cjs +124 -0
  20. package/dist/playwright/index.cjs.map +1 -0
  21. package/dist/playwright/index.d.ts +35 -0
  22. package/dist/playwright/index.js +120 -0
  23. package/dist/playwright/index.js.map +1 -0
  24. package/dist/playwright/index.umd.js +130 -0
  25. package/dist/playwright/index.umd.js.map +1 -0
  26. package/dist/stencil/index.cjs +281 -0
  27. package/dist/stencil/index.cjs.map +1 -0
  28. package/dist/stencil/index.d.ts +111 -0
  29. package/dist/stencil/index.js +277 -0
  30. package/dist/stencil/index.js.map +1 -0
  31. package/dist/stencil/index.umd.js +287 -0
  32. package/dist/stencil/index.umd.js.map +1 -0
  33. package/dist/storybook/index.cjs +2109 -0
  34. package/dist/storybook/index.cjs.map +1 -0
  35. package/dist/storybook/index.d.ts +85 -0
  36. package/dist/storybook/index.js +2103 -0
  37. package/dist/storybook/index.js.map +1 -0
  38. package/dist/storybook/index.umd.js +2115 -0
  39. package/dist/storybook/index.umd.js.map +1 -0
  40. package/dist/tests/index.cjs +166 -0
  41. package/dist/tests/index.cjs.map +1 -0
  42. package/dist/tests/index.d.ts +91 -0
  43. package/dist/tests/index.js +161 -0
  44. package/dist/tests/index.js.map +1 -0
  45. package/dist/tests/index.umd.js +172 -0
  46. package/dist/tests/index.umd.js.map +1 -0
  47. package/dist/utils/index.cjs +433 -0
  48. package/dist/utils/index.cjs.map +1 -0
  49. package/dist/utils/index.d.ts +265 -0
  50. package/dist/utils/index.js +409 -0
  51. package/dist/utils/index.js.map +1 -0
  52. package/dist/utils/index.umd.js +439 -0
  53. package/dist/utils/index.umd.js.map +1 -0
  54. package/dist/vue/index.cjs +1934 -0
  55. package/dist/vue/index.cjs.map +1 -0
  56. package/dist/vue/index.d.ts +35 -0
  57. package/dist/vue/index.js +1932 -0
  58. package/dist/vue/index.js.map +1 -0
  59. package/dist/vue/index.umd.js +1940 -0
  60. package/dist/vue/index.umd.js.map +1 -0
  61. package/json-doc.test.json +253 -0
  62. package/package.json +80 -0
  63. package/rollup.config.mjs +61 -0
  64. package/src/angular/index.spec.ts +8 -0
  65. package/src/angular/index.ts +1 -0
  66. package/src/angular/mg-angular-logger/index.ts +1 -0
  67. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +3 -0
  68. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +40 -0
  69. package/src/angular/mg-angular-logger/mg-angular-logger.ts +23 -0
  70. package/src/locale/index.spec.ts +14 -0
  71. package/src/locale/index.ts +1 -0
  72. package/src/locale/locale.spec.ts +173 -0
  73. package/src/locale/locale.ts +166 -0
  74. package/src/playwright/config/index.ts +43 -0
  75. package/src/playwright/index.spec.ts +16 -0
  76. package/src/playwright/index.ts +4 -0
  77. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +48 -0
  78. package/src/playwright/utils/index.ts +1 -0
  79. package/src/playwright/utils/utils.spec.ts +43 -0
  80. package/src/playwright/utils/utils.ts +64 -0
  81. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +527 -0
  82. package/src/stencil/ide/ide.spec.ts +25 -0
  83. package/src/stencil/ide/ide.ts +214 -0
  84. package/src/stencil/ide/index.ts +1 -0
  85. package/src/stencil/index.spec.ts +12 -0
  86. package/src/stencil/index.ts +1 -0
  87. package/src/storybook/index.spec.ts +15 -0
  88. package/src/storybook/index.ts +1 -0
  89. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +329 -0
  90. package/src/storybook/stencil/index.ts +1 -0
  91. package/src/storybook/stencil/stencil-shim.d.ts +1 -0
  92. package/src/storybook/stencil/stencil.conf.ts +4 -0
  93. package/src/storybook/stencil/stencil.spec.ts +142 -0
  94. package/src/storybook/stencil/stencil.ts +366 -0
  95. package/src/tests/index.spec.ts +13 -0
  96. package/src/tests/index.ts +1 -0
  97. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +52 -0
  98. package/src/tests/unit/index.ts +1 -0
  99. package/src/tests/unit/unit.conf.ts +9 -0
  100. package/src/tests/unit/unit.spec.ts +44 -0
  101. package/src/tests/unit/unit.ts +171 -0
  102. package/src/utils/attributes/class-list/class-list.spec.ts +36 -0
  103. package/src/utils/attributes/class-list/class-list.ts +51 -0
  104. package/src/utils/attributes/class-list/index.ts +1 -0
  105. package/src/utils/attributes/id/id.spec.ts +51 -0
  106. package/src/utils/attributes/id/id.ts +46 -0
  107. package/src/utils/attributes/id/index.ts +1 -0
  108. package/src/utils/attributes/index.ts +3 -0
  109. package/src/utils/attributes/to-kebab-case/index.ts +1 -0
  110. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +29 -0
  111. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +23 -0
  112. package/src/utils/components/__snapshots__/components.spec.ts.snap +313 -0
  113. package/src/utils/components/components.conf.ts +11 -0
  114. package/src/utils/components/components.spec.ts +192 -0
  115. package/src/utils/components/components.ts +138 -0
  116. package/src/utils/components/index.ts +2 -0
  117. package/src/utils/date/date.spec.ts +12 -0
  118. package/src/utils/date/date.ts +20 -0
  119. package/src/utils/date/index.ts +1 -0
  120. package/src/utils/elements/elements.spec.ts +17 -0
  121. package/src/utils/elements/elements.ts +16 -0
  122. package/src/utils/elements/index.ts +1 -0
  123. package/src/utils/index.spec.ts +84 -0
  124. package/src/utils/index.ts +8 -0
  125. package/src/utils/number/index.ts +1 -0
  126. package/src/utils/number/number.spec.ts +14 -0
  127. package/src/utils/number/number.ts +6 -0
  128. package/src/utils/object/index.ts +2 -0
  129. package/src/utils/object/object.conf.ts +4 -0
  130. package/src/utils/object/object.spec.ts +98 -0
  131. package/src/utils/object/object.ts +26 -0
  132. package/src/utils/string/index.ts +1 -0
  133. package/src/utils/string/string.spec.ts +47 -0
  134. package/src/utils/string/string.ts +38 -0
  135. package/src/utils/windows/index.ts +1 -0
  136. package/src/utils/windows/windows.spec.ts +65 -0
  137. package/src/utils/windows/windows.ts +50 -0
  138. package/src/vue/index.spec.ts +8 -0
  139. package/src/vue/index.ts +1 -0
  140. package/src/vue/mg-model/index.ts +1 -0
  141. package/src/vue/mg-model/mg-model.conf.ts +6 -0
  142. package/src/vue/mg-model/mg-model.spec.ts +50 -0
  143. package/src/vue/mg-model/mg-model.ts +50 -0
  144. package/tsconfig.json +17 -0
  145. package/typedoc.json +14 -0
  146. package/vitest.config.mts +13 -0
@@ -0,0 +1,17 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { isTagName } from './elements';
3
+
4
+ describe('elements', () => {
5
+ describe('isTagName', () => {
6
+ test.each([
7
+ { querySelector: 'h1', expected: true },
8
+ { querySelector: 'span', expected: false },
9
+ { querySelector: 'blu', expected: false },
10
+ ])('Should test if all otems are string: %s', ({ querySelector, expected }) => {
11
+ const div = document.createElement('div');
12
+ div.innerHTML = '<h1></h1><span></span>';
13
+ const elm = div.querySelector(querySelector);
14
+ expect(isTagName(elm as HTMLElement, ['h1'])).toEqual(expected);
15
+ });
16
+ });
17
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Check if element belongs to the given tagNames list
3
+ * @param element - element to check
4
+ * @param tagNames - allowed tag names list
5
+ * @returns `true` if element tagName is in the tagNames list
6
+ */
7
+ export const isTagName = (element: Element, tagNames: string[]): boolean => tagNames.includes(element?.tagName.toLowerCase());
8
+
9
+ /**
10
+ * CSS selector to select focusable elements.
11
+ * @example
12
+ * ```ts
13
+ * const allFocusableElements: HTMLElement[] = Array.from(this.element.querySelectorAll(focusableElements));
14
+ * ```
15
+ */
16
+ export const focusableElements = 'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"]), [identifier], mg-button';
@@ -0,0 +1 @@
1
+ export * from './elements';
@@ -0,0 +1,84 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import {
3
+ allItemsAreString,
4
+ ClassList,
5
+ cleanString,
6
+ createID,
7
+ Cursor,
8
+ dateRegExp,
9
+ dateToString,
10
+ focusableElements,
11
+ formatID,
12
+ getObjectValueFromKey,
13
+ getChildWindows,
14
+ getParentWindows,
15
+ getWindows,
16
+ isObject,
17
+ isTagName,
18
+ isValideID,
19
+ isValidNumber,
20
+ isValidString,
21
+ nextTick,
22
+ Page,
23
+ Paginate,
24
+ toKebabCase,
25
+ toString,
26
+ } from './';
27
+
28
+ describe('utils', () => {
29
+ describe('attributes', () => {
30
+ test('import', () => {
31
+ expect(ClassList).toBeDefined();
32
+ expect(createID).toBeDefined();
33
+ expect(formatID).toBeDefined();
34
+ expect(isValideID).toBeDefined();
35
+ expect(toKebabCase).toBeDefined();
36
+ });
37
+ });
38
+ describe('components', () => {
39
+ test('import', () => {
40
+ expect(nextTick).toBeDefined();
41
+ expect(Cursor).toBeDefined();
42
+ expect(Page).toBeDefined();
43
+ expect(Paginate).toBeDefined();
44
+ });
45
+ });
46
+ describe('date', () => {
47
+ test('import', () => {
48
+ expect(dateRegExp).toBeDefined();
49
+ expect(dateToString).toBeDefined();
50
+ });
51
+ });
52
+ describe('elements', () => {
53
+ test('import', () => {
54
+ expect(focusableElements).toBeDefined();
55
+ expect(isTagName).toBeDefined();
56
+ });
57
+ });
58
+ describe('number', () => {
59
+ test('import', () => {
60
+ expect(isValidNumber).toBeDefined();
61
+ });
62
+ });
63
+ describe('object', () => {
64
+ test('import', () => {
65
+ expect(isObject).toBeDefined();
66
+ expect(getObjectValueFromKey).toBeDefined();
67
+ });
68
+ });
69
+ describe('string', () => {
70
+ test('import', () => {
71
+ expect(allItemsAreString).toBeDefined();
72
+ expect(isValidString).toBeDefined();
73
+ expect(toString).toBeDefined();
74
+ expect(cleanString).toBeDefined();
75
+ });
76
+ });
77
+ describe('windows', () => {
78
+ test('import', () => {
79
+ expect(getWindows).toBeDefined();
80
+ expect(getParentWindows).toBeDefined();
81
+ expect(getChildWindows).toBeDefined();
82
+ });
83
+ });
84
+ });
@@ -0,0 +1,8 @@
1
+ export * from './attributes';
2
+ export * from './components';
3
+ export * from './date';
4
+ export * from './elements';
5
+ export * from './number';
6
+ export * from './object';
7
+ export * from './string';
8
+ export * from './windows';
@@ -0,0 +1 @@
1
+ export * from './number';
@@ -0,0 +1,14 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { isValidNumber } from './number';
3
+
4
+ describe('number', () => {
5
+ describe('isValidNumber', () => {
6
+ test.each(['', ' ', null, undefined, '1', {}, []])('Should return "false" for invalid values', value => {
7
+ expect(isValidNumber(value)).toEqual(false);
8
+ });
9
+
10
+ test.each([1, 1.0])('Should return "true" for valid value', value => {
11
+ expect(isValidNumber(value)).toEqual(true);
12
+ });
13
+ });
14
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Validate number
3
+ * @param value - value to check
4
+ * @returns `true` if number is valid
5
+ */
6
+ export const isValidNumber = (value: unknown): value is number => typeof value === 'number' && !Number.isNaN(value);
@@ -0,0 +1,2 @@
1
+ export * from './object';
2
+ export * from './object.conf';
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Define json object type
3
+ */
4
+ export type ObjectType = Record<string, unknown>;
@@ -0,0 +1,98 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { getObjectValueFromKey, isObject } from './object';
3
+
4
+ describe('object', () => {
5
+ describe('isObject', () => {
6
+ test.each([{}, new Date()])('Should validate an object', value => {
7
+ const res = isObject(value);
8
+ expect(res).toEqual(true);
9
+ });
10
+
11
+ test.each([undefined, null, '', 'batman', []])('Should NOT validate %s', value => {
12
+ const res = isObject(value);
13
+ expect(res).toEqual(false);
14
+ });
15
+ });
16
+
17
+ describe('getObjectValueFromKey', () => {
18
+ test.each([
19
+ {
20
+ object: { batman: 'hero' },
21
+ key: undefined,
22
+ result: undefined,
23
+ },
24
+ {
25
+ object: { batman: 'hero' },
26
+ key: { filter: 'batman' },
27
+ result: undefined,
28
+ },
29
+ {
30
+ object: { batman: 'hero' },
31
+ key: { filter: 'batman.test' },
32
+ defaultValue: 0,
33
+ result: 0,
34
+ },
35
+ {
36
+ object: 'batman',
37
+ key: 'batman',
38
+ result: undefined,
39
+ },
40
+ {
41
+ object: null,
42
+ key: 'batman',
43
+ result: undefined,
44
+ },
45
+ {
46
+ object: undefined,
47
+ key: 'batman',
48
+ result: undefined,
49
+ },
50
+ {
51
+ value: { heroes: { batman: 'hero' } },
52
+ key: 'heroes.robin',
53
+ result: undefined,
54
+ },
55
+ {
56
+ object: {},
57
+ key: 'batman',
58
+ result: undefined,
59
+ },
60
+ {
61
+ object: { batman: 'hero' },
62
+ key: { filter: 'batman' },
63
+ defaultValue: { batman: 'hero' },
64
+ result: { batman: 'hero' },
65
+ },
66
+ {
67
+ object: {},
68
+ key: 'batman',
69
+ defaultValue: {},
70
+ result: {},
71
+ },
72
+ {
73
+ object: 'batman',
74
+ key: 'batman',
75
+ defaultValue: 'batman',
76
+ result: 'batman',
77
+ },
78
+ {
79
+ value: { batman: undefined },
80
+ key: 'batman',
81
+ result: undefined,
82
+ },
83
+ {
84
+ value: { batman: 'hero' },
85
+ key: 'batman',
86
+ result: 'hero',
87
+ },
88
+ {
89
+ value: { heroes: { batman: 'hero' } },
90
+ key: 'heroes.batman',
91
+ result: 'hero',
92
+ },
93
+ ])('Should parse object %', ({ value, key, defaultValue, result }) => {
94
+ const res = getObjectValueFromKey(value, key as string, defaultValue);
95
+ expect(res).toEqual(result);
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Check if a value is of object type.
3
+ * @param object - The value to validate.
4
+ * @returns `true` if the value is a valid object (non-null and not an array), otherwise `false`.
5
+ */
6
+ export const isObject = <T>(object: unknown): object is T => typeof object === 'object' && !Array.isArray(object) && object !== null;
7
+
8
+ /**
9
+ * Get object value from key
10
+ * @param object - object to query
11
+ * @param path - path of the property to get. Nested keys are allowed with `.` separators (eg: 'key0.key1.key2' = object[key0][key1][key2])
12
+ * @param defaultValue - The value returned for `undefined` resolved values
13
+ * @returns object value
14
+ */
15
+ export const getObjectValueFromKey = <T, R>(object: T, path: string, defaultValue?: R): R | undefined => {
16
+ const separator = '.';
17
+ if (!isObject<Record<string, R>>(object) || typeof path !== 'string') {
18
+ return defaultValue;
19
+ }
20
+ const [current, ...next] = path.split(separator);
21
+ if (next.length) {
22
+ return getObjectValueFromKey(object[current as keyof T], next.join(separator));
23
+ } else {
24
+ return object[current as keyof T];
25
+ }
26
+ };
@@ -0,0 +1 @@
1
+ export * from './string';
@@ -0,0 +1,47 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { allItemsAreString, cleanString, isValidString, toString } from './string';
3
+
4
+ describe('string', () => {
5
+ describe('allItemsAreString', () => {
6
+ test.each([
7
+ { items: ['blu', 'bli', 'blo', 'bla'], expected: true },
8
+ { items: ['blu', 'bli', 'blo', { value: 'bla' }], expected: false },
9
+ ])('Should test if all otems are string: %s', ({ items, expected }) => {
10
+ expect(allItemsAreString(items as string[])).toEqual(expected);
11
+ });
12
+ });
13
+
14
+ describe('isValidString', () => {
15
+ test.each(['', ' ', null, undefined, 1, {}, []])('Should return "false" for invalid values', value => {
16
+ expect(isValidString(value)).toEqual(false);
17
+ });
18
+
19
+ test.each(['batman', 'batman '])('Should return "true" for valid value', value => {
20
+ expect(isValidString(value)).toEqual(true);
21
+ });
22
+ });
23
+
24
+ describe('toString', () => {
25
+ test.each([undefined, null, 1, 'string', '', [undefined], [{ value: null }], {}, ['string'], { hello: 'batman' }])('Should return stringified value, original %s', value => {
26
+ expect(toString(value)).toEqual(typeof value === 'object' || (Array.isArray(value) && typeof value !== 'string') ? JSON.stringify(value) : `${value}`);
27
+ });
28
+ });
29
+
30
+ describe('cleanString', () => {
31
+ test.each([
32
+ { string: 'batman', expected: 'batman' },
33
+ { string: 'BATMAN', expected: 'batman' },
34
+ { string: 'Batman', expected: 'batman' },
35
+ { string: ' batman ', expected: ' batman ' },
36
+ { string: ' batman', expected: ' batman' },
37
+ { string: 'batman ', expected: 'batman ' },
38
+ { string: 'âäàçéèêñù', expected: 'aaaceeenu' },
39
+ ])('Should format string properly', ({ string, expected }) => {
40
+ expect(cleanString(string)).toEqual(expected);
41
+ });
42
+
43
+ test('Should return orignal value when param is NOT a string', () => {
44
+ expect(cleanString(undefined as unknown as string)).toEqual(undefined);
45
+ });
46
+ });
47
+ });
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Typeguard function to check if all array items are strings.
3
+ * @param items - items to check
4
+ * @returns `true` if all items are strings
5
+ */
6
+ export const allItemsAreString = (items: unknown): items is string[] => Array.isArray(items) && items.every(item => typeof item === 'string');
7
+
8
+ /**
9
+ * Validate string
10
+ * @param value - value to check
11
+ * @returns `true` if string is valid
12
+ */
13
+ export const isValidString = (value: unknown): value is string => typeof value === 'string' && value.trim() !== '';
14
+
15
+ /**
16
+ * Stringify value
17
+ * @param value - value to stringify
18
+ * @returns stringified value
19
+ */
20
+ export const toString = (value: unknown): string => (typeof value === 'object' ? JSON.stringify(value) : String(value));
21
+
22
+ /**
23
+ * Cleans string characters by removing special characters and converting to lowercase.
24
+ * @param text - text to clean
25
+ * @returns cleaned string
26
+ * @example
27
+ * ```ts
28
+ * cleanString('âäàçéèêñù') // 'aaaceeenu'
29
+ * cleanString('BATMAN') // 'batman'
30
+ * ```
31
+ */
32
+ export const cleanString = (text: string): string =>
33
+ typeof text === 'string'
34
+ ? text
35
+ .toLocaleLowerCase()
36
+ .normalize('NFD')
37
+ .replaceAll(/[\u0300-\u036f]/g, '')
38
+ : text;
@@ -0,0 +1 @@
1
+ export * from './windows';
@@ -0,0 +1,65 @@
1
+ import { describe, expect, test, afterEach, vi } from 'vitest';
2
+ import { getWindows } from './windows';
3
+
4
+ describe('windows', () => {
5
+ describe('getWindows', () => {
6
+ const mockWindowFramesLength = vi.fn();
7
+ const mockWindowIndexZero = vi.fn();
8
+ Object.defineProperty(window, 'frames', {
9
+ value: {
10
+ get length() {
11
+ return mockWindowFramesLength();
12
+ },
13
+ get 0() {
14
+ return mockWindowIndexZero();
15
+ },
16
+ },
17
+ });
18
+
19
+ afterEach(() => {
20
+ vi.clearAllMocks();
21
+ });
22
+
23
+ test('Should return the window in an array', () => {
24
+ mockWindowFramesLength.mockReturnValue(0);
25
+ const localWindow: Window = window;
26
+ const windows: Window[] = getWindows(localWindow);
27
+ expect(windows).toHaveLength(1);
28
+ expect(windows[0]).toEqual(localWindow);
29
+ });
30
+
31
+ test('Should return the window with a parent', () => {
32
+ mockWindowFramesLength.mockReturnValue(0);
33
+ const spyWindowSelf = vi.spyOn(window, 'self', 'get').mockImplementationOnce(() => ({}) as Window & typeof globalThis);
34
+ const spyWindowParent = vi.spyOn(window, 'parent', 'get').mockImplementationOnce(() => window);
35
+
36
+ const windows = getWindows(window);
37
+ expect(windows).toHaveLength(2);
38
+ expect(spyWindowSelf).toBeCalledTimes(2);
39
+ expect(spyWindowParent).toBeCalledTimes(2);
40
+ });
41
+
42
+ test('Should return the window with a child', () => {
43
+ mockWindowFramesLength.mockReturnValueOnce(1).mockReturnValueOnce(1).mockReturnValue(0);
44
+ mockWindowIndexZero.mockReturnValueOnce(window);
45
+ const windows = getWindows(window);
46
+ expect(windows).toHaveLength(2);
47
+ });
48
+
49
+ test('Should throw an error when cannot access top parent window', () => {
50
+ mockWindowFramesLength.mockReturnValue(0);
51
+ const spyConsole = vi.spyOn(console, 'error');
52
+ const spyWindowSelf = vi.spyOn(window, 'self', 'get').mockImplementationOnce(() => ({}) as Window & typeof globalThis);
53
+ const spyWindowParent = vi.spyOn(window, 'parent', 'get').mockImplementationOnce(() => {
54
+ throw new Error('non');
55
+ });
56
+ const localWindow: Window = window;
57
+ const windows: Window[] = getWindows(localWindow);
58
+ expect(windows).toHaveLength(1);
59
+ expect(windows[0]).toEqual(localWindow);
60
+ expect(spyConsole).toBeCalledTimes(1);
61
+ expect(spyWindowSelf).toBeCalledTimes(1);
62
+ expect(spyWindowParent).toBeCalledTimes(1);
63
+ });
64
+ });
65
+ });
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Get windows
3
+ * @param localWindow - the window we are lookink for other windows
4
+ * @returns The list of windows found
5
+ */
6
+ export const getWindows = (localWindow: Window): Window[] => {
7
+ const parentWindows = getParentWindows(localWindow);
8
+ const childWindows = getChildWindows(localWindow);
9
+ return [localWindow, ...parentWindows, ...childWindows];
10
+ };
11
+
12
+ /**
13
+ * Get parent windows
14
+ * @param localWindow - the window we are lookink for parents
15
+ * @param windows - The list of allready found windows
16
+ * @returns The list of windows found
17
+ */
18
+ export const getParentWindows = (localWindow: Window, windows: Window[] = []): Window[] => {
19
+ // Check if is in iframe
20
+ if (localWindow.self !== localWindow.top) {
21
+ // Check if we have permission to access parent
22
+ try {
23
+ const parentWindow: Window = localWindow.parent;
24
+ if (parentWindow) {
25
+ windows.push(parentWindow);
26
+ return getParentWindows(parentWindow, windows);
27
+ } else return windows;
28
+ } catch (err) {
29
+ console.error('Different hosts between iframes:', err);
30
+ return windows;
31
+ }
32
+ }
33
+ return windows;
34
+ };
35
+
36
+ /**
37
+ * Get child windows
38
+ * @param localWindow - the window we are lookink for children
39
+ * @param windows - The list of allready found windows
40
+ * @returns The list of windows found
41
+ */
42
+ export const getChildWindows = (localWindow: Window, windows: Window[] = []): Window[] => {
43
+ if (localWindow.frames.length > 0) {
44
+ for (const childWindow of Array.from(localWindow.frames)) {
45
+ windows.push(childWindow);
46
+ getChildWindows(childWindow, windows);
47
+ }
48
+ }
49
+ return windows;
50
+ };
@@ -0,0 +1,8 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { mgModel } from '.';
3
+
4
+ describe('VUE', () => {
5
+ test('import', () => {
6
+ expect(mgModel).toBeDefined();
7
+ });
8
+ });
@@ -0,0 +1 @@
1
+ export * from './mg-model';
@@ -0,0 +1 @@
1
+ export * from './mg-model';
@@ -0,0 +1,6 @@
1
+ import { type Directive } from 'vue';
2
+
3
+ export interface App {
4
+ directive(name: string): Directive | undefined;
5
+ directive(name: string, directive: Directive): this;
6
+ }
@@ -0,0 +1,50 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { createLocalVue } from '@vue/test-utils';
3
+ import { mgModel } from '.';
4
+
5
+ const localVue = createLocalVue();
6
+
7
+ describe('mg-model', () => {
8
+ test('nominal', () => {
9
+ mgModel.install(localVue);
10
+ const directive = localVue.directive('mg-model');
11
+
12
+ expect(directive.bind).toBeDefined();
13
+ expect(directive.componentUpdated).toBeDefined();
14
+ expect(directive.unbind).toBeDefined();
15
+
16
+ const el = document.createElement('div');
17
+ const vnode = { context: {} };
18
+ const binding = { value: 'blu', expression: 'blu' };
19
+
20
+ directive.bind(el, binding, vnode);
21
+ expect(el.value).toEqual('blu');
22
+
23
+ expect(vnode.context[binding.expression]).not.toBeDefined();
24
+ el.dispatchEvent(new CustomEvent('value-change', { bubbles: true }));
25
+ expect(vnode.context[binding.expression]).toEqual(el.value);
26
+
27
+ directive.componentUpdated(el, { value: 'bla' });
28
+ expect(el.value).toEqual('bla');
29
+
30
+ directive.unbind(el);
31
+ el.dispatchEvent(new CustomEvent('value-change', { bubbles: true }));
32
+ expect(vnode.context[binding.expression]).not.toEqual(el.value);
33
+ });
34
+ test('without binding values', () => {
35
+ mgModel.install(localVue);
36
+ const directive = localVue.directive('mg-model');
37
+
38
+ const el = document.createElement('div');
39
+ const vnode = { context: {} };
40
+
41
+ directive.bind(el, {}, vnode);
42
+ expect(el.value).toEqual('');
43
+
44
+ directive.bind(el, { value: 'blu' }, vnode);
45
+ expect(el.value).toEqual('blu');
46
+
47
+ directive.componentUpdated(el, {});
48
+ expect(el.value).toEqual('');
49
+ });
50
+ });
@@ -0,0 +1,50 @@
1
+ import set from 'lodash/set';
2
+ import { type Directive, type VNode } from 'vue';
3
+ import { type App } from './mg-model.conf';
4
+
5
+ const wm = new WeakMap();
6
+
7
+ /**
8
+ * Custom `v-mg-model` directive
9
+ *
10
+ * @remarks
11
+ * Packages requirements
12
+ * - "\@mgdis/mg-components": \>=3
13
+ * - "vue": \>=2
14
+ * - "lodash"
15
+ *
16
+ * @example
17
+ * ```js
18
+ * // src/main.js
19
+ * import Vue from 'vue';
20
+ * import App from './App.vue';
21
+ * import { mgModel } from '@mgdis/core-ui-helpers/dist/vue';
22
+ *
23
+ * Vue.use(mgModel);
24
+ *
25
+ * new Vue({
26
+ * render: (h) => h(App),
27
+ * }).$mount('#app');
28
+ * ```
29
+ */
30
+ export const mgModel = {
31
+ install(Vue: App) {
32
+ Vue.directive('mg-model', <Directive<HTMLInputElement, string>>{
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ bind(el: HTMLInputElement, binding: any, vnode: VNode) {
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ const valueChangeHandler = (event: Event) => set(vnode.context as any, binding.expression, (event as unknown as { target: EventTarget & { value: unknown } }).target.value);
37
+ wm.set(el, valueChangeHandler);
38
+ el.value = binding.value ?? '';
39
+ el.addEventListener('value-change', valueChangeHandler);
40
+ },
41
+ componentUpdated(el, binding) {
42
+ el.value = binding.value ?? '';
43
+ },
44
+ unbind(el: HTMLElement) {
45
+ const valueChangeHandler = wm.get(el);
46
+ el.removeEventListener('value-change', valueChangeHandler);
47
+ },
48
+ });
49
+ },
50
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "@core-ui/tsconfig/base.node.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src",
6
+ "composite": false,
7
+ "declaration": false,
8
+ "declarationMap": false,
9
+ "incremental": false,
10
+ "lib": ["dom", "es2021"],
11
+ "resolveJsonModule": false,
12
+ "moduleResolution": "Bundler",
13
+ "module": "ES6"
14
+ },
15
+ "include": ["src"],
16
+ "exclude": ["src/**/*.spec.ts"]
17
+ }
package/typedoc.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": ["@core-ui/typedoc-config/base.json"],
3
+ "out": "docs",
4
+ "entryPoints": [
5
+ "./src/angular/index.ts",
6
+ "./src/locale/index.ts",
7
+ "./src/playwright/index.ts",
8
+ "./src/stencil/index.ts",
9
+ "./src/storybook/index.ts",
10
+ "./src/tests/index.ts",
11
+ "./src/utils/index.ts",
12
+ "./src/vue/index.ts"
13
+ ]
14
+ }