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.

38 lines
830 B

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 }
  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. }
  18. const actions = new Actions
  19. let display
  20. const dispatch = target => {
  21. const match = target.match(/\/([^/]+)\/([^/]+)/)
  22. if (match && Object.hasOwnProperty.call(Actions.prototype, match[2])) {
  23. actions[match[2]](match[1])
  24. } else actions.reset()
  25. display.render(state)
  26. }
  27. window.onpopstate = e => {
  28. dispatch(document.location.search)
  29. }
  30. display = new Display('', dispatch, target)
  31. dispatch(document.location.search)
  32. document.body.appendChild(target)