123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- define(function () {
- return function () {
- var self = this
- var objects = { nodes: {}, links: {} }
- var targets = []
- var views = {}
- var currentView
- var currentObject
- var running = false
- function saveState() {
- var e = []
- if (currentView)
- e.push("v:" + currentView)
- if (currentObject) {
- if ("node" in currentObject)
- e.push("n:" + encodeURIComponent(currentObject.node.nodeinfo.node_id))
- if ("link" in currentObject)
- e.push("l:" + encodeURIComponent(currentObject.link.id))
- }
- var s = "#!" + e.join(";")
- window.history.pushState(s, undefined, s)
- }
- function resetView(push) {
- push = trueDefault(push)
- targets.forEach( function (t) {
- t.resetView()
- })
- if (push) {
- currentObject = undefined
- saveState()
- }
- }
- function gotoNode(d) {
- if (!d)
- return false
- targets.forEach( function (t) {
- t.gotoNode(d)
- })
- return true
- }
- function gotoLink(d) {
- if (!d)
- return false
- targets.forEach( function (t) {
- t.gotoLink(d)
- })
- return true
- }
- function loadState(s) {
- if (!s)
- return false
- if (!s.startsWith("#!"))
- return false
- var targetSet = false
- s.slice(2).split(";").forEach(function (d) {
- var args = d.split(":")
- if (args[0] === "v" && args[1] in views) {
- currentView = args[1]
- views[args[1]]()
- }
- var id
- if (args[0] === "n") {
- id = decodeURIComponent(args[1])
- if (id in objects.nodes) {
- currentObject = { node: objects.nodes[id] }
- gotoNode(objects.nodes[id])
- targetSet = true
- }
- }
- if (args[0] === "l") {
- id = decodeURIComponent(args[1])
- if (id in objects.links) {
- currentObject = { link: objects.links[id] }
- gotoLink(objects.links[id])
- targetSet = true
- }
- }
- })
- return targetSet
- }
- self.start = function () {
- running = true
- if (!loadState(window.location.hash))
- resetView(false)
- window.onpopstate = function (d) {
- if (!loadState(d.state))
- resetView(false)
- }
- }
- self.view = function (d) {
- if (d in views) {
- views[d]()
- if (!currentView || running)
- currentView = d
- if (!running)
- return
- saveState()
- if (!currentObject) {
- resetView(false)
- return
- }
- if ("node" in currentObject)
- gotoNode(currentObject.node)
- if ("link" in currentObject)
- gotoLink(currentObject.link)
- }
- }
- self.node = function (d) {
- return function () {
- var sidebar = document.getElementById("sidebar")
- sidebar.classList.remove("hidden")
- if (gotoNode(d)) {
- currentObject = { node: d }
- saveState()
- }
- return false
- }
- }
- self.link = function (d) {
- return function () {
- if (gotoLink(d)) {
- currentObject = { link: d }
- saveState()
- }
- return false
- }
- }
- self.reset = function () {
- resetView()
- }
- self.addTarget = function (d) {
- targets.push(d)
- }
- self.removeTarget = function (d) {
- targets = targets.filter( function (e) {
- return d !== e
- })
- }
- self.addView = function (k, d) {
- views[k] = d
- }
- self.setData = function (data) {
- objects.nodes = {}
- objects.links = {}
- data.nodes.all.forEach( function (d) {
- objects.nodes[d.nodeinfo.node_id] = d
- })
- data.graph.links.forEach( function (d) {
- objects.links[d.id] = d
- })
- }
- return self
- }
- })
|