-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem-property-node.html
More file actions
306 lines (246 loc) · 10.4 KB
/
system-property-node.html
File metadata and controls
306 lines (246 loc) · 10.4 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<script type="text/javascript">
RED.nodes.registerType('system-property-node', {
category: 'Web of Things',
color: '#a6bbcf',
defaults: {
name: { value: "" },
thingDirectoryURI: { value: "" },
thingProperty: { value: "" },
thingPropertyValue: { value: "" },
mode: { value: "Read" },
redeploy: { value: "false" } //Only used to trigger UI redeploy
},
inputs: 1,
outputs: 1,
outputLabels: function () {
try {
return JSON.parse(this.thingPropertyValue)["type"];
}
catch {
return "undefined";
}
},
icon: "bridge.svg",
label: function () {
if (this.name) {
return this.name;
}
else if (this.thingPropertyValue) {
return JSON.parse(this.thingPropertyValue).property;
}
else {
return "system-property-node";
}
},
oneditprepare: function () {
var node = this;
setUpModeSelect();
// hideFunctionOption(); //TODO: Buffer options whilst waiting for connection to Thing Directory
if(node.thingPropertyValue === undefined) { //Ensures value is added to this node object
node.thingPropertyValue = "";
}
else {
try {
let propertyObj = JSON.parse(node.thingPropertyValue);
updateTypeLabel(propertyObj);
}
catch {
node.thingPropertyValue = "";
}
}
$("#node-input-thingDirectoryURI").on("change", async function (event) {
let input = $(this).val();
let emptyInput = ["", undefined, null].includes(input);
if (!emptyInput) await populateProperties(input, node);
displayPropertyOption(!emptyInput);
});
function showPropertyOption() {
$("div#property-row").show();
}
function hidePropertyOption() {
$("#node-input-thingProperty").innerText = "";
this.thingProperty = "";
$("div#property-row").hide();
}
function displayPropertyOption(on) {
if (on) {
showPropertyOption();
} else {
hidePropertyOption();
}
}
/**
* @returns Boolean, the completion of this functionality
*/
async function populateProperties(tdURI, node) {
emptyPropertiesSelect();
let thingURIs;
try {
thingURIs = await fetchThingURIs(tdURI);
}
catch (reason) {
//TODO: Handle failure (display error, with icon and notifiction)
// node.status({fill:"red",shape:"ring",text:"Disconnected from Thing directory"});
// error("Failed to connect to Thing Directory");
return reason;
}
let propertyMap = await getProperties(thingURIs);
let propertySelect = document.getElementById("node-input-thingProperty");
if (Object.keys(propertyMap).length === 0) { //No properties
let option = document.createElement("option");
option.innerHTML = "No properties available";
propertySelect.appendChild(option);
propertySelect.disabled = true;
// node.status({fill:"yellow",shape:"ring",text:"No properties found"}); //TODO: Allow status changes
document.getElementById("label-type").innerText = "";
return;
}
propertySelect.disabled = false;
propertySelect.addEventListener("change", async function () { //Set thingProperty value to new selection
let selection = propertySelect.options[propertySelect.selectedIndex];
let propertyObject = $(selection).data("value");
node.thingProperty = selection.text;
let propertyJSON = JSON.stringify(propertyObject); //Access object in select's data-value
this.thingPropertyValue = propertyJSON;
node.thingPropertyValue = propertyJSON;
//TODO: Use description and title
updateTypeLabel(propertyObject);
enableDeploy();
});
let index = 0;
for (let property of Object.keys(propertyMap)) {
let option = document.createElement('option');
propertyMap[property]["property"] = property; //Move property name from a key to a value
$(option).data("value", propertyMap[property]);
option.innerHTML = property;
propertySelect.appendChild(option);
if (property === node.thingProperty) { //Select the thingProperty option
propertySelect.selectedIndex = index;
}
index++;
}
let selection = propertySelect.options[propertySelect.selectedIndex];
let propertyObject = $(selection).data("value");
let propertyJSON = JSON.stringify(propertyObject);
node.thingPropertyValue = propertyJSON;
updateTypeLabel(propertyObject);
}
function emptyPropertiesSelect() {
$("select#node-input-thingProperty").empty();
}
async function fetchThingURIs(tdURI) {
return fetch(tdURI + "/actions/getURIs", {
method: "POST"
})
.then((response) => {
return response.json();
})
.catch((reason) => {
throw reason;
});
}
/**
* Note: No property names may be duplicated in the system
* @returns A map of property to the Thing URIs, output type, description and title
*/
async function getProperties(uriList) {
let uriMap = {}; //Maps properties to the URIs they are associated with
for (let uri of uriList) {
let properties = await fetchObject(uri, "properties");
if (properties === undefined) continue;
for (let property of Object.keys(properties)) {
uriMap[property] = {
"uri" : uri,
"type" : undefined,
"description" : undefined,
"title" : undefined
}; //Note: Will override an property if a duplicate occurs
let propertyObj = properties[property];
uriMap[property]["type"] = propertyObj["type"];
uriMap[property]["description"] = propertyObj["description"];
uriMap[property]["title"] = propertyObj["title"];
}
}
return uriMap;
}
async function fetchObject(uri, key) {
let response = await fetch(uri, {
method: "GET"
})
.then((response) => {
return response.json();
})
.catch((reason) => {
throw reason;
});
return response[key]; //Only allows keys from top-level
}
function enableDeploy() {
let holder = document.getElementById("node-input-redeploy");
if (holder.value !== "true") {
holder.value = "true";
}
else {
holder.value = "false";
}
}
function updateTypeLabel(propertyObject) {
let type = propertyObject["type"];
let typeHolder = document.getElementById("label-type");
typeHolder.innerText = type;
}
function setUpModeSelect() {
$("#node-input-mode").typedInput({
types: [{
value: "mode",
options: [
{ value: "read", label: "Read"},
{ value: "write", label: "Write"}
]
}]
});
//TODO: Hide write only properties when in write mode
}
}
});
</script>
<style>
.parameterLabel {
vertical-align: top;
width: 60%;
}
#label-parameters {
width: 60%;
}
#label-type {
width: 60%;
}
</style>
<script type="text/html" data-template-name="system-property-node">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-thingDirectoryURI"><i class="fa fa-tag"></i> Thing Directory</label>
<input type="text" id="node-input-thingDirectoryURI" placeholder="http://localhost:8080/td">
</div>
<div id="property-row" class="form-row">
<label for="node-input-thingProperty"><i class="fa fa-tag"></i> Property</label>
<select name="propertySelect" id="node-input-thingProperty">
<option>Placeholder</option>
</select>
<div class="form-row">
<label for="label-type"> Type</label>
<label id="label-type" class="parameterLabel"></label>
</div>
</div>
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-tag"></i> Mode</label>
<input name="modeSelect" id="node-input-mode">
</div>
<input type="hidden" id="node-input-redeploy" name="holder">
</script>
<script type="text/html" data-help-name="system-property-node">
<p>A node describing all Thing properties held within its ThingDirectory</p>
</script>