-
Notifications
You must be signed in to change notification settings - Fork 224
/
featureMessages.ts
176 lines (155 loc) · 6.11 KB
/
featureMessages.ts
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
/*
* 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 require-jsdoc */
import * as API from '../api.js';
import * as Environments from '../environments/environments.js';
import * as Utils from '../utils.js';
import featureMessagesHTML from './featureMessages.html';
import * as Features from './features.js';
import * as Things from './things.js';
let theFeatureId;
const dom = {
inputMessageSubject: null,
inputMessageTimeout: null,
inputMessageTemplate: null,
buttonMessageSend: null,
buttonMessageFavorite: null,
ulMessageTemplates: null,
favIconMessage: null,
tableValidationFeature: null,
};
let acePayload;
let aceResponse;
/**
* Initializes components. Should be called after DOMContentLoaded event
*/
export async function ready() {
Environments.addChangeListener(onEnvironmentChanged);
Features.addChangeListener(onFeatureChanged);
Utils.addTab(
document.getElementById('tabItemsFeatures'),
document.getElementById('tabContentFeatures'),
'Send Message',
featureMessagesHTML,
);
Utils.getAllElementsById(dom);
acePayload = Utils.createAceEditor('acePayload', 'ace/mode/json');
aceResponse = Utils.createAceEditor('aceResponse', 'ace/mode/json', true);
dom.buttonMessageSend.onclick = () => {
Utils.assert(theFeatureId, 'Please select a Feature', dom.tableValidationFeature);
Utils.assert(dom.inputMessageSubject.value, 'Please give a Subject', dom.inputMessageSubject);
Utils.assert(dom.inputMessageTimeout.value, 'Please give a timeout', dom.inputMessageTimeout);
dom.buttonMessageSend.classList.add('busy');
dom.buttonMessageSend.disabled = true;
messageFeature();
};
dom.buttonMessageFavorite.onclick = () => {
const templateName = dom.inputMessageTemplate.value;
const featureId = theFeatureId;
const payload = acePayload.getValue();
Utils.assert(featureId, 'Please select a Feature', dom.tableValidationFeature);
Utils.assert(templateName, 'Please give a name for the template', dom.inputMessageTemplate);
Environments.current().messageTemplates[featureId] = Environments.current().messageTemplates[featureId] || {};
if (Object.keys(Environments.current().messageTemplates[featureId]).includes(templateName) &&
dom.favIconMessage.classList.contains('bi-star-fill')) {
dom.favIconMessage.classList.replace('bi-star-fill', 'bi-star');
delete Environments.current().messageTemplates[featureId][templateName];
} else {
dom.favIconMessage.classList.replace('bi-star', 'bi-star-fill');
Environments.current().messageTemplates[featureId][templateName] = {
subject: dom.inputMessageSubject.value,
timeout: dom.inputMessageTimeout.value,
...(payload) && {payload: JSON.parse(payload)},
};
acePayload.session.getUndoManager().markClean();
}
Environments.environmentsJsonChanged('messageTemplates');
};
dom.ulMessageTemplates.addEventListener('click', (event) => {
if (event.target && event.target.classList.contains('dropdown-item')) {
dom.favIconMessage.classList.replace('bi-star', 'bi-star-fill');
const template = Environments.current().messageTemplates[theFeatureId][event.target.textContent];
dom.inputMessageTemplate.value = event.target.textContent;
dom.inputMessageSubject.value = template.subject;
dom.inputMessageTimeout.value = template.timeout;
acePayload.setValue(JSON.stringify(template.payload, null, 2), -1);
acePayload.session.getUndoManager().markClean();
}
});
[dom.inputMessageTemplate, dom.inputMessageSubject, dom.inputMessageTimeout].forEach((e) => {
e.addEventListener('change', () => {
dom.favIconMessage.classList.replace('bi-star-fill', 'bi-star');
});
});
acePayload.on('input', () => {
if (!acePayload.session.getUndoManager().isClean()) {
dom.favIconMessage.classList.replace('bi-star-fill', 'bi-star');
}
});
}
/**
* Calls Ditto to send a message with the parameters of the fields in the UI
*/
function messageFeature() {
const payload = acePayload && acePayload.getValue().length > 0 && JSON.parse(acePayload.getValue());
aceResponse.setValue('');
API.callDittoREST('POST', '/things/' + Things.theThing.thingId +
'/features/' + theFeatureId +
'/inbox/messages/' + dom.inputMessageSubject.value +
'?timeout=' + dom.inputMessageTimeout.value,
payload,
).then((data) => {
dom.buttonMessageSend.classList.remove('busy');
dom.buttonMessageSend.disabled = false;
if (dom.inputMessageTimeout.value > 0) {
aceResponse.setValue(JSON.stringify(data, null, 2), -1);
}
}).catch((err) => {
dom.buttonMessageSend.classList.remove('busy');
dom.buttonMessageSend.disabled = false;
aceResponse.setValue('');
});
}
function onEnvironmentChanged(modifiedField) {
Environments.current()['messageTemplates'] = Environments.current()['messageTemplates'] || {};
if (!modifiedField) {
clearAllFields();
}
if (modifiedField === 'messageTemplates') {
refillTemplates();
}
}
function clearAllFields() {
dom.favIconMessage.classList.replace('bi-star-fill', 'bi-star');
dom.inputMessageTemplate.value = null;
dom.inputMessageSubject.value = null;
dom.inputMessageTimeout.value = '10';
acePayload.setValue('');
aceResponse.setValue('');
dom.ulMessageTemplates.innerHTML = '';
}
function refillTemplates() {
dom.ulMessageTemplates.innerHTML = '';
Utils.addDropDownEntries(dom.ulMessageTemplates, ['Saved message templates'], true);
if (theFeatureId && Environments.current().messageTemplates[theFeatureId]) {
Utils.addDropDownEntries(
dom.ulMessageTemplates,
Object.keys(Environments.current().messageTemplates[theFeatureId]),
);
}
}
function onFeatureChanged(featureId) {
theFeatureId = featureId;
clearAllFields();
refillTemplates();
}