-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.js
94 lines (90 loc) · 2.26 KB
/
helper.js
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
/* eslint-disable camelcase */
/* eslint-disable no-unused-vars */
"use strict";
/**
* Populate a select with options
* @param {Object} select html select element
* @param {Object} options object of key->value pairs
* @param {string} def_value default value
* @param {boolean} clear_first remove remove all existing values prior to populating
*/
function helper_populate_select(
select,
options,
def_value = null,
clear_first = true
) {
if (clear_first) {
// remove all options
select.innerHTML = "";
}
// add options
const keys = Object.keys(options);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = options[key];
const option = document.createElement("option");
option.value = key;
option.text = value;
if (def_value && key === def_value) {
option.selected = true;
}
select.appendChild(option);
}
}
/**
* Populate a select with options
* @param {array} array array/set
* @return {Object} object of map of key -> value pairs with key=value
*/
function helper_array_to_object_of_key_eq_value(array) {
return array.reduce((obj, value) => {
obj[value] = value;
return obj;
}, {});
}
/**
* Tabulator table, column definition - string
* @param {string} field field id/name
* @param {string} title title to be displayed
* @param {int} width width
* @return {Object} object of column definition
*/
function helper_tabulator_col_str(field, title, width = 0) {
const obj = {
field: field,
title: title,
sorter: "string",
headerFilter: true,
};
if (width > 0) {
obj.width = width;
}
return obj;
}
/**
* Tabulator table, column definition - numeric
* @param {string} field field id/name
* @param {string} title title to be displayed
* @return {Object} object of column definition
*/
function helper_tabulator_col_num(field, title) {
const obj = {
field: field,
title: title,
sorter: "number",
headerFilter: "number",
hozAlign: "right",
sorterParams: {
alignEmptyValues: "bottom",
},
headerFilterPlaceholder: ">=",
headerFilterFunc: ">=",
formatter: function (cell, formatterParams, onRendered) {
if (cell.getValue()) {
return Math.round(cell.getValue() * 10) / 10;
}
},
};
return obj;
}