track.ts 4.53 KB
Newer Older
Élodie Thiéblin's avatar
Élodie Thiéblin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*******************************************************************************
 * Copyright 2003-2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 * contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 *
 * This file is part of CubicWeb.
 *
 * CubicWeb is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * CubicWeb is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

21
22
23
import { application, definition, implementation, rdfEntities } from '@logilab/libview';
import * as $rdf from 'rdflib';
import Vue from 'vue';
Élodie Thiéblin's avatar
Élodie Thiéblin committed
24
/// <reference path="../common/vue.d.ts" />
25
import TrackImpl from './TrackImpl.vue';
26
import { ONTOLOGY, loadTrack, Talk, Conference } from '../common/schema';
Élodie Thiéblin's avatar
Élodie Thiéblin committed
27
28
29
30
31

/**
 * The descritptor for the default Dbpedia Track view
 */
const DESCRIPTOR_TRACK: definition.ViewDescriptor = {
32
33
34
35
36
37
38
39
40
41
42
43
44
    identifier: '::Logilab::ConferenceTrack',
    name: 'Logilab: Track View',
    description: 'Renders a track from a dataset',
    entrypoint: 'VIEW_TRACK_ENTRYPOINT',
    resourceCss: [
        {
            uri: 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css',
        },
    ],
    resourceJs: [],
    resourceMain: {
        uri: 'http://localhost:8080/view_track.js',
    },
Élodie Thiéblin's avatar
Élodie Thiéblin committed
45
46
47
};

class TrackRendering implements implementation.ViewImplementation {
48
49
50
51
52
53
    descriptor: definition.ViewDescriptor;
    constructor() {
        this.descriptor = DESCRIPTOR_TRACK;
        this.priorityFor = this.priorityFor.bind(this);
        this.render = this.render.bind(this);
    }
Élodie Thiéblin's avatar
Élodie Thiéblin committed
54

55
56
57
58
59
    priorityFor(store: $rdf.Formula, target: application.Resource): number {
        let repo = new rdfEntities.RdfEntityStore(store, ONTOLOGY.aliases, ONTOLOGY.inverses);
        let entity = repo.entityForUri(target.uri);
        let focus = entity.getEntityForS('http://xmlns.com/foaf/0.1/focus');
        if (focus !== null) entity = focus;
Élodie Thiéblin's avatar
Élodie Thiéblin committed
60

61
62
63
64
65
66
67
68
69
70
71
72
        let entityTypes = entity.getEntitiesForS('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
        if (entityTypes.length > 0) {
            let x = entityTypes.find(
                (entity: rdfEntities.RdfEntity) =>
                    entity.uris.indexOf('http://data.semanticweb.org/ns/swc/ontology#TrackEvent') >= 0
            );
            if (x) {
                // The entity is a Track
                return 10;
            }
        }
        return implementation.VIEW_PRIORITY_INAPPROPRIATE;
Élodie Thiéblin's avatar
Élodie Thiéblin committed
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
    render(
        renderer: application.ViewRenderer,
        context: application.RenderingContext,
        target: application.Resource
    ): implementation.ViewRendering {
        let track = loadTrack(renderer, context, target);
        let externals = track.talks
            .map((talk: Talk) => application.cloneResource(talk.__resource))
            .concat(
                track.conferences.map((conference: Conference) => application.cloneResource(conference.__resource))
            );
        let errors: string[] = [];
        let previous = Vue.config.errorHandler;
        Vue.config.errorHandler = function (err, vm, info) {
            errors.push(err.message);
        };
        let vm = new Vue({
            data: {
                descriptor: this.descriptor,
                track: track,
            },
            components: { TrackImpl },
            render: function (createElement) {
                return createElement(
                    'TrackImpl',
                    {
                        props: {
                            context: context,
                            track: track,
                        },
                    },
                    []
                );
            },
        });
        let result = document.createElement('div');
        vm.$mount(result);
        vm.$forceUpdate();
        Vue.config.errorHandler = previous;
        if (errors.length > 0) throw errors[0];
        return {
            dom: vm.$el,
            suggestedResources: externals,
        };
    }
Élodie Thiéblin's avatar
Élodie Thiéblin committed
120
121
122
123
124
125
}

/**
 * Export the view
 */
export const VIEW_TRACK_ENTRYPOINT: TrackRendering = new TrackRendering();