<commit_before>import {RouteRecognizer} from 'aurelia-route-recognizer';
import {join} from 'aurelia-path';
import {NavigationContext} from './navigation-context';
import {NavigationInstruction} from './navigation-instruction';
import {RouterConfiguration} from './router-configuration';
import {processPotential} from './util';

export class Router {
  constructor(history) {
    this.history = history;
    this.viewPorts = {};
    this.reset();
    this.baseUrl = '';
  }

  registerViewPort(viewPort, name) {
    name = name || 'default';

    if (typeof this.viewPorts[name] == 'function') {
      var callback = this.viewPorts[name];
      this.viewPorts[name] = viewPort;
      this.configureRouterForViewPort(viewPort, callback);
    } else {
      this.configureRouterForViewPort(viewPort, () =>{
        if (typeof this.viewPorts[name] == 'function') {
          var callback = this.viewPorts[name];
          this.viewPorts[name] = viewPort;
          callback(viewPort);
        }else{
          this.viewPorts[name] = viewPort;
        }
      });
    }
  }

  configureRouterForViewPort(viewPort, callback){
    if('configureRouter' in viewPort.executionContext){
      var result = viewPort.executionContext.configureRouter() || Promise.resolve();
      result.then(() => callback(viewPort));
    }else{
      callback(viewPort);
    }
  }

  refreshBaseUrl() {
    if (this.parent) {
      var baseUrl = this.parent.currentInstruction.getBaseUrl();
      this.baseUrl = this.parent.baseUrl + baseUrl;
    }
  }

  refreshNavigation() {
    var nav = this.navigation;

    for(var i = 0, length = nav.length; i < length; i++) {
      var current = nav[i];

      if (this.baseUrl[0] == '/') {
        current.href = '#' + this.baseUrl;
      } else {
        current.href = '#/' + this.baseUrl;
      }

      if (current.href[current.href.length - 1] != '/') {
        current.href += '/';
      }

      current.href += current.relativeHref;
    }
  }

  configure(callbackOrConfig) {
    if (typeof callbackOrConfig == 'function') {
      var config = new RouterConfiguration();
      callbackOrConfig(config);
      config.exportToRouter(this);
    } else {
      callbackOrConfig.exportToRouter(this);
    }

    return this;
  }

  navigate(fragment, options) {
    fragment = join(this.baseUrl, fragment);
    return this.history.navigate(fragment, options);
  }

  navigateBack() {
    this.history.navigateBack();
  }

  createChild() {
    var childRouter = new Router(this.history);
    childRouter.parent = this;
    return childRouter;
  }

  createNavigationInstruction(url='', parentInstruction=null) {
    var results = this.recognizer.recognize(url);
    var fragment, queryIndex, queryString;

    if (!results || !results.length) {
      results = this.childRecognizer.recognize(url);
    }

    fragment = url
    queryIndex = fragment.indexOf("?");

    if (queryIndex != -1) {
      fragment = url.substr(0, queryIndex);
      queryString = url.substr(queryIndex + 1);
    }

    if((!results || !results.length) && this.catchAllHandler){
      results = [{
        config:{},
        handler:this.catchAllHandler,
        params:{
          path:fragment
        }
      }];
    }

    if (results && results.length) {
      var first = results[0],
          fragment = url,
          queryIndex = fragment.indexOf('?'),
          queryString;

      if (queryIndex != -1) {
        fragment = url.substr(0, queryIndex);
        queryString = url.substr(queryIndex + 1);
      }

      var instruction = new NavigationInstruction(
        fragment,
        queryString,
        first.params,
        first.queryParams,
        first.config || first.handler,
        parentInstruction
        );

      if (typeof first.handler == 'function') {
        return first.handler(instruction);
      }

      return Promise.resolve(instruction);
    } else {
      return Promise.reject(new Error(`Route Not Found: ${url}`));<commit_msg>fix(router): add missing config data for dynamic routing scenarios<commit_after>import {RouteRecognizer} from 'aurelia-route-recognizer';
import {join} from 'aurelia-path';
import {NavigationContext} from './navigation-context';
import {NavigationInstruction} from './navigation-instruction';
import {RouterConfiguration} from './router-configuration';
import {processPotential} from './util';

export class Router {
  constructor(history) {
    this.history = history;
    this.viewPorts = {};
    this.reset();
    this.baseUrl = '';
  }

  registerViewPort(viewPort, name) {
    name = name || 'default';

    if (typeof this.viewPorts[name] == 'function') {
      var callback = this.viewPorts[name];
      this.viewPorts[name] = viewPort;
      this.configureRouterForViewPort(viewPort, callback);
    } else {
      this.configureRouterForViewPort(viewPort, () =>{
        if (typeof this.viewPorts[name] == 'function') {
          var callback = this.viewPorts[name];
          this.viewPorts[name] = viewPort;
          callback(viewPort);
        }else{
          this.viewPorts[name] = viewPort;
        }
      });
    }
  }

  configureRouterForViewPort(viewPort, callback){
    if('configureRouter' in viewPort.executionContext){
      var result = viewPort.executionContext.configureRouter() || Promise.resolve();
      result.then(() => callback(viewPort));
    }else{
      callback(viewPort);
    }
  }

  refreshBaseUrl() {
    if (this.parent) {
      var baseUrl = this.parent.currentInstruction.getBaseUrl();
      this.baseUrl = this.parent.baseUrl + baseUrl;
    }
  }

  refreshNavigation() {
    var nav = this.navigation;

    for(var i = 0, length = nav.length; i < length; i++) {
      var current = nav[i];

      if (this.baseUrl[0] == '/') {
        current.href = '#' + this.baseUrl;
      } else {
        current.href = '#/' + this.baseUrl;
      }

      if (current.href[current.href.length - 1] != '/') {
        current.href += '/';
      }

      current.href += current.relativeHref;
    }
  }

  configure(callbackOrConfig) {
    if (typeof callbackOrConfig == 'function') {
      var config = new RouterConfiguration();
      callbackOrConfig(config);
      config.exportToRouter(this);
    } else {
      callbackOrConfig.exportToRouter(this);
    }

    return this;
  }

  navigate(fragment, options) {
    fragment = join(this.baseUrl, fragment);
    return this.history.navigate(fragment, options);
  }

  navigateBack() {
    this.history.navigateBack();
  }

  createChild() {
    var childRouter = new Router(this.history);
    childRouter.parent = this;
    return childRouter;
  }

  createNavigationInstruction(url='', parentInstruction=null) {
    var results = this.recognizer.recognize(url);
    var fragment, queryIndex, queryString;

    if (!results || !results.length) {
      results = this.childRecognizer.recognize(url);
    }

    fragment = url
    queryIndex = fragment.indexOf("?");

    if (queryIndex != -1) {
      fragment = url.substr(0, queryIndex);
      queryString = url.substr(queryIndex + 1);
    }

    if((!results || !results.length) && this.catchAllHandler){
      results = [{
        config:{
          navModel:{}
        },
        handler:this.catchAllHandler,
        params:{
          path:fragment
        }
      }];
    }

    if (results && results.length) {
      var first = results[0],
          fragment = url,
          queryIndex = fragment.indexOf('?'),
          queryString;

      if (queryIndex != -1) {
        fragment = url.substr(0, queryIndex);
        queryString = url.substr(queryIndex + 1);
      }

      var instruction = new NavigationInstruction(
        fragment,
        queryString,
        first.params,
        first.queryParams,
        first.config || first.handler,
        parentInstruction
        );

      if (typeof first.handler == "function") {
        return first.handler(instruction).then(instruction => {
          if (!("viewPorts" in instruction.config)) {
            instruction.config.viewPorts = {
              "default": {
                moduleId: instruction.config.moduleId
              }
            };
          }

          return instruction;
        });
      }

      return Promise.resolve(instruction);
    } else {
      return Promise.reject(new Error(`Route Not Found: ${url}`));
{'fraction_non_alphanumeric': 0.13410376790658482, 'fraction_numerical': 0.002407608041410858, 'mean_word_length': 2.5054852320675107, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608045', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2390, 'n_words': 476}