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.

173 lines
5.6 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
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. var toScreen = function(p) {
  79. var size = currentBB.topright.subtract(currentBB.bottomleft);
  80. var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * (width - 60) + 30;
  81. var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - 60) + 30;
  82. return new Springy.Vector(sx, sy);
  83. };
  84. var renderer = new Springy.Renderer(
  85. this.layout,
  86. () => {
  87. currentBB = this.layout.getBoundingBox()
  88. width = this.target.scrollWidth
  89. height = this.target.scrollHeight
  90. field.innerHTML = ''
  91. },
  92. function drawEdge(edge, p1, p2) {
  93. const dom = document.createElement('span')
  94. dom.innerText = edge.data.text
  95. p1 = toScreen(p1)
  96. p2 = toScreen(p2)
  97. if (p1.y > p2.y) [p1, p2] = [p2, p1]
  98. const a = p2.x - p1.x
  99. const b = p2.y - p1.y
  100. const negative = (a < 0) != (b < 0)
  101. let rad = Math.atan(b / a)
  102. if (negative) rad = rad + Math.PI
  103. if (rad > Math.PI / 2) {
  104. rad += Math.PI
  105. ;[p2, p1] = [p1, p2]
  106. }
  107. dom.className = 'line'
  108. dom.style.transform = 'rotate(' + rad + 'rad) translateY(-1em)'
  109. dom.style.left = p1.x + 'px'
  110. dom.style.top = p1.y + 'px'
  111. dom.style.right = p2.x + 'px'
  112. dom.style.bottom = p2.y + 'px'
  113. dom.style.width = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) + 'px'
  114. // FIXME eigentlich falsches parent
  115. field.appendChild(dom)
  116. },
  117. function drawNode(item, p) {
  118. const target = toScreen(p)
  119. const dom = document.createElement('li')
  120. const a = document.createElement('a')
  121. const action = item.data.state == 'face-down' ? 'flip' : 'show'
  122. a.href = `/${item.id}/${action}`
  123. a.draggable = false
  124. if (item.data.state !== 'face-down') {
  125. a.style.backgroundImage = `url("/img/${item.data.icon}")`
  126. a.dataset['id'] = item.id
  127. a.draggable = true
  128. }
  129. dom.className = item.data.state
  130. dom.style.position = 'absolute'
  131. dom.style.left = (target.x - 60) + 'px'
  132. dom.style.top = (target.y - 60) + 'px'
  133. dom.appendChild(a)
  134. field.appendChild(dom)
  135. }, undefined, undefined,
  136. () => {
  137. const dd = new diffDOM.DiffDOM()
  138. const diff = dd.diff(this.target.children[0], field)
  139. dd.apply(this.target.children[0], diff)
  140. }
  141. );
  142. target.appendChild(field)
  143. if (state.show !== null) {
  144. const data = state.items[state.show]
  145. const name = data.name
  146. const desc = data.desc
  147. const img = '/img/' + data.img
  148. const text = (data.text || []).map(v => '<p>' + v + '</p>').join('')
  149. const wrapper = modal(`
  150. <header>
  151. <img src=${img} />
  152. <h1>${name}</h1>
  153. <p class=desc>${desc}</p>
  154. </header>
  155. <div class=fulltext>${text}
  156. <blockquote>${data.quote || ''}</blockquote></div>
  157. `)
  158. target.appendChild(wrapper)
  159. }
  160. const dd = new diffDOM.DiffDOM()
  161. const diff = dd.diff(this.target, target)
  162. dd.apply(this.target, diff)
  163. renderer.start();
  164. }
  165. }