<commit_before>define [
  'underscore'
  'backbone'
  'chaplin/mediator'
  'chaplin/lib/event_broker'
  'chaplin/lib/route'
], (_, Backbone, mediator, EventBroker, Route) ->
  'use strict'

  # The router which is a replacement for Backbone.Router.
  # Like the standard router, it creates a Backbone.History
  # instance and registers routes on it.

  class Router # This class does not extend Backbone.Router

    # Borrow the static extend method from Backbone
    @extend = Backbone.Model.extend

    # Mixin an EventBroker
    _(@prototype).extend EventBroker

    constructor: (@options = {}) ->
      _(@options).defaults
        pushState: true

      @subscribeEvent '!router:route', @routeHandler
      @subscribeEvent '!router:changeURL', @changeURLHandler

      @createHistory()

    # Create a Backbone.History instance
    createHistory: ->
      Backbone.history or= new Backbone.History()

    startHistory: ->
      # Start the Backbone.History instance to start routing
      # This should be called after all routes have been registered
      Backbone.history.start @options

    # Stop the current Backbone.History instance from observing URL changes
    stopHistory: ->
      Backbone.history.stop() if Backbone.History.started

    # Connect an address with a controller action
    # Directly create a route on the Backbone.History instance
    match: (pattern, target, options = {}) =>
      # Create the route
      route = new Route pattern, target, options
      # Register the route at the Backbone.History instance.
      # Don’t use Backbone.history.route here because it calls
      # handlers.unshift, inserting the handler at the top of the list.
      # Since we want routes to match in the order they were specified,
      # we’re appending the route at the end.
      Backbone.history.handlers.push {route, callback: route.handler}
      route

    # Route a given URL path manually, returns whether a route matched
    # This looks quite like Backbone.History::loadUrl but it
    # accepts an absolute URL with a leading slash (e.g. /foo)
    # and passes a changeURL param to the callback function.
    route: (path, options = {}) =>
      # Default options to changeURL: true to mimic existing behavior while
      # allowing additional options to be forwarded on
      _(options).defaults
        changeURL: true

      # Remove leading hash or slash
      path = path.replace /^(\/#|\/)/, ''
      for handler in Backbone.history.handlers
        if handler.route.test(path)
          handler.callback path, options
          return true
      false

    # Handler for the global !router:route event
    routeHandler: (path, options, callback) ->
      # Assume only path and callback were passed if we only got 2 arguments
      # and the second argument is a function; so as to mimic existing chaplin
      # behavior
      if arguments.length is 2 and _(options).isFunction()
        [callback, options] = [options, {}]

      # Continue on to handle the route; pass in options hash
      routed = @route path, options
      callback? routed

    # Change the current URL, add a history entry.
    changeURL: (url, options = {}) ->
      # Default options to trigger: false (which is Backbone’s
      # default behavior, but added for clarity)
      _(options).defaults
        trigger: false

      # Navigate to the passed URL and forward options to backbone
      Backbone.history.navigate url, options

    # Handler for the global !router:changeURL event
    # Accepts both the url and an options hash that is forwarded to backbone
    changeURLHandler: (url, options = {}) ->
      @changeURL url, options

    # Disposal
    # --------

    disposed: false<commit_msg>Remove compatibility, this will be a breaking change Whitelist navigate options in changeURL Comments<commit_after>define [
  'underscore'
  'backbone'
  'chaplin/mediator'
  'chaplin/lib/event_broker'
  'chaplin/lib/route'
], (_, Backbone, mediator, EventBroker, Route) ->
  'use strict'

  # The router which is a replacement for Backbone.Router.
  # Like the standard router, it creates a Backbone.History
  # instance and registers routes on it.

  class Router # This class does not extend Backbone.Router

    # Borrow the static extend method from Backbone
    @extend = Backbone.Model.extend

    # Mixin an EventBroker
    _(@prototype).extend EventBroker

    constructor: (@options = {}) ->
      _(@options).defaults
        pushState: true

      @subscribeEvent '!router:route', @routeHandler
      @subscribeEvent '!router:changeURL', @changeURLHandler

      @createHistory()

    # Create a Backbone.History instance
    createHistory: ->
      Backbone.history or= new Backbone.History()

    startHistory: ->
      # Start the Backbone.History instance to start routing
      # This should be called after all routes have been registered
      Backbone.history.start @options

    # Stop the current Backbone.History instance from observing URL changes
    stopHistory: ->
      Backbone.history.stop() if Backbone.History.started

    # Connect an address with a controller action
    # Creates a route on the Backbone.History instance
    match: (pattern, target, options = {}) =>
      # Create the route
      route = new Route pattern, target, options
      # Register the route at the Backbone.History instance.
      # Don’t use Backbone.history.route here because it calls
      # handlers.unshift, inserting the handler at the top of the list.
      # Since we want routes to match in the order they were specified,
      # we’re appending the route at the end.
      Backbone.history.handlers.push {route, callback: route.handler}
      route

    # Route a given URL path manually, returns whether a route matched
    # This looks quite like Backbone.History::loadUrl but it
    # accepts an absolute URL with a leading slash (e.g. /foo)
    # and passes a changeURL param to the callback function.
    route: (path, options = {}) =>
      _(options).defaults
        changeURL: true

      # Remove leading hash or slash
      path = path.replace /^(\/#|\/)/, ''
      for handler in Backbone.history.handlers
        if handler.route.test(path)
          handler.callback path, options
          return true
      false

    # Handler for the global !router:route event
    routeHandler: (path, options, callback) ->
      routed = @route path, options
      callback? routed

    # Change the current URL, add a history entry.
    changeURL: (url, options = {}) ->
      navigateOptions =
        # Do not trigger or replace per default
        trigger: options.trigger is true
        replace: options.replace is true

      # Navigate to the passed URL and forward options to Backbone
      Backbone.history.navigate url, navigateOptions

    # Handler for the global !router:changeURL event
    # Accepts both the url and an options hash that is forwarded to Backbone
    changeURLHandler: (url, options) ->
      @changeURL url, options

    # Disposal
    # --------

    disposed: false
{'fraction_non_alphanumeric': 0.07964224872231687, 'fraction_numerical': 0.00028392958546280523, 'mean_word_length': 2.797843665768194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4600275', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1815, 'n_words': 739}