@equinor/fusion-framework-module-navigation 0.0.0-next-20230915185612
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/CHANGELOG.md +200 -0
- package/LICENSE +21 -0
- package/dist/esm/configurator.js +3 -0
- package/dist/esm/configurator.js.map +1 -0
- package/dist/esm/createHistory.js +15 -0
- package/dist/esm/createHistory.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lib/index.js +2 -0
- package/dist/esm/lib/index.js.map +1 -0
- package/dist/esm/lib/provider/INavigationProvider.js +2 -0
- package/dist/esm/lib/provider/INavigationProvider.js.map +1 -0
- package/dist/esm/lib/provider/NavigationProvider.js +93 -0
- package/dist/esm/lib/provider/NavigationProvider.js.map +1 -0
- package/dist/esm/lib/provider/index.js +2 -0
- package/dist/esm/lib/provider/index.js.map +1 -0
- package/dist/esm/module.js +37 -0
- package/dist/esm/module.js.map +1 -0
- package/dist/esm/navigator.js +93 -0
- package/dist/esm/navigator.js.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/configurator.d.ts +9 -0
- package/dist/types/createHistory.d.ts +4 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/lib/index.d.ts +1 -0
- package/dist/types/lib/provider/INavigationProvider.d.ts +17 -0
- package/dist/types/lib/provider/NavigationProvider.d.ts +22 -0
- package/dist/types/lib/provider/index.d.ts +2 -0
- package/dist/types/module.d.ts +13 -0
- package/dist/types/navigator.d.ts +32 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/version.d.ts +1 -0
- package/package.json +45 -0
- package/src/configurator.ts +11 -0
- package/src/createHistory.ts +18 -0
- package/src/index.ts +10 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/provider/INavigationProvider.ts +46 -0
- package/src/lib/provider/NavigationProvider.ts +119 -0
- package/src/lib/provider/index.ts +2 -0
- package/src/module.ts +64 -0
- package/src/navigator.ts +123 -0
- package/src/types.ts +4 -0
- package/src/version.ts +2 -0
- package/tsconfig.json +20 -0
package/src/module.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IModuleConfigurator,
|
|
3
|
+
IModulesConfigurator,
|
|
4
|
+
Module,
|
|
5
|
+
ModuleConfigType,
|
|
6
|
+
ModuleInstance,
|
|
7
|
+
SemanticVersion,
|
|
8
|
+
} from '@equinor/fusion-framework-module';
|
|
9
|
+
import { type INavigationConfigurator, NavigationConfigurator } from './configurator';
|
|
10
|
+
import { createHistory } from './createHistory';
|
|
11
|
+
import { type INavigationProvider, NavigationProvider } from './lib';
|
|
12
|
+
|
|
13
|
+
import { version } from './version';
|
|
14
|
+
|
|
15
|
+
export const moduleKey = 'navigation';
|
|
16
|
+
|
|
17
|
+
export type NavigationModule = Module<
|
|
18
|
+
typeof moduleKey,
|
|
19
|
+
INavigationProvider,
|
|
20
|
+
INavigationConfigurator
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
export const module: NavigationModule = {
|
|
24
|
+
version: new SemanticVersion(version),
|
|
25
|
+
name: moduleKey,
|
|
26
|
+
configure: (ref?: unknown) => {
|
|
27
|
+
const configurator = new NavigationConfigurator();
|
|
28
|
+
if (ref) {
|
|
29
|
+
configurator.history = (ref as ModuleInstance).navigation.navigator;
|
|
30
|
+
}
|
|
31
|
+
return configurator;
|
|
32
|
+
},
|
|
33
|
+
initialize: ({ config }) => {
|
|
34
|
+
config.history ??= createHistory();
|
|
35
|
+
return new NavigationProvider({ version, config });
|
|
36
|
+
},
|
|
37
|
+
dispose({ instance }) {
|
|
38
|
+
instance.dispose();
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const enableNavigation = <TRef = unknown>(
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
configurator: IModulesConfigurator<any, any>,
|
|
45
|
+
basenameOrOptions?: string | Omit<IModuleConfigurator<NavigationModule, TRef>, 'module'>,
|
|
46
|
+
): void => {
|
|
47
|
+
const options =
|
|
48
|
+
typeof basenameOrOptions === 'string'
|
|
49
|
+
? {
|
|
50
|
+
configure: (config: ModuleConfigType<NavigationModule>) => {
|
|
51
|
+
config.basename = basenameOrOptions;
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
: basenameOrOptions;
|
|
55
|
+
configurator.addConfig({ module, ...options });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export default module;
|
|
59
|
+
|
|
60
|
+
declare module '@equinor/fusion-framework-module' {
|
|
61
|
+
interface Modules {
|
|
62
|
+
navigation: NavigationModule;
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/navigator.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { History, To, Path } from '@remix-run/router';
|
|
2
|
+
import { BehaviorSubject, Observable, skip, Subscription } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
import type { NavigationUpdate, NavigationListener } from './types';
|
|
5
|
+
|
|
6
|
+
export interface INavigator<T extends NavigationUpdate = NavigationUpdate>
|
|
7
|
+
extends Observable<T>,
|
|
8
|
+
History {
|
|
9
|
+
readonly basename?: string;
|
|
10
|
+
readonly origin?: string;
|
|
11
|
+
readonly value: NavigationUpdate;
|
|
12
|
+
readonly location: NavigationUpdate['location'];
|
|
13
|
+
dispose: VoidFunction;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class Navigator<T extends NavigationUpdate = NavigationUpdate>
|
|
17
|
+
extends Observable<T>
|
|
18
|
+
implements INavigator<T>
|
|
19
|
+
{
|
|
20
|
+
#history: History;
|
|
21
|
+
#baseName?: string;
|
|
22
|
+
#origin?: string = window.location.origin;
|
|
23
|
+
#subscriptions = new Subscription();
|
|
24
|
+
#state: BehaviorSubject<T>;
|
|
25
|
+
|
|
26
|
+
#logger: typeof console;
|
|
27
|
+
|
|
28
|
+
get basename(): string | undefined {
|
|
29
|
+
return this.#baseName;
|
|
30
|
+
}
|
|
31
|
+
get origin(): string | undefined {
|
|
32
|
+
return this.#origin;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get state(): Observable<T> {
|
|
36
|
+
return this.#state.asObservable();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get value(): T {
|
|
40
|
+
return this.#state.value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get location(): T['location'] {
|
|
44
|
+
return this.value.location;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get action(): T['action'] {
|
|
48
|
+
return this.value.action;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
constructor(args: { basename?: string; history: History; logger?: any }) {
|
|
53
|
+
super((subscriber) => this.#state.subscribe(subscriber));
|
|
54
|
+
const { basename, history } = args;
|
|
55
|
+
this.#logger = args.logger || console;
|
|
56
|
+
this.#baseName = basename;
|
|
57
|
+
this.#history = history;
|
|
58
|
+
this.#state = new BehaviorSubject<T>({
|
|
59
|
+
action: this.#history.action,
|
|
60
|
+
location: this.#history.location,
|
|
61
|
+
delta: null,
|
|
62
|
+
} as T);
|
|
63
|
+
this.#subscriptions.add(
|
|
64
|
+
this.#history.listen((update) => {
|
|
65
|
+
if (update.location.key !== this.#state.value.location.key) {
|
|
66
|
+
this.#logger.debug(
|
|
67
|
+
'Navigator::constructor',
|
|
68
|
+
'history changed',
|
|
69
|
+
update,
|
|
70
|
+
this.#state.value,
|
|
71
|
+
);
|
|
72
|
+
this.#state.next(update as T);
|
|
73
|
+
}
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public encodeLocation(to: To): Path {
|
|
79
|
+
return this.#history.encodeLocation(to);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public listen(listener: NavigationListener): VoidFunction {
|
|
83
|
+
this.#logger.debug('Navigator::listen', listener);
|
|
84
|
+
const subscription = this.#state
|
|
85
|
+
.pipe(
|
|
86
|
+
/** skip the first, since we only want up-coming events */
|
|
87
|
+
skip(1),
|
|
88
|
+
// TODO filter only locations within this basename
|
|
89
|
+
)
|
|
90
|
+
.subscribe((x) => {
|
|
91
|
+
this.#logger.debug('Navigator::listen[onchange]');
|
|
92
|
+
listener(x);
|
|
93
|
+
});
|
|
94
|
+
return () => {
|
|
95
|
+
this.#logger.debug('Navigator::listen[unsubscribe]');
|
|
96
|
+
return subscription.unsubscribe();
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public createHref(to: To): string {
|
|
101
|
+
return this.#history.createHref(to);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public go(delta: number): void {
|
|
105
|
+
this.#history.go(delta);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public push(to: To, state?: unknown): void {
|
|
109
|
+
return this.#history.push(to, state);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public replace(to: To, state?: unknown): void {
|
|
113
|
+
return this.#history.replace(to, state);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public createURL(to: To): URL {
|
|
117
|
+
return this.#history.createURL(to);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
dispose() {
|
|
121
|
+
this.#subscriptions.unsubscribe();
|
|
122
|
+
}
|
|
123
|
+
}
|
package/src/types.ts
ADDED
package/src/version.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist/esm",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"declarationDir": "./dist/types",
|
|
7
|
+
},
|
|
8
|
+
"references": [
|
|
9
|
+
{
|
|
10
|
+
"path": "../module"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
"include": [
|
|
14
|
+
"src/**/*",
|
|
15
|
+
],
|
|
16
|
+
"exclude": [
|
|
17
|
+
"node_modules",
|
|
18
|
+
"lib"
|
|
19
|
+
]
|
|
20
|
+
}
|