All files / sp-form/fields/sp-select-field ViewModel.js

66.67% Statements 14/21
58.33% Branches 7/12
62.5% Functions 5/8
66.67% Lines 14/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                      1x                                               1x                         2x         2x 3x     3x                                                     7x 3x 3x   4x                     2x                 1x 1x   1x        
 
import Field from '../../../util/field/Field';
import DefineMap from 'can-define/map/map';
import DefineList from 'can-define/list/list';
import debounce from '../../../sp-admin/util/debounce';
 
/**
 * the select option type - used to display <option> tags values/labels
 * @class SelectOption
 * @memberof sp-select-field
 */
export const SelectOption = DefineMap.extend('SelectOption', {
    /** @lends sp-select-field.SelectOption.prototype */
    /**
     * The value to set when option is selected
     * @type {Any} 
     * @memberof sp-select-field.SelectOption.prototype
     */
    value: 'string',
    /**
     * The label to display to the user. If not provided, `value` is used
     * @type {String} 
     * @memberof sp-select-field.SelectOption.prototype
     */
    label: {
        type: 'string',
        get (label) {
            if (label) {
                return label;
            }
            return this.value;
        }
    }
});
 
export const SelectOptionList = DefineList.extend('SelectOptionList', {
    '#': SelectOption
});
 
 
/**
 * A `<sp-select-field />` component's ViewModel
 * @class ViewModel
 * @memberof sp-select-field
 */
export default Field.extend('SelectField', {
    /** @lends sp-select-field.ViewModel.prototype */
    init () {
        Iif (this.optionsPromise) {
            this.optionsPromise.then((result) => {
                this.options = result;
            });
        }
        this.listenTo('optionsPromise', (event, newPromise) => {
            Iif (!newPromise) {
                return;
            }
            newPromise.then((result) => { 
                this.options = result; 
            });
        });
    },
    /**
     * The default label when no items are selected
     * @type {String}
     * @memberof sp-select-field.ViewModel.prototype
     */
    defaultLabel: {type: 'string', default: 'Choose a value...'},
    /**
     * The array of options to display in the dropdown
     * @type {Array<sp-select-field.SelectOption>}
     * @memberof sp-select-field.ViewModel.prototype
     */
    options: {        
        Type: SelectOptionList,
        Default: SelectOptionList
    },
    /**
     * A promise that resolves to the array of options. 
     * @type {Promise<Array>}
     * @memberof sp-select-field.ViewModel.prototype
     */
    optionsPromise: {
        get () {
            if (typeof this.getOptions === 'function') {
                const data = this.object ? this.object.get() : {};
                return Promise.resolve(this.getOptions(data));
            }
            return null;
        }
    },
    /**
     * Determines whether a value is the currently selected value
     * @param {String} value The value to check
     * @return {Boolean} whether or not it is selected
     */
    isSelected (value) {
        // coerce check into this value type
        // eslint-disable-next-line eqeqeq
        return value == this.value;
    },
    /**
     * An optional function to return options from a form object...ie cascading dropdowns 
     * @param {Object} formObject the form object
     * @returns {SelectOption[]|Promise<SelectOption[]>} the filtered array of select options
     */
    getOptions: {
        set (getter) {
            Eif (getter) {
                getter = debounce(this, getter);
            }
            return getter;
        }
    }
});