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.

49 lines
1.0 KiB

  1. const EventEmitter = require('events')
  2. const queryString = require('query-string')
  3. module.exports = class Browser extends EventEmitter {
  4. constructor (id, dom) {
  5. super()
  6. this.id = id
  7. this.dom = dom
  8. this.history = []
  9. }
  10. buildPage (parameters) {
  11. this.clear()
  12. hooks.call('browser-' + this.id, this, parameters)
  13. this.emit('buildPage', parameters)
  14. this.parameters = parameters
  15. }
  16. clear () {
  17. while (this.dom.lastChild) {
  18. this.dom.removeChild(this.dom.lastChild)
  19. }
  20. }
  21. catchLinks () {
  22. const links = this.dom.getElementsByTagName('a')
  23. Array.from(links).forEach(link => {
  24. const href = link.getAttribute('href')
  25. if (href.substr(0, this.id.length + 2) === '#' + this.id + '?') {
  26. link.onclick = () => {
  27. this.history.push(this.parameters)
  28. const parameters = queryString.parse(href.substr(this.id.length + 2))
  29. this.buildPage(parameters)
  30. return false
  31. }
  32. }
  33. })
  34. }
  35. close () {
  36. this.clear()
  37. this.emit('close')
  38. }
  39. }