/
_internalLinkManager.js
131 lines (112 loc) · 3.73 KB
/
_internalLinkManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { GenericHelpers } from '../utilities/helpers';
import { LuigiCoreAPIBase } from './baseClass';
export class linkManager extends LuigiCoreAPIBase {
/**
* @private
*/
constructor(values) {
// @param {object} values TODO: is it necessary at all, where is it used?
super();
Object.assign(this, values);
this.options = {
preserveView: false,
nodeParams: {},
errorSkipNavigation: false,
fromContext: null,
fromClosestContext: false,
relative: false,
link: ''
};
}
async navigate(path, preserveView, modalSettings, splitViewSettings, drawerSettings) {
if (this.options.errorSkipNavigation) {
this.options.errorSkipNavigation = false;
return Promise.reject(new Error('navigation skipped'));
}
this.options.preserveView = preserveView;
const relativePath = path[0] !== '/';
if (path === '/' && (modalSettings || splitViewSettings || drawerSettings)) {
console.warn('Navigation with an absolute path prevented.');
return Promise.reject(new Error('Navigation with an absolute path prevented.'));
}
const remotePromise = GenericHelpers.createRemotePromise();
const navigationOpenMsg = {
msg: 'luigi.navigation.open',
params: Object.assign(this.options, {
link: path,
relative: relativePath,
modal: modalSettings,
splitView: splitViewSettings,
drawer: drawerSettings
}),
remotePromiseId: remotePromise.id
};
this.sendPostMessageToLuigiCore(navigationOpenMsg);
return remotePromise;
}
/**
* This function navigates to a modal after adding the onClosePromise that handles the callback for when the modal is closed.
* @param {string} path the navigation path to open in the modal
* @param {Object} modalSettings settings to configure the modal's title, size, width and height
* @param {Function} onCloseCallback callback function called upon closing the openened modal
*/
openAsModal(path, modalSettings = {}, onCloseCallback) {
if (GenericHelpers.isFunction(onCloseCallback)) {
const onClosePromise = GenericHelpers.createRemotePromise();
onClosePromise.then(value => {
onCloseCallback(value);
});
modalSettings.onClosePromiseId = onClosePromise.id;
}
this.navigate(path, true, modalSettings);
}
openAsDrawer(path, drawerSettings = {}) {
this.navigate(path, true, undefined, undefined, drawerSettings);
}
openAsSplitView(path, splitViewSettings = {}) {
this.navigate(path, true, undefined, splitViewSettings);
return Luigi.splitView.splitViewHandle;
}
fromContext(navigationContext) {
this.options.fromContext = navigationContext;
return this;
}
fromClosestContext() {
this.options.fromContext = null;
this.options.fromClosestContext = true;
return this;
}
fromVirtualTreeRoot() {
this.options.fromContext = null;
this.options.fromClosestContext = false;
this.options.fromVirtualTreeRoot = true;
return this;
}
withParams(nodeParams) {
if (nodeParams) {
Object.assign(this.options.nodeParams, nodeParams);
}
return this;
}
pathExists(path) {
if (GenericHelpers.isFunction(Luigi.pathExists)) {
return Luigi.pathExists(path);
} else {
console.error(
'Luigi.navigation().pathExists(path) is only available inside your configuration, after the configuration was initialized with Luigi.setConfig().'
);
}
}
hasBack() {
return Luigi.hasBack();
}
goBack(goBackValue) {
this.sendPostMessageToLuigiCore({
msg: 'luigi.navigation.back',
goBackContext: goBackValue && JSON.stringify(goBackValue)
});
}
sendPostMessageToLuigiCore(msg) {
window.postMessage(msg, '*');
}
}