Skip to content

Commit d8514bb

Browse files
committed
refactor check.ts to typescript
1 parent c8ef28b commit d8514bb

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

modules/globals.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare global {
99
declare var after: typeof afterEach;
1010
declare var VITEST: true;
1111

12-
declare type Tags = { [key: string]: string };
12+
declare type Tags = { [key: string]: string | undefined };
1313

1414
/**
1515
* A class method that acts as both a getter and a
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ export { uiFieldCheck as uiFieldDefaultCheck };
1515
export { uiFieldCheck as uiFieldOnewayCheck };
1616

1717

18-
export function uiFieldCheck(field, context) {
19-
var dispatch = d3_dispatch('change');
20-
var options = field.options;
21-
var values = [];
22-
var texts = [];
18+
export function uiFieldCheck(field: any, context: iD.Context) {
19+
const dispatch = d3_dispatch('change');
20+
let options = field.options;
21+
let values: (string | undefined)[] = [];
22+
let texts: ReturnType<typeof t.append>[] = [];
2323

24-
var _tags;
24+
let _tags: Tags;
2525

26-
var input = d3_select(null);
27-
var text = d3_select(null);
28-
var label = d3_select(null);
29-
var reverser = d3_select(null);
26+
let input: d3.Selection<HTMLInputElement> | d3.Selection<null> = d3_select(null);
27+
let text: d3.Selection<HTMLSpanElement> | d3.Selection<null> = d3_select(null);
28+
let label: d3.Selection<HTMLLabelElement> | d3.Selection<null> = d3_select(null);
29+
let reverser: d3.Selection<HTMLButtonElement> | d3.Selection<null> = d3_select(null);
3030

31-
var _impliedYes;
32-
var _entityIDs = [];
33-
var _value;
31+
let _impliedYes: boolean;
32+
let _entityIDs: string[] = [];
33+
let _value: string | undefined;
3434

3535

3636
var stringsField = field.resolveReference('stringsCrossReference');
@@ -76,7 +76,7 @@ export function uiFieldCheck(field, context) {
7676
}
7777

7878

79-
function reverserSetText(selection) {
79+
function reverserSetText(selection: d3.Selection) {
8080
var entity = _entityIDs.length && context.hasEntity(_entityIDs[0]);
8181
if (reverserHidden() || !entity) return selection;
8282

@@ -94,10 +94,10 @@ export function uiFieldCheck(field, context) {
9494
}
9595

9696

97-
var check = function(selection) {
97+
const check = function(selection: d3.Selection) {
9898
checkImpliedYes();
9999

100-
label = selection.selectAll('.form-field-input-wrap')
100+
label = selection.selectAll<HTMLLabelElement, any>('.form-field-input-wrap')
101101
.data([0]);
102102

103103
var enter = label.enter()
@@ -124,16 +124,16 @@ export function uiFieldCheck(field, context) {
124124
}
125125

126126
label = label.merge(enter);
127-
input = label.selectAll('input');
128-
text = label.selectAll('span.value');
127+
input = label.selectAll<HTMLInputElement, any>('input');
128+
text = label.selectAll<HTMLSpanElement, any>('span.value');
129129

130130
input
131131
.on('click', function(d3_event) {
132132
d3_event.stopPropagation();
133-
var t = {};
133+
var t: Tags = {};
134134

135135
if (Array.isArray(_tags[field.key])) {
136-
if (values.indexOf('yes') !== -1) {
136+
if (values.includes('yes')) {
137137
t[field.key] = 'yes';
138138
} else {
139139
t[field.key] = values[0];
@@ -152,15 +152,15 @@ export function uiFieldCheck(field, context) {
152152
});
153153

154154
if (field.type === 'onewayCheck') {
155-
reverser = label.selectAll('.reverser');
155+
reverser = label.selectAll<HTMLButtonElement, any>('.reverser');
156156

157157
reverser
158158
.call(reverserSetText)
159159
.on('click', function(d3_event) {
160160
d3_event.preventDefault();
161161
d3_event.stopPropagation();
162162
context.perform(
163-
function(graph) {
163+
function(graph: iD.Graph) {
164164
for (var i in _entityIDs) {
165165
graph = actionReverse(_entityIDs[i])(graph);
166166
}
@@ -179,22 +179,22 @@ export function uiFieldCheck(field, context) {
179179
};
180180

181181

182-
check.entityIDs = function(val) {
182+
check.entityIDs = function(val?: string[]) {
183183
if (!arguments.length) return _entityIDs;
184-
_entityIDs = val;
184+
_entityIDs = val!;
185185
return check;
186186
};
187187

188188

189-
check.tags = function(tags) {
189+
check.tags = function(tags: Tags) {
190190

191191
_tags = tags;
192192

193-
function isChecked(val) {
193+
function isChecked(val: string | undefined) {
194194
return val !== 'no' && val !== '' && val !== undefined && val !== null;
195195
}
196196

197-
function textFor(val) {
197+
function textFor(val: string | undefined) {
198198
if (val === '') val = undefined;
199199
var index = values.indexOf(val);
200200
return (index !== -1 ? texts[index] : ('"' + val + '"'));
@@ -204,7 +204,7 @@ export function uiFieldCheck(field, context) {
204204

205205
var isMixed = Array.isArray(tags[field.key]);
206206

207-
_value = !isMixed && tags[field.key] && tags[field.key].toLowerCase();
207+
_value = !isMixed && tags[field.key] ? tags[field.key]?.toLowerCase() : undefined;
208208

209209
if (field.type === 'onewayCheck' && (_value === '1' || _value === '-1')) {
210210
_value = 'yes';
@@ -215,11 +215,12 @@ export function uiFieldCheck(field, context) {
215215
.property('checked', isChecked(_value));
216216

217217
const textForValue = textFor(_value);
218-
text
219-
.text('')
220-
.call(isMixed
218+
text.text('');
219+
text.call(isMixed
221220
? t.append('inspector.multiple_values')
222-
: typeof textForValue === 'string' ? selection => selection.text(textForValue) : textForValue)
221+
: typeof textForValue === 'string'
222+
? (selection: d3.Selection) => selection.text(textForValue)
223+
: textForValue)
223224
.classed('mixed', isMixed);
224225

225226
label
@@ -234,7 +235,7 @@ export function uiFieldCheck(field, context) {
234235

235236

236237
check.focus = function() {
237-
input.node().focus();
238+
input.node()?.focus();
238239
};
239240

240241
return utilRebind(check, dispatch, 'on');

0 commit comments

Comments
 (0)