Skip to content

Commit

Permalink
Merge 147275e into 393caf5
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver committed Jul 15, 2018
2 parents 393caf5 + 147275e commit 3754cfb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
38 changes: 22 additions & 16 deletions webroot/js/store.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
const current = {},
list = {};

// Returns the node with specified id (or null if node doesn't exist).
export function getNode (nodeid) {
let node = {};
if (!list[nodeid]) {
return null;
}

if (list[nodeid]) {
node = list[nodeid];
if (current[nodeid]) {
const cNode = current[nodeid];
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;
}
} else {
// eslint-disable-next-line camelcase
node.node_id = nodeid;
node.wireless = {};
node.location = {};
list[nodeid] = node;
// eslint-disable-next-line no-underscore-dangle
node._wireless = cNode.wireless;
node.lastseen = cNode.lastseen;
}

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) {
if (system) {
list[node.node_id] = node;
Expand All @@ -32,6 +37,7 @@ export function updateNode (node, system) {
}
};

// Returns a list of all known nodes.
export function getNodes () {
return Object.keys(list).map(getNode);
};
37 changes: 14 additions & 23 deletions webroot/js/view/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ export class NodeView extends View {
this.hostnameInput.addEventListener('focusout', () => {
this.editing = false;

const node = store.getNode(this.currentNodeID),
old = node.hostname;
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
localNodeCopy = Object.assign({}, node);

node.hostname = this.hostnameInput.value;

socket.sendnode(node, (msg)=>{
if (!msg.body) {
node.hostname = old;
}
});
localNodeCopy.hostname = this.hostnameInput.value;
socket.sendnode(localNodeCopy);
});

domlib.newAt(owner, 'span').innerHTML = 'Owner: ';
Expand All @@ -60,16 +55,11 @@ export class NodeView extends View {
this.ownerInput.addEventListener('focusout', () => {
this.editing = false;

const node = store.getNode(this.currentNodeID),
old = node.owner;
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
localNodeCopy = Object.assign({}, node);

node.owner = this.ownerInput.value;

socket.sendnode(node, (msg)=>{
if (!msg.body) {
node.owner = old;
}
});
localNodeCopy.owner = this.ownerInput.value;
socket.sendnode(localNodeCopy);
});


Expand Down Expand Up @@ -144,29 +134,30 @@ export class NodeView extends View {
}

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,
newlng = lng || this.storePosition.longitude || false;

if (!newLat || !newlng) {
return;
}

node.location = {
localNodeCopy.location = {
'latitude': newLat,
'longitude': newlng
};
socket.sendnode(node);
socket.sendnode(localNodeCopy);
}

render () {
this.geoJsonLayer.refresh();
this.titleID.innerHTML = this.currentNodeID;
const node = store.getNode(this.currentNodeID),
const node = store.getNode(this.currentNodeID) || store.createNode(this.currentNodeID),
startdate = new Date();

if (!node) {
console.log(`node not found: ${this.currentNodeID}`);
console.log(`internal error: node not found: ${this.currentNodeID}`);

return;
}
Expand Down

0 comments on commit 3754cfb

Please sign in to comment.