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.

54 lines
1.1 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
  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. state.links.push({id: state.links.length, from, to})
  23. return true
  24. }
  25. }
  26. const actions = new Actions
  27. let display
  28. const dispatch = target => {
  29. let ret
  30. if (target.action) {
  31. ret = actions[target.action](target.from, target.to)
  32. } else {
  33. const match = target.match(/\/([^/]+)\/([^/]+)/)
  34. if (match && Object.hasOwnProperty.call(Actions.prototype, match[2])) {
  35. ret = actions[match[2]](match[1])
  36. } else actions.reset()
  37. }
  38. display.render(state)
  39. return ret
  40. }
  41. window.onpopstate = e => {
  42. dispatch(document.location.search)
  43. }
  44. display = new Display('', dispatch, target)
  45. dispatch(document.location.search)
  46. document.body.appendChild(target)