/
radio.js
77 lines (61 loc) 路 2.19 KB
/
radio.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
import { t } from '../../util/locale';
export function radio(field) {
var dispatch = d3.dispatch('change'),
labels, radios, placeholder;
function radio(selection) {
selection.classed('preset-radio', true);
var wrap = selection.selectAll('.preset-input-wrap')
.data([0]);
var buttonWrap = wrap.enter().append('div')
.attr('class', 'preset-input-wrap toggle-list');
buttonWrap.append('span')
.attr('class', 'placeholder');
placeholder = selection.selectAll('.placeholder');
labels = wrap.selectAll('label')
.data(field.options || field.keys);
var enter = labels.enter().append('label');
enter.append('input')
.attr('type', 'radio')
.attr('name', field.id)
.attr('value', function(d) { return field.t('options.' + d, { 'default': d }); })
.attr('checked', false);
enter.append('span')
.text(function(d) { return field.t('options.' + d, { 'default': d }); });
radios = labels.selectAll('input')
.on('change', change);
}
function change() {
var t = {};
if (field.key) t[field.key] = undefined;
radios.each(function(d) {
var active = d3.select(this).property('checked');
if (field.key) {
if (active) t[field.key] = d;
} else {
t[d] = active ? 'yes' : undefined;
}
});
dispatch.change(t);
}
radio.tags = function(tags) {
function checked(d) {
if (field.key) {
return tags[field.key] === d;
} else {
return !!(tags[d] && tags[d] !== 'no');
}
}
labels.classed('active', checked);
radios.property('checked', checked);
var selection = radios.filter(function() { return this.checked; });
if (selection.empty()) {
placeholder.text(t('inspector.none'));
} else {
placeholder.text(selection.attr('value'));
}
};
radio.focus = function() {
radios.node().focus();
};
return d3.rebind(radio, dispatch, 'on');
}