web: rework store.js and interaction with view/node.js
- store.getNode() no longer adds new node to the list - instead, store.createNode() can be used to create an empty new node - view/node.js now now transfer a local copy of the node object it wants to modify, rather then modifying the real object from store.list. If the change is accepted by server, the server will send the new values for the changed object anyway. Previously, if the user visited the node page for a nonexistent node but didn't make any changes, the list view would always throw an exception because an incomplete node object was contained in the list.
This commit is contained in:
parent
393caf55f8
commit
cd511dce8e
|
@ -1,29 +1,34 @@
|
||||||
const current = {},
|
const current = {},
|
||||||
list = {};
|
list = {};
|
||||||
|
|
||||||
|
// Returns the node with specified id (or null if node doesn't exist).
|
||||||
export function getNode (nodeid) {
|
export function getNode (nodeid) {
|
||||||
let node = {};
|
if (!list[nodeid]) {
|
||||||
|
return null;
|
||||||
if (list[nodeid]) {
|
|
||||||
node = list[nodeid];
|
|
||||||
if (current[nodeid]) {
|
|
||||||
const cNode = current[nodeid];
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
|
||||||
node._wireless = cNode.wireless;
|
|
||||||
node.lastseen = cNode.lastseen;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// eslint-disable-next-line camelcase
|
|
||||||
node.node_id = nodeid;
|
|
||||||
node.wireless = {};
|
|
||||||
node.location = {};
|
|
||||||
list[nodeid] = node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let node = list[nodeid];
|
||||||
|
if (current[nodeid]) {
|
||||||
|
const cNode = current[nodeid];
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
|
node._wireless = cNode.wireless;
|
||||||
|
node.lastseen = cNode.lastseen;
|
||||||
|
}
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Creates an empty node, but doesn't add it to the list.
|
||||||
|
export function createNode (nodeid) {
|
||||||
|
return {
|
||||||
|
'node_id': nodeid,
|
||||||
|
'wireless': {},
|
||||||
|
'location': {}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Overwrites the values for the specified node (identified by its node_id) with new values.
|
||||||
|
// If system==false, the special "current" node will be modified instead.
|
||||||
export function updateNode (node, system) {
|
export function updateNode (node, system) {
|
||||||
if (system) {
|
if (system) {
|
||||||
list[node.node_id] = node;
|
list[node.node_id] = node;
|
||||||
|
@ -32,6 +37,7 @@ export function updateNode (node, system) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns a list of all known nodes.
|
||||||
export function getNodes () {
|
export function getNodes () {
|
||||||
return Object.keys(list).map(getNode);
|
return Object.keys(list).map(getNode);
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,16 +39,11 @@ export class NodeView extends View {
|
||||||
this.hostnameInput.addEventListener('focusout', () => {
|
this.hostnameInput.addEventListener('focusout', () => {
|
||||||
this.editing = false;
|
this.editing = false;
|
||||||
|
|
||||||
const node = store.getNode(this.currentNodeID),
|
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
|
||||||
old = node.hostname;
|
localNodeCopy = Object.assign({}, node);
|
||||||
|
|
||||||
node.hostname = this.hostnameInput.value;
|
localNodeCopy.hostname = this.hostnameInput.value;
|
||||||
|
socket.sendnode(localNodeCopy);
|
||||||
socket.sendnode(node, (msg)=>{
|
|
||||||
if (!msg.body) {
|
|
||||||
node.hostname = old;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
domlib.newAt(owner, 'span').innerHTML = 'Owner: ';
|
domlib.newAt(owner, 'span').innerHTML = 'Owner: ';
|
||||||
|
@ -60,16 +55,11 @@ export class NodeView extends View {
|
||||||
this.ownerInput.addEventListener('focusout', () => {
|
this.ownerInput.addEventListener('focusout', () => {
|
||||||
this.editing = false;
|
this.editing = false;
|
||||||
|
|
||||||
const node = store.getNode(this.currentNodeID),
|
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
|
||||||
old = node.owner;
|
localNodeCopy = Object.assign({}, node);
|
||||||
|
|
||||||
node.owner = this.ownerInput.value;
|
localNodeCopy.owner = this.ownerInput.value;
|
||||||
|
socket.sendnode(localNodeCopy);
|
||||||
socket.sendnode(node, (msg)=>{
|
|
||||||
if (!msg.body) {
|
|
||||||
node.owner = old;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -144,7 +134,8 @@ export class NodeView extends View {
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePosition (lat, lng) {
|
updatePosition (lat, lng) {
|
||||||
const node = store.getNode(this.currentNodeID),
|
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
|
||||||
|
localNodeCopy = Object.assign({}, node),
|
||||||
newLat = lat || this.storePosition.latitude || false,
|
newLat = lat || this.storePosition.latitude || false,
|
||||||
newlng = lng || this.storePosition.longitude || false;
|
newlng = lng || this.storePosition.longitude || false;
|
||||||
|
|
||||||
|
@ -152,21 +143,21 @@ export class NodeView extends View {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
node.location = {
|
localNodeCopy.location = {
|
||||||
'latitude': newLat,
|
'latitude': newLat,
|
||||||
'longitude': newlng
|
'longitude': newlng
|
||||||
};
|
};
|
||||||
socket.sendnode(node);
|
socket.sendnode(localNodeCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
this.geoJsonLayer.refresh();
|
this.geoJsonLayer.refresh();
|
||||||
this.titleID.innerHTML = this.currentNodeID;
|
this.titleID.innerHTML = this.currentNodeID;
|
||||||
const node = store.getNode(this.currentNodeID),
|
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
|
||||||
startdate = new Date();
|
startdate = new Date();
|
||||||
|
|
||||||
if (!node) {
|
if (!node) {
|
||||||
console.log(`node not found: ${this.currentNodeID}`);
|
console.log(`internal error: node not found: ${this.currentNodeID}`);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue