<commit_before>/**
 * Developer: <NAME>
 * Date: 11/19/2014
 * Copyright: 2009-2016 Comindware®
 *       All Rights Reserved
 * Published under the MIT license
 */

import FieldView from '../fields/FieldView';
import helpers from '../../utils/helpers';

const Form = Marionette.Object.extend({
    /**
     * Constructor
     *
     * @param {Object} [options.schema]
     * @param {Backbone.Model} [options.model]
     */
    initialize(options = {}) {
        this.options = options;

        this.__regionManager = new Marionette.RegionManager();
        this.schema = _.result(options, 'schema');
        this.model = options.model;

        this.fields = {};
        _.each(this.schema, (fieldSchema, key) => {
            const FieldType = fieldSchema.field || options.field || FieldView;
            const field = new FieldType({
                form: this,
                key,
                schema: fieldSchema,
                model: this.model
            });
            this.listenTo(field.editor, 'all', this.__handleEditorEvent);
            this.fields[key] = field;
        });

        const $target = this.options.$target;

        const usedFields = {};
        //Render standalone editors
        $target.find('[data-editors]').each((i, el) => {
            const $editorRegion = $(el);
            const key = $editorRegion.attr('data-editors');
            const regionName = `${key}Region`;
            if (usedFields[key]) {
                helpers.throwFormatError(`Duplicated field '${key}'.`);
            }
            this.__regionManager.addRegion(regionName, { el: $editorRegion });
            this.__regionManager.get(regionName).show(this.fields[key].editor);
            usedFields[key] = true;
        });

        //Render standalone fields
        $target.find('[data-fields]').each((i, el) => {
            const $fieldRegion = $(el);
            const key = $fieldRegion.attr('data-fields');
            const regionName = `${key}Region`;
            if (usedFields[key]) {
                helpers.throwFormatError(`Duplicated field '${key}'.`);
            }
            this.__regionManager.addRegion(regionName, { el: $fieldRegion });
            this.__regionManager.get(regionName).show(this.fields[key]);
            usedFields[key] = true;
        });
    },

    /**
     * Update the model with all latest values.
     *
     * @param {Object} [options]  Options to pass to Model#set (e.g. { silent: true })
     *
     * @return {Object}  Validation errors
     */
    commit(options = {}) {
        // Validate
        const errors = this.validate({
            skipModelValidate: !options.validate
        });
        if (errors) {
            return errors;
        }

        // Commit
        let modelError = null;
        const setOptions = Object.assign({
            error(model, e) {
                modelError = e;
            }
        }, options);

        this.model.set(this.getValue(), setOptions);
        return modelError;<commit_msg>Form: bug fixes<commit_after>/**
 * Developer: <NAME>
 * Date: 11/19/2014
 * Copyright: 2009-2016 Comindware®
 *       All Rights Reserved
 * Published under the MIT license
 */

import FieldView from '../fields/FieldView';

const Form = Marionette.Object.extend({
    /**
     * Constructor
     *
     * @param {Object} [options.schema]
     * @param {Backbone.Model} [options.model]
     */
    initialize(options = {}) {
        this.options = options;

        this.__regionManager = new Marionette.RegionManager();
        this.schema = _.result(options, 'schema');
        this.model = options.model;

        this.fields = {};
        _.each(this.schema, (fieldSchema, key) => {
            const FieldType = fieldSchema.field || options.field || FieldView;
            const field = new FieldType({
                form: this,
                key,
                schema: fieldSchema,
                model: this.model
            });
            this.listenTo(field.editor, 'all', this.__handleEditorEvent);
            this.fields[key] = field;
        });

        const $target = this.options.$target;

        //Render standalone editors
        $target.find('[data-editors]').each((i, el) => {
            const $editorRegion = $(el);
            const key = $editorRegion.attr('data-editors');
            const regionName = `${key}Region`;

            this.__regionManager.addRegion(regionName, { el: $editorRegion });
            this.fields[key] && this.__regionManager.get(regionName).show(this.fields[key].editor);
        });

        //Render standalone fields
        $target.find('[data-fields]').each((i, el) => {
            const $fieldRegion = $(el);
            const key = $fieldRegion.attr('data-fields');
            const regionName = `${key}Region`;

            this.__regionManager.addRegion(regionName, { el: $fieldRegion });
            this.fields[key] && this.__regionManager.get(regionName).show(this.fields[key]);
        });
    },

    /**
     * Update the model with all latest values.
     *
     * @param {Object} [options]  Options to pass to Model#set (e.g. { silent: true })
     *
     * @return {Object}  Validation errors
     */
    commit(options = {}) {
        // Validate
        const errors = this.validate({
            skipModelValidate: !options.validate
        });
        if (errors) {
            return errors;
        }

        // Commit
        let modelError = null;
        const setOptions = Object.assign({
            error(model, e) {
                modelError = e;
            }
        }, options);

        this.model.set(this.getValue(), setOptions);
        return modelError;
{'fraction_non_alphanumeric': 0.1377911079745942, 'fraction_numerical': 0.0056457304163726185, 'mean_word_length': 1.860242179616549, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3821527', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1523, 'n_words': 331}