Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 6cde9c9

Browse files
Scott Mathismathisscott
Scott Mathis
authored andcommitted
feat(modal): adding global state service
• added service to manage and update global state • this serves as the foundation for the focus trap and registry fixes Signed-off-by: Scott Mathis <[email protected]>
1 parent 2395ab9 commit 6cde9c9

File tree

6 files changed

+104
-16
lines changed

6 files changed

+104
-16
lines changed

packages/core/src/internal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from './decorators/id.js';
2323
export * from './decorators/i18n.js';
2424
export * from './decorators/global-style.js';
2525
export * from './services/focus-trap-tracker.service.js';
26+
export * from './services/global.service.js';
2627
export * from './services/i18n.service.js';
2728
export * from './services/log.service.js';
2829
export * from './utils/async.js';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
3+
* This software is released under MIT license.
4+
* The full license information can be found in LICENSE in the root directory of this project.
5+
*/
6+
7+
import { GlobalState } from './global.service.js';
8+
import { setupCDSGlobal } from '../utils/global.js';
9+
import { LogService } from './log.service.js';
10+
11+
function resetGlobalState() {
12+
window.CDS.state = {};
13+
}
14+
15+
describe('Global State Service', () => {
16+
beforeAll(() => {
17+
setupCDSGlobal();
18+
});
19+
20+
afterEach(() => {
21+
resetGlobalState();
22+
});
23+
24+
it('.state should return all global state', () => {
25+
window.CDS.state.focusTraps = ['ohai'];
26+
const current = GlobalState.state;
27+
expect(current.focusTraps[0]).toBe('ohai');
28+
window.CDS.state = {};
29+
});
30+
31+
it('getValue should return value of state key', () => {
32+
(window.CDS.state as any).focusTraps = ['yolo', 'howdy'];
33+
const testme = GlobalState.getValue('focusTraps');
34+
expect(testme).toEqual(['yolo', 'howdy']);
35+
});
36+
37+
it('setValue should assign value to key', () => {
38+
(window.CDS.state as any).focusTraps = ['yolo', 'howdy'];
39+
GlobalState.setValue('focusTraps', ['ohai']);
40+
expect(GlobalState.getValue('focusTraps')).toEqual(['ohai']);
41+
});
42+
43+
it('.log() should log state to the console', () => {
44+
const spy = spyOn(LogService, 'log');
45+
GlobalState.log();
46+
expect(spy).toHaveBeenCalled();
47+
});
48+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
3+
* This software is released under MIT license.
4+
* The full license information can be found in LICENSE in the root directory of this project.
5+
*/
6+
7+
import { CDSState, setupCDSGlobal } from '../utils/global.js';
8+
import { LogService } from './log.service.js';
9+
10+
export class GlobalState {
11+
static get state(): Readonly<CDSState> {
12+
setupCDSGlobal();
13+
return window?.CDS?.state;
14+
}
15+
16+
static getValue(key: string): any {
17+
setupCDSGlobal();
18+
return (window.CDS.state as any)[key];
19+
}
20+
21+
static setValue(key: string, val: any) {
22+
setupCDSGlobal();
23+
(window.CDS.state as any)[key] = val;
24+
}
25+
26+
static log() {
27+
setupCDSGlobal();
28+
LogService.log(JSON.stringify(window?.CDS?.state, null, 2));
29+
}
30+
}

packages/core/src/internal/utils/framework.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2020 VMware, Inc. All Rights Reserved.
2+
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
33
* This software is released under MIT license.
44
* The full license information can be found in LICENSE in the root directory of this project.
55
*/
@@ -60,5 +60,5 @@ export function getVueVersion(useCache = true) {
6060
}
6161

6262
export function isStorybook() {
63-
return window.location.href.includes('localhost:6006');
63+
return window?.location?.href?.includes('localhost:6006');
6464
}

packages/core/src/internal/utils/global.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface CDSGlobal {
2020
/** Set to true for production env to disable dev time logging and tooling */
2121
production: boolean;
2222
};
23+
state?: CDSState;
2324
}
2425

2526
export interface CDSLog {
@@ -36,6 +37,9 @@ export interface CDSLog {
3637
};
3738
}
3839

40+
export interface CDSState {
41+
focusTraps?: string[];
42+
}
3943
declare global {
4044
interface Window {
4145
CDS: CDSGlobal;
@@ -50,22 +54,26 @@ export function setupCDSGlobal() {
5054
}
5155

5256
function getVersion() {
53-
const log: CDSLog = {
54-
versions: window.CDS._version,
55-
environment: window.CDS.environment,
56-
userAgent: navigator.userAgent,
57-
supports: window.CDS._supports,
58-
angularVersion: getAngularVersion(false),
59-
angularJSVersion: getAngularJSVersion(false),
60-
reactVersion: getReactVersion(false),
61-
vueVersion: getVueVersion(false),
62-
loadedElements: window.CDS._loadedElements,
63-
};
64-
return log;
57+
if (isBrowser()) {
58+
const log: CDSLog = {
59+
versions: window.CDS._version,
60+
environment: window.CDS.environment,
61+
userAgent: navigator.userAgent,
62+
supports: window.CDS._supports,
63+
angularVersion: getAngularVersion(false),
64+
angularJSVersion: getAngularJSVersion(false),
65+
reactVersion: getReactVersion(false),
66+
vueVersion: getVueVersion(false),
67+
loadedElements: window.CDS._loadedElements,
68+
};
69+
return log;
70+
}
71+
72+
return void 0;
6573
}
6674

6775
function logVersion() {
68-
console.log(JSON.stringify(getVersion(), null, 2));
76+
LogService.log(JSON.stringify(getVersion(), null, 2));
6977
}
7078

7179
function initializeCDSGlobal() {
@@ -77,6 +85,7 @@ function initializeCDSGlobal() {
7785
environment: {
7886
production: false,
7987
},
88+
state: {},
8089
getVersion,
8190
logVersion,
8291
};

packages/core/src/internal/utils/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const addElementToRegistry = curryN(
2020
registry.define(tagName, applyCSSGapShim(elementClass));
2121
setupCDSGlobal();
2222

23-
if (!window.CDS._loadedElements.some(i => i === tagName)) {
23+
if (window && !window.CDS._loadedElements.some(i => i === tagName)) {
2424
window.CDS._loadedElements.push(tagName);
2525
}
2626
}

0 commit comments

Comments
 (0)