diff --git a/.changeset/hot-comics-act.md b/.changeset/hot-comics-act.md new file mode 100644 index 00000000..e8974109 --- /dev/null +++ b/.changeset/hot-comics-act.md @@ -0,0 +1,5 @@ +--- +'@lblod/ember-rdfa-editor-lblod-plugins': minor +--- + +Add support for inserting truncated addresses diff --git a/addon/components/location-plugin/edit.gts b/addon/components/location-plugin/edit.gts index f5f5a467..3b01c2c2 100644 --- a/addon/components/location-plugin/edit.gts +++ b/addon/components/location-plugin/edit.gts @@ -102,10 +102,10 @@ export default class LocationPluginEditComponent extends Component { @trackedReset({ memo: 'currentAddress', update(component: LocationPluginEditComponent) { - return component.currentTruncateAddressValue; + return component.currentTruncatedValue; }, }) - newTruncateAddressValue?: boolean; + newTruncatedValue?: boolean; get message(): Message | undefined { const value = this.newAddress.value as Address | undefined; @@ -192,12 +192,8 @@ export default class LocationPluginEditComponent extends Component { } } - get currentTruncateAddressValue() { - if (this.currentAddress instanceof Address) { - return this.currentAddress.truncated; - } else { - return undefined; - } + get currentTruncatedValue() { + return this.currentAddress?.truncated; } get canUpdateStreet() { @@ -230,7 +226,7 @@ export default class LocationPluginEditComponent extends Component { newMunicipality, newHousenumber, newBusnumber, - newTruncateAddressValue, + newTruncatedValue, } = this; if ( this.currentAddress && @@ -238,7 +234,7 @@ export default class LocationPluginEditComponent extends Component { newMunicipality === this.currentAddress.municipality && newHousenumber === this.currentAddress.housenumber && newBusnumber === this.currentAddress.busnumber && - newTruncateAddressValue === this.currentAddress.truncated + newTruncatedValue === this.currentAddress.truncated ) { // No need to re-search, nothing has changed return; @@ -252,7 +248,7 @@ export default class LocationPluginEditComponent extends Component { municipality: newMunicipality, busnumber: newBusnumber, housenumber: newHousenumber, - truncated: newTruncateAddressValue, + truncated: newTruncatedValue ?? false, }) ) { return this.currentAddress; @@ -265,7 +261,7 @@ export default class LocationPluginEditComponent extends Component { municipality: newMunicipality, housenumber: newHousenumber, busnumber: newBusnumber, - truncated: newTruncateAddressValue, + truncated: newTruncatedValue ?? false, }, this.args.nodeContentsUtils, ); @@ -276,7 +272,7 @@ export default class LocationPluginEditComponent extends Component { { street: newStreetName, municipality: newMunicipality, - truncated: newTruncateAddressValue, + truncated: newTruncatedValue ?? false, }, this.args.nodeContentsUtils, ); @@ -295,18 +291,7 @@ export default class LocationPluginEditComponent extends Component { this.args.setIsLoading?.(false); } } else { - const address = await resolveAddress( - { - street: newStreetName, - municipality: newMunicipality, - housenumber: newHousenumber, - busnumber: newBusnumber, - truncated: true, - }, - this.args.nodeContentsUtils, - ); - this.args.setAddressToInsert(address); - return address; + return; } }); @@ -318,7 +303,7 @@ export default class LocationPluginEditComponent extends Component { this.newStreetName, this.newHousenumber, this.newBusnumber, - this.newTruncateAddressValue, + this.newTruncatedValue, ], ); @@ -355,8 +340,7 @@ export default class LocationPluginEditComponent extends Component { @action truncateAddress(value: boolean) { - this.newTruncateAddressValue = value; - console.log('this.newTruncateAddressValue', this.newTruncateAddressValue); + this.newTruncatedValue = value; } searchMunicipality = restartableTask(async (term: string) => { @@ -507,7 +491,7 @@ export default class LocationPluginEditComponent extends Component { {{#if this.newStreetName}} {{t 'location-plugin.modal.checkbox-message'}} {{t diff --git a/addon/plugins/location-plugin/node-contents/address.ts b/addon/plugins/location-plugin/node-contents/address.ts index ce750cbf..401590a1 100644 --- a/addon/plugins/location-plugin/node-contents/address.ts +++ b/addon/plugins/location-plugin/node-contents/address.ts @@ -2,6 +2,7 @@ import { ADRES, ADRES_TYPO, DCT, + EXT, LOCN, } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants'; import { findChildWithRdfaAttribute } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace'; @@ -12,45 +13,70 @@ import { constructGeometrySpec } from './point'; import { type NodeContentsUtils } from './'; export const constructAddressSpec = (address: Address) => { - const housenumberNode = address.housenumber - ? [ - ' ', - span( - { property: ADRES('Adresvoorstelling.huisnummer').full }, - address.housenumber, - ), - ] - : []; - const busnumberNode = address.busnumber - ? [ - ' bus ', - span( - { property: ADRES('Adresvoorstelling.busnummer').full }, - address.busnumber, - ), - ] - : []; - const belgianUriNode = address.belgianAddressUri - ? [ - span({ - property: ADRES('verwijstNaar').full, - resource: address.belgianAddressUri, - }), - ] - : []; + const streetNode = span( + { + property: LOCN('thoroughfare').full, + }, + address.street, + ); + const housenumberNode = + address.housenumber && + span( + { property: ADRES('Adresvoorstelling.huisnummer').full }, + address.housenumber, + ); + const busnumberNode = + address.busnumber && + span( + { property: ADRES('Adresvoorstelling.busnummer').full }, + address.busnumber, + ); + const zipcodeNode = address.truncated + ? span({ + property: LOCN('postcode').full, + content: address.zipcode, + }) + : span( + { + property: LOCN('postcode').full, + }, + address.zipcode, + ); + const municipalityNode = address.truncated + ? span({ + property: ADRES('gemeentenaam').full, + language: 'nl', + content: address.municipality, + }) + : span( + { + property: ADRES('gemeentenaam').full, + language: 'nl', + }, + address.municipality, + ); + const belgianUriNode = + address.belgianAddressUri && + span({ + property: ADRES('verwijstNaar').full, + resource: address.belgianAddressUri, + }); + const truncatedNode = span({ + property: EXT('truncated').full, + content: address.truncated, + }); return span( { resource: address.uri, typeof: LOCN('Address').full }, - span( - { - property: LOCN('thoroughfare').full, - }, - address.street, - ), - ...housenumberNode, - ...busnumberNode, - ', ', - ...belgianUriNode, + streetNode, + ...(housenumberNode ? [' ', housenumberNode] : []), + ...(busnumberNode ? [' bus ', busnumberNode] : []), + address.truncated ? '' : ', ', + zipcodeNode, + address.truncated ? '' : ' ', + municipalityNode, + ...(belgianUriNode ? [belgianUriNode] : []), + truncatedNode, constructGeometrySpec(address.location, ADRES('positie')), ); }; @@ -113,6 +139,7 @@ export const parseOldAddressElement = municipality, busnumber: busnumber ?? undefined, location, + truncated: false, }); } else { return; @@ -163,16 +190,29 @@ export const parseAddressElement = 'property', ADRES('Adresvoorstelling.busnummer'), )?.textContent; - const zipcode = findChildWithRdfaAttribute( + const zipCodeNode = findChildWithRdfaAttribute( addressNode, 'property', LOCN('postcode'), - )?.textContent; - const municipality = findChildWithRdfaAttribute( + ); + const zipcode = + zipCodeNode?.getAttribute('content') ?? zipCodeNode?.textContent; + const municipalityNode = findChildWithRdfaAttribute( addressNode, 'property', ADRES('gemeentenaam'), - )?.textContent; + ); + + const municipality = + municipalityNode?.getAttribute('content') ?? + municipalityNode?.textContent; + + const truncated = + findChildWithRdfaAttribute( + addressNode, + 'property', + EXT('truncated'), + )?.getAttribute('content') === 'true'; const pointNode = findChildWithRdfaAttribute( addressNode, @@ -191,6 +231,7 @@ export const parseAddressElement = municipality, busnumber: busnumber ?? undefined, location, + truncated, }); } else { return; diff --git a/addon/plugins/location-plugin/utils/address-helpers.ts b/addon/plugins/location-plugin/utils/address-helpers.ts index ddb2f182..dec39be7 100644 --- a/addon/plugins/location-plugin/utils/address-helpers.ts +++ b/addon/plugins/location-plugin/utils/address-helpers.ts @@ -156,14 +156,15 @@ export class Address { sameAs( other?: Pick< Address, - 'street' | 'housenumber' | 'busnumber' | 'municipality' + 'street' | 'housenumber' | 'busnumber' | 'municipality' | 'truncated' > | null, ) { return ( this.street === other?.street && this.housenumber === other?.housenumber && this.busnumber === other?.busnumber && - this.municipality === other?.municipality + this.municipality === other?.municipality && + this.truncated === other?.truncated ); } @@ -256,6 +257,7 @@ export async function fetchStreets(term: string, municipality: string) { type StreetInfo = { municipality: string; street: string; + truncated: boolean; }; export async function resolveStreet( @@ -283,7 +285,7 @@ export async function resolveStreet( street: unwrap(streetinfo.Thoroughfarename), municipality: streetinfo.Municipality, zipcode: unwrap(streetinfo.Zipcode), - truncated: false, + truncated: info.truncated, location: new Point({ uri: nodeContentsUtils.fallbackGeometryUri(), location: { @@ -317,6 +319,7 @@ type AddressInfo = { street: string; housenumber: string; busnumber?: string; + truncated: boolean; }; export async function resolveAddress( @@ -340,7 +343,7 @@ export async function resolveAddress( municipality: result.gemeente.gemeentenaam.geografischeNaam.spelling, uri: nodeContentsUtils.fallbackAddressUri(), belgianAddressUri: result.identificator.id, - truncated: false, + truncated: info.truncated, location: new Point({ uri: `${result.identificator.id}/1`, location: { @@ -361,6 +364,7 @@ export async function resolveAddress( { street: info.street, municipality: info.municipality, + truncated: info.truncated, }, nodeContentsUtils, ); diff --git a/addon/utils/namespace.ts b/addon/utils/namespace.ts index 842b1d89..95603bad 100644 --- a/addon/utils/namespace.ts +++ b/addon/utils/namespace.ts @@ -102,7 +102,7 @@ export function findChildWithRdfaAttribute( attr: string, value: Resource, ) { - return [...element.children].find((child) => { + return Array.from(element.children).find((child) => { const result = child.getAttribute(attr)?.split(' '); return result?.includes(value.full) || result?.includes(value.prefixed); });