-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.js
70 lines (67 loc) · 1.7 KB
/
filter.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
const {
option,
a,
h5,
span,
text_attr,
script,
input,
style,
select,
domReady,
} = require("@saltcorn/markup/tags");
const { select_options } = require("@saltcorn/markup/helpers");
module.exports = {
type: "Key",
isFilter: true,
isEdit: false,
configFields: [
{ name: "multiple", label: "Multiple", type: "Bool" },
/*
//Doesnt work
{
name: "stay_open_on_select",
label: "Stay open",
sublabel: "Do not close on select",
type: "Bool",
},*/
],
run: (nm, v, attrs = {}, cls, required, field, state = {}) => {
const selected = Array.isArray(v)
? v
: typeof v === "undefined" || v === null
? []
: [v];
const options = (field.options || []).map((o) =>
option({ value: o.value, selected: selected.includes(o.value) }, o.label)
);
return (
select(
{
id: `input${text_attr(nm)}filter`,
class: `form-control ${cls} ${field.class || ""}`,
multiple: attrs.multiple ? "multiple" : undefined,
},
options
) +
script(
domReady(`
function update() {
const selected = $('#input${text_attr(
nm
)}filter').select2('data');
const sel_ids = selected.map(s=>s.id);
set_state_field("${nm}", sel_ids, $("#input${text_attr(nm)}filter"))
}
$('#input${text_attr(nm)}filter').select2({
width: '100%',
${attrs.stay_open_on_select ? "closeOnSelect: false," : ""}
dropdownParent: $('#input${text_attr(
nm
)}filter').parent(),
}).on('select2:select', update).on('select2:unselect', update);
`)
)
);
},
};