{"size": 1216, "ext": "js", "max_stars_repo_path": "frontend/node_modules/sme-router/src/history/Html5History.js", "max_stars_repo_name": "WangJianyong1124/zhaopin_admin", "max_stars_repo_stars_event_min_datetime": "2017-10-25T05:38:32.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T23:22:31.000Z", "max_issues_repo_path": "src/history/Html5History.js", "max_issues_repo_name": "xuxuejin/sme-router", "max_issues_repo_issues_event_min_datetime": "2018-08-15T02:27:38.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-09T23:16:13.000Z", "max_forks_repo_path": "src/history/Html5History.js", "max_forks_repo_name": "xuxuejin/sme-router", "max_forks_repo_forks_event_min_datetime": "2017-11-14T03:24:51.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-19T07:02:34.000Z"}
{"max_stars_count": 135, "max_issues_count": 4, "max_forks_count": 36, "avg_line_length": 22.5185185185, "max_line_length": 59, "alphanum_fraction": 0.6463815789}
import History from './History'

export default class Html5History extends History {
  /**
   * Creates an instance of Html5History.
   * @param {Object} options 
   *             
   * @memberof Html5History
   */
  constructor (options) {
    super(options)

    this._init()
    window.addEventListener('load', this._listen)
    window.addEventListener('popstate', this._listen)
  }

  _init () {
    // bind this._listen with this
    this._listen = (event) => {
      const path = `${location.pathname}${location.search}`
      const matchedRoutes = this.matcher.match(path)
      this._matchedCount = matchedRoutes.length
      this._fireHandlers(matchedRoutes, event.state)
    }
  }

  _routeTo (url, body) {
    const matchedRoutes = this.matcher.match(url)
    this._matchedCount = matchedRoutes.length

    this._fireHandlers(matchedRoutes, body)
  }

  go (url, body) {
    history.pushState(body, '', url)
    this._routeTo(url, body)
  }

  redirect (url, body) {
    history.replaceState(body, '', url)
    this._routeTo(url, body)
  }

  back () {
    history.go(-1)
  }

  stop () {
    window.removeEventListener('load', this._listen)
    window.removeEventListener('popstate', this._listen)
  }
}
