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.

45 lines
1.3 KiB

4 years ago
  1. export class Display {
  2. constructor(baseUrl, dispatch, target) {
  3. this.baseUrl = baseUrl
  4. this.target = target
  5. target.addEventListener('click', e => {
  6. let target = e.target
  7. while (target.nodeName !== 'A') {
  8. target = target.parentNode
  9. if (!target) return
  10. }
  11. window.history.pushState(null, "", target.href)
  12. dispatch(target.getAttribute('href'))
  13. e.preventDefault()
  14. })
  15. }
  16. render(state) {
  17. const target = document.createElement('div')
  18. target.className = 'wrapper'
  19. const field = document.createElement('ul')
  20. field.className = 'items'
  21. for (const item of state.items) {
  22. const dom = document.createElement('li')
  23. const a = document.createElement('a')
  24. const action = item.state == 'face-down' ? 'flip' : 'show'
  25. a.href = `/${item.id}/${action}`
  26. dom.className = item.state
  27. dom.appendChild(a)
  28. field.appendChild(dom)
  29. }
  30. target.appendChild(field)
  31. if (state.show !== null) {
  32. const wrapper = document.createElement('a')
  33. wrapper.href = '/'
  34. const fullview = document.createElement('div')
  35. wrapper.className = 'fullview'
  36. wrapper.appendChild(fullview)
  37. target.appendChild(wrapper)
  38. }
  39. const dd = new diffDOM.DiffDOM()
  40. const diff = dd.diff(this.target, target)
  41. dd.apply(this.target, diff)
  42. }
  43. }