forked from datagouv/udata-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.vue
437 lines (430 loc) · 18.1 KB
/
resource.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<template>
<article :class="{'drop-shadow': expanded}">
<header
class="fr-p-5v fr-grid-row fr-grid-row--middle no-wrap wrap-md justify-between border-default-grey"
:class="{'border-bottom': !expanded}"
:id="resourceHeaderId"
>
<div class="fr-col-auto fr-grid-row fr-grid-row--top no-wrap">
<div class="fr-col-auto fr-mr-2w fr-icon-svg fr-icon--sm">
<img :src="resourceImage" alt="" />
</div>
<div class="fr-col-auto">
<h4
class="fr-mb-1v"
:id="resourceTitleId"
>
{{ resource.title || $t('Nameless resource') }}
</h4>
<div class="fr-my-0 text-grey-380 fr-grid-row fr-grid-row--middle">
<p v-if="hasSchema" class="fr-tag fr-tag--sm z-2">
<strong class="fr-mr-1v">{{ $t("schema:") }}</strong>{{resource.schema.name ? resource.schema.name : resource.schema.url}}
</p>
<p
v-if="hasSchema && hasSchemaErrors"
class="fr-tag fr-tag--error fr-tag--on-right fr-tag--sm fr-ml-n3v fr-pl-2w fr-icon-error-line fr-tag--icon-left"
>
{{ $t("Invalid") }}
</p>
<p :class="{'dash-before': hasSchema}" class="fr-text--xs fr-m-0 dash-after">{{ $t('Updated {date}', {date: formatRelativeIfRecentDate(lastUpdate)}) }}</p>
<p v-if="resource.format" class="fr-text--xs fr-m-0 dash-after">
{{ resource.format?.trim()?.toLowerCase() }}
<template v-if="resource.filesize">({{ filesize(resource.filesize) }})</template>
</p>
<p class="fr-text--xs fr-m-0">{{ $t('X downloads', resource.metrics.views || 0) }}</p>
</div>
<p class="fr-mb-0 fr-mt-1v fr-text--xs text-grey-380" v-if="isCommunityResource && (resource.organization || resource.owner)">
{{ $t('From') }}
<a class="fr-link" :href="resource.organization.page" v-if="resource.organization">
<OrganizationNameWithCertificate :organization="resource.organization" />
</a>
<template v-else-if="owner">{{owner}}</template>
</p>
</div>
</div>
<div class="fr-col-auto fr-ml-auto">
<div class="fr-grid-row fr-grid-row--middle no-wrap wrap-md">
<p class="text-default-error fr-m-0" v-if="unavailable">
{{$t('Unavailable')}}
</p>
<p class="fr-col-auto fr-ml-3v fr-m-0">
<button
@click="expand"
role="button"
:aria-expanded="expanded"
:aria-controls="resourceContentId"
class="fr-btn fr-btn--tertiary-no-outline fr-btn--icon-left fr-btn--secondary-grey-500"
:class="{'fr-icon-arrow-up-s-line': expanded, 'fr-icon-arrow-down-s-line': !expanded}"
>
<template v-if="expanded">
{{ $t('Close details') }}
</template>
<template v-else>
{{hasExplore ? $t('See data') : $t('See metadata')}}
</template>
</button>
</p>
<p class="fr-col-auto fr-ml-3v fr-m-0" v-if="resource.format === 'url'">
<a
:href="resource.latest"
:title="$t('Resource link - opens a new window')"
rel="nofollow noopener"
target="_blank"
class="fr-btn fr-btn--secondary fr-btn--secondary-grey-500 fr-icon-external-link-line fr-icon--sm"
>
</a>
</p>
<p class="fr-col-auto fr-ml-3v fr-m-0" v-else>
<a
:href="resource.latest"
:title="$t('Download resource')"
download
class="fr-btn fr-btn--secondary fr-btn--secondary-grey-500 fr-icon-download-line fr-icon--sm matomo_download"
>
</a>
</p>
<p class="fr-col-auto fr-ml-3v fr-m-0" v-if="canEdit">
<EditButton
:dataset-id="datasetId"
:resource-id="resource.id"
:is-community-resource="isCommunityResource"
/>
</p>
</div>
</div>
</header>
<section
class="accordion-content"
:aria-labelledby="resourceTitleId"
:id="resourceContentId"
ref="content"
>
<div class="fr-tabs fr-tabs--no-border fr-mt-1v">
<ul class="fr-tabs__list" role="tablist" :aria-label="$t('Resource menu')">
<template v-if="hasExplore">
<li role="presentation">
<button
:id="resourcePreviewButtonId"
class="fr-tabs__tab"
tabindex="0"
role="tab"
aria-selected="true"
:aria-controls="resourcePreviewTabId"
@click="registerEvent(resourcePreviewButtonId)"
>
{{$t('Preview')}}
</button>
</li>
<li role="presentation">
<button
:id="resourceStructureButtonId"
class="fr-tabs__tab"
tabindex="0"
role="tab"
aria-selected="false"
:aria-controls="resourceStructureTabId"
@click="registerEvent(resourceStructureButtonId)"
>
{{$t('Data structure')}}
</button>
</li>
</template>
<li role="presentation">
<button
:id="resourceInformationButtonId"
class="fr-tabs__tab"
:tabindex="resourceInformationTabIndex"
role="tab"
:aria-selected="resourceInformationSelectedTab"
:aria-controls="resourceInformationTabId"
@click="registerEvent(resourceInformationButtonId)"
>
{{$t('Metadata')}}
</button>
</li>
</ul>
<div
:id="resourcePreviewTabId"
class="fr-tabs__panel fr-tabs__panel--selected fr-tabs__panel--no-padding"
role="tabpanel"
:aria-labelledby="resourcePreviewButtonId"
tabindex="0"
v-if="hasExplore"
>
<component v-if="expanded" v-for="ex in explore" :is="ex.component" :resource="resource"/>
</div>
<div
:id="resourceStructureTabId"
class="fr-tabs__panel fr-tabs__panel--selected"
role="tabpanel"
:aria-labelledby="resourceStructureButtonId"
tabindex="0"
v-if="hasExplore || hasSchema"
>
<component v-if="expanded" v-for="dataStructure in structure" :is="dataStructure.component" :resource="resource"/>
<hr class="fr-my-5v fr-p-1v" v-if="hasExplore && hasSchema"/>
<template v-if="hasSchema">
<div class="fr-grid-row fr-grid-row--gutters fr-grid-row--middle">
<div class="fr-col-12 fr-col-lg">
<h5 class="fr-h6 fr-mb-5v">{{$t('Data schema')}}</h5>
<ul class="fr-tags-group" v-if="hasSchemaErrors">
<li v-for="[key, schemaError] in schemaReport">
<p class="fr-tag fr-tag--sm">{{schemaError.name}}</p>
</li>
</ul>
<template v-else>
<div class="fr-grid-row fr-grid-row--middle fr-mb-1w">
<p class="fr-text--xs fr-m-0">{{$t('This file is following a schema: ')}}</p>
<p class="fr-tag fr-tag--sm fr-icon-checkbox-circle-line fr-tag--icon-left">
{{resource.schema.name ? resource.schema.name : resource.schema.url}}
</p>
</div>
<i18n-t keypath="Schemas allow to describe data models, discover how schemas improve your data quality and the available use cases on {address}" class="fr-text--xs fr-m-0" tag="p" scope="global">
<template #address>
<a :href="schema_documentation_url">schema.data.gouv.fr</a>
</template>
</i18n-t>
</template>
</div>
<div class="fr-col-auto">
<div v-if="loading">
<SchemaLoader/>
</div>
<div v-else>
<a
v-if="authorizeValidation"
class="fr-btn fr-btn--secondary fr-btn--secondary-grey-500 fr-mr-3v fr-btn--icon-left fr-icon-checkbox-line"
:href="validationUrl"
>
{{ $t('See validation report') }}
</a>
<a
v-if="resource.schema?.name"
class="fr-btn fr-btn--secondary fr-btn--secondary-grey-500 fr-btn--icon-left fr-icon-book-2-line"
:href="documentationUrl"
>
{{ $t('See schema documentation') }}
</a>
</div>
</div>
</div>
</template>
</div>
<div :id="resourceInformationTabId" class="fr-tabs__panel" role="tabpanel" :aria-labelledby="resourceInformationButtonId" tabindex="0">
<div class="fr-grid-row fr-grid-row--gutters">
<DescriptionList>
<DescriptionTerm>{{ $t('URL') }}</DescriptionTerm>
<DescriptionDetails :withEllipsis="false">
<div class="fr-col text-overflow-ellipsis">
<a :href="resource.url">{{resource.url}}</a>
</div>
<div class="fr-ml-1w fr-col-auto">
<CopyButton :text="resource.url"/>
</div>
</DescriptionDetails>
<DescriptionTerm>{{ $t('Permalink') }}</DescriptionTerm>
<DescriptionDetails :withEllipsis="false">
<div class="fr-col text-overflow-ellipsis">
<a :href="resource.latest">{{resource.latest}}</a>
</div>
<div class="fr-ml-1w fr-col-auto">
<CopyButton :text="resource.latest"/>
</div>
</DescriptionDetails>
<template v-if="resource.checksum">
<DescriptionTerm>{{resource.checksum.type}}</DescriptionTerm>
<DescriptionDetails :withEllipsis="false">
<div class="fr-col text-overflow-ellipsis">
{{resource.checksum.value}}
</div>
<div class="fr-col-auto">
<CopyButton :text="resource.checksum.value"/>
</div>
</DescriptionDetails>
</template>
<template v-if="resource.mime">
<DescriptionTerm>{{ $t('MIME Type') }}</DescriptionTerm>
<DescriptionDetails>
{{resource.mime}}
</DescriptionDetails>
</template>
</DescriptionList>
<DescriptionList>
<DescriptionTerm>{{ $t('Created on') }}</DescriptionTerm>
<DescriptionDetails>
{{formatDate(resource.created_at)}}
</DescriptionDetails>
<DescriptionTerm>{{ $t('Modified on') }}</DescriptionTerm>
<DescriptionDetails>
{{formatDate(resource.last_modified)}}
</DescriptionDetails>
</DescriptionList>
<DescriptionList>
<template v-if="resource.filesize">
<DescriptionTerm>{{ $t('Size') }}</DescriptionTerm>
<DescriptionDetails>
{{ filesize(resource.filesize) }}
</DescriptionDetails>
</template>
</DescriptionList>
</div>
<template v-if="resource.description">
<h5 class="fr-text--sm fr-my-0 fr-text--bold">
{{ $t("File description") }}
</h5>
<div class="fr-mt-0 markdown fr-text--sm text-mention-grey" v-html="markdown(resource.description)">
</div>
</template>
<div class="text-align-right" v-if="!hasExplore && resource.preview_url">
<a
:href="resource.preview_url"
class="fr-btn fr-btn--icon-left fr-icon-test-tube-line"
>
{{ $t("Explore data") }}
</a>
</div>
</div>
</div>
</section>
</article>
</template>
<script>
import { defineComponent, ref, computed, unref } from "vue";
import SchemaLoader from "./schema-loader.vue";
import useOwnerName from "../../../composables/useOwnerName";
import useResourceImage from "../../../composables/useResourceImage";
import CopyButton from "../../utils/copy-button.vue";
import EditButton from "./edit-button.vue";
import { toggleAccordion } from "../../vanilla/accordion";
import DescriptionDetails from "../../utils/description-list/description-details.vue";
import DescriptionList from "../../utils/description-list/description-list.vue";
import DescriptionTerm from "../../utils/description-list/description-term.vue";
import OrganizationNameWithCertificate from "../../organization/organization-name-with-certificate.vue";
import useSchema from "../../../composables/useSchema";
import useComponentsForHook from "../../../composables/useComponentsForHook";
import { explorable_resources, schema_documentation_url } from "../../../config";
import { filesize, formatRelativeIfRecentDate, formatDate, markdown } from "../../../helpers";
export default defineComponent({
components: {DescriptionDetails, DescriptionList, DescriptionTerm, CopyButton, EditButton, OrganizationNameWithCertificate, SchemaLoader},
inheritAttrs: false,
props: {
datasetId: {
type: String,
required: true,
},
isCommunityResource: {
type: Boolean,
default: false,
},
resource: {
/** @type {import("vue").PropType<import("../../../api/resources").Resource>} */
type: Object,
required: true,
},
canEdit: {
type: Boolean,
default: false,
},
typeLabel: {
type: String,
required: true,
},
},
setup(props) {
const owner = useOwnerName(props.resource);
const resourceImage = useResourceImage(props.resource);
const { getComponentsForHook } = useComponentsForHook();
/** @type {import("vue").Ref<HTMLElement | undefined>} */
const content = ref();
const expanded = ref(false);
const expand = () => {
if(expanded.value) {
globalThis._paq?.push(['trackEvent', 'navigation', 'Close resource', props.resource.id]);
} else {
globalThis._paq?.push(['trackEvent', 'navigation', 'Open resource', props.resource.id]);
if(hasExplore.value) {
registerEvent(resourcePreviewButtonId);
} else if (hasSchema.value) {
registerEvent(resourceStructureButtonId);
} else {
registerEvent(resourceInformationButtonId);
}
}
expanded.value = !expanded.value;
if(content.value) {
toggleAccordion(content.value, expanded.value);
}
}
/**
*
* @param {import("vue").ComputedRef<string> | string} tab Tab name
*/
const registerEvent = (tab) => {
const tabName = unref(tab);
globalThis._paq?.push(['trackEvent', 'navigation', 'View resource tab', props.resource.id, tab]);
if(tabName === resourcePreviewButtonId.value) {
globalThis._paq?.push(['trackEvent', 'explore', 'Show preview', props.resource.id]);
} else if (tabName === resourceStructureButtonId.value) {
globalThis._paq?.push(['trackEvent', 'schema', 'Show data structure', props.resource.id]);
}
}
const availabilityChecked = computed(() => props.resource.extras && props.resource.extras['check:status']);
const lastUpdate = computed(() => props.resource.last_modified);
const unavailable = computed(() => availabilityChecked.value && availabilityChecked.value >= 400);
const { authorizeValidation, documentationUrl, loading, validationUrl, schemaReport} = useSchema(props.resource);
const hasSchema = computed(() => !!props.resource.schema.name || !!props.resource.schema.url);
const hasSchemaErrors = computed(() => !!schemaReport.value.size);
const explore = getComponentsForHook("explore");
const structure = getComponentsForHook("data-structure");
const hasExplore = computed(() => explore.length > 0 && explorable_resources && explorable_resources.includes(props.resource.id));
const resourceContentId = computed(() => 'resource-' + props.resource.id);
const resourceHeaderId = computed(() => 'resource-' + props.resource.id + '-header');
const resourceInformationButtonId = computed(() => 'resource-' + props.resource.id + '-information-button');
const resourceInformationTabId = computed(() => 'resource-' + props.resource.id + '-information-tab');
const resourceStructureButtonId = computed(() => 'resource-' + props.resource.id + '-structure-button');
const resourceStructureTabId = computed(() => 'resource-' + props.resource.id + '-structure-tab');
const resourcePreviewButtonId = computed(() => 'resource-' + props.resource.id + '-preview-button');
const resourcePreviewTabId = computed(() => 'resource-' + props.resource.id + '-preview-tab');
const resourceTitleId = computed(() => 'resource-' + props.resource.id + '-title');
const resourceInformationSelectedTab = computed(() => !hasExplore.value);
const resourceInformationTabIndex = computed(() => hasExplore.value ? -1 : 0);
return {
registerEvent,
owner,
resourceImage,
filesize,
formatRelativeIfRecentDate,
formatDate,
markdown,
content,
expanded,
expand,
availabilityChecked,
lastUpdate,
unavailable,
authorizeValidation,
documentationUrl,
loading,
validationUrl,
resourceContentId,
resourceHeaderId,
resourceInformationButtonId,
resourceInformationTabId,
resourcePreviewButtonId,
resourcePreviewTabId,
resourceTitleId,
resourceStructureButtonId,
resourceStructureTabId,
resourceInformationSelectedTab,
resourceInformationTabIndex,
explore,
hasExplore,
structure,
schemaReport,
hasSchema,
hasSchemaErrors,
schema_documentation_url,
}
},
});
</script>