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.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
  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(baseUrl, dispatch, target) {
  12. this.baseUrl = baseUrl
  13. this.target = target
  14. const findA = target => {
  15. while (target.nodeName !== 'A') {
  16. target = target.parentNode
  17. if (!target) return
  18. }
  19. return target
  20. }
  21. target.addEventListener('click', e => {
  22. let target = findA(e.target)
  23. if (!target) return
  24. window.history.pushState(null, "", target.href)
  25. dispatch(target.getAttribute('href'))
  26. e.preventDefault()
  27. })
  28. target.addEventListener('dragstart', e => {
  29. let target = findA(e.target)
  30. if (!target || !target.draggable) return
  31. e.dataTransfer.clearData()
  32. e.dataTransfer.setData('application/prs.x', target.dataset['id'])
  33. e.dataTransfer.effectAllowed = 'link'
  34. })
  35. target.addEventListener('dragenter', e => {
  36. let target = findA(e.target)
  37. if (!target) return
  38. e.preventDefault()
  39. })
  40. target.addEventListener('dragover', e => {
  41. let target = findA(e.target)
  42. if (!target) return
  43. e.preventDefault()
  44. })
  45. target.addEventListener('drop', e => {
  46. let target = findA(e.target)
  47. if (!target || !target.draggable) return
  48. dispatch({ action: 'link', from: e.dataTransfer.getData("application/prs.x"), to: target.dataset['id']})
  49. && e.preventDefault()
  50. })
  51. this.graph = new Springy.Graph()
  52. this.layout = new Springy.Layout.ForceDirected(
  53. this.graph,
  54. 15,
  55. 1000.0, // Node repulsion
  56. 0.5 // Damping
  57. )
  58. }
  59. renderIntro(intro) {
  60. this.target.appendChild(modal(intro))
  61. }
  62. render(state) {
  63. const target = document.createElement('div')
  64. target.className = 'wrapper'
  65. const field = document.createElement('ul')
  66. field.className = 'items'
  67. const graph = this.graph
  68. for (const item of state.items) {
  69. graph.addNode(new Springy.Node(item.id, item))
  70. }
  71. for (const item of state.links) {
  72. graph.addEdge(new Springy.Edge(item.id, graph.nodeSet[item.from], graph.nodeSet[item.to], {
  73. text: state.items[item.from].linkable.filter(i => state.items[item.to].linkable.includes(i)).join(', ')
  74. }))
  75. }
  76. let currentBB = this.layout.getBoundingBox()
  77. let width = this.target.scrollWidth || 100
  78. let height = this.target.scrollHeight || 100
  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 - 60) + 30;
  82. var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - 60) + 30;
  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 - 60) + 'px'
  133. dom.style.top = (target.y - 60) + '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. }