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