<commit_before>module.exports = ItemsControl;

var UIElement = require('./uiElement');

function ItemsControl() {
  if (!(this instanceof ItemsControl)){
    return new ItemsControl();
  }

  UIElement.call(this);
}

ItemsControl.prototype = Object.create(UIElement.prototype);
ItemsControl.prototype.constructor = ItemsControl;

ItemsControl.prototype.setItemTemplate = function (itemTemplate) {
  this._itemTemplate = itemTemplate;
};

ItemsControl.prototype.setItemSource = function (itemSource) {
  this._itemSource = itemSource;
};

ItemsControl.prototype._appendToDom = function (parentDom) {
  this._dom = require('../utils/svg')('g');
  renderChildren(this);
  parentDom.appendChild(this._dom);
};

ItemsControl.prototype.render = function () {
  var children = this._children;
  for (var i = 0; i < children.length; ++i) {
    children[i].render();
  }
};

function renderChildren(itemsControl) {
  if (itemsControl._markup) {
    var markup = itemsControl._markup;
    var source = markup.getAttributeNS(null, 'source');
    // todo: should be a better binding mechanism
    var replacer = require('../utils/bindingReplace')(itemsControl._dataContext);
    var match = source.match(/{{(.+?)}}/);
    if (match) {
      itemsControl.setItemSource(replacer(null, match[1]));
    }

    itemsControl.setItemTemplate(markup.innerHTML);
  }

  if (!itemsControl._itemSource || !itemsControl._itemTemplate) {
    throw new Error('Can not use items control without itemsSource and itemTemplate');
  }

  var ContentControl = require('./contentControl');
  var nodePrototype = require('../utils/domParser')(itemsControl._itemTemplate);

  var itemSource = itemsControl._itemSource;
  for (var i = 0; i < itemSource.length; ++i) {
    var contentControl = new ContentControl();
    // override default data context to current item:
    contentControl.dataContext(itemSource[i]);
    contentControl.markup(nodePrototype);
    itemsControl.appendChild(contentControl);
  }<commit_msg>Extracted validation and initalization into method<commit_after>module.exports = ItemsControl;

var UIElement = require('./uiElement');

function ItemsControl() {
  if (!(this instanceof ItemsControl)){
    return new ItemsControl();
  }

  UIElement.call(this);
}

ItemsControl.prototype = Object.create(UIElement.prototype);
ItemsControl.prototype.constructor = ItemsControl;

ItemsControl.prototype.setItemTemplate = function (itemTemplate) {
  this._itemTemplate = itemTemplate;
};

ItemsControl.prototype.setItemSource = function (itemSource) {
  this._itemSource = itemSource;
};

ItemsControl.prototype._appendToDom = function (parentDom) {
  this._dom = require('../utils/svg')('g');
  renderChildren(this);
  parentDom.appendChild(this._dom);
};

ItemsControl.prototype.render = function () {
  var children = this._children;
  for (var i = 0; i < children.length; ++i) {
    children[i].render();
  }
};

function renderChildren(itemsControl) {
  ensureItemsControlValid(itemsControl);

  var ContentControl = require('./contentControl');
  var nodePrototype = require('../utils/domParser')(itemsControl._itemTemplate);

  var itemSource = itemsControl._itemSource;
  for (var i = 0; i < itemSource.length; ++i) {
    var contentControl = new ContentControl();
    // override default data context to current item:
    contentControl.dataContext(itemSource[i]);
    contentControl.markup(nodePrototype);
    itemsControl.appendChild(contentControl);
  }
}

function ensureItemsControlValid(itemsControl) {
  if (itemsControl._markup && !itemsControl._itemSource) {
    var markup = itemsControl._markup;
    var source = markup.getAttributeNS(null, 'source');
    // todo: should be a better binding mechanism
    var replacer = require('../utils/bindingReplace')(itemsControl._dataContext);
    var match = source.match(/{{(.+?)}}/);
    if (match) {
      itemsControl.setItemSource(replacer(null, match[1]));
    }

    itemsControl.setItemTemplate(markup.innerHTML);
  }

  if (!itemsControl._itemSource || !itemsControl._itemTemplate) {
    throw new Error('Can not use items control without itemsSource and itemTemplate');
  }
{'fraction_non_alphanumeric': 0.13066666666666665, 'fraction_numerical': 0.0014545454545454545, 'mean_word_length': 4.6598079561042525, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1853240', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1154, 'n_words': 253}