localisation.tsx 2.69 KB
Newer Older
Fabien Amarger's avatar
Fabien Amarger committed
1
2
3
4
5
6
import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { application, implementation, definition } from '@logilab/libview';
import * as $rdf from 'rdflib';

7
8
9
const RDF = $rdf.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
const RDFS = $rdf.Namespace('http://www.w3.org/2000/01/rdf-schema#');

Fabien Amarger's avatar
Fabien Amarger committed
10
11
12
13
14
15
16
17
18
19
const DESCRIPTOR_LOCALISATION: definition.ViewDescriptor = {
    identifier: '::Culture::Localisation',
    name: 'Culture: Localisation de bien culturel',
    description:
        'Affiche une carte contenant la localisation des biens culturels dans les données ouvertes du ministère de la culture',
    entrypoint: 'VIEW_ENTRYPOINT',
    resourceCss: [],
    resourceJs: [],
    resourceMain: {
        location: 'remote',
20
        uri: `${process.env.DOMAIN ?? "http://localhost:8080"}/view_localisation.js`,
Fabien Amarger's avatar
Fabien Amarger committed
21
22
23
    } as definition.ViewResourceRemote,
};

24
25
26
27
28
29
30
31
32
33
34
interface BienCulturelProps {
    typeUris: string[];
    label: string;
}

const BienCulturel: React.FC<BienCulturelProps> = ({ typeUris, label }) => (
    <h1>
        {label} -- {typeUris.join(', ')}
    </h1>
);

Fabien Amarger's avatar
Fabien Amarger committed
35
36
class LocalisationRendering implements implementation.ViewImplementation {
    descriptor: definition.ViewDescriptor = DESCRIPTOR_LOCALISATION;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    priorityFor(context: $rdf.Formula, target: application.Resource): number {
        let score = 0;

        const targetSym = $rdf.sym(target.uri);

        // has at least one type
        const firstType = context.any(targetSym, RDF('type'));
        if (firstType !== null) {
            score += 10;
        }

        // has at least one label
        const firstLabel = context.any(targetSym, RDFS('label'));
        if (firstLabel !== null) {
            score += 10;
        }
        return score;
Fabien Amarger's avatar
Fabien Amarger committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    }

    render(
        renderer: application.ViewRenderer,
        context: application.RenderingContext,
        target: application.Resource
    ): implementation.ViewRendering {
        return {
            dom: this.simpleRender(context.store, target.uri),
            suggestedResources: [],
        };
    }

    simpleRender(context: $rdf.Formula, target: string): Element {
        const targetSym = $rdf.sym(target);

70
71
72
        const bienCulturel: BienCulturelProps = {
            typeUris: context.each(targetSym, RDF('type')).map($rdf.Node.toJS) as string[],
            label: context.anyJS(targetSym, RDFS('label')).value as string,
Fabien Amarger's avatar
Fabien Amarger committed
73
74
75
        };

        const result = document.createElement('div');
76
        ReactDOM.render(<BienCulturel {...bienCulturel} />, result);
Fabien Amarger's avatar
Fabien Amarger committed
77
78
79
80
81
82
83
84
85
        return result;
    }
}

/**
 * Export the view
 */
export const VIEW_LOCALISATION_ENTRYPOINT: LocalisationRendering = new LocalisationRendering();
export const VIEW_ENTRYPOINT = VIEW_LOCALISATION_ENTRYPOINT;