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.

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