{"size": 1144, "ext": "js", "max_stars_repo_path": "src/elements/AbstractFormElement.js", "max_stars_repo_name": "mikoweb/polymer-boilerplate", "max_stars_repo_stars_event_min_datetime": "2017-10-02T22:45:13.000Z", "max_stars_repo_stars_event_max_datetime": "2019-09-11T02:05:39.000Z", "max_issues_repo_path": "src/elements/AbstractFormElement.js", "max_issues_repo_name": "mikoweb/polymer-boilerplate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/elements/AbstractFormElement.js", "max_forks_repo_name": "mikoweb/polymer-boilerplate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.8333333333, "max_line_length": 88, "alphanum_fraction": 0.5812937063}
import AbstractElement from './AbstractElement.js';

export default class AbstractFormElement extends AbstractElement {
    get formElement() {
        return this.shadowRoot.querySelector('iron-form');
    }

    disabled(disabled) {
        for (const input of this.shadowRoot.querySelectorAll('*[name], paper-button')) {
            input.disabled = disabled;
        }
    }

    disabledSubmitButton(disabled) {
        this.shadowRoot.querySelector('*[type="submit"]').disabled = disabled;
    }

    /**
     * @return {boolean}
     */
    isFormDataChanged() {
        return !!diff(this.primaryForm, this.form);
    }

    updateFormState() {
        if (this.formElement) {
            const valid = this.formElement.validate();
            this.disabledSubmitButton(!valid);

            if (valid) {
                this.disabledSubmitButton(!this.isFormDataChanged());
            }
        }
    }

    updatePrimaryData() {
        this.primaryForm = Object.assign({}, this.form);
    }

    /**
     * @protected
     */
    _onLoaded() {
        new App.FormBehavior(this.formElement);
        this.updateFormState();
    }
}
