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 |
| 88 | for (var i in _matchTerms) { |
| 89 | var term = _matchTerms[i]; |
| 90 | this[term] = match[term] !== undefined ? match[term] : undefined; |
| 91 | }; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 92 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 94 | this._avail = { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 95 | tokens : [], |
| 96 | spans : [], |
| 97 | rels : [] |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 98 | }; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 99 | |
| 100 | // Iterate over info layers |
| 101 | for (var i = 0; i < this.available.length; i++) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 102 | var term = this.available[i]; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 103 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 104 | // Create info layer objects |
| 105 | try { |
| 106 | var layer = require('match/infolayer').create(term); |
| 107 | this._avail[layer.type].push(layer); |
| 108 | } |
| 109 | catch (e) { |
| 110 | continue; |
| 111 | }; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 112 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 113 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 114 | return this; |
| 115 | }, |
| 116 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Return a list of parseable tree annotations. |
| 119 | */ |
| 120 | getSpans : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 121 | return this._avail.spans; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 122 | }, |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Return a list of parseable token annotations. |
| 127 | */ |
| 128 | getTokens : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 129 | return this._avail.tokens; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 130 | }, |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Return a list of parseable relation annotations. |
| 135 | */ |
| 136 | getRels : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 137 | return this._avail.rels; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 138 | }, |
| 139 | |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 140 | /** |
| 141 | * Initialize match |
| 142 | */ |
| 143 | init : function () { |
| 144 | if (this._initialized) |
| 145 | return this; |
| 146 | |
| 147 | // Add actions unless it's already activated |
| 148 | var element = this._element; |
| 149 | |
| 150 | // There is an element to open |
| 151 | if (element === undefined || element === null) |
| 152 | return undefined; |
| 153 | |
| 154 | // Add meta button |
| 155 | var refLine = element.querySelector("p.ref"); |
| 156 | |
| 157 | // No reference found |
| 158 | if (!refLine) |
| 159 | return undefined; |
| 160 | |
| 161 | // Create panel |
| 162 | this.panel = matchPanelClass.create(this); |
| 163 | |
| 164 | this._element.insertBefore( |
| 165 | this.panel.element(), |
| 166 | this._element.querySelector("p.ref") |
| 167 | ); |
| 168 | |
| 169 | // Insert before reference line |
| 170 | refLine.insertBefore( |
| 171 | this.panel.actions.element(), |
| 172 | refLine.firstChild |
| 173 | ); |
| 174 | |
| 175 | this._initialized = true; |
| 176 | return this; |
| 177 | }, |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 178 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 179 | /** |
| 180 | * Open match |
| 181 | */ |
| 182 | open : function () { |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 183 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 184 | // Add actions unless it's already activated |
| 185 | var element = this._element; |
| 186 | |
| 187 | // There is an element to open |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 188 | if (element === undefined || element === null) |
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 | // The element is already opened |
| 192 | if (element.classList.contains('active')) |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 193 | return false; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 194 | |
| 195 | // Add active class to element |
| 196 | element.classList.add('active'); |
| 197 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 198 | var btn = buttonGroupClass.create( |
| 199 | ['action','button-view'] |
| 200 | ); |
| 201 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 202 | var that = this; |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame^] | 203 | btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () { |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 204 | that.minimize(); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 205 | }); |
| 206 | element.appendChild(btn.element()); |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 207 | |
| 208 | if (this.init() == undefined) { |
| 209 | return false; |
| 210 | }; |
Akron | 13448c2 | 2018-07-10 13:05:46 +0200 | [diff] [blame] | 211 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 212 | return true; |
| 213 | }, |
| 214 | |
Akron | 8c468a1 | 2016-11-13 23:57:41 +0100 | [diff] [blame] | 215 | |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 216 | // Todo: Test toggle |
| 217 | toggle : function () { |
| 218 | if (this._element.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 () { |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 229 | this._element.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 () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 237 | return this._element; // 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 | }); |