-
Notifications
You must be signed in to change notification settings - Fork 218
/
thingMessages.ts
159 lines (141 loc) · 5.74 KB
/
thingMessages.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
/*
* Copyright (c) 2023 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 messagesHTML from './thingMessages.html';
import * as Things from './things.js';
const dom = {
inputThingMessageSubject: null,
inputThingMessageTimeout: null,
inputThingMessageTemplate: null,
buttonThingMessageSend: null,
buttonThingMessageFavorite: null,
ulThingMessageTemplates: null,
favIconThingMessage: null,
};
let acePayload;
let aceResponse;
/**
* Initializes components. Should be called after DOMContentLoaded event
*/
export async function ready() {
Environments.addChangeListener(onEnvironmentChanged);
Utils.addTab(
document.getElementById('tabItemsThing'),
document.getElementById('tabContentThing'),
'send Message',
messagesHTML,
);
Utils.getAllElementsById(dom);
acePayload = Utils.createAceEditor('acePayloadThingMessage', 'ace/mode/json');
aceResponse = Utils.createAceEditor('aceResponseThingMessage', 'ace/mode/json', true);
dom.buttonThingMessageSend.onclick = () => {
Utils.assert(dom.inputThingMessageSubject.value, 'Please give a Subject', dom.inputThingMessageSubject);
Utils.assert(dom.inputThingMessageTimeout.value, 'Please give a timeout', dom.inputThingMessageTimeout);
dom.buttonThingMessageSend.classList.add('busy');
dom.buttonThingMessageSend.disabled = true;
messageThing();
};
dom.buttonThingMessageFavorite.onclick = () => {
const templateName = dom.inputThingMessageTemplate.value;
Utils.assert(templateName, 'Please give a name for the template', dom.inputThingMessageTemplate);
Environments.current().messageTemplates['/'] = Environments.current().messageTemplates['/'] || {};
if (Object.keys(Environments.current().messageTemplates['/']).includes(templateName) &&
dom.favIconThingMessage.classList.contains('bi-star-fill')) {
dom.favIconThingMessage.classList.replace('bi-star-fill', 'bi-star');
delete Environments.current().messageTemplates['/'][templateName];
} else {
dom.favIconThingMessage.classList.replace('bi-star', 'bi-star-fill');
Environments.current().messageTemplates['/'][templateName] = {
subject: dom.inputThingMessageSubject.value,
timeout: dom.inputThingMessageTimeout.value,
payload: JSON.parse(acePayload.getValue()),
};
acePayload.session.getUndoManager().markClean();
}
Environments.environmentsJsonChanged('messageTemplates');
};
dom.ulThingMessageTemplates.addEventListener('click', (event) => {
if (event.target && event.target.classList.contains('dropdown-item')) {
dom.favIconThingMessage.classList.replace('bi-star', 'bi-star-fill');
const template = Environments.current().messageTemplates['/'][event.target.textContent];
dom.inputThingMessageTemplate.value = event.target.textContent;
dom.inputThingMessageSubject.value = template.subject;
dom.inputThingMessageTimeout.value = template.timeout;
acePayload.setValue(JSON.stringify(template.payload, null, 2), -1);
acePayload.session.getUndoManager().markClean();
}
});
[dom.inputThingMessageTemplate, dom.inputThingMessageSubject, dom.inputThingMessageTimeout].forEach((e) => {
e.addEventListener('change', () => {
dom.favIconThingMessage.classList.replace('bi-star-fill', 'bi-star');
});
});
acePayload.on('input', () => {
if (!acePayload.session.getUndoManager().isClean()) {
dom.favIconThingMessage.classList.replace('bi-star-fill', 'bi-star');
}
});
}
/**
* Calls Ditto to send a message with the parameters of the fields in the UI
*/
function messageThing() {
const payload = acePayload && acePayload.getValue().length > 0 && JSON.parse(acePayload.getValue());
aceResponse.setValue('');
API.callDittoREST('POST', '/things/' + Things.theThing.thingId +
'/inbox/messages/' + dom.inputThingMessageSubject.value +
'?timeout=' + dom.inputThingMessageTimeout.value,
payload,
).then((data) => {
dom.buttonThingMessageSend.classList.remove('busy');
dom.buttonThingMessageSend.disabled = false;
if (dom.inputThingMessageTimeout.value > 0) {
aceResponse.setValue(JSON.stringify(data, null, 2), -1);
}
}).catch((err) => {
dom.buttonThingMessageSend.classList.remove('busy');
dom.buttonThingMessageSend.disabled = false;
aceResponse.setValue('');
});
}
function onEnvironmentChanged(modifiedField) {
Environments.current()['messageTemplates'] = Environments.current()['messageTemplates'] || {};
if (!modifiedField) {
clearAllFields();
}
if (modifiedField === 'messageTemplates') {
refillTemplates();
}
}
function clearAllFields() {
dom.favIconThingMessage.classList.replace('bi-star-fill', 'bi-star');
dom.inputThingMessageTemplate.value = null;
dom.inputThingMessageSubject.value = null;
dom.inputThingMessageTimeout.value = '10';
acePayload.setValue('');
aceResponse.setValue('');
dom.ulThingMessageTemplates.innerHTML = '';
}
function refillTemplates() {
dom.ulThingMessageTemplates.innerHTML = '';
Utils.addDropDownEntries(dom.ulThingMessageTemplates, ['Saved message templates'], true);
if (Environments.current().messageTemplates['/']) {
Utils.addDropDownEntries(
dom.ulThingMessageTemplates,
Object.keys(Environments.current().messageTemplates['/']),
);
}
}