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.

61 lines
1.4 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} 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. link(from, to) {
  26. if (from == to) return false
  27. const ok = state.items[from].linkable.filter(i => state.items[to].linkable.includes(i)).length > 0
  28. if (!ok) return false
  29. state.links.push({id: state.links.length, from, to})
  30. return true
  31. }
  32. }
  33. const actions = new Actions
  34. let display
  35. const dispatch = target => {
  36. let ret
  37. if (target.action) {
  38. ret = actions[target.action](target.from, target.to)
  39. } else if (target === '/end') {
  40. actions.end()
  41. } else {
  42. const match = target.match(/\/([^/]+)\/([^/]+)/)
  43. if (match && Object.hasOwnProperty.call(Actions.prototype, match[2])) {
  44. ret = actions[match[2]](match[1])
  45. } else actions.reset()
  46. }
  47. display.render(state)
  48. return ret
  49. }
  50. window.onpopstate = e => {
  51. dispatch(document.location.search)
  52. }
  53. display = new Display(dispatch, document.body.firstChild)
  54. if (document.location.search) dispatch(document.location.search)
  55. else display.renderIntro(intro)