All files / src/fieldcontrols Percentage.ts

86.67% Statements 13/15
50% Branches 2/4
100% Functions 4/4
86.67% Lines 13/15
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 44 45 46 47 48 49 50 51 52 53          1x   1x 1x                     1x         1x     3x   3x 1x 1x         3x 3x 3x               1x            
/**
 * @module FieldControls
 * 
 */ /** */
 
import { FieldBaseControl } from './FieldBaseControl';
import { FieldSettings } from 'sn-client-js';
import { slider } from 'material-components-web/dist/material-components-web';
import { bindable } from 'aurelia-framework';
 
 
/**
 * Field control for percentage fields.
 * Usage:
 * 
 * ``` html
 * <percentage content.bind="content" settings.bind="myIntegerFieldSetting"></percentage>
 * ```
 */
export class Percentage extends FieldBaseControl<number, FieldSettings.IntegerFieldSetting> {
    sliderElement: HTMLElement;
    mdcSlider: slider.MDCSlider;
 
    @bindable
    public value: number;
 
    attached() {
        this.mdcSlider = new slider.MDCSlider(this.sliderElement);
        // this.mdcSlider.value = this.value;
        this.mdcSlider.listen('MDCSlider:change', () => {
            Eif (this.value !== this.mdcSlider.value){
                this.value = this.mdcSlider.value;
            }
        });
 
        // Tempfix. Related issue: https://github.com/material-components/material-components-web/issues/1017
        setTimeout(() => {
            try {
                window.dispatchEvent(new Event('resize'));                
            } catch (error) {
                // jsdom will throw...
            }
        }, 100)
    }
 
    valueChanged(){
        Iif (this.value !== this.mdcSlider.value){
            this.mdcSlider.value = this.value;
            this.mdcSlider.emit('MDCSlider:change');
        }
        
    }
}