summaryrefslogtreecommitdiff
path: root/public/app/views/js/select2-supplementary.php
blob: a7aa7e2255d2133cd667839b179ea4d050649dd5 (plain)
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
<script>

    // supplementary functions to select2 control
    ////////////////////////////////////////////////////////////////////////////

    /** getValues
     * --- -- -- - - -
     * Get selected value(s) of Select2 control
     * 
     * @param arg: source-element
     * @return (string) value(s) comma-separated
     */
    function getValues(srcElm) {
        var res;
        var obj = srcElm.select2('data');      // get delected data array

        if (obj.length === 0)    return '';    // if empty list then nothig is selected
        else {
            var i = 0;
            obj.forEach(function(elm){         // loop through object elements array

                if (i)  res += ',' + elm.id;   // if not first item, prepend ',' befor value(id)
                else    res = elm.id;        	 // else set (first) checked value

                i++;
            });
        }
        return res;
    }

    /** select2_set_text 
     * --- -- -- - - -
     * select an (select2-)option by text
     * onto a (single) select2 control
     * 
     * @param control = jquery selector,
     * @param t (string) = text of option
     */
    function select2_set_text(control, t) {
        opts = control.select2()[0].options;
        var id;
        for(i=0; i<opts.length; i++) { if (opts[i].text == t) id = i; }
        control.val(id).trigger('change');
    }

    /** get_id_of_text
     * --- -- -- - - -
     * get id (=value) of an option with `t` as text
     * onto a select2 control;
     * 
     * Can be used on single or multiple select2 contols
     * 
     * @param control = jquery selector,
     * @param t (string) = text of option
     */
    function get_id_of_text( control , t) {
        opts = control.select2()[0].options;
        var id = false;
        for(i=0; i<opts.length; i++) { if (opts[i].text == t) id = i; }
        return id;
    }

    /** select2_set_multi_text
     * --- -- -- - - -
     * select several (select2-)options by text
     * 
     * @param control  = jquery selector,
     * @param t_list (string) = list of selected texts
     */
    function select2_set_multi_text(control, t_list) {   /* define function */
        var ids = [];
        t_list.forEach (t => { let id = get_id_of_text(control, t);
            if (id !== false) ids.push(id);
        })
        control.val(ids).change();
    }

    /** selected_text
     * --- -- -- - - -
     * get selected text of a select2 control
     */
    function selected_text(control) {
        if (typeof control.select2('data') === 'undefined') return false;
        else if (control.select2('data').length == 0) return false;
        else return control.select2('data')[0].text;
    }

    /**
     * get all selected texts from a multi-select2 contol
     */
    function selected_multitexts(control) {
        if (typeof control.select2('data') === 'undefined') return false;
        else if (control.select2('data').length == 0) return false;
        else {
            var result = [];
            control.select2('data').forEach( item => { result.push(item.text); });
            return result;
        }
    }

</script>