blob: d1aabe7077a733b2b7d91c93c5c24b7f364ec222 [file] [log] [blame]
Akronc565b7c2021-08-18 18:54:16 +02001window.KorAP = window.KorAP || {};
2
Akronf2279c42017-12-21 13:48:46 +01003KorAP.annotationHelper = KorAP.annotationHelper || { '-' : [] };
4
Akronda5bd3a2020-10-16 17:37:49 +02005"use strict";
6
7define(["util"], function () {
Akronf2279c42017-12-21 13:48:46 +01008
Akron0b489ad2018-02-02 16:49:32 +01009 const ah = KorAP.annotationHelper;
Akronf2279c42017-12-21 13:48:46 +010010
Akronda5bd3a2020-10-16 17:37:49 +020011 ah.getDesc = function (foundryLayer, value) {
Akronf2279c42017-12-21 13:48:46 +010012
13 if (!foundryLayer)
14 return;
15
Akronda5bd3a2020-10-16 17:37:49 +020016 let anno = this[foundryLayer];
Akronf2279c42017-12-21 13:48:46 +010017
18 if (!anno)
19 return;
20
Akron3b253d32018-07-15 10:16:06 +020021 if (!value.includes(':')) {
Akronf2279c42017-12-21 13:48:46 +010022 value += ' ';
23
24 // Iterate over all annotations and add the descriptions
25 // This is a classic hash-lookup-case, but we have
26 // to deal with lists ...
27 for (var i = 0; i < anno.length; i++) {
28 if (anno[i] &&
29 anno[i][1] == value) {
30 if (anno[i][2])
31 return anno[i][2];
32 else
33 return;
34 };
35 };
36
37 return;
38 }
Akronf2279c42017-12-21 13:48:46 +010039
Akronda5bd3a2020-10-16 17:37:49 +020040 else {
41 const v = value.split(":");
42 let l1 = v[0] + ':';
43 let l2 = v[1] + ' ';
44 let text = '';
Akronf2279c42017-12-21 13:48:46 +010045
46 // Add key description
Akronda5bd3a2020-10-16 17:37:49 +020047 for (let i = 0; i < anno.length; i++) {
Akronf2279c42017-12-21 13:48:46 +010048 if (anno[i] &&
49 anno[i][1] == l1) {
50 if (anno[i][2])
51 text += anno[i][2];
52 else
53 text += anno[i][0];
54 break;
55 };
56 };
57
Akron369cbc42018-02-11 00:43:15 +010058 // Nothing found
Akronf2279c42017-12-21 13:48:46 +010059 if (text.length === 0)
60 return;
61
62 // Check next level
63 anno = this[foundryLayer + l1];
64
Akron369cbc42018-02-11 00:43:15 +010065 if (!anno)
66 return;
67
Akronf2279c42017-12-21 13:48:46 +010068 // Add value description
Akronda5bd3a2020-10-16 17:37:49 +020069 for (let i = 0; i < anno.length; i++) {
Akronf2279c42017-12-21 13:48:46 +010070 if (anno[i] &&
71 anno[i][1] == l2) {
72 if (anno[i][2])
73 text += ': ' + anno[i][2];
74
75 return text;
76 };
77 };
78 };
79
80 return '';
81 };
82
Marc Kupietzaa6709c2025-12-19 20:03:54 +010083 /**
84 * Filter available foundries based on configuration.
85 * Reads from data-hint-foundries attribute on body element.
86 * Each foundry module pushes entries like ["Name", "prefix/", "Description"]
Marc Kupietzff51eb62026-01-15 17:39:42 +010087 * to ah["-"]. The prefix (e.g., "corenlp/", "base/s=", "tt/") is matched
88 * against the enabled list by extracting the foundry name (part before /).
Marc Kupietzaa6709c2025-12-19 20:03:54 +010089 */
90 ah.filterByConfig = function () {
91 const body = document.body;
92 if (!body) return;
93
94 const configAttr = body.getAttribute('data-hint-foundries');
95 if (!configAttr) return; // No filter - show all
96
97 const enabledFoundries = configAttr.split(',').map(f => f.trim().toLowerCase());
98 if (enabledFoundries.length === 0) return;
99
100 // Filter the root foundry list ah["-"]
Marc Kupietzff51eb62026-01-15 17:39:42 +0100101 // Each entry is ["Name", "prefix/...", "Description"]
102 // Extract foundry name from prefix: "corenlp/" -> "corenlp", "base/s=" -> "base", "tt/" -> "tt"
Marc Kupietzaa6709c2025-12-19 20:03:54 +0100103 this["-"] = this["-"].filter(entry => {
104 if (!entry || !entry[1]) return false;
Marc Kupietzff51eb62026-01-15 17:39:42 +0100105 // Extract foundry name as the part before the first '/'
106 const foundryName = entry[1].split('/')[0].toLowerCase();
Marc Kupietzaa6709c2025-12-19 20:03:54 +0100107 return enabledFoundries.includes(foundryName);
108 });
109 };
110
Akronf2279c42017-12-21 13:48:46 +0100111 return ah;
Akron80055992017-12-20 16:30:52 +0100112});