|
| 1 | +import { |
| 2 | + NgClass, |
| 3 | + NgIf, |
| 4 | + NgStyle, |
| 5 | +} from '@angular/common'; |
| 6 | +import { |
| 7 | + Component, |
| 8 | + Input, |
| 9 | + OnInit, |
| 10 | +} from '@angular/core'; |
| 11 | +import { TranslateModule } from '@ngx-translate/core'; |
| 12 | +import { Item } from 'src/app/core/shared/item.model'; |
| 13 | +import { MetadataFieldWrapperComponent } from 'src/app/shared/metadata-field-wrapper/metadata-field-wrapper.component'; |
| 14 | + |
| 15 | +@Component({ |
| 16 | + selector: 'ds-item-page-cc-license-field', |
| 17 | + templateUrl: './item-page-cc-license-field.component.html', |
| 18 | + standalone: true, |
| 19 | + imports: [NgIf, NgClass, NgStyle, TranslateModule, MetadataFieldWrapperComponent], |
| 20 | +}) |
| 21 | +/** |
| 22 | + * Displays the item's Creative Commons license image in it's simple item page |
| 23 | + */ |
| 24 | +export class ItemPageCcLicenseFieldComponent implements OnInit { |
| 25 | + /** |
| 26 | + * The item to display the CC license image for |
| 27 | + */ |
| 28 | + @Input() item: Item; |
| 29 | + |
| 30 | + /** |
| 31 | + * 'full' variant shows image, a disclaimer (optional) and name (always), better for the item page content. |
| 32 | + * 'small' variant shows image and name (optional), better for the item page sidebar |
| 33 | + */ |
| 34 | + @Input() variant?: 'small' | 'full' = 'small'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Filed name containing the CC license URI, as configured in the back-end, in the 'dspace.cfg' file, propertie |
| 38 | + * 'cc.license.uri' |
| 39 | + */ |
| 40 | + @Input() ccLicenseUriField? = 'dc.rights.uri'; |
| 41 | + |
| 42 | + /** |
| 43 | + * Filed name containing the CC license name, as configured in the back-end, in the 'dspace.cfg' file, propertie |
| 44 | + * 'cc.license.name' |
| 45 | + */ |
| 46 | + @Input() ccLicenseNameField? = 'dc.rights'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Shows the CC license name with the image. Always show if image fails to load |
| 50 | + */ |
| 51 | + @Input() showName? = true; |
| 52 | + |
| 53 | + /** |
| 54 | + * Shows the disclaimer in the 'full' variant of the component |
| 55 | + */ |
| 56 | + @Input() showDisclaimer? = true; |
| 57 | + |
| 58 | + uri: string; |
| 59 | + name: string; |
| 60 | + showImage = true; |
| 61 | + imgSrc: string; |
| 62 | + |
| 63 | + ngOnInit() { |
| 64 | + this.uri = this.item.firstMetadataValue(this.ccLicenseUriField); |
| 65 | + this.name = this.item.firstMetadataValue(this.ccLicenseNameField); |
| 66 | + |
| 67 | + // Extracts the CC license code from the URI |
| 68 | + const regex = /.*creativecommons.org\/(licenses|publicdomain)\/([^/]+)/gm; |
| 69 | + const matches = regex.exec(this.uri ?? '') ?? []; |
| 70 | + const ccCode = matches.length > 2 ? matches[2] : null; |
| 71 | + this.imgSrc = ccCode ? `assets/images/cc-licenses/${ccCode}.png` : null; |
| 72 | + } |
| 73 | +} |
0 commit comments