{"size": 2237, "ext": "js", "max_stars_repo_path": "src/helpers/index.js", "max_stars_repo_name": "U-like/angular-formly-templates-material", "max_stars_repo_stars_event_min_datetime": "2015-02-12T02:52:28.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-27T14:36:45.000Z", "max_issues_repo_path": "src/helpers/index.js", "max_issues_repo_name": "U-like/angular-formly-templates-material", "max_issues_repo_issues_event_min_datetime": "2015-02-08T10:53:54.000Z", "max_issues_repo_issues_event_max_datetime": "2017-09-19T22:44:20.000Z", "max_forks_repo_path": "src/helpers/index.js", "max_forks_repo_name": "U-like/angular-formly-templates-material", "max_forks_repo_forks_event_min_datetime": "2015-02-23T15:22:11.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-15T07:40:46.000Z"}
{"max_stars_count": 107, "max_issues_count": 65, "max_forks_count": 57, "avg_line_length": 27.9625, "max_line_length": 88, "alphanum_fraction": 0.6866338847}
import angular from 'angular';

/**
 * Sets attribute with optional value.
 * Does not owerwrite.
 * @param {Array} nodes nodes
 * @param {String} attr  attribute name
 * @param {String} val   atrtibute value
 */
function addIfNotPresent(nodes, attr, val) {
  angular.forEach(nodes, (node) => {
    if (!node.getAttribute(attr)) {
      node.setAttribute(attr, val);
    }
  });
}

/**
 * Gets all ngModels from node
 */
function getNgModelNodes(node) {
  const query = '[ng-model], [data-ng-model]';

  return node.querySelectorAll(query);
}

/**
 * Adds attribute with optional value to all elements using ngModel directive.
 * Handles extras.skipNgModelAttrsManipulator
 * And does not overwrite attriutes
 *
 * @param  {String} template  Template provided by formly template manipulator
 * @param  {Object} options   Options provided by formly template manipulator
 * @param  {String} attrName  Attribute's name
 * @param  {String|undefined} Attribute's value (optional)
 * @return {String} result
 */
export function ngModelAttrsManipulator(template, options, attrName, attrValue) {
  const node = document.createElement('div');
  const skip = options.extras && options.extras.skipNgModelAttrsManipulator;

  if (skip === true) {
    return template;
  }
  node.innerHTML = template;
  const modelNodes = getNgModelNodes(node);

  if (!modelNodes || !modelNodes.length) {
    return template;
  }

  addIfNotPresent(modelNodes, attrName, attrValue);

  return node.innerHTML;
}

/**
 * Adds ngModelAttr to the field when specified condition is true.
 * @param  {Array} fields    fields provided by formly's fieldTranform
 * @param  {Funcion} condition with field as only parameter
 * @param  {String} name      ngModelAttr's name
 * @param  {Object} settings  ngModelAttr's settings
 * @return {Array}           returns fields
 */
export function ngModelAttrsTransformer(fields, condition, name, settings) {
  (fields || []).forEach((field) => {
    if (condition(field) === true) {
      if (!field.ngModelAttrs) {
        field.ngModelAttrs = {};
      }

      if (field.templateOptions && typeof field.templateOptions[name] !== 'undefined') {
        field.ngModelAttrs[name] = settings;
      }
    }
  });

  return fields;
}
