You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. export class Display {
  2. constructor(baseUrl, dispatch, target) {
  3. this.baseUrl = baseUrl
  4. this.target = target
  5. target.addEventListener('click', e => {
  6. let target = e.target
  7. while (target.nodeName !== 'A') {
  8. target = target.parentNode
  9. if (!target) return
  10. }
  11. window.history.pushState(null, "", target.href)
  12. dispatch(target.getAttribute('href'))
  13. e.preventDefault()
  14. })
  15. this.graph = new Springy.Graph()
  16. this.layout = new Springy.Layout.ForceDirected(
  17. this.graph,
  18. 400.0, // Spring stiffness
  19. 400.0, // Node repulsion
  20. 0.5 // Damping
  21. )
  22. }
  23. render(state) {
  24. const target = document.createElement('div')
  25. target.className = 'wrapper'
  26. const field = document.createElement('ul')
  27. field.className = 'items'
  28. const graph = this.graph
  29. for (const item of state.items) {
  30. graph.addNode(new Springy.Node(item.id, item))
  31. }
  32. let currentBB = this.layout.getBoundingBox()
  33. let width = this.target.scrollWidth || 100
  34. let height = this.target.scrollHeight || 100
  35. var toScreen = function(p) {
  36. var size = currentBB.topright.subtract(currentBB.bottomleft);
  37. var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * (width - 100);
  38. var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - 100);
  39. return new Springy.Vector(sx, sy);
  40. };
  41. var fromScreen = function(s) {
  42. var size = currentBB.topright.subtract(currentBB.bottomleft);
  43. var px = (s.x / canvas.width) * size.x + currentBB.bottomleft.x;
  44. var py = (s.y / canvas.height) * size.y + currentBB.bottomleft.y;
  45. return new Springy.Vector(px, py);
  46. };
  47. var renderer = new Springy.Renderer(
  48. this.layout,
  49. () => {
  50. currentBB = this.layout.getBoundingBox()
  51. width = this.target.scrollWidth
  52. height = this.target.scrollHeight
  53. field.innerHTML = ''
  54. },
  55. function drawEdge(edge, p1, p2) {
  56. // draw an edge
  57. },
  58. function drawNode(item, p) {
  59. const target = toScreen(p)
  60. const dom = document.createElement('li')
  61. const a = document.createElement('a')
  62. const action = item.data.state == 'face-down' ? 'flip' : 'show'
  63. a.href = `/${item.id}/${action}`
  64. a.style.backgroundImage = `url(img/${item.data.img})`
  65. dom.className = item.data.state
  66. dom.style.position = 'absolute'
  67. dom.style.left = target.x + 'px'
  68. dom.style.top = target.y + 'px'
  69. dom.appendChild(a)
  70. field.appendChild(dom)
  71. }, undefined, undefined,
  72. () => {
  73. const dd = new diffDOM.DiffDOM()
  74. const diff = dd.diff(this.target.children[0], field)
  75. dd.apply(this.target.children[0], diff)
  76. }
  77. );
  78. target.appendChild(field)
  79. if (state.show !== null) {
  80. const wrapper = document.createElement('a')
  81. wrapper.href = '/'
  82. const fullview = document.createElement('div')
  83. wrapper.className = 'fullview'
  84. wrapper.appendChild(fullview)
  85. target.appendChild(wrapper)
  86. }
  87. const dd = new diffDOM.DiffDOM()
  88. const diff = dd.diff(this.target, target)
  89. dd.apply(this.target, diff)
  90. renderer.start();
  91. }
  92. }