-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
175 lines (154 loc) · 5.55 KB
/
main.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
/* eslint-disable require-jsdoc */
/* eslint-disable no-invalid-this */
// import $ from 'jquery';
// global.$ = global.jQuery = $;
import * as Things from './modules/things.js';
import * as Features from './modules/features.js';
import * as Policies from './modules/policies.js';
import * as Connections from './modules/connections.js';
import * as Environments from './modules/environments.js';
let ws;
$(document).ready(async function() {
document.getElementById('thingsHtml').innerHTML = await (await fetch('modules/things.html')).text();
document.getElementById('featuresHtml').innerHTML = await (await fetch('modules/features.html')).text();
document.getElementById('policyHtml').innerHTML = await (await fetch('modules/policies.html')).text();
document.getElementById('connectionsHtml').innerHTML = await (await fetch('modules/connections.html')).text();
document.getElementById('environmentsHtml').innerHTML = await (await fetch('modules/environments.html')).text();
$('.nav-item').on('click', function() {
$(this).addClass('active').siblings().removeClass('active');
});
$('.table').on('click', 'tr', function() {
$(this).toggleClass('bg-info').siblings().removeClass('bg-info');
});
// make ace editor resize when user changes height
$('.resizable_pane').mouseup(function(event) {
const oldHeight = $(this).data('oldHeight');
if (oldHeight && oldHeight != $(this).height()) {
window.dispatchEvent(new Event('resize'));
}
$(this).data('oldHeight', $(this).height());
});
Things.ready();
Features.ready();
Policies.ready();
Connections.ready();
Environments.ready();
});
export function openWebSocket() {
try {
ws = new WebSocket(createWSURI(Environments.getCurrentEnv()));
ws.onopen = function() {
ws.onmessage = onMessage;
ws.onerror = onMessage;
ws.onclose = onClose;
ws.send('START-SEND-EVENTS');
ws.send('START-SEND-MESSAGES');
ws.send('START-SEND-LIVE-EVENTS');
ws.send('START-SEND-LIVE-COMMANDS');
};
} catch (error) {
console.log(error);
}
};
export function createWSURI(environment) {
const wsuri = new URL(environment.api_uri.replace(/https/, 'wss').replace(/http/, 'ws'));
wsuri.pathname = '/ws/2';
if (environment.useBasicAuth) {
wsuri.username = environment.usernamePassword.split(':')[0];
wsuri.password = environment.usernamePassword.split(':')[1];
} else {
wsuri.search = '?access_token=' + environment.bearer;
}
return wsuri.toString();
};
function onClose() {
console.log('CLOSE: WebSocket was closed');
};
function onMessage(message) {
Features.onMessage(message);
};
// function buildfilterEditFilter() {
// const query = $('#filterEdit').val();
// const filter = Environments.getCurrentEnv().fieldList.map(
// (field) => 'like(' + field.path + ',' + '"' + query + '*")').toString();
// if (Environments.getCurrentEnv().fieldList.length < 2) {
// return filter;
// } else {
// return 'or(' + filter + ')';
// };
// };
// function modifyThing(method, type, key, value) {
// if (!key) { showError(null, 'Error', 'FeatureId is empty'); return; }
// $.ajax(Environments.getCurrentEnv().api_uri + '/api/2/things/' + Things.theThing.thingId + type + key, {
// type: method,
// contentType: 'application/json',
// data: value,
// success: ,
// error: showError
// });
// };
export function callDittoREST(method, path, body, success) {
$.ajax(Environments.getCurrentEnv().api_uri + '/api/2' + path, {
type: method,
contentType: 'application/json',
data: body,
success: success,
error: showError});
};
export const addTableRow = function(table, key, value, selected, withClipBoardCopy) {
const row = table.insertRow();
row.insertCell(0).innerHTML = key;
if (value) {
row.insertCell(1).innerHTML = value;
}
if (selected) {
row.classList.add('bg-info');
}
if (withClipBoardCopy) {
addClipboardCopyToRow(row);
}
};
export function addCheckboxToRow(row, id, checked, onToggle) {
const td = row.insertCell(0);
td.style.verticalAlign = 'middle';
const checkBox = document.createElement('input');
checkBox.type = 'checkbox';
checkBox.id = id;
checkBox.checked = checked;
checkBox.onchange = onToggle;
td.append(checkBox);
}
export function addClipboardCopyToRow(row) {
const td = row.insertCell();
td.style.textAlign = 'right';
const button = document.createElement('button');
button.classList.add('btn', 'btn-sm');
button.style.padding = 0;
button.innerHTML = `<i class="fa-regular fa-clone"></i>`;
button.onclick = (evt) => {
const td = evt.currentTarget.parentNode.previousSibling;
navigator.clipboard.writeText(td.innerText);
};
td.appendChild(button);
}
export function addRadioButton(target, groupName, value, checked) {
const radio = document.createElement('div');
radio.innerHTML = `<div class="form-check">
<input class="form-check-input" type="radio" id="${ value}" name="${ groupName}" value="${ value}"
${checked ? 'checked' : ''}>
<label class="form-check-label" for="${ value}">
${ value}
</label>
</div>`;
target.appendChild(radio);
}
export function showError(xhr, status, message) {
$('#errorHeader').text(xhr ? xhr.status : status);
$('#errorBody').text(xhr ? (xhr.responseJSON ? JSON.stringify(xhr.responseJSON, null, 2) : xhr.statusText) : message);
$('#errorToast').toast('show');
}
export function showSuccess(data, status, xhr) {
$('#successHeader').text(xhr.status ? xhr.status : status);
$('#successBody').text(status);
$('#successToast').toast('show');
}