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.

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