-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoform-selectable.js
66 lines (58 loc) · 1.96 KB
/
autoform-selectable.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
export const name = 'muqube:autoform-selectable';
AutoForm.addInputType("selectable", {
template: "afSelectable",
valueOut: function() {
const data = this.data();
let selection = this.find('.selected.selectable-option');
if (data.multiple) {
let values = [];
selection.each((index, elem) => {
values.push($(elem).data('value'));
});
return values;
} else {
return selection.data('value');
}
}
});
Template.afSelectable.helpers({
atts: function () {
var data = Template.currentData();
var atts = data.atts;
atts["data-multiple"] = data.atts["multiple"];
atts["data-schema-key"] = data.atts["data-schema-key"];
return atts;
},
isOptionSelected (option) {
const value = Template.currentData().value;
let isSelected = false;
if (Array.isArray(value)) {
isSelected = value.indexOf(option) != -1;
} else {
isSelected = option === value;
}
return (isSelected)? "selected" : "" ;
},
options() {
var data = Template.currentData();
return data.selectOptions;
}
});
Template.afSelectable.events({
'click .selectable-option'(e, t) {
const target = $(e.target),
isMultiple = t && t.data && t.data.atts && t.data.atts.multiple,
isSelected = target.hasClass('selected'),
numSelected = t.findAll('.selected.selectable-option').length,
min = t.data.atts.min || Number.MIN_SAFE_INTEGER,
max = t.data.atts.max || Number.MAX_SAFE_INTEGER;
if (isMultiple){
if ( ((min < numSelected) && isSelected) || ((numSelected < max) && !isSelected) ) {
target.toggleClass('selected');
}
} else {
target.toggleClass('selected');
target.siblings().removeClass('selected');
}
}
});