/
attributes.ts
180 lines (161 loc) · 5.55 KB
/
attributes.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
176
177
178
179
180
/*
* 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 */
/* eslint-disable comma-dangle */
import * as API from '../api.js';
import * as Utils from '../utils.js';
import * as Things from './things.js';
const dom = {
tbodyAttributes: null,
crudAttribute: null,
inputAttributeValue: null,
badgeAttributeCount: null,
};
let eTag;
/**
* Initializes components. Should be called after DOMContentLoaded event
*/
export function ready() {
Things.addChangeListener(onThingChanged);
Utils.getAllElementsById(dom);
dom.tbodyAttributes.onclick = onAttributeTableClick;
dom.crudAttribute.addEventListener('onCreateClick', onCreateAttributeClick);
dom.crudAttribute.addEventListener('onUpdateClick', onUpdateAttributeClick);
dom.crudAttribute.addEventListener('onDeleteClick', onDeleteAttributeClick);
dom.crudAttribute.addEventListener('onEditToggle', onEditToggle);
}
function onCreateAttributeClick() {
Utils.assert(dom.crudAttribute.idValue, 'Attribute path must not be empty', dom.crudAttribute.validationElement);
Utils.assert(!Things.theThing['attributes'] || !Object.keys(Things.theThing.attributes).includes(dom.crudAttribute.idValue),
`Attribute path ${dom.crudAttribute.idValue} already exists in Thing`,
dom.crudAttribute.validationElement);
Utils.assert(dom.inputAttributeValue.value, 'Attribute value must not be empty', dom.inputAttributeValue);
updateAttribute('PUT', true);
}
function onUpdateAttributeClick() {
Utils.assert(dom.inputAttributeValue.value, 'Attribute value must not be empty');
updateAttribute('PUT');
}
function onDeleteAttributeClick() {
Utils.confirm(`Are you sure you want to delete attribute<br>'${dom.crudAttribute.idValue}'?`, 'Delete', () => {
updateAttribute('DELETE');
});
}
function onAttributeTableClick(event) {
if (event.target && event.target.nodeName === 'TD') {
const path = event.target.parentNode.children[0].innerText;
if (dom.crudAttribute.idValue === path) {
refreshAttribute(null);
} else {
refreshAttribute(Things.theThing, path);
}
}
}
/**
* Creates a onclick handler function
* @param {String} method PUT or DELETE
* @param {boolean} isNewAttribute if a new attribute is created. default = false
*/
function updateAttribute(method, isNewAttribute = false) {
API.callDittoREST(
method,
`/things/${Things.theThing.thingId}/attributes/${dom.crudAttribute.idValue}`,
method === 'PUT' ? attributeFromString(dom.inputAttributeValue.value) : null,
isNewAttribute ?
{
'If-None-Match': '*'
} :
{
'If-Match': method === 'PUT' ? eTag : '*'
},
).then(() => {
if (method === 'PUT') {
dom.crudAttribute.toggleEdit();
}
Things.refreshThing(Things.theThing.thingId);
});
}
function refreshAttribute(thing, attributePath = null) {
if (dom.crudAttribute.isEditing) {
return;
}
if (thing) {
dom.crudAttribute.idValue = attributePath;
dom.inputAttributeValue.value = attributeToString(thing.attributes[attributePath]);
} else {
dom.crudAttribute.idValue = null;
dom.inputAttributeValue.value = null;
}
}
function onThingChanged(thing) {
dom.crudAttribute.editDisabled = (thing === null);
dom.tbodyAttributes.innerHTML = '';
let count = 0;
let thingHasAttribute = false;
if (thing && thing.attributes) {
Object.keys(Things.theThing.attributes).forEach((path) => {
if (path === dom.crudAttribute.idValue) {
refreshAttribute(Things.theThing, path);
thingHasAttribute = true;
}
Utils.addTableRow(dom.tbodyAttributes, path, path === dom.crudAttribute.idValue, null,
attributeToString(Things.theThing.attributes[path]));
count++;
});
}
dom.badgeAttributeCount.innerText = count > 0 ? count : '';
if (!thingHasAttribute) {
refreshAttribute(null);
}
}
/**
* checks if the attribute is an array or json and returns a parsed string
* @param {object} attribute
* @return {String} parsed json for objects or toString for json values
*/
function attributeToString(attribute) {
return typeof attribute === 'object' ?
JSON.stringify(attribute) :
attribute.toString();
}
/**
* Converts a String into a json value for a Ditto attribute
* @param {String} attribute Ditto attribute as a String
* @return {Object} object in case it could be parsed, else the orignal String
*/
function attributeFromString(attribute) {
try {
return JSON.parse(attribute);
} catch (err) {
return attribute;
}
}
function onEditToggle(event) {
const isEditing = event.detail.isEditing;
dom.inputAttributeValue.disabled = !isEditing;
if (isEditing && dom.crudAttribute.idValue && dom.crudAttribute.idValue !== '') {
API.callDittoREST('GET', `/things/${Things.theThing.thingId}/attributes/${dom.crudAttribute.idValue}`,
null, null, true)
.then((response) => {
eTag = response.headers.get('ETag');
return response.json();
})
.then((attributeValue) => {
dom.inputAttributeValue.value = attributeToString(attributeValue);
});
} else {
dom.inputAttributeValue.value = dom.crudAttribute.idValue ?
attributeToString(Things.theThing.attributes[dom.crudAttribute.idValue]) :
null;
}
}