Skip to content

Commit 66c5925

Browse files
committed
Factor TooltipModel from TooltipView (#448 #447)
1 parent 66b4010 commit 66c5925

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { enableI18n, event } from '../test-util';
2+
3+
import { readit, rdfs, skos } from '../common-rdf/ns';
4+
import { FlatLdObject } from '../common-rdf/json';
5+
import Node from '../common-rdf/node';
6+
import FlatItem from '../common-adapters/flat-item-model';
7+
8+
import toTooltip from './tooltip-model';
9+
10+
function getDefaultAttributes(): FlatLdObject {
11+
return {
12+
'@id': readit('test'),
13+
"@type": [rdfs.Class],
14+
[skos.prefLabel]: [
15+
{ '@value': 'Content' },
16+
],
17+
[skos.altLabel]: [
18+
{ '@value': 'alternativeLabel' }
19+
],
20+
[skos.definition]: [
21+
{ '@value': 'This is a test definition' }
22+
],
23+
[rdfs.comment]: [
24+
{ '@value': 'Also, I have a comment' }
25+
],
26+
}
27+
}
28+
29+
function getDefaultItem(): FlatItem {
30+
return new FlatItem(new Node(getDefaultAttributes()));
31+
}
32+
33+
describe('Tooltip model adapter', function () {
34+
beforeAll(enableI18n);
35+
36+
beforeEach(function() {
37+
this.item = getDefaultItem();
38+
});
39+
40+
it('uses skos:definition if available', async function () {
41+
const model = toTooltip(this.item);
42+
await event(this.item, 'complete');
43+
expect(model.get('text')).toEqual('This is a test definition');
44+
});
45+
46+
it('uses rdfs:comment otherwise', async function() {
47+
this.item.underlying.unset(skos.definition);
48+
const model = toTooltip(this.item);
49+
await event(this.item, 'complete');
50+
expect(model.get('text')).toEqual('Also, I have a comment');
51+
});
52+
53+
it('unsets the text when definition and comment are absent', async function() {
54+
this.item.underlying.unset(skos.definition);
55+
this.item.underlying.unset(rdfs.comment);
56+
const model = toTooltip(this.item);
57+
await event(this.item, 'complete');
58+
expect(model.has('text')).toBe(false);
59+
});
60+
});

frontend/src/tooltip/tooltip-model.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as i18next from 'i18next';
2+
3+
import Model from '../core/model';
4+
import { rdfs, skos } from '../common-rdf/ns';
5+
import FlatItem from '../common-adapters/flat-item-model';
6+
7+
/**
8+
* Model adapter that lets you feed a `FlatItem` to a `TooltipView`.
9+
*
10+
* `TooltipView` expects a model with just a `text` attribute. The adapter
11+
* extracts either the `skos:definition` or the `rdfs:comment`, depending on
12+
* which is available, from the underlying model, and sets this as the `text`
13+
* attribute. It takes the currently selected language into account.
14+
*
15+
* For optimum convenience, use the `toTooltip` helper function instead of
16+
* instantiating the class directly.
17+
*/
18+
export class FlatAsTooltipAdapter extends Model {
19+
constructor(underlying: FlatItem, options?: any) {
20+
super(null, options);
21+
underlying.when('classLabel', this.adapt, this);
22+
}
23+
24+
adapt(underlying): void {
25+
const cls = underlying.get('class');
26+
const languageOption = { '@language': i18next.language };
27+
const definition = cls.get(skos.definition, languageOption);
28+
const comment = definition || cls.get(rdfs.comment, languageOption);
29+
const text = definition && definition[0] || comment && comment[0];
30+
this.set({text});
31+
}
32+
}
33+
34+
/**
35+
* Helper function to wrap a `FlatItem` in a model with the expected attributes
36+
* for a `TooltipView`.
37+
*/
38+
export default function toTooltip(model: FlatItem) {
39+
return new FlatAsTooltipAdapter(model);
40+
}

0 commit comments

Comments
 (0)