<commit_before>/*global rendr*/

var AppView, Backbone, BaseRouter, BaseView, ClientRouter, extractParamNamesRe, firstRender, plusRe, _, defaultRootPath;

_ = require('underscore');
Backbone = require('backbone');
BaseRouter = require('../shared/base/router');
BaseView = require('../shared/base/view');

extractParamNamesRe = /:(\w+)/g;

plusRe = /\+/g;

firstRender = true;

defaultRootPath = '';

function noop() {}

module.exports = ClientRouter;

function ClientRouter(options) {
  this._router = new Backbone.Router();
  BaseRouter.apply(this, arguments);
}

/**
 * Set up inheritance.
 */
ClientRouter.prototype = Object.create(BaseRouter.prototype);
ClientRouter.prototype.constructor = ClientRouter;

ClientRouter.prototype.currentFragment = null;

ClientRouter.prototype.previousFragment = null;

/*
 * In a controller action, can access the current route
 * definition with `this.currentRoute`.
 */
ClientRouter.prototype.currentRoute = null;

/*
 * Instance of Backbone.Router used to manage browser history.
 */
ClientRouter.prototype._router = null;

/*
 * We need to reverse the routes in the client because
 * Backbone.History matches in reverse.
 */
ClientRouter.prototype.reverseRoutes = true;

ClientRouter.prototype.initialize = function(options) {
  this.app = options.app;

  try {
    AppView = require(options.entryPath + 'app/views/app_view');
  } catch (e) {
    AppView = require('./app_view');
  }

  // We do this here so that it's available in AppView initialization.
  this.app.router = this;

  this.on('route:add', this.addBackboneRoute, this);
  this.on('action:start', this.trackAction, this);
  this.app.on('reload', this.renderView, this);

  this.appView = new AppView({
    app: this.app
  });

  this.appView.render();
  this.buildRoutes();
  this.postInitialize();
};

ClientRouter.prototype.postInitialize = noop;

/*
 * Piggyback on adding new route definition events
 * to also add to Backbone.Router.
 */
ClientRouter.prototype.addBackboneRoute = function(routeObj) {
  var handler, name, pattern, route;

  // Backbone.History wants no leading slash on strings.
  pattern = (routeObj[0] instanceof RegExp) ? routeObj[0] : routeObj[0].slice(1);
  route = routeObj[1];
  handler = routeObj[2];
  name = route.controller + ":" + route.action;

  this._router.route(pattern, name, handler);
};

ClientRouter.prototype.getHandler = function(action, pattern, route) {
  var router = this;

  // abstract action call
  function actionCall(action, params)
  {
    action.call(router, params, router.getRenderCallback(route));
  }

  // This returns a function which is called by Backbone.history.
  return function() {
    var params, paramsArray, views, redirect;

    router.trigger('action:start', route, firstRender);
    router.currentRoute = route;

    if (firstRender) {
      views = BaseView.attach(router.app);
      router.currentView = router.getMainView(views);
      router.trigger('action:end', route, firstRender);
      firstRender = false;
    } else {
      paramsArray = _.toArray(arguments);
      params = router.getParamsHash(pattern, paramsArray, window.location.search);

      redirect = router.getRedirect(route, params);
      /*
       * If `redirect` is present, then do a redirect and return.
       */
      if (redirect != null) {
        router.redirectTo(redirect, {replace: true});
      } else {
        if (!action) {
          throw new Error("Missing action \"" + route.action + "\" for controller \"" + route.controller + "\"");
        }
        // in AMD environment action is the string containing path to the controller
        // which will be loaded async (might be preloaded)
        else if (typeof action == 'string')
        {
          require([action], function(controller)
          {
            // check we have everything we need
            if (typeof controller[route.action] != 'function')
            {
              throw new Error("Missing action \"" + route.action + "\" for controller \"" + route.controller + "\"");
            }

            actionCall(controller[route.action], params);
          });
        }
        else
        {
          actionCall(action, params);
        }
      }
    }
  };
};

/*
 * Can be overridden by applications
 * if the initial render is more complicated.
 */
ClientRouter.prototype.getMainView = function(views) {
  var $content = this.appView.$content;<commit_msg>Another workaround for requirejs after-thought-conversion.<commit_after>/*global rendr*/

var AppView, Backbone, BaseRouter, BaseView, ClientRouter, extractParamNamesRe, firstRender, plusRe, _, defaultRootPath;
// Since we make rendr files AMD friendly on app setup stage
// we need to pretend that this code is pure commonjs
// means no AMD-style require calls
var requireAMD = require;

_ = require('underscore');
Backbone = require('backbone');
BaseRouter = require('../shared/base/router');
BaseView = require('../shared/base/view');

extractParamNamesRe = /:(\w+)/g;

plusRe = /\+/g;

firstRender = true;

defaultRootPath = '';

function noop() {}

module.exports = ClientRouter;

function ClientRouter(options) {
  this._router = new Backbone.Router();
  BaseRouter.apply(this, arguments);
}

/**
 * Set up inheritance.
 */
ClientRouter.prototype = Object.create(BaseRouter.prototype);
ClientRouter.prototype.constructor = ClientRouter;

ClientRouter.prototype.currentFragment = null;

ClientRouter.prototype.previousFragment = null;

/*
 * In a controller action, can access the current route
 * definition with `this.currentRoute`.
 */
ClientRouter.prototype.currentRoute = null;

/*
 * Instance of Backbone.Router used to manage browser history.
 */
ClientRouter.prototype._router = null;

/*
 * We need to reverse the routes in the client because
 * Backbone.History matches in reverse.
 */
ClientRouter.prototype.reverseRoutes = true;

ClientRouter.prototype.initialize = function(options) {
  this.app = options.app;

  try {
    AppView = require(options.entryPath + 'app/views/app_view');
  } catch (e) {
    AppView = require('./app_view');
  }

  // We do this here so that it's available in AppView initialization.
  this.app.router = this;

  this.on('route:add', this.addBackboneRoute, this);
  this.on('action:start', this.trackAction, this);
  this.app.on('reload', this.renderView, this);

  this.appView = new AppView({
    app: this.app
  });

  this.appView.render();
  this.buildRoutes();
  this.postInitialize();
};

ClientRouter.prototype.postInitialize = noop;

/*
 * Piggyback on adding new route definition events
 * to also add to Backbone.Router.
 */
ClientRouter.prototype.addBackboneRoute = function(routeObj) {
  var handler, name, pattern, route;

  // Backbone.History wants no leading slash on strings.
  pattern = (routeObj[0] instanceof RegExp) ? routeObj[0] : routeObj[0].slice(1);
  route = routeObj[1];
  handler = routeObj[2];
  name = route.controller + ":" + route.action;

  this._router.route(pattern, name, handler);
};

ClientRouter.prototype.getHandler = function(action, pattern, route) {
  var router = this;

  // abstract action call
  function actionCall(action, params)
  {
    action.call(router, params, router.getRenderCallback(route));
  }

  // This returns a function which is called by Backbone.history.
  return function() {
    var params, paramsArray, views, redirect;

    router.trigger('action:start', route, firstRender);
    router.currentRoute = route;

    if (firstRender) {
      views = BaseView.attach(router.app);
      router.currentView = router.getMainView(views);
      router.trigger('action:end', route, firstRender);
      firstRender = false;
    } else {
      paramsArray = _.toArray(arguments);
      params = router.getParamsHash(pattern, paramsArray, window.location.search);

      redirect = router.getRedirect(route, params);
      /*
       * If `redirect` is present, then do a redirect and return.
       */
      if (redirect != null) {
        router.redirectTo(redirect, {replace: true});
      } else {
        if (!action) {
          throw new Error("Missing action \"" + route.action + "\" for controller \"" + route.controller + "\"");
        }
        // in AMD environment action is the string containing path to the controller
        // which will be loaded async (might be preloaded)
        else if (typeof action == 'string')
        {
          // Only used in AMD environment
          requireAMD([action], function(controller)
          {
            // check we have everything we need
            if (typeof controller[route.action] != 'function')
            {
              throw new Error("Missing action \"" + route.action + "\" for controller \"" + route.controller + "\"");
            }

            actionCall(controller[route.action], params);
          });
        }
        else
        {
          actionCall(action, params);
        }
      }
    }
  };
};

/*
 * Can be overridden by applications
 * if the initial render is more complicated.
 */
ClientRouter.prototype.getMainView = function(views) {
  var $content = this.appView.$content;
{'fraction_non_alphanumeric': 0.12661015083122315, 'fraction_numerical': 0.0013211493999779808, 'mean_word_length': 3.1707988980716255, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '4178136', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2548, 'n_words': 689}