-
Notifications
You must be signed in to change notification settings - Fork 219
/
environments.js
184 lines (156 loc) · 5.34 KB
/
environments.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
/* eslint-disable arrow-parens */
/* eslint-disable prefer-const */
/* eslint-disable require-jsdoc */
import * as Authorization from '../environments/authorization.js';
import * as Utils from '../utils.js';
let environments = {
local_ditto: {
api_uri: 'http://localhost:8080',
solutionId: null,
bearer: null,
usernamePassword: 'ditto:ditto',
usernamePasswordDevOps: 'devops:foobar',
useBasicAuth: true,
useDittoPreAuthenticatedAuth: false,
dittoPreAuthenticatedUsername: null,
},
local_ditto_ide: {
api_uri: 'http://localhost:8080',
solutionId: null,
bearer: null,
usernamePassword: null,
usernamePasswordDevOps: null,
useBasicAuth: false,
useDittoPreAuthenticatedAuth: true,
dittoPreAuthenticatedUsername: "pre:ditto",
},
ditto_sandbox: {
api_uri: 'https://ditto.eclipseprojects.io',
solutionId: null,
bearer: null,
usernamePassword: "ditto:ditto",
usernamePasswordDevOps: null,
useBasicAuth: true,
useDittoPreAuthenticatedAuth: false,
dittoPreAuthenticatedUsername: null,
},
};
let settingsEditor;
let dom = {
environmentSelector: null,
buttonCreateEnvironment: null,
buttonDeleteEnvironment: null,
buttonUpdateFields: null,
buttonUpdateJson: null,
inputEnvironmentName: null,
inputApiUri: null,
tbodyEnvironments: null,
};
let observers = [];
export function current() {
return environments[dom.environmentSelector.value];
};
export function addChangeListener(observer) {
observers.push(observer);
}
function notifyAll(modifiedField) {
// Notify Authorization first to set right auth header
Authorization.onEnvironmentChanged(modifiedField);
// Notify others
observers.forEach(observer => observer.call(null, modifiedField));
}
export function ready() {
Utils.getAllElementsById(dom);
const restoredEnv = localStorage.getItem('ditto-ui-env');
if (restoredEnv) {
environments = JSON.parse(restoredEnv);
}
settingsEditor = ace.edit('settingsEditor');
settingsEditor.session.setMode('ace/mode/json');
dom.buttonUpdateJson.onclick = () => {
environments[dom.inputEnvironmentName.value] = JSON.parse(settingsEditor.getValue());
environmentsJsonChanged();
};
dom.buttonUpdateFields.onclick = () => {
environments[dom.inputEnvironmentName.value].api_uri = dom.inputApiUri.value;
environmentsJsonChanged();
};
dom.tbodyEnvironments.addEventListener('click', (event) => {
if (event.target && event.target.tagName === 'TD') {
dom.inputEnvironmentName.value = event.target.parentNode.id;
updateEnvEditors();
}
});
dom.buttonCreateEnvironment.onclick = () => {
Utils.assert(dom.inputEnvironmentName.value, 'Please provide an environment name', dom.inputEnvironmentName);
Utils.assert(!environments[dom.inputEnvironmentName.value], 'Name already used', dom.inputEnvironmentName);
environments[dom.inputEnvironmentName.value] = {
api_uri: '',
};
environmentsJsonChanged();
};
dom.buttonDeleteEnvironment.onclick = () => {
Utils.assert(dom.inputEnvironmentName.value, 'No environment selected');
Utils.assert(Object.keys(environments).length >= 2, 'At least one environment is required');
delete environments[dom.inputEnvironmentName.value];
dom.inputEnvironmentName.value = null;
environmentsJsonChanged();
};
// Ensure that ace editor to refresh when updated while hidden
document.querySelector('a[data-bs-target="#tabEnvJson"]').addEventListener('shown.bs.tab', () => {
settingsEditor.renderer.updateFull();
});
document.getElementById('environmentSelector').onchange = () => {
notifyAll();
};
environmentsJsonChanged();
}
export function environmentsJsonChanged(modifiedField) {
localStorage.setItem('ditto-ui-env', JSON.stringify(environments));
updateEnvSelector();
updateEnvEditors();
updateEnvTable();
notifyAll(modifiedField);
function updateEnvSelector() {
let activeEnvironment = dom.environmentSelector.value;
if (!activeEnvironment || !environments[activeEnvironment]) {
activeEnvironment = Object.keys(environments)[0];
};
dom.environmentSelector.innerHTML = '';
Object.keys(environments).forEach((key) => {
let option = document.createElement('option');
option.text = key;
dom.environmentSelector.add(option);
});
dom.environmentSelector.value = activeEnvironment;
}
function updateEnvTable() {
dom.tbodyEnvironments.innerHTML = '';
Object.keys(environments).forEach((key) => {
Utils.addTableRow(dom.tbodyEnvironments, key, key === dom.inputEnvironmentName.value);
});
}
}
function updateEnvEditors() {
const selectedEnvironment = environments[dom.inputEnvironmentName.value];
if (selectedEnvironment) {
settingsEditor.setValue(JSON.stringify(selectedEnvironment, null, 2), -1);
dom.inputApiUri.value = selectedEnvironment.api_uri;
} else {
dom.inputEnvironmentName.value = null;
settingsEditor.setValue('');
dom.inputApiUri.value = null;
}
}