<commit_before>import {containerless} from 'aurelia-framework';
import {Field} from './abstract/field';

/**
 * LazyLinkfield is a field that lazily proxies the a whole field (value & UI)
 * to another place.
 */
@containerless
export class LazyLinkfield extends Field {
  target = '#';
  overrides = {};
  _child = undefined;

  /** @inheritdoc */
  init(id = '', args = {}) {
    this.target = args.target || '#';
    this.overrides = args.overrides || {};
    return super.init(id, args);
  }

  createChild() {
    this._child = this.resolveRef(this.target).clone();
    this._child.parent = this;
    for (const [field, value] of Object.entries(this.overrides)) {
      const lastSlash = field.lastIndexOf('/');
      const path = field.substr(0, lastSlash);
      const fieldName = field.substr(lastSlash + 1, field.length);
      this._child.resolveRef(path)[fieldName] = value;
    }
  }

  deleteChild() {
    this._child = undefined;
  }

  shouldDisplay() {
    const display = super.shouldDisplay();
    if (display) {
      if (this._child === undefined) {
        this.createChild();
      }
    } else if (this._child !== undefined) {
      this.deleteChild();
    }
    return display;
  }

  get child() {
    return this.display ? this._child : undefined;
  }

  /**
   * Set the value of the target field.
   *
   * @override
   * @param {Object} value The new value to set to the target field.
   */
  setValue(value) {
    if (!this._child) {
      return;<commit_msg>Allow more powerful overrides in lazylinkfield<commit_after>import {containerless} from 'aurelia-framework';
import {Field} from './abstract/field';

/**
 * LazyLinkfield is a field that lazily proxies the a whole field (value & UI)
 * to another place.
 */
@containerless
export class LazyLinkfield extends Field {
  target = '#';
  overrides = {};
  _child = undefined;

  /** @inheritdoc */
  init(id = '', args = {}) {
    this.target = args.target || '#';
    this.overrides = args.overrides || {};
    return super.init(id, args);
  }

  createChild() {
    this._child = this.resolveRef(this.target).clone();
    this._child.parent = this;
    for (const [field, value] of Object.entries(this.overrides)) {
      let target;
      let fieldPath;
      if (field.includes(';')) {
        [elementPath, fieldPath] = field.split(';');
        fieldPath = fieldPath.split('/');
        target = this._child.resolveRef(elementPath);
      } else {
        fieldPath = field.split('/');
        target = this._child;
      }
      const lastFieldPathEntry = fieldPath.splice(-1)[0];
      target = this.resolveRawPath(target, fieldPath);
      if (value === null) {
        delete target[lastFieldPathEntry];
      } else {
        target[lastFieldPathEntry] = value;
      }
    }
  }

  resolveRawPath(object, path) {
    if (path.length === 0) {
      return object;
    } else if (path[0] === '#') {
      return this.resolveRawPath(object, path.splice(1));
    }
    return this.resolveRawPath(object[path[0]], path.splice(1));
  }

  deleteChild() {
    this._child = undefined;
  }

  shouldDisplay() {
    const display = super.shouldDisplay();
    if (display) {
      if (this._child === undefined) {
        this.createChild();
      }
    } else if (this._child !== undefined) {
      this.deleteChild();
    }
    return display;
  }

  get child() {
    return this.display ? this._child : undefined;
  }

  /**
   * Set the value of the target field.
   *
   * @override
   * @param {Object} value The new value to set to the target field.
   */
  setValue(value) {
    if (!this._child) {
      return;
{'fraction_non_alphanumeric': 0.14689578713968957, 'fraction_numerical': 0.0024944567627494456, 'mean_word_length': 2.660243407707911, 'pattern_counts': {'":': 0, '<': 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': '3298915', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1125, 'n_words': 276}