<commit_before>/*****************************************************************************/
/* Imports */
/*****************************************************************************/
var DynamicTemplate = Iron.DynamicTemplate;

/*****************************************************************************/
/* Helpers */
/*****************************************************************************/
/**
 * Find the first Layout in the rendered parent hierarchy.
 */
findFirstLayout = function (view) {
  while (view) {
    if (view.kind === 'Iron.Layout')
      return view.__dynamicTemplate__;
    else
      view = view.parentView;
  }

  return null;
};

/*****************************************************************************/
/* Layout */
/*****************************************************************************/

/**
 * Dynamically render templates into regions.
 *
 * Layout inherits from Iron.DynamicTemplate and provides the ability to create
 * regions that a user can render templates or content blocks into. The layout
 * and each region is an instance of DynamicTemplate so the template and data
 * contexts are completely dynamic and programmable in javascript.
 */
Layout = function (options) {
  var self = this;

  Layout.__super__.constructor.apply(this, arguments);

  options = options || {};
  this.kind = 'Iron.Layout';
  this._regions = {};
  this._regionHooks = {};
  this.defaultTemplate('__IronDefaultLayout__');

  // if there's block content then render that
  // to the main region
  if (options.content)
    this.render(options.content);
};

/**
 * The default region for a layout where the main content will go.
 */
DEFAULT_REGION = Layout.DEFAULT_REGION = 'main';

/**
 * Inherits from Iron.DynamicTemplate which gives us the ability to set the
 * template and data context dynamically.
 */
Meteor._inherits(Layout, Iron.DynamicTemplate);

/**
 * Return the DynamicTemplate instance for a given region. If the region doesn't
 * exist it is created.
 *
 * The regions object looks like this:
 *
 *  {
 *    "main": DynamicTemplate,
 *    "footer": DynamicTemplate,
 *    .
 *    .
 *    .
 *  }
 */
Layout.prototype.region = function (name, options) {
  return this._ensureRegion(name, options);
};

/**
 * Set the template for a region.
 */
Layout.prototype.render = function (template, options) {
  // having options is usually good
  options = options || {};

  // let the user specify the region to render the template into
  var region = options.to || options.region || DEFAULT_REGION;

  // get the DynamicTemplate for this region
  var dynamicTemplate = this.region(region);

  // if we're in a rendering transaction, track that we've rendered this
  // particular region
  this._trackRenderedRegion(region);

  // set the template value for the dynamic template
  dynamicTemplate.template(template);

  // if we have data go ahead and set the data for the dynamic template,
  // otherwise, leave it be.
  if (options.data)
    dynamicTemplate.data(options.data);
};

/**
 * Returns true if the given region is defined and false otherwise.
 */
Layout.prototype.has = function (region) {
  region = region || Layout.DEFAULT_REGION;
  return !!this._regions[region];
};

/**
 * Returns an array of region keys.
 */
Layout.prototype.regionKeys = function () {
  return _.keys(this._regions);
};

/**
 * Clear a given region or the "main" region by default.
 */
Layout.prototype.clear = function (region) {
  region = region || Layout.DEFAULT_REGION;

  // we don't want to create a region if it didn't exist before
  if (this.has(region))
    this.region(region).template(null);

  // chain it up
  return this;
};

/**
 * Clear all regions.
 */
Layout.prototype.clearAll = function () {
  _.each(this._regions, function (dynamicTemplate) {
    dynamicTemplate.template(null);
  });

  // chain it up
  return this;
};

/**
 * Start tracking rendered regions.
 */
Layout.prototype.beginRendering = function () {
  if (this._renderedRegions)
    throw new Error("You called beginRendering again before calling endRendering");
  this._renderedRegions = {};
};

/**
 * Track a rendered region if we're in a transaction.
 */
Layout.prototype._trackRenderedRegion = function (region) {
  if (!this._renderedRegions)
    return;
  this._renderedRegions[region] = true;
};

/**
 * Stop a rendering transaction and retrieve the rendered regions.
 */
Layout.prototype.endRendering = function () {
  // we flush here to ensure all of the {{#contentFor}} inclusions have had a
  // chance to render from our templates, otherwise we'll never know about
  // them. 
  Deps.flush();
  var renderedRegions = this._renderedRegions;
  this._renderedRegions = null;
  return _.keys(renderedRegions);
};<commit_msg>add {flush: false} option to endRendering to prevent a flush.<commit_after>/*****************************************************************************/
/* Imports */
/*****************************************************************************/
var DynamicTemplate = Iron.DynamicTemplate;

/*****************************************************************************/
/* Helpers */
/*****************************************************************************/
/**
 * Find the first Layout in the rendered parent hierarchy.
 */
findFirstLayout = function (view) {
  while (view) {
    if (view.kind === 'Iron.Layout')
      return view.__dynamicTemplate__;
    else
      view = view.parentView;
  }

  return null;
};

/*****************************************************************************/
/* Layout */
/*****************************************************************************/

/**
 * Dynamically render templates into regions.
 *
 * Layout inherits from Iron.DynamicTemplate and provides the ability to create
 * regions that a user can render templates or content blocks into. The layout
 * and each region is an instance of DynamicTemplate so the template and data
 * contexts are completely dynamic and programmable in javascript.
 */
Layout = function (options) {
  var self = this;

  Layout.__super__.constructor.apply(this, arguments);

  options = options || {};
  this.kind = 'Iron.Layout';
  this._regions = {};
  this._regionHooks = {};
  this.defaultTemplate('__IronDefaultLayout__');

  // if there's block content then render that
  // to the main region
  if (options.content)
    this.render(options.content);
};

/**
 * The default region for a layout where the main content will go.
 */
DEFAULT_REGION = Layout.DEFAULT_REGION = 'main';

/**
 * Inherits from Iron.DynamicTemplate which gives us the ability to set the
 * template and data context dynamically.
 */
Meteor._inherits(Layout, Iron.DynamicTemplate);

/**
 * Return the DynamicTemplate instance for a given region. If the region doesn't
 * exist it is created.
 *
 * The regions object looks like this:
 *
 *  {
 *    "main": DynamicTemplate,
 *    "footer": DynamicTemplate,
 *    .
 *    .
 *    .
 *  }
 */
Layout.prototype.region = function (name, options) {
  return this._ensureRegion(name, options);
};

/**
 * Set the template for a region.
 */
Layout.prototype.render = function (template, options) {
  // having options is usually good
  options = options || {};

  // let the user specify the region to render the template into
  var region = options.to || options.region || DEFAULT_REGION;

  // get the DynamicTemplate for this region
  var dynamicTemplate = this.region(region);

  // if we're in a rendering transaction, track that we've rendered this
  // particular region
  this._trackRenderedRegion(region);

  // set the template value for the dynamic template
  dynamicTemplate.template(template);

  // if we have data go ahead and set the data for the dynamic template,
  // otherwise, leave it be.
  if (options.data)
    dynamicTemplate.data(options.data);
};

/**
 * Returns true if the given region is defined and false otherwise.
 */
Layout.prototype.has = function (region) {
  region = region || Layout.DEFAULT_REGION;
  return !!this._regions[region];
};

/**
 * Returns an array of region keys.
 */
Layout.prototype.regionKeys = function () {
  return _.keys(this._regions);
};

/**
 * Clear a given region or the "main" region by default.
 */
Layout.prototype.clear = function (region) {
  region = region || Layout.DEFAULT_REGION;

  // we don't want to create a region if it didn't exist before
  if (this.has(region))
    this.region(region).template(null);

  // chain it up
  return this;
};

/**
 * Clear all regions.
 */
Layout.prototype.clearAll = function () {
  _.each(this._regions, function (dynamicTemplate) {
    dynamicTemplate.template(null);
  });

  // chain it up
  return this;
};

/**
 * Start tracking rendered regions.
 */
Layout.prototype.beginRendering = function () {
  if (this._renderedRegions)
    throw new Error("You called beginRendering again before calling endRendering");
  this._renderedRegions = {};
};

/**
 * Track a rendered region if we're in a transaction.
 */
Layout.prototype._trackRenderedRegion = function (region) {
  if (!this._renderedRegions)
    return;
  this._renderedRegions[region] = true;
};

/**
 * Stop a rendering transaction and retrieve the rendered regions.
 */
Layout.prototype.endRendering = function (opts) {
  // we flush here to ensure all of the {{#contentFor}} inclusions have had a
  // chance to render from our templates, otherwise we'll never know about
  // them. 
  opts = opts || {};
  if (opts.flush !== false)
    Deps.flush();
  var renderedRegions = this._renderedRegions;
  this._renderedRegions = null;
  return _.keys(renderedRegions);
};
{'fraction_non_alphanumeric': 0.20147778124674784, 'fraction_numerical': 0.0, 'mean_word_length': 3.8658227848101268, 'pattern_counts': {'":': 4, '<': 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': '3035658', 'n_tokens_mistral': 2559, 'n_tokens_neox': 2429, 'n_words': 853}