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