Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 1 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 2 | * Get information on matches, |
| 3 | * generate annotation tables and trees. |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 4 | * |
| 5 | * @author Nils Diewald |
| 6 | */ |
| 7 | /* |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 8 | * - Highlight (at least mark as bold) the match |
| 9 | * - Scroll to match vertically per default |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 10 | * - A click on a table field and a tree node should at the field description to the fragments list. |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 11 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 12 | |
| 13 | "use strict"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | define([ |
Akron | 13448c2 | 2018-07-10 13:05:46 +0200 | [diff] [blame] | 15 | 'buttongroup', |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 16 | 'panel/match', |
Akron | 8b592d4 | 2018-01-26 18:33:06 +0100 | [diff] [blame] | 17 | 'util' |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 18 | ], function (buttonGroupClass,matchPanelClass) { |
Nils Diewald | 4f6521a | 2015-03-20 21:30:13 +0000 | [diff] [blame] | 19 | |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 20 | // Localization values |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 21 | const loc = KorAP.Locale; |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 22 | loc.MINIMIZE = loc.MINIMIZE || 'Minimize'; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 23 | |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame] | 24 | // 'corpusID', 'docID', 'textID' |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 25 | const _matchTerms = ['textSigle', 'matchID', 'available']; |
| 26 | |
| 27 | const d = document; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Match object |
| 31 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | return { |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 35 | * Create a new match object. |
| 36 | * Expects an element with match descriptions. |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 37 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 38 | create : function (match) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | return Object.create(this)._init(match); |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 40 | }, |
| 41 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 42 | |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 43 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 44 | * Initialize match. |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 45 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 46 | _init : function (match) { |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 47 | const t= this; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 48 | t._el = null; |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 49 | t._initialized = false; |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 50 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 51 | // No match defined |
| 52 | if (arguments.length < 1 || |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 53 | match === null || |
| 54 | match === undefined) { |
| 55 | throw new Error('Missing parameters'); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 56 | } |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 57 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 58 | // Match defined as a node |
| 59 | else if (match instanceof Node) { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 60 | t._el = match; |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 61 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 62 | // Circular reference !! |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 63 | match["_match"] = t; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 64 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 65 | if (match.hasAttribute('data-text-sigle')) { |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 66 | t.textSigle = match.getAttribute('data-text-sigle') |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 67 | } |
| 68 | else { |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 69 | t.textSigle = match.getAttribute('data-corpus-id') + |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 70 | '/' + |
| 71 | match.getAttribute('data-doc-id') + |
| 72 | '/' + |
| 73 | match.getAttribute('data-text-id'); |
| 74 | }; |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame] | 75 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 76 | t.matchID = match.getAttribute('data-match-id'); |
| 77 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 78 | // List of available annotations |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 79 | t.available = match.getAttribute('data-available-info').split(' '); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Match as an object |
| 83 | else { |
| 84 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 85 | // Iterate over allowed match terms |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 86 | _matchTerms.forEach(function(term) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 87 | this[term] = match[term] !== undefined ? match[term] : undefined; |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 88 | }, t); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 89 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 90 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 91 | t._avail = { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 92 | tokens : [], |
| 93 | spans : [], |
| 94 | rels : [] |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 95 | }; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 96 | |
| 97 | // Iterate over info layers |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 98 | let layer; |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 99 | t.available.forEach(function(term){ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 100 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 101 | // Create info layer objects |
| 102 | try { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 103 | layer = require('match/infolayer').create(term); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 104 | this._avail[layer.type].push(layer); |
| 105 | } |
| 106 | catch (e) { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 107 | return; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 108 | }; |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 109 | }, t); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 110 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 111 | return t; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 112 | }, |
| 113 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Return a list of parseable tree annotations. |
| 116 | */ |
| 117 | getSpans : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 118 | return this._avail.spans; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 119 | }, |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Return a list of parseable token annotations. |
| 124 | */ |
| 125 | getTokens : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 126 | return this._avail.tokens; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 127 | }, |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Return a list of parseable relation annotations. |
| 132 | */ |
| 133 | getRels : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 134 | return this._avail.rels; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 135 | }, |
| 136 | |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 137 | /** |
| 138 | * Initialize match |
| 139 | */ |
| 140 | init : function () { |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 141 | const t = this; |
| 142 | if (t._initialized) |
| 143 | return t; |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 144 | |
| 145 | // Add actions unless it's already activated |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 146 | const element = t._el; |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 147 | |
| 148 | // There is an element to open |
| 149 | if (element === undefined || element === null) |
| 150 | return undefined; |
| 151 | |
| 152 | // Add meta button |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 153 | const refLine = element.querySelector("p.ref"); |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 154 | |
| 155 | // No reference found |
| 156 | if (!refLine) |
| 157 | return undefined; |
| 158 | |
| 159 | // Create panel |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 160 | t.panel = matchPanelClass.create(t); |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 161 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 162 | t._el.insertBefore( |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 163 | t.panel.element(), |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 164 | t._el.querySelector("p.ref") |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 165 | ); |
| 166 | |
| 167 | // Insert before reference line |
| 168 | refLine.insertBefore( |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 169 | t.panel.actions().element(), |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 170 | refLine.firstChild |
| 171 | ); |
| 172 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 173 | t._initialized = true; |
| 174 | return t; |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 175 | }, |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 176 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Open match |
| 179 | */ |
| 180 | open : function () { |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 181 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 182 | // Add actions unless it's already activated |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 183 | const element = this._el; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 184 | |
| 185 | // There is an element to open |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 186 | if (element === undefined || element === null) |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 187 | return false; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 188 | |
| 189 | // The element is already opened |
| 190 | if (element.classList.contains('active')) |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 191 | return false; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 192 | |
| 193 | // Add active class to element |
| 194 | element.classList.add('active'); |
| 195 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 196 | const btn = buttonGroupClass.create( |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 197 | ['action','button-view'] |
| 198 | ); |
| 199 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 200 | const that = this; |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 201 | btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () { |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 202 | that.minimize(); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 203 | }); |
| 204 | element.appendChild(btn.element()); |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 205 | |
| 206 | if (this.init() == undefined) { |
| 207 | return false; |
| 208 | }; |
Akron | 13448c2 | 2018-07-10 13:05:46 +0200 | [diff] [blame] | 209 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 210 | return true; |
| 211 | }, |
| 212 | |
Akron | 8c468a1 | 2016-11-13 23:57:41 +0100 | [diff] [blame] | 213 | |
Akron | e57e880 | 2020-10-17 08:23:45 +0200 | [diff] [blame] | 214 | /** |
| 215 | * Toggle match view |
| 216 | */ |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 217 | toggle : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 218 | if (this._el.classList.contains('active')) |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 219 | this.minimize(); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 220 | else |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 221 | this.open(); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 222 | }, |
| 223 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 224 | |
Nils Diewald | 8bc7e41 | 2015-03-19 22:08:27 +0000 | [diff] [blame] | 225 | /** |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 226 | * Minimize info view |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 227 | */ |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 228 | minimize : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 229 | this._el.classList.remove('active'); |
Akron | 8b592d4 | 2018-01-26 18:33:06 +0100 | [diff] [blame] | 230 | }, |
| 231 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 232 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Get match element. |
| 235 | */ |
| 236 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 237 | return this._el; // May be null |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 238 | } |
| 239 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 240 | }); |