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.

64 lines
1.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
  1. import {Display} from './display.js'
  2. import {intro, outro, sources} from './texts.js'
  3. const state = {items: [], show: null, links: [] }
  4. import {items} from './data.js'
  5. for (let i = 0; i < items.length; ++i) {
  6. let item = items[i]
  7. item.id = i
  8. item.state = 'face-down'
  9. state.items.push(item)
  10. }
  11. class Actions {
  12. flip(id) {
  13. state.items[id].state = 'face-up'
  14. }
  15. show(id) {
  16. state.show = id
  17. }
  18. reset() {
  19. state.show = null
  20. state.special = null
  21. }
  22. end() {
  23. state.special = outro
  24. }
  25. sources() {
  26. state.special = sources
  27. }
  28. link(from, to) {
  29. if (from == to) return false
  30. const ok = state.items[from].linkable.filter(i => state.items[to].linkable.includes(i)).length > 0
  31. if (!ok) return false
  32. state.links.push({id: state.links.length, from, to})
  33. return true
  34. }
  35. }
  36. const actions = new Actions
  37. let display
  38. const dispatch = target => {
  39. let ret
  40. if (target.action) {
  41. ret = actions[target.action](target.from, target.to)
  42. } else {
  43. const match = target.match(/\/([^/]+)(?:\/([^/]+))?/)
  44. if (match && match.length > 1 && Object.hasOwnProperty.call(Actions.prototype, match[2])) {
  45. ret = actions[match[2]](match[1])
  46. } else if (match&& Object.hasOwnProperty.call(Actions.prototype, match[1])) {
  47. ret = actions[match[1]]()
  48. } else actions.reset()
  49. }
  50. display.render(state)
  51. return ret
  52. }
  53. window.onpopstate = e => {
  54. dispatch(document.location.search)
  55. }
  56. display = new Display(dispatch, document.body.firstChild)
  57. if (document.location.search) dispatch(document.location.search)
  58. else display.renderIntro(intro)