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