Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<CopyButton
:label="t('Copier le lien')"
:copied-label="t('Lien copié !')"
:text="resourceExternalUrl"
:text="externalUrl"
class="z-2"
/>
</div>
Expand Down Expand Up @@ -327,7 +327,7 @@ import { trackEvent } from '../../functions/matomo'
import CopyButton from '../CopyButton.vue'
import { useComponentsConfig } from '../../config'
import { getOwnerName } from '../../functions/owned'
import { getResourceFormatIcon, getResourceTitleId, detectOgcService, getResourceExternalUrl, getResourceFilesize, isImagePreviewFormat } from '../../functions/resources'
import { getResourceFormatIcon, getResourceTitleId, detectOgcService, getResourceFilesize, isImagePreviewFormat, resolveResourceExternalUrl } from '../../functions/resources'
import BrandedButton from '../BrandedButton.vue'
import { useTranslation } from '../../composables/useTranslation'
import { useHasTabularData } from '../../composables/useHasTabularData'
Expand All @@ -349,6 +349,8 @@ const props = withDefaults(defineProps<{
isCommunityResource?: boolean
resource: Resource | CommunityResource
canEdit?: boolean
// Overrides the "Copier le lien" target.
resourceExternalUrl?: (resource: Resource | CommunityResource) => string
}>(), {
expandedOnMount: false,
isCommunityResource: false,
Expand Down Expand Up @@ -468,7 +470,7 @@ const resourceFilesize = computed(() => getResourceFilesize(props.resource))
const unavailable = availabilityChecked && props.resource.extras['check:available'] === false
const downloadButtonTitle = unavailable ? t(`Le robot de {certifier} n'a pas pu accéder à ce fichier - Télécharger le fichier en {format}`, { certifier: config.name, format: format.value }) : t(`Télécharger le fichier en {format}`, { format: format.value })

const resourceExternalUrl = computed(() => getResourceExternalUrl(props.dataset, props.resource))
const externalUrl = computed(() => resolveResourceExternalUrl(props.dataset, props.resource, props.resourceExternalUrl))

const resourceContentId = 'resource-' + props.resource.id
const resourceHeaderId = 'resource-' + props.resource.id + '-header'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
:resources="flatResources"
:resource-to="resourceTo"
:explore-to="exploreTo"
:resource-external-url="resourceExternalUrl"
replace
:fullscreen
/>
Expand All @@ -59,6 +60,7 @@
:resources="flatResources"
:resource-to="resourceTo"
:explore-to="exploreTo"
:resource-external-url="resourceExternalUrl"
replace
:fullscreen
/>
Expand Down Expand Up @@ -122,6 +124,8 @@ const props = withDefaults(defineProps<{
// Inline mode only: link builder for the "Explorer" button in the viewer header
// that opens the fullscreen explorer on the current resource.
exploreTo?: (resource: Resource) => string
// Overrides the "Copier le lien" target.
resourceExternalUrl?: (resource: Resource) => string
}>(), {
noResultsImage: '',
fullscreen: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:resources
:resource-to
:explore-to="exploreTo"
:resource-external-url="resourceExternalUrl"
:replace
:fullscreen
/>
Expand Down Expand Up @@ -217,6 +218,8 @@ const props = withDefaults(defineProps<{
// When provided (inline mode), shows an "Explorer" button next to the download
// action that opens the fullscreen explorer on the current resource.
exploreTo?: (resource: Resource) => string
// Overrides the "Copier le lien" target.
resourceExternalUrl?: (resource: Resource) => string
replace?: boolean
// Fullscreen mode: make the viewer a flex column so the table fills down to the
// bottom, and hide the inline download/visit/copy actions — they're shown in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<CopyButton
:label="t('Copier le lien')"
:copied-label="t('Lien copié !')"
:text="resourceExternalUrl"
:text="externalUrl"
icon-only
class="hidden shrink-0 md:inline-flex"
/>
Expand Down Expand Up @@ -94,7 +94,7 @@ import ResourceMainAction from './ResourceMainAction.vue'
import FormattedDate from '../FormattedDate.vue'
import TranslationT from '../TranslationT.vue'
import { filesize, summarize } from '../../functions/helpers'
import { getResourceExternalUrl, getResourceFilesize } from '../../functions/resources'
import { getResourceFilesize, resolveResourceExternalUrl } from '../../functions/resources'
import { trackEvent } from '../../functions/matomo'
import { useTranslation } from '../../composables/useTranslation'
import type { RouteLocationRaw } from 'vue-router'
Expand All @@ -110,6 +110,8 @@ const props = defineProps<{
resources?: Resource[]
resourceTo?: (resource: Resource) => RouteLocationRaw
exploreTo?: (resource: Resource) => string
// Overrides the "Copier le lien" target.
resourceExternalUrl?: (resource: Resource) => string
replace?: boolean
// Fullscreen mode hides the inline actions — they live in the dataset context bar above.
fullscreen?: boolean
Expand All @@ -118,5 +120,5 @@ const props = defineProps<{
const { t } = useTranslation()

const resourceFilesize = computed(() => getResourceFilesize(props.resource))
const resourceExternalUrl = computed(() => getResourceExternalUrl(props.dataset, props.resource))
const externalUrl = computed(() => resolveResourceExternalUrl(props.dataset, props.resource, props.resourceExternalUrl))
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:resources
:resource-to
:explore-to="exploreTo"
:resource-external-url="resourceExternalUrl"
:replace
:fullscreen
/>
Expand Down Expand Up @@ -80,6 +81,7 @@ const props = defineProps<{
resources?: Resource[]
resourceTo?: (resource: Resource) => RouteLocationRaw
exploreTo?: (resource: Resource) => string
resourceExternalUrl?: (resource: Resource) => string
replace?: boolean
fullscreen?: boolean
}>()
Expand Down
8 changes: 0 additions & 8 deletions datagouv-components/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { inject, type Component, type InjectionKey } from 'vue'
import type { UseFetchFunction } from './functions/api.types'
import type { $Fetch, FetchOptions } from 'ofetch'
import type { Dataset, DatasetV2 } from './types/datasets'
import type { CommunityResource, Resource } from './types/resources'

export type PluginConfig = {
name: string // Name of the application (ex: data.gouv.fr)
Expand Down Expand Up @@ -47,12 +45,6 @@ export type PluginConfig = {
clientOnly?: Component | null
searchDebounce?: number
forumUrl?: string
/**
* Overrides the "Copier le lien" target built by `getResourceExternalUrl`.
* Must return an absolute URL: it is copied to the clipboard and used as an
* external, `_blank` link target.
*/
getResourceExternalUrl?: (dataset: Dataset | DatasetV2 | Omit<Dataset, 'resources' | 'community_resources'>, resource: Resource | CommunityResource) => string
}

export const configKey = Symbol() as InjectionKey<PluginConfig>
Expand Down
11 changes: 8 additions & 3 deletions datagouv-components/src/functions/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@ export function isCommunityResource(resource: Resource | CommunityResource): boo
}

export function getResourceExternalUrl(dataset: Dataset | DatasetV2 | Omit<Dataset, 'resources' | 'community_resources'>, resource: Resource | CommunityResource): string {
const config = useComponentsConfig()
if (config.getResourceExternalUrl) return config.getResourceExternalUrl(dataset, resource)

return `${dataset.page}${isCommunityResource(resource) ? '/community-resources' : ''}?resource_id=${resource.id}`
}

export function resolveResourceExternalUrl<R extends Resource | CommunityResource>(
dataset: Dataset | DatasetV2 | Omit<Dataset, 'resources' | 'community_resources'>,
resource: R,
override?: (resource: R) => string,
): string {
return override ? override(resource) : getResourceExternalUrl(dataset, resource)
}

export function getResourceFilesize(resource: Resource): null | number {
if (resource.filesize) return resource.filesize
if ('analysis:content-length' in resource.extras) return resource.extras['analysis:content-length'] as number
Expand Down
Loading