router.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. define(function () {
  2. return function () {
  3. var self = this
  4. var objects = { nodes: {}, links: {} }
  5. var targets = []
  6. var running = false
  7. function saveState(d) {
  8. var s = "#!"
  9. if (d) {
  10. if ("node" in d)
  11. s += "n:" + encodeURIComponent(d.node.nodeinfo.node_id)
  12. if ("link" in d)
  13. s += "l:" + encodeURIComponent(d.link.id)
  14. }
  15. window.history.pushState(s, undefined, s)
  16. }
  17. function resetView(push) {
  18. push = trueDefault(push)
  19. targets.forEach( function (t) {
  20. t.resetView()
  21. })
  22. if (push)
  23. saveState()
  24. }
  25. function gotoNode(d) {
  26. if (!d)
  27. return false
  28. targets.forEach( function (t) {
  29. t.gotoNode(d)
  30. })
  31. return true
  32. }
  33. function gotoLink(d) {
  34. if (!d)
  35. return false
  36. targets.forEach( function (t) {
  37. t.gotoLink(d)
  38. })
  39. return true
  40. }
  41. function loadState(s) {
  42. if (!s)
  43. return false
  44. if (!s.startsWith("#!"))
  45. return false
  46. var args = s.slice(2).split(":")
  47. var id
  48. if (args[1] !== undefined) {
  49. id = decodeURIComponent(args[1])
  50. if (args[0] === "n" && id in objects.nodes) {
  51. gotoNode(objects.nodes[id])
  52. return true
  53. }
  54. if (args[0] === "l" && id in objects.links) {
  55. gotoLink(objects.links[id])
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. self.start = function () {
  62. running = true
  63. if (!loadState(window.location.hash))
  64. resetView(false)
  65. window.onpopstate = function (d) {
  66. if (!loadState(d.state))
  67. resetView(false)
  68. }
  69. }
  70. self.node = function (d) {
  71. return function () {
  72. if (gotoNode(d))
  73. saveState({ node: d })
  74. return false
  75. }
  76. }
  77. self.link = function (d) {
  78. return function () {
  79. if (gotoLink(d))
  80. saveState({ link: d })
  81. return false
  82. }
  83. }
  84. self.reset = function () {
  85. resetView()
  86. saveState()
  87. }
  88. self.addTarget = function (d) {
  89. targets.push(d)
  90. }
  91. self.removeTarget = function (d) {
  92. targets = targets.filter( function (e) {
  93. return d !== e
  94. })
  95. }
  96. self.setData = function (data) {
  97. objects.nodes = {}
  98. objects.links = {}
  99. data.nodes.all.forEach( function (d) {
  100. objects.nodes[d.nodeinfo.node_id] = d
  101. })
  102. data.graph.links.forEach( function (d) {
  103. objects.links[d.id] = d
  104. })
  105. }
  106. self.reload = function () {
  107. if (!running)
  108. return
  109. if (!loadState(window.history.state))
  110. resetView(false)
  111. }
  112. return self
  113. }
  114. })