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.

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