All files / src/fieldcontrols Choice.ts

94.74% Statements 18/19
72.22% Branches 13/18
85.71% Functions 6/7
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43          1x   1x 1x                     1x             1x     3x 3x 1x         1x 2x 1x 1x 1x    
/**
 * @module FieldControls
 * 
 */ /** */
 
import { FieldBaseControl } from './FieldBaseControl';
import { FieldSettings } from 'sn-client-js';
import { customElement, bindable } from 'aurelia-framework';
import { select } from 'material-components-web/dist/material-components-web';
 
/**
 * Field control that represents a Choice field. Available values will be populated from the FieldSettings.
 * Usage:
 * 
 * ``` html
 * <choice content.bind="content" settings.bind="myChoiceFieldSettings"></choice>
 * ```
 */
@customElement('choice')
export class Choice extends FieldBaseControl<string[], FieldSettings.ChoiceFieldSetting> {
 
 
    select: HTMLElement;
    mdcSelect: select.MDCSelect;
 
    @bindable
    public DefaultText: string;
 
    attached(){
        this.mdcSelect = new select.MDCSelect(this.select);
        this.mdcSelect.listen('MDCSelect:change', (event) => {
            this.value = [].map.call(event.detail.selectedOptions, o => o.id);
        });
    }
 
    activate(model){
        super.activate(model);
        const selectedOptions = this.settings && this.settings.Options && this.settings.Options && this.settings.Options.filter(o => this.value && this.value.indexOf(o.Value) > -1);
        const defaultOption = this.settings && this.settings.Options && this.settings.Options.find(o => o.Value === this.settings.DefaultValue);
        this.value = defaultOption && [defaultOption.Value] || [];
        this.DefaultText = selectedOptions && selectedOptions.map(o => o.Text).join() || defaultOption && defaultOption.Text || this.settings.DisplayName || '';
    }
}