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.

53 lines
1.1 KiB

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