|
| 1 | +import { IdentifierSubtypesConfig, IdentifierSubtypesIconPositionEnum } from './../../../../../../../../config/identifier-subtypes-config.interface'; |
1 | 2 | import { Component, Inject, OnInit } from '@angular/core'; |
2 | 3 | import { FieldRenderingType, MetadataBoxFieldRendering } from '../metadata-box.decorator'; |
3 | 4 | import { ResolverStrategyService } from '../../../../../../services/resolver-strategy.service'; |
4 | | -import { hasValue, isNotEmpty } from '../../../../../../../shared/empty.util'; |
| 5 | +import { hasNoValue, hasValue, isNotEmpty } from '../../../../../../../shared/empty.util'; |
5 | 6 | import { MetadataLinkValue } from '../../../../../../models/cris-layout-metadata-link-value.model'; |
6 | 7 | import { RenderingTypeValueModelComponent } from '../rendering-type-value.model'; |
7 | 8 | import { Item } from '../../../../../../../core/shared/item.model'; |
8 | 9 | import { TranslateService } from '@ngx-translate/core'; |
9 | 10 | import { LayoutField } from '../../../../../../../core/layout/models/box.model'; |
10 | 11 | import { MetadataValue } from '../../../../../../../core/shared/metadata.models'; |
| 12 | +import { environment } from 'src/environments/environment'; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * This component renders the identifier metadata fields. |
@@ -37,6 +39,31 @@ export class IdentifierComponent extends RenderingTypeValueModelComponent implem |
37 | 39 | */ |
38 | 40 | target = '_blank'; |
39 | 41 |
|
| 42 | + /** |
| 43 | + * The identifier subtype configurations |
| 44 | + */ |
| 45 | + identifierSubtypeConfig: IdentifierSubtypesConfig[] = environment.identifierSubtypes; |
| 46 | + |
| 47 | + /** |
| 48 | + * The icon to display for the identifier subtype |
| 49 | + */ |
| 50 | + subTypeIcon: string; |
| 51 | + |
| 52 | + /** |
| 53 | + * The position of the icon relative to the identifier |
| 54 | + */ |
| 55 | + iconPosition: IdentifierSubtypesIconPositionEnum = IdentifierSubtypesIconPositionEnum.NONE; |
| 56 | + |
| 57 | + /** |
| 58 | + * The link to navigate to when the icon is clicked |
| 59 | + */ |
| 60 | + iconLink = ''; |
| 61 | + |
| 62 | + /** |
| 63 | + * The identifier subtype to render |
| 64 | + */ |
| 65 | + iconPositionEnum = IdentifierSubtypesIconPositionEnum; |
| 66 | + |
40 | 67 | constructor( |
41 | 68 | @Inject('fieldProvider') public fieldProvider: LayoutField, |
42 | 69 | @Inject('itemProvider') public itemProvider: Item, |
@@ -80,27 +107,82 @@ export class IdentifierComponent extends RenderingTypeValueModelComponent implem |
80 | 107 | return identifier; |
81 | 108 | } |
82 | 109 |
|
| 110 | + /** |
| 111 | + * Create a MetadataLinkValue object with the given href and text |
| 112 | + * @param href the href value |
| 113 | + * @param text the text value |
| 114 | + * @returns MetadataLinkValue object |
| 115 | + */ |
| 116 | + private createMetadataLinkValue(href: string, text: string): MetadataLinkValue { |
| 117 | + text = text.trim() !== '' ? text : href; |
| 118 | + return { href, text }; |
| 119 | + } |
| 120 | + |
83 | 121 | /** |
84 | 122 | * Set href and text of the component based on urn |
85 | | - * and the given metadata value |
| 123 | + * and the given metadata value. |
| 124 | + * Is handling the case when the urn is configured in the default-app-config |
| 125 | + * and the link is pre-configured. |
86 | 126 | * @param metadataValue the metadata value |
87 | 127 | * @param urn URN type (doi, hdl, mailto) |
88 | 128 | */ |
89 | 129 | composeLink(metadataValue: string, urn: string): MetadataLinkValue { |
| 130 | + const subtypeValue = this.getIdentifierSubtypeValue(); |
| 131 | + |
| 132 | + if (hasValue(subtypeValue)) { |
| 133 | + const href = this.validateLink(metadataValue) ? metadataValue : `${subtypeValue.link}/${metadataValue}`; |
| 134 | + return this.createMetadataLinkValue(href, metadataValue); |
| 135 | + } |
| 136 | + |
90 | 137 | let value = metadataValue; |
91 | | - const rep = urn + ':'; |
| 138 | + const rep = `${urn}:`; |
92 | 139 | if (metadataValue.startsWith(rep)) { |
93 | 140 | value = metadataValue.replace(rep, ''); |
94 | 141 | } |
95 | 142 | const href = this.resolver.getBaseUrl(urn) + value; |
96 | | - const text = isNotEmpty(value) && value !== '' ? value : href; |
97 | | - return { |
98 | | - href, |
99 | | - text |
100 | | - }; |
| 143 | + return this.createMetadataLinkValue(href, value); |
101 | 144 | } |
102 | 145 |
|
103 | 146 | ngOnInit(): void { |
104 | 147 | this.identifier = this.getIdentifierFromValue(); |
| 148 | + this.setIconDetails(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the icon details based on the identifier subtype configuration. |
| 153 | + * If the identifier subtype is not empty, it searches for the subtype with a matching name to the rendering subtype. |
| 154 | + * If a matching subtype is found, it sets the icon position, subtype icon, and icon link based on the subtype's properties. |
| 155 | + */ |
| 156 | + private setIconDetails() { |
| 157 | + const subtypeVal = this.getIdentifierSubtypeValue(); |
| 158 | + if (hasNoValue(subtypeVal)) { |
| 159 | + return; |
| 160 | + } |
| 161 | + this.iconPosition = subtypeVal.iconPosition; |
| 162 | + this.subTypeIcon = subtypeVal.iconPosition !== IdentifierSubtypesIconPositionEnum.NONE ? subtypeVal?.icon : ''; |
| 163 | + this.iconLink = subtypeVal?.link; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Retrieves the value of the identifier subtype configuration based on the rendering subtype. |
| 168 | + * @returns The identifier subtype configuration object. |
| 169 | + */ |
| 170 | + private getIdentifierSubtypeValue(): IdentifierSubtypesConfig { |
| 171 | + if (isNotEmpty(this.identifierSubtypeConfig)) { |
| 172 | + const subtypeVal = this.identifierSubtypeConfig.find((subtype) => subtype.name === this.renderingSubType); |
| 173 | + return subtypeVal; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Check if the given link is valid |
| 179 | + * @param link the link to check |
| 180 | + * @returns true if the link is valid, false otherwise |
| 181 | + */ |
| 182 | + private validateLink(link: string): boolean { |
| 183 | + const urlRegex = /^(http|https):\/\/[^ "]+$/; |
| 184 | + return urlRegex.test(link); |
105 | 185 | } |
106 | 186 | } |
| 187 | + |
| 188 | + |
0 commit comments