views/mdc/assets/js/components/selects.js in voom-presenters-0.2.0 vs views/mdc/assets/js/components/selects.js in voom-presenters-2.0.0

- old
+ new

@@ -1,20 +1,22 @@ import {MDCSelect} from '@material/select'; import {VBaseComponent, hookupComponents} from './base-component'; -import {eventHandlerMixin} from './mixins/event-handler'; +import {visibilityObserverMixin} from "./mixins/visibility-observer"; +import {dirtyableMixin} from './mixins/dirtyable'; -export function initSelects() { - console.log('\tSelects'); - hookupComponents('.v-select', VSelect, MDCSelect); +export function initSelects(e) { + console.debug('\tSelects'); + hookupComponents(e, '.v-select', VSelect, MDCSelect); } - -export class VSelect extends eventHandlerMixin(VBaseComponent) { +export class VSelect extends dirtyableMixin(visibilityObserverMixin(VBaseComponent)) { constructor(element, mdcComponent) { super(element, mdcComponent); this.select = element.querySelector('select'); this.select.vComponent = this; + this.recalcWhenVisible(this); + this.originalValue = this.value(); } prepareSubmit(params) { params.push([this.name(), this.value()]); } @@ -22,11 +24,11 @@ name() { return this.select.name; } value() { - return this.select.options.length === 0 ? null : this.select.options[this.select.selectedIndex].value; + return this.select.options.length === 0 || this.select.selectedIndex === -1 ? null : this.select.options[this.select.selectedIndex].value; } clear() { let before = this.select.selectedIndex; this.select.selectedIndex = 0; @@ -38,9 +40,17 @@ }); this.select .dispatchEvent(event); } } + reset() { + this.select.value = this.originalValue; + } + setValue(value) { this.select.value = value; } -} \ No newline at end of file + + isDirty() { + return this.dirtyable && this.value() !== this.originalValue; + } +}