Sha256: fc9fba72d932a5456402d38cdf8860111491dbf671260176eb4dc2ed5808ac42

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

import {VBaseComponent} from './base-component';
import {eventHandlerMixin} from './mixins/event-handler';
import {dirtyableMixin} from './mixins/dirtyable';

export class VBaseToggle extends dirtyableMixin(eventHandlerMixin(VBaseComponent)) {
    constructor(element, mdcComponent) {
        super(element, mdcComponent);

        this.input = element.querySelector('input') || element;

        element.addEventListener('V:postFailed', (event) => {
            // Revert to previous checked state on failed post.
            this.mdcComponent.checked = !this.mdcComponent.checked;
        });

        this.originalValue = this.input.checked;
    }

    prepareSubmit(params) {
        if (this.submittedValue()) {
            params.push([this.name(), this.submittedValue()]);
        }
    }

    submittedValue() {
        return this.input.checked ? this.value() : this.offValue();
    }

    name() {
        return this.input.name;
    }

    value() {
        return this.input.value;
    }

    offValue() {
        return this.input.dataset.off;
    }

    clear() {
        this.input.checked = false;
    }

    reset() {
        this.input.checked = this.originalValue;
    }

    setValue(value) {
        this.input.value = value;
    }

    isDirty() {
        return this.dirtyable && this.input.checked !== this.originalValue;
    }
}

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
coprl-3.0.0.beta.12 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.11 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.10 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.9 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.8 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.7 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.6 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.5 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.1.2 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.4 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.3 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.2 views/mdc/assets/js/components/base-toggle.js
coprl-3.0.0.beta.1 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.1.0 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.0.3 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.0.2 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.0.1 views/mdc/assets/js/components/base-toggle.js
voom-presenters-2.0.0 views/mdc/assets/js/components/base-toggle.js