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