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.

163 lines
5.3 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
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
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
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
4 years ago
  1. export class Display {
  2. constructor(baseUrl, dispatch, target) {
  3. this.baseUrl = baseUrl
  4. this.target = target
  5. const findA = target => {
  6. while (target.nodeName !== 'A') {
  7. target = target.parentNode
  8. if (!target) return
  9. }
  10. return target
  11. }
  12. target.addEventListener('click', e => {
  13. let target = findA(e.target)
  14. if (!target) return
  15. window.history.pushState(null, "", target.href)
  16. dispatch(target.getAttribute('href'))
  17. e.preventDefault()
  18. })
  19. target.addEventListener('dragstart', e => {
  20. let target = findA(e.target)
  21. if (!target || !target.draggable) return
  22. e.dataTransfer.setData('application/prs.x', target.dataset['id'])
  23. e.dataTransfer.effectAllowed = 'link'
  24. })
  25. target.addEventListener('dragenter', e => {
  26. let target = findA(e.target)
  27. if (!target) return
  28. e.preventDefault()
  29. })
  30. target.addEventListener('dragover', e => {
  31. let target = findA(e.target)
  32. if (!target) return
  33. e.preventDefault()
  34. })
  35. target.addEventListener('drop', e => {
  36. let target = findA(e.target)
  37. if (!target || !target.draggable) return
  38. dispatch({ action: 'link', from: e.dataTransfer.getData("application/prs.x"), to: target.dataset['id']})
  39. && e.preventDefault()
  40. })
  41. this.graph = new Springy.Graph()
  42. this.layout = new Springy.Layout.ForceDirected(
  43. this.graph,
  44. 15,
  45. 1000.0, // Node repulsion
  46. 0.5 // Damping
  47. )
  48. }
  49. render(state) {
  50. const target = document.createElement('div')
  51. target.className = 'wrapper'
  52. const field = document.createElement('ul')
  53. field.className = 'items'
  54. const graph = this.graph
  55. for (const item of state.items) {
  56. graph.addNode(new Springy.Node(item.id, item))
  57. }
  58. for (const item of state.links) {
  59. graph.addEdge(new Springy.Edge(item.id, graph.nodeSet[item.from], graph.nodeSet[item.to], {
  60. text: state.items[item.from].linkable.filter(i => state.items[item.to].linkable.includes(i)).join(', ')
  61. }))
  62. }
  63. let currentBB = this.layout.getBoundingBox()
  64. let width = this.target.scrollWidth || 100
  65. let height = this.target.scrollHeight || 100
  66. var toScreen = function(p) {
  67. var size = currentBB.topright.subtract(currentBB.bottomleft);
  68. var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * (width - 60) + 30;
  69. var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - 60) + 30;
  70. return new Springy.Vector(sx, sy);
  71. };
  72. var renderer = new Springy.Renderer(
  73. this.layout,
  74. () => {
  75. currentBB = this.layout.getBoundingBox()
  76. width = this.target.scrollWidth
  77. height = this.target.scrollHeight
  78. field.innerHTML = ''
  79. },
  80. function drawEdge(edge, p1, p2) {
  81. const dom = document.createElement('span')
  82. dom.innerText = edge.data.text
  83. p1 = toScreen(p1)
  84. p2 = toScreen(p2)
  85. if (p1.y > p2.y) [p1, p2] = [p2, p1]
  86. const a = p2.x - p1.x
  87. const b = p2.y - p1.y
  88. const negative = (a < 0) != (b < 0)
  89. let rad = Math.atan(b / a)
  90. if (negative) rad = rad + Math.PI
  91. if (rad > Math.PI / 2) {
  92. rad += Math.PI
  93. ;[p2, p1] = [p1, p2]
  94. }
  95. dom.className = 'line'
  96. dom.style.transform = 'rotate(' + rad + 'rad) translateY(-1em)'
  97. dom.style.left = p1.x + 'px'
  98. dom.style.top = p1.y + 'px'
  99. dom.style.right = p2.x + 'px'
  100. dom.style.bottom = p2.y + 'px'
  101. dom.style.width = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) + 'px'
  102. // FIXME eigentlich falsches parent
  103. field.appendChild(dom)
  104. },
  105. function drawNode(item, p) {
  106. const target = toScreen(p)
  107. const dom = document.createElement('li')
  108. const a = document.createElement('a')
  109. const action = item.data.state == 'face-down' ? 'flip' : 'show'
  110. a.href = `/${item.id}/${action}`
  111. a.draggable = false
  112. if (item.data.state !== 'face-down') {
  113. a.style.backgroundImage = `url(/img/${item.data.img})`
  114. a.dataset['id'] = item.id
  115. a.draggable = true
  116. }
  117. dom.className = item.data.state
  118. dom.style.position = 'absolute'
  119. dom.style.left = (target.x - 60) + 'px'
  120. dom.style.top = (target.y - 60) + 'px'
  121. dom.appendChild(a)
  122. field.appendChild(dom)
  123. }, undefined, undefined,
  124. () => {
  125. const dd = new diffDOM.DiffDOM()
  126. const diff = dd.diff(this.target.children[0], field)
  127. dd.apply(this.target.children[0], diff)
  128. }
  129. );
  130. target.appendChild(field)
  131. if (state.show !== null) {
  132. const wrapper = document.createElement('a')
  133. wrapper.href = '/'
  134. wrapper.className = 'fullview'
  135. const fullview = document.createElement('div')
  136. const name = 'Gretchen'
  137. const desc = 'Näherin, 20'
  138. const img = '/img/gretchen.jpg'
  139. const text = '<p>Text</p><p>More text text text</p>'
  140. fullview.innerHTML = `
  141. <header>
  142. <img src=${img} />
  143. <h1>${name}</h1>
  144. <p class=desc>${desc}</p>
  145. </header>
  146. <div class=fulltext>${text}</div>
  147. `
  148. wrapper.appendChild(fullview)
  149. target.appendChild(wrapper)
  150. }
  151. const dd = new diffDOM.DiffDOM()
  152. const diff = dd.diff(this.target, target)
  153. dd.apply(this.target, diff)
  154. renderer.start();
  155. }
  156. }