<commit_before>/**
 * Created by <NAME> on 07.06.2017.
 */
import {FormDialog} from 'phovea_ui/src/dialogs';
import {randomId} from 'phovea_core/src';
import FormBuilder from 'ordino/src/form/FormBuilder';
import {IFormElementDesc} from 'ordino/src/form/interfaces';
import {select} from 'd3';


export default class FormBuilderDialog extends FormDialog {
  readonly builder: FormBuilder;

  constructor(title: string, primaryButton: string, formId = 'form' + randomId(5)) {
    super(title, primaryButton, formId);
    this.body.innerHTML = ''; //clear old form since the form builder brings its own
    this.builder = new FormBuilder(select(this.body), formId);

    this.onHide(() => {
      this.destroy();
    });
  }

  append(...elements: IFormElementDesc[]) {
    this.builder.build(elements);
  }

  onSubmitForm(callback: (builder: FormBuilder)=>void) {
    this.onSubmit(() => {
      if (!this.builder.validate()) {
        return false;
      }
      callback(this.builder);
      return false;
    });
  }

  showAsPromise<T>(processData: (builder: FormBuilder) => T) {
    return new Promise<T>((resolve) => {
      this.onSubmitForm((builder) => {
        const data = processData(builder);<commit_msg>adapt form dialog to watch for form submission to trigger integrated validation<commit_after>/**
 * Created by <NAME> on 07.06.2017.
 */
import {FormDialog} from 'phovea_ui/src/dialogs';
import {randomId} from 'phovea_core/src';
import FormBuilder from 'ordino/src/form/FormBuilder';
import {IFormElementDesc} from 'ordino/src/form/interfaces';
import {select} from 'd3';


export default class FormBuilderDialog extends FormDialog {
  readonly builder: FormBuilder;

  constructor(title: string, primaryButton: string, formId = 'form' + randomId(5)) {
    super(title, primaryButton, formId);
    this.body.innerHTML = ''; //clear old form since the form builder brings its own
    this.builder = new FormBuilder(select(this.body), formId);

    this.onHide(() => {
      this.destroy();
    });
  }

  append(...elements: IFormElementDesc[]) {
    this.builder.build(elements);
  }

  onSubmit(callback: (builder?: FormBuilder)=>void) {
    return super.onSubmit(() => {
      if (!this.builder.validate()) {
        return false;
      }
      callback(this.builder);
      return false;
    });
  }

  showAsPromise<T>(processData: (builder: FormBuilder) => T) {
    return new Promise<T>((resolve) => {
      this.onSubmit((builder) => {
        const data = processData(builder);
{'fraction_non_alphanumeric': 0.14342788653615662, 'fraction_numerical': 0.007990411506192568, 'mean_word_length': 3.6979362101313322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '2935629', 'n_tokens_mistral': 778, 'n_tokens_neox': 752, 'n_words': 185}