Skip to content

Commit

Permalink
Factor TooltipModel from TooltipView (#448 #447)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Jun 13, 2022
1 parent a274d89 commit 17c9c53
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
60 changes: 60 additions & 0 deletions frontend/src/tooltip/tooltip-model-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { enableI18n, event } from '../test-util';

import { readit, rdfs, skos } from '../common-rdf/ns';
import { FlatLdObject } from '../common-rdf/json';
import Node from '../common-rdf/node';
import FlatItem from '../common-adapters/flat-item-model';

import toTooltip from './tooltip-model';

function getDefaultAttributes(): FlatLdObject {
return {
'@id': readit('test'),
"@type": [rdfs.Class],
[skos.prefLabel]: [
{ '@value': 'Content' },
],
[skos.altLabel]: [
{ '@value': 'alternativeLabel' }
],
[skos.definition]: [
{ '@value': 'This is a test definition' }
],
[rdfs.comment]: [
{ '@value': 'Also, I have a comment' }
],
}
}

function getDefaultItem(): FlatItem {
return new FlatItem(new Node(getDefaultAttributes()));
}

describe('Tooltip model adapter', function () {
beforeAll(enableI18n);

beforeEach(function() {
this.item = getDefaultItem();
});

it('uses skos:definition if available', async function () {
const model = toTooltip(this.item);
await event(this.item, 'complete');
expect(model.get('text')).toEqual('This is a test definition');
});

it('uses rdfs:comment otherwise', async function() {
this.item.underlying.unset(skos.definition);
const model = toTooltip(this.item);
await event(this.item, 'complete');
expect(model.get('text')).toEqual('Also, I have a comment');
});

it('unsets the text when definition and comment are absent', async function() {
this.item.underlying.unset(skos.definition);
this.item.underlying.unset(rdfs.comment);
const model = toTooltip(this.item);
await event(this.item, 'complete');
expect(model.has('text')).toBe(false);
});
});
40 changes: 40 additions & 0 deletions frontend/src/tooltip/tooltip-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as i18next from 'i18next';

import Model from '../core/model';
import { rdfs, skos } from '../common-rdf/ns';
import FlatItem from '../common-adapters/flat-item-model';

/**
* Model adapter that lets you feed a `FlatItem` to a `TooltipView`.
*
* `TooltipView` expects a model with just a `text` attribute. The adapter
* extracts either the `skos:definition` or the `rdfs:comment`, depending on
* which is available, from the underlying model, and sets this as the `text`
* attribute. It takes the currently selected language into account.
*
* For optimum convenience, use the `toTooltip` helper function instead of
* instantiating the class directly.
*/
export class FlatAsTooltipAdapter extends Model {
constructor(underlying: FlatItem, options?: any) {
super(null, options);
underlying.when('classLabel', this.adapt, this);
}

adapt(underlying): void {
const cls = underlying.get('class');
const languageOption = { '@language': i18next.language };
const definition = cls.get(skos.definition, languageOption);
const comment = definition || cls.get(rdfs.comment, languageOption);
const text = definition && definition[0] || comment && comment[0];
this.set({text});
}
}

/**
* Helper function to wrap a `FlatItem` in a model with the expected attributes
* for a `TooltipView`.
*/
export default function toTooltip(model: FlatItem) {
return new FlatAsTooltipAdapter(model);
}

0 comments on commit 17c9c53

Please sign in to comment.