<commit_before>var currentLayoutName = null;
var currentLayout = null;
var currentRegions = new ReactiveDict();
var currentData;

FlowLayout.setRoot = function(root) {
  FlowLayout._root = root;
};

FlowLayout.render = function render(layout, regions) {
  Meteor.startup(function() {
    var rootDomNode = FlowLayout._getRootDomNode();
    if(currentLayoutName != layout) {
      // remove old view
      FlowLayout.reset();
      currentData = FlowLayout._regionsToData(regions);

      currentLayout = Blaze._TemplateWith(currentData, function() {
        return Spacebars.include(Template[layout]);
      });

      Blaze.render(currentLayout, rootDomNode);
      currentLayoutName = layout;
    } else {
      FlowLayout._updateRegions(regions);
    }
  });
};

FlowLayout.reset = function reset() {
  var layout = currentLayout;
  if(layout) {
    if(layout._domrange) {
      // if it's rendered let's remove it right away
      Blaze.remove(layout);
    } else {
      // if not let's remove it when it rendered
      layout.onViewReady(function() {
        Blaze.remove(layout);
      });
    }
    
    currentLayout = null;
    currentLayoutName = null;
    currentRegions = new ReactiveDict();
  }
};

FlowLayout._regionsToData = function _regionsToData(regions, data) {
  data = data || {};
  _.each(regions, function(value, key) {
    currentRegions.set(key, value);
    data[key] = FlowLayout._buildRegionGetter(key);
  });

  return data;
};

FlowLayout._updateRegions = function _updateRegions(regions) {
  var needsRerender = false;
  _.each(regions, function(value, key) {
    // if this key does not yet exist then blaze
    // has no idea about this key and it won't get the value of this key
    // so, we need to force a re-render
    if(currentData && currentData[key] === undefined) {
      needsRerender = true;
      // and, add the data function for this new key
      currentData[key] = FlowLayout._buildRegionGetter(key);
    }
    currentRegions.set(key, value);
  });

  // force re-render if we need to
  if(currentLayout && needsRerender) {
    currentLayout.dataVar.dep.changed();
  }
};

FlowLayout._getRootDomNode = function _getRootDomNode() {
  var root = FlowLayout._root
  if(!root) {
    root = $('<div id="__flow-root"></div>');
    $('body').append(root);
    FlowLayout.setRoot(root);
  }

  // We need to use $(root) here because when calling FlowLayout.setRoot(), 
  // there won't have any available DOM elements
  // So, we need to defer that.
  var domNode = $(root).get(0);
  if(!domNode) {
    throw new Error("Root element does not exist");
  }

  return domNode;
};

FlowLayout._buildRegionGetter = function _buildRegionGetter(key) {
  return function() {
    return currentRegions.get(key);
  };
};<commit_msg>make sure dom is loaded before rendering<commit_after>var currentLayoutName = null;
var currentLayout = null;
var currentRegions = new ReactiveDict();
var currentData;
var _isReady = false;

FlowLayout.setRoot = function(root) {
  FlowLayout._root = root;
};

FlowLayout.render = function render(layout, regions) {
  Meteor.startup(function() {
    // To make sure dom is loaded before we do rendering layout.
    // Related to issue #25
    if(!_isReady) {
      Meteor.defer(function() {
        _isReady = true;
        FlowLayout._render(layout, regions)
      });
    } else {
      FlowLayout._render(layout, regions);
    }
  });
};

FlowLayout.reset = function reset() {
  var layout = currentLayout;
  if(layout) {
    if(layout._domrange) {
      // if it's rendered let's remove it right away
      Blaze.remove(layout);
    } else {
      // if not let's remove it when it rendered
      layout.onViewReady(function() {
        Blaze.remove(layout);
      });
    }

    currentLayout = null;
    currentLayoutName = null;
    currentRegions = new ReactiveDict();
  }
};

FlowLayout._regionsToData = function _regionsToData(regions, data) {
  data = data || {};
  _.each(regions, function(value, key) {
    currentRegions.set(key, value);
    data[key] = FlowLayout._buildRegionGetter(key);
  });

  return data;
};

FlowLayout._updateRegions = function _updateRegions(regions) {
  var needsRerender = false;
  _.each(regions, function(value, key) {
    // if this key does not yet exist then blaze
    // has no idea about this key and it won't get the value of this key
    // so, we need to force a re-render
    if(currentData && currentData[key] === undefined) {
      needsRerender = true;
      // and, add the data function for this new key
      currentData[key] = FlowLayout._buildRegionGetter(key);
    }
    currentRegions.set(key, value);
  });

  // force re-render if we need to
  if(currentLayout && needsRerender) {
    currentLayout.dataVar.dep.changed();
  }
};

FlowLayout._getRootDomNode = function _getRootDomNode() {
  var root = FlowLayout._root
  if(!root) {
    root = $('<div id="__flow-root"></div>');
    $('body').append(root);
    FlowLayout.setRoot(root);
  }

  // We need to use $(root) here because when calling FlowLayout.setRoot(),
  // there won't have any available DOM elements
  // So, we need to defer that.
  var domNode = $(root).get(0);
  if(!domNode) {
    throw new Error("Root element does not exist");
  }

  return domNode;
};

FlowLayout._buildRegionGetter = function _buildRegionGetter(key) {
  return function() {
    return currentRegions.get(key);
  };
};

FlowLayout._render = function _render(layout, regions) {
  var rootDomNode = FlowLayout._getRootDomNode();
  if(currentLayoutName != layout) {
    // remove old view
    FlowLayout.reset();
    currentData = FlowLayout._regionsToData(regions);

    currentLayout = Blaze._TemplateWith(currentData, function() {
      return Spacebars.include(Template[layout]);
    });

    Blaze.render(currentLayout, rootDomNode);
    currentLayoutName = layout;
  } else {
    FlowLayout._updateRegions(regions);
  }
};
{'fraction_non_alphanumeric': 0.12319701340573562, 'fraction_numerical': 0.0006787714237230613, 'mean_word_length': 3.2772133526850507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1960368', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1781, 'n_words': 467}