Skip to content

Commit

Permalink
allow duplicate values in semiCombo for some fields
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Dec 30, 2024
1 parent 28183c7 commit 49c838f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// cdns for external data packages
const presetsCdnUrl = ENV__ID_PRESETS_CDN_URL
|| 'https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema@{presets_version}/';
|| 'https://raw.githubusercontent.com/k-yle/id-tagging-schema/kyle-deploy/'; // FIXME: TEMPORARY
const ociCdnUrl = ENV__ID_OCI_CDN_URL
|| 'https://cdn.jsdelivr.net/npm/osm-community-index@{version}/';
const wmfSitematrixCdnUrl = ENV__ID_WMF_SITEMATRIX_CDN_URL
Expand Down
38 changes: 27 additions & 11 deletions modules/ui/fields/combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ export function uiFieldCombo(field, context) {
_comboData = _comboData.filter(filter);
}

_comboData = objectDifference(_comboData, _multiData);
if (!field.allowDuplicates) {
_comboData = objectDifference(_comboData, _multiData);
}
_combobox.data(_comboData);
if (callback) callback(_comboData);
}
Expand Down Expand Up @@ -289,7 +291,9 @@ export function uiFieldCombo(field, context) {

_comboData = _comboData.filter(queryFilter);

_comboData = objectDifference(_comboData, _multiData);
if (!field.allowDuplicates) {
_comboData = objectDifference(_comboData, _multiData);
}
if (callback) callback(_comboData, hasStaticValues());
});
}
Expand Down Expand Up @@ -384,7 +388,10 @@ export function uiFieldCombo(field, context) {
} else if (_isSemi) {
var arr = _multiData.map(function(d) { return d.key; });
arr = arr.concat(vals);
t[field.key] = context.cleanTagValue(utilArrayUniq(arr).filter(Boolean).join(';'));
if (!field.allowDuplicates) {
arr = utilArrayUniq(arr);
}
t[field.key] = context.cleanTagValue(arr.filter(Boolean).join(';'));
}

window.setTimeout(function() { _input.node().focus(); }, 10);
Expand All @@ -410,11 +417,15 @@ export function uiFieldCombo(field, context) {
if (_isMulti) {
t[d.key] = undefined;
} else if (_isSemi) {
var arr = _multiData.map(function(md) {
return md.key === d.key ? null : md.key;
}).filter(Boolean);
let arr = _multiData.map(item => item.key);

// delete the value using the index, since a value
// may exist multiple times in the array.
arr.splice(d.index, 1);

arr = utilArrayUniq(arr);
if (!field.allowDuplicates) {
arr = utilArrayUniq(arr);
}
t[field.key] = arr.length ? arr.join(';') : undefined;

_lengthIndicator.update(t[field.key]);
Expand Down Expand Up @@ -620,21 +631,26 @@ export function uiFieldCombo(field, context) {
if (Array.isArray(tags[field.key])) {

tags[field.key].forEach(function(tagVal) {
var thisVals = utilArrayUniq((tagVal || '').split(';')).filter(Boolean);
var thisVals = (tagVal || '').split(';').filter(Boolean);
allValues = allValues.concat(thisVals);
if (!commonValues) {
commonValues = thisVals;
} else {
commonValues = commonValues.filter(value => thisVals.includes(value));
}
});
allValues = utilArrayUniq(allValues).filter(Boolean);
allValues = allValues.filter(Boolean);

} else {
allValues = utilArrayUniq((tags[field.key] || '').split(';')).filter(Boolean);
allValues = (tags[field.key] || '').split(';').filter(Boolean);
commonValues = allValues;
}

if (!field.allowDuplicates) {
commonValues = utilArrayUniq(commonValues);
allValues = utilArrayUniq(allValues);
}

_multiData = allValues.map(function(v) {
return {
key: v,
Expand Down Expand Up @@ -667,7 +683,7 @@ export function uiFieldCombo(field, context) {

// Render chips
var chips = _container.selectAll('.chip')
.data(_multiData);
.data(_multiData.map((item, index) => ({ ...item, index })));

chips.exit()
.remove();
Expand Down

0 comments on commit 49c838f

Please sign in to comment.