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
1018 B

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. for (let i = 0; i < 16; ++i) {
  5. state.items.push({id: i, state: 'face-down', img: 'weben.svg'})
  6. }
  7. class Actions {
  8. flip(id) {
  9. state.items[id].state = 'face-up'
  10. }
  11. show(id) {
  12. state.show = id
  13. }
  14. reset() {
  15. state.show = null
  16. }
  17. link(from, to) {
  18. state.links.push({id: state.links.length, from, to})
  19. }
  20. }
  21. const actions = new Actions
  22. let display
  23. const dispatch = target => {
  24. if (target.action) {
  25. actions[target.action](target.from, target.to)
  26. } else {
  27. const match = target.match(/\/([^/]+)\/([^/]+)/)
  28. if (match && Object.hasOwnProperty.call(Actions.prototype, match[2])) {
  29. actions[match[2]](match[1])
  30. } else actions.reset()
  31. }
  32. display.render(state)
  33. }
  34. window.onpopstate = e => {
  35. dispatch(document.location.search)
  36. }
  37. display = new Display('', dispatch, target)
  38. dispatch(document.location.search)
  39. document.body.appendChild(target)