-
Notifications
You must be signed in to change notification settings - Fork 215
/
environments.js
131 lines (111 loc) · 3.18 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
/*
*
* 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 no-invalid-this */
/* eslint-disable require-jsdoc */
import * as Utils from '../utils.js';
import * as API from '../api.js';
let environments = {
local_ditto: {
api_uri: 'http://localhost:8080',
solutionId: null,
bearer: null,
usernamePassword: 'ditto:ditto',
usernamePasswordDevOps: 'devops:foobar',
useBasicAuth: true,
},
eclipse_sandbox: {
api_uri: 'https://ditto.eclipseprojects.io',
bearer: '',
usernamePassword: null,
useBasicAuth: false,
},
};
let theEnv;
let settingsEditor;
let dom = {
environmentSelector: null,
};
let observers = [];
export function current() {
return environments[theEnv];
};
export function addChangeListener(observer) {
observers.push(observer);
}
function notifyAll() {
observers.forEach(observer => observer.call());
}
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');
settingsEditor.setValue(JSON.stringify(environments, null, 2), -1);
environmentsJsonChanged();
settingsEditor.on('blur', () => {
environments = JSON.parse(settingsEditor.getValue());
environmentsJsonChanged();
});
document.getElementById('tabEnvironments').onclick = () => {
settingsEditor.setValue(JSON.stringify(environments, null, 2), -1);
};
document.querySelectorAll('.mainUser,.devOpsUser').forEach((menuTab) => {
menuTab.addEventListener('click', (event) => {
API.setAuthHeader(event.target.parentNode.classList.contains('devOpsUser'));
});
});
document.getElementById('environmentSelector').onchange = (event) => {
theEnv = event.target.value;
activateEnvironment();
};
}
export function togglePinnedThing(evt) {
if (evt.target.checked) {
current().pinnedThings.push(this.id);
} else {
const index = current().pinnedThings.indexOf(this.id);
if (index > -1) {
current().pinnedThings.splice(index, 1);
};
};
environmentsJsonChanged();
};
export function environmentsJsonChanged() {
localStorage.setItem('ditto-ui-env', JSON.stringify(environments));
dom.environmentSelector.innerHTML = '';
if (theEnv && !current()) {
theEnv = null;
};
for (const key of Object.keys(environments)) {
let option = document.createElement('option');
option.text = key;
dom.environmentSelector.add(option);
if (!theEnv) {
theEnv = key;
};
};
dom.environmentSelector.value = theEnv;
activateEnvironment();
}
function activateEnvironment() {
if (!current()['pinnedThings']) {
current().pinnedThings = [];
};
notifyAll();
}