{"size": 933, "ext": "js", "max_stars_repo_path": "src/components/route-link/component.js", "max_stars_repo_name": "HansUXdev/marko-router5", "max_stars_repo_stars_event_min_datetime": "2018-08-06T13:04:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-09T09:41:00.000Z", "max_issues_repo_path": "src/components/route-link/component.js", "max_issues_repo_name": "HansUXdev/marko-router5", "max_issues_repo_issues_event_min_datetime": "2018-08-10T14:28:50.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T01:33:56.000Z", "max_forks_repo_path": "src/components/route-link/component.js", "max_forks_repo_name": "jesse1983/marko-router5", "max_forks_repo_forks_event_min_datetime": "2018-09-07T12:41:18.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-16T22:36:47.000Z"}
{"max_stars_count": 27, "max_issues_count": 14, "max_forks_count": 3, "avg_line_length": 32.1724137931, "max_line_length": 118, "alphanum_fraction": 0.6334405145}
const { isActive, navigate } = require('../../index');

module.exports = class {
  onMount() {
    if (window.router) {
      window.router.useMiddleware(route => (toState, fromState, done) => this.middleActive(toState, fromState, done));
    }
    if (!this.pass) this.middleActive({ path: window.location.pathname });
  }

  navigate(ev) {
    ev.preventDefault();
    if (this.input.href) navigate(this.input.href);
    else throw new Error('Missing attribute \'href\' on route-link component');
  }

  middleActive(toState, fromState, done) {
    this.pass = true;
    const a = this.input.parentClass ? this.getEl('a').parentElement : this.getEl('a');
    const className = this.input.activeClass || 'active';
    if (isActive(this.input.href, toState)) {
      className.split(' ').forEach(c => a.classList.add(c));
    } else {
      className.split(' ').forEach(c => a.classList.remove(c));
    }
    if (done) done();
  }
}
