<commit_before>BaseView = require('../shared/base/view')

try
  AppView = require(rendr.entryPath + '/views/app_view')
catch e
  AppView = require('../shared/base/app_view')

extractParamNamesRe = /:(\w+)/g
firstRender = true

module.exports = class Router extends Backbone.Router

  currentFragment: null
  previousFragment: null

  initialize: (options) ->
    @app = options.app
    @initRoutes()
    @on 'action', @trackAction

    @appView = new AppView({@app})
    @appView.render()

    @postInitialize()

  postInitialize: ->

  initRoutes: ->
    routes = require(rendr.entryPath + '/routes')

    # We have to iterate through the routes backwards,
    # so Backbone.History matches in same order as Express.
    patterns = _.keys(routes).reverse()
    routeInfos = _.values(routes).reverse()

    for pattern, i in patterns
      route = routeInfos[i]
      controller = route.controller
      action = route.action
      name = "#{controller}:#{action}"
      @route pattern, name, @getHandler(pattern, route)

  getHandler: (pattern, route) ->
    (paramsArray...) =>
      @trigger 'action', route
      if firstRender
        firstRender = false
        BaseView.attach(@app)
      else
        params = @getParamsHash(pattern, paramsArray)
        handler = @getController(route.controller)[route.action]
        handler = @authenticationFilter(handler, route)
        handler.call(@, params, @render)

  authenticationFilter: (handler, route) ->
    (params, callback) =>
      if route.authenticated && !@app.loggedIn()
        @redirectTo('/login')
      else
        handler.call(@, params, callback)

  getController: (controller) ->
    require("controllers/#{controller}_controller")

  getParamsHash: (pattern, paramsArray) ->
    paramNames = _.map(pattern.match(extractParamNamesRe), (name) -> name.slice(1))
    paramsHash = {}
    for param, i in paramsArray
      paramsHash[paramNames[i]] = paramsArray[i]
    paramsHash

  redirectTo: (path) ->
    @navigate path, true<commit_msg>throw a better exception when missing a controller action in the client router<commit_after>BaseView = require('../shared/base/view')

try
  AppView = require(rendr.entryPath + '/views/app_view')
catch e
  AppView = require('../shared/base/app_view')

extractParamNamesRe = /:(\w+)/g
firstRender = true

module.exports = class Router extends Backbone.Router

  currentFragment: null
  previousFragment: null

  initialize: (options) ->
    @app = options.app
    @initRoutes()
    @on 'action', @trackAction

    @appView = new AppView({@app})
    @appView.render()

    @postInitialize()

  postInitialize: ->

  initRoutes: ->
    routes = require(rendr.entryPath + '/routes')

    # We have to iterate through the routes backwards,
    # so Backbone.History matches in same order as Express.
    patterns = _.keys(routes).reverse()
    routeInfos = _.values(routes).reverse()

    for pattern, i in patterns
      route = routeInfos[i]
      controller = route.controller
      action = route.action
      name = "#{controller}:#{action}"
      @route pattern, name, @getHandler(pattern, route)

  getHandler: (pattern, route) ->
    (paramsArray...) =>
      @trigger 'action', route
      if firstRender
        firstRender = false
        BaseView.attach(@app)
      else
        params = @getParamsHash(pattern, paramsArray)
        handler = @getController(route.controller)[route.action]
        throw new Error("Missing action \"#{route.action}\" for controller \"#{route.controller}\"") unless handler
        handler = @authenticationFilter(handler, route)
        handler.call(@, params, @render)

  authenticationFilter: (handler, route) ->
    (params, callback) =>
      if route.authenticated && !@app.loggedIn()
        @redirectTo('/login')
      else
        handler.call(@, params, callback)

  getController: (controller) ->
    require("controllers/#{controller}_controller")

  getParamsHash: (pattern, paramsArray) ->
    paramNames = _.map(pattern.match(extractParamNamesRe), (name) -> name.slice(1))
    paramsHash = {}
    for param, i in paramsArray
      paramsHash[paramNames[i]] = paramsArray[i]
    paramsHash

  redirectTo: (path) ->
    @navigate path, true
{'fraction_non_alphanumeric': 0.13038305971924816, 'fraction_numerical': 0.0004758505829169641, 'mean_word_length': 3.1998001998001997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '5790346', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1217, 'n_words': 301}