|
1 | 1 | import { ShaclPropertyTemplate } from './property-template' |
2 | 2 | import { Term } from '@rdfjs/types' |
3 | 3 |
|
4 | | -export class Plugins { |
5 | | - private plugins: Record<string, Plugin> = {} |
6 | | - |
7 | | - register(plugin: Plugin) { |
8 | | - if (plugin.predicate === undefined && plugin.datatype === undefined) { |
9 | | - console.warn('not registering plugin because it does neither define "predicate" nor "datatype"', plugin) |
10 | | - } else { |
11 | | - this.plugins[`${plugin.predicate}^${plugin.datatype}`] = plugin |
12 | | - } |
| 4 | +// store plugins in module scope so that they apply to all shacl-form elements |
| 5 | +const plugins: Record<string, Plugin> = {} |
| 6 | + |
| 7 | +export function registerPlugin(plugin: Plugin) { |
| 8 | + if (plugin.predicate === undefined && plugin.datatype === undefined) { |
| 9 | + console.warn('not registering plugin because it does neither define "predicate" nor "datatype"', plugin) |
| 10 | + } else { |
| 11 | + plugins[`${plugin.predicate}^${plugin.datatype}`] = plugin |
13 | 12 | } |
| 13 | +} |
14 | 14 |
|
15 | | - list(): Plugin[] { |
16 | | - return Object.entries(this.plugins).map((value: [_: string, plugin: Plugin]) => { return value[1] }) |
17 | | - } |
| 15 | +export function listPlugins(): Plugin[] { |
| 16 | + return Object.entries(plugins).map((value: [_: string, plugin: Plugin]) => { return value[1] }) |
| 17 | +} |
18 | 18 |
|
19 | | - find(predicate: string | undefined, datatype: string | undefined): Plugin | undefined { |
20 | | - // first try to find plugin with matching predicate and datatype |
21 | | - let plugin = this.plugins[`${predicate}^${datatype}`] |
22 | | - if (plugin) { |
23 | | - return plugin |
24 | | - } |
25 | | - // now prefer predicate over datatype |
26 | | - plugin = this.plugins[`${predicate}^${undefined}`] |
27 | | - if (plugin) { |
28 | | - return plugin |
29 | | - } |
30 | | - // last, try to find plugin with matching datatype |
31 | | - return this.plugins[`${undefined}^${datatype}`] |
| 19 | +export function findPlugin(predicate: string | undefined, datatype: string | undefined): Plugin | undefined { |
| 20 | + // first try to find plugin with matching predicate and datatype |
| 21 | + let plugin = plugins[`${predicate}^${datatype}`] |
| 22 | + if (plugin) { |
| 23 | + return plugin |
| 24 | + } |
| 25 | + // now prefer predicate over datatype |
| 26 | + plugin = plugins[`${predicate}^${undefined}`] |
| 27 | + if (plugin) { |
| 28 | + return plugin |
32 | 29 | } |
| 30 | + // last, try to find plugin with matching datatype |
| 31 | + return plugins[`${undefined}^${datatype}`] |
33 | 32 | } |
34 | 33 |
|
35 | 34 | export type PluginOptions = { |
|
0 commit comments