| Akron | c565b7c | 2021-08-18 18:54:16 +0200 | [diff] [blame] | 1 | window.KorAP = window.KorAP || {}; |
| 2 | |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 3 | KorAP.annotationHelper = KorAP.annotationHelper || { '-' : [] }; |
| 4 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 5 | "use strict"; |
| 6 | |
| 7 | define(["util"], function () { |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 8 | |
| Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 9 | const ah = KorAP.annotationHelper; |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 10 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 11 | ah.getDesc = function (foundryLayer, value) { |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 12 | |
| 13 | if (!foundryLayer) |
| 14 | return; |
| 15 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 16 | let anno = this[foundryLayer]; |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 17 | |
| 18 | if (!anno) |
| 19 | return; |
| 20 | |
| Akron | 3b253d3 | 2018-07-15 10:16:06 +0200 | [diff] [blame] | 21 | if (!value.includes(':')) { |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 22 | 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 | } |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 39 | |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 40 | else { |
| 41 | const v = value.split(":"); |
| 42 | let l1 = v[0] + ':'; |
| 43 | let l2 = v[1] + ' '; |
| 44 | let text = ''; |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 45 | |
| 46 | // Add key description |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 47 | for (let i = 0; i < anno.length; i++) { |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 48 | 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 | |
| Akron | 369cbc4 | 2018-02-11 00:43:15 +0100 | [diff] [blame] | 58 | // Nothing found |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 59 | if (text.length === 0) |
| 60 | return; |
| 61 | |
| 62 | // Check next level |
| 63 | anno = this[foundryLayer + l1]; |
| 64 | |
| Akron | 369cbc4 | 2018-02-11 00:43:15 +0100 | [diff] [blame] | 65 | if (!anno) |
| 66 | return; |
| 67 | |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 68 | // Add value description |
| Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 69 | for (let i = 0; i < anno.length; i++) { |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 70 | 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 Kupietz | aa6709c | 2025-12-19 20:03:54 +0100 | [diff] [blame] | 83 | /** |
| 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 Kupietz | ff51eb6 | 2026-01-15 17:39:42 +0100 | [diff] [blame^] | 87 | * 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 Kupietz | aa6709c | 2025-12-19 20:03:54 +0100 | [diff] [blame] | 89 | */ |
| 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 Kupietz | ff51eb6 | 2026-01-15 17:39:42 +0100 | [diff] [blame^] | 101 | // Each entry is ["Name", "prefix/...", "Description"] |
| 102 | // Extract foundry name from prefix: "corenlp/" -> "corenlp", "base/s=" -> "base", "tt/" -> "tt" |
| Marc Kupietz | aa6709c | 2025-12-19 20:03:54 +0100 | [diff] [blame] | 103 | this["-"] = this["-"].filter(entry => { |
| 104 | if (!entry || !entry[1]) return false; |
| Marc Kupietz | ff51eb6 | 2026-01-15 17:39:42 +0100 | [diff] [blame^] | 105 | // Extract foundry name as the part before the first '/' |
| 106 | const foundryName = entry[1].split('/')[0].toLowerCase(); |
| Marc Kupietz | aa6709c | 2025-12-19 20:03:54 +0100 | [diff] [blame] | 107 | return enabledFoundries.includes(foundryName); |
| 108 | }); |
| 109 | }; |
| 110 | |
| Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 111 | return ah; |
| Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 112 | }); |