meshviewer/lib/router.js

237 lines
4.5 KiB
JavaScript
Raw Normal View History

define(['helper'], function (helper) {
'use strict';
2016-05-27 21:59:01 +00:00
2015-03-29 14:14:10 +00:00
return function () {
var self = this;
var objects = { nodes: {}, links: {} };
var targets = [];
var views = {};
var currentView;
var currentObject;
var running = false;
2015-03-25 19:25:41 +00:00
2015-07-06 22:06:38 +00:00
function saveState() {
var e = [];
2015-03-29 14:14:10 +00:00
if (currentView) {
e.push('v:' + currentView);
}
2015-03-29 14:14:10 +00:00
2015-07-06 22:06:38 +00:00
if (currentObject) {
if ('node' in currentObject) {
e.push('n:' + encodeURIComponent(currentObject.node.nodeinfo.node_id));
}
2015-07-06 22:06:38 +00:00
if ('link' in currentObject) {
e.push('l:' + encodeURIComponent(currentObject.link.id));
}
2015-03-29 14:14:10 +00:00
}
var s = '#!' + e.join(';');
2015-07-06 22:06:38 +00:00
window.history.pushState(s, undefined, s);
2015-03-29 14:14:10 +00:00
}
2015-03-25 19:25:41 +00:00
function resetView(push) {
2016-05-26 16:37:24 +00:00
push = helper.trueDefault(push);
2015-03-25 19:25:41 +00:00
targets.forEach(function (t) {
t.resetView();
});
2015-03-25 19:25:41 +00:00
2015-07-06 22:06:38 +00:00
if (push) {
currentObject = undefined;
saveState();
2015-07-06 22:06:38 +00:00
}
2015-03-25 19:25:41 +00:00
}
function gotoNode(d, update) {
if (!d) {
return false;
}
2015-03-31 14:30:16 +00:00
targets.forEach(function (t) {
t.gotoNode(d, update);
});
2015-03-31 14:30:16 +00:00
return true;
2015-03-25 19:25:41 +00:00
}
function gotoLink(d, update) {
if (!d) {
return false;
}
2015-03-31 14:30:16 +00:00
targets.forEach(function (t) {
t.gotoLink(d, update);
});
2015-03-31 14:30:16 +00:00
return true;
2015-03-25 19:25:41 +00:00
}
function gotoLocation(d) {
if (!d) {
return false;
}
targets.forEach(function (t) {
if (!t.gotoLocation) {
console.warn('has no gotoLocation', t);
}
t.gotoLocation(d);
});
return true;
}
function loadState(s, update) {
if (!s) {
return false;
}
2015-03-25 19:25:41 +00:00
s = decodeURIComponent(s);
if (!s.startsWith('#!')) {
return false;
}
2015-03-25 19:25:41 +00:00
var targetSet = false;
2015-03-25 19:25:41 +00:00
s.slice(2).split(';').forEach(function (d) {
var args = d.split(':');
2015-03-25 19:25:41 +00:00
if (update !== true && args[0] === 'v' && args[1] in views) {
currentView = args[1];
views[args[1]]();
2015-03-27 21:25:28 +00:00
}
2015-03-25 19:25:41 +00:00
var id;
2015-07-06 22:06:38 +00:00
if (args[0] === 'n') {
id = args[1];
2015-07-06 22:06:38 +00:00
if (id in objects.nodes) {
currentObject = { node: objects.nodes[id] };
gotoNode(objects.nodes[id], update);
targetSet = true;
2015-07-06 22:06:38 +00:00
}
2015-03-27 21:25:28 +00:00
}
if (args[0] === 'l') {
id = args[1];
2015-07-06 22:06:38 +00:00
if (id in objects.links) {
currentObject = { link: objects.links[id] };
gotoLink(objects.links[id], update);
targetSet = true;
2015-07-06 22:06:38 +00:00
}
}
});
2015-07-06 22:06:38 +00:00
return targetSet;
2015-03-27 21:25:28 +00:00
}
self.start = function start() {
running = true;
if (!loadState(window.location.hash)) {
resetView(false);
}
2015-03-27 21:25:28 +00:00
window.onpopstate = function onpopstate(d) {
if (!loadState(d.state)) {
resetView(false);
}
};
};
2015-03-25 19:25:41 +00:00
self.view = function view(d) {
2015-07-06 22:06:38 +00:00
if (d in views) {
views[d]();
2015-07-06 22:06:38 +00:00
if (!currentView || running) {
currentView = d;
}
2015-07-06 22:10:42 +00:00
if (!running) {
return;
}
2015-07-06 22:30:35 +00:00
saveState();
2015-07-06 22:30:35 +00:00
if (!currentObject) {
resetView(false);
return;
2015-07-06 22:06:38 +00:00
}
2015-07-06 22:30:35 +00:00
if ('node' in currentObject) {
gotoNode(currentObject.node);
}
2015-07-06 22:30:35 +00:00
if ('link' in currentObject) {
gotoLink(currentObject.link);
}
2015-07-06 22:06:38 +00:00
}
};
2015-07-06 22:06:38 +00:00
self.node = function node(d) {
2015-03-25 19:55:49 +00:00
return function () {
2015-07-06 22:06:38 +00:00
if (gotoNode(d)) {
currentObject = { node: d };
saveState();
2015-07-06 22:06:38 +00:00
}
2015-03-31 14:30:16 +00:00
return false;
};
};
2015-03-25 19:55:49 +00:00
self.link = function link(d) {
2015-03-25 19:55:49 +00:00
return function () {
2015-07-06 22:06:38 +00:00
if (gotoLink(d)) {
currentObject = { link: d };
saveState();
2015-07-06 22:06:38 +00:00
}
2015-03-31 14:30:16 +00:00
return false;
};
};
2015-03-25 19:55:49 +00:00
self.gotoLocation = gotoLocation;
self.reset = function reset() {
resetView();
};
2015-03-25 19:55:49 +00:00
self.addTarget = function addTarget(d) {
targets.push(d);
};
2015-03-31 20:18:09 +00:00
self.removeTarget = function removeTarget(d) {
targets = targets.filter(function (e) {
return d !== e;
});
};
2015-03-25 19:25:41 +00:00
self.addView = function addView(k, d) {
views[k] = d;
};
2015-07-06 22:06:38 +00:00
self.setData = function setData(data) {
objects.nodes = {};
objects.links = {};
2015-03-25 19:25:41 +00:00
data.nodes.all.forEach(function (d) {
objects.nodes[d.nodeinfo.node_id] = d;
});
2015-03-25 19:25:41 +00:00
data.graph.links.forEach(function (d) {
objects.links[d.id] = d;
});
};
self.update = function update() {
loadState(window.location.hash, true);
};
2015-03-25 19:25:41 +00:00
return self;
};
});