all files / src/domUtil/ SelectBoxBase.ts

29.79% Statements 14/47
0% Branches 0/12
20% Functions 2/10
28.89% Lines 13/45
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                                                                                                                                                                                   
/**
 * Created by gavorhes on 5/13/2016.
 */
 
import makeGuid from '../util/makeGuid';
import provide from '../util/provide';
const nm = provide('domUtil');
 
export interface selectChangeCallback{
    /**
     *
     * @param theValue the current select value of the select box
     */
    (theValue: string) : void
}
 
/**
 * must return in the contents <select id="${guid}"></select> among other things
 */
export interface contentGenerator{
    /**
     * @param aGuid
     */
    (aGuid: string): string
}
 
 
export class SelectBoxBase{
    _$container: JQuery;
    $label: JQuery;
    _box: JQuery;
    _changeListeners: Array<selectChangeCallback>;
    /**
     *
     * @param {jQuery} parent - parent container
     * @param {string} labelContent
     * @param {contentGenerator} [contentGen=undefined]
     */
    constructor(parent, labelContent, contentGen?: contentGenerator){
        let guidTop = makeGuid();
        let guid = makeGuid();
 
        let htmlString = `<div id="${guidTop}">`;
        htmlString += `<label for="${guid}">${labelContent}</label>`;
 
        if (contentGen){
            htmlString += contentGen(guid);
        } else {
            htmlString += `<select id="${guid}"></select>`;
        }
        htmlString += '</div>';
 
        parent.append(htmlString);
 
        this._$container = parent.find('#' + guidTop);
 
        this.$label = this._$container.find('label');
 
        /**
         *
         * @type {Array<selectChangeCallback>}
         * @private
         */
        this._changeListeners = [];
 
        this._box = parent.find(`#${guid}`);
        
        if (!this._box){
            throw 'the select box was not found';
        }
 
        this._box.change(() => {
            this.changed();
        })
    }
 
    /**
     * 
     * @returns {jQuery}
     */
    get box(){
        return this._box;
    }
 
    changed(){
        let v = this._box.val();
        
        for (let f of this._changeListeners){
            f(v);
        }
    }
 
    /**
     *
     * @param {selectChangeCallback} func
     */
    addChangeListener(func: selectChangeCallback){
        this._changeListeners.push(func);
    }
 
 
    get selectedValue(): string|number{
        let theVal = this.box.val();
 
        if (theVal == null || typeof theVal == 'undefined'){
            return null;
        } else if (isNaN(theVal)){
            return theVal
        } else {
            if (theVal.indexOf('.') > -1){
                return parseFloat(theVal)
            } else {
                return parseInt(theVal);
            }
        }
    }
 
    /**
     *
     * @param {string|number} v
     */
    set selectedValue(v: string|number){
        this.box.val(v);
    }
    
    get selectedText(){
        return this.box.find('option:selected').text();
    }
 
}
 
nm.SelectBoxBase = SelectBoxBase;
 
export default SelectBoxBase;