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([ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 13 | 'match/info', |
Akron | 30ee514 | 2015-06-26 01:50:14 +0200 | [diff] [blame] | 14 | 'match/reference', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | 'util' |
Akron | 30ee514 | 2015-06-26 01:50:14 +0200 | [diff] [blame] | 16 | ], function (infoClass, 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 |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | var loc = KorAP.Locale; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 20 | loc.ADDTREE = loc.ADDTREE || 'Add tree view'; |
| 21 | loc.SHOWINFO = loc.SHOWINFO || 'Show information'; |
| 22 | loc.CLOSE = loc.CLOSE || 'Close'; |
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' |
| 25 | var _matchTerms = ['textSigle', 'matchID', 'available']; |
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 | /** |
| 33 | * Create a new annotation object. |
| 34 | * Expects an array of available foundry/layer=type terms. |
| 35 | * Supported types are 'spans', 'tokens' and 'rels'. |
| 36 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 37 | create : function (match) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | return Object.create(this)._init(match); |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 39 | }, |
| 40 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 41 | |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 42 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 43 | * Initialize match. |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 44 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 45 | _init : function (match) { |
| 46 | this._element = null; |
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 || |
| 50 | match === null || |
| 51 | match === undefined) { |
| 52 | throw new Error('Missing parameters'); |
| 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) { |
| 57 | this._element = match; |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 58 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 59 | // Circular reference !! |
| 60 | match["_match"] = this; |
| 61 | |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame] | 62 | /* |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 63 | this.corpusID = match.getAttribute('data-corpus-id'), |
| 64 | this.docID = match.getAttribute('data-doc-id'), |
| 65 | this.textID = match.getAttribute('data-text-id'), |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame] | 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 | }; |
| 77 | |
| 78 | this.matchID = match.getAttribute('data-match-id'); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 79 | |
| 80 | // List of available annotations |
| 81 | this.available = match.getAttribute('data-available-info').split(' '); |
| 82 | } |
| 83 | |
| 84 | // Match as an object |
| 85 | else { |
| 86 | |
| 87 | // Iterate over allowed match terms |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 88 | for (var i in _matchTerms) { |
| 89 | var term = _matchTerms[i]; |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 90 | this[term] = match[term] !== undefined ? match[term] : undefined; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 91 | }; |
| 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 = { |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 95 | tokens : [], |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 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++) { |
| 102 | var term = this.available[i]; |
| 103 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 104 | // Create info layer objects |
| 105 | try { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 106 | var layer = require('match/infolayer').create(term); |
| 107 | this._avail[layer.type].push(layer); |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 108 | } |
| 109 | catch (e) { |
| 110 | continue; |
| 111 | }; |
| 112 | }; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 113 | |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 114 | return this; |
| 115 | }, |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Return a list of parseable tree annotations. |
| 120 | */ |
| 121 | getSpans : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 122 | return this._avail.spans; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 123 | }, |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Return a list of parseable token annotations. |
| 128 | */ |
| 129 | getTokens : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 130 | return this._avail.tokens; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 131 | }, |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Return a list of parseable relation annotations. |
| 136 | */ |
| 137 | getRels : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 138 | return this._avail.rels; |
Nils Diewald | e8518f8 | 2015-03-18 22:41:49 +0000 | [diff] [blame] | 139 | }, |
| 140 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 141 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Open match |
| 144 | */ |
| 145 | open : function () { |
| 146 | |
| 147 | // Add actions unless it's already activated |
| 148 | var element = this._element; |
| 149 | |
| 150 | // There is an element to open |
| 151 | if (this._element === undefined || this._element === null) |
| 152 | return false; |
| 153 | |
| 154 | // The element is already opened |
| 155 | if (element.classList.contains('active')) |
| 156 | return false; |
| 157 | |
| 158 | // Add active class to element |
| 159 | element.classList.add('active'); |
| 160 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 161 | // Already there |
| 162 | if (element.classList.contains('action')) |
| 163 | return true; |
| 164 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 165 | // Create action buttons |
| 166 | var ul = document.createElement('ul'); |
| 167 | ul.classList.add('action', 'right'); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 168 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 169 | element.appendChild(ul); |
| 170 | element.classList.add('action'); |
| 171 | |
| 172 | // Todo: Open in new frame |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 173 | |
| 174 | // Add close button |
| 175 | var close = document.createElement('li'); |
| 176 | close.appendChild(document.createElement('span')) |
| 177 | .appendChild(document.createTextNode(loc.CLOSE)); |
| 178 | close.classList.add('close'); |
| 179 | close.setAttribute('title', loc.CLOSE); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 180 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 181 | // Add info button |
| 182 | var info = document.createElement('li'); |
| 183 | info.appendChild(document.createElement('span')) |
| 184 | .appendChild(document.createTextNode(loc.SHOWINFO)); |
| 185 | info.classList.add('info'); |
| 186 | info.setAttribute('title', loc.SHOWINFO); |
| 187 | |
| 188 | var that = this; |
| 189 | |
| 190 | // Close match |
| 191 | close.addEventListener('click', function (e) { |
| 192 | e.halt(); |
| 193 | that.close() |
| 194 | }); |
| 195 | |
| 196 | // Add information, unless it already exists |
| 197 | info.addEventListener('click', function (e) { |
| 198 | e.halt(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 199 | that.info().toggle(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 200 | }); |
| 201 | |
| 202 | ul.appendChild(close); |
| 203 | ul.appendChild(info); |
| 204 | |
| 205 | return true; |
| 206 | }, |
| 207 | |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 208 | // Todo: Test toggle |
| 209 | toggle : function () { |
| 210 | if (this._element.classList.contains('active')) |
| 211 | this.close(); |
| 212 | else |
| 213 | this.open(); |
| 214 | }, |
| 215 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 216 | |
Nils Diewald | 8bc7e41 | 2015-03-19 22:08:27 +0000 | [diff] [blame] | 217 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 218 | * Close info view |
| 219 | */ |
| 220 | close : function () { |
| 221 | this._element.classList.remove('active'); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 222 | /* if (this._info !== undefined) { |
| 223 | * this._info.destroy(); |
| 224 | * }; |
| 225 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 226 | }, |
| 227 | |
| 228 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 229 | /** |
| 230 | * Get and open associated match info. |
| 231 | */ |
| 232 | info : function () { |
| 233 | |
| 234 | // Create match info |
| 235 | if (this._info === undefined) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 236 | this._info = infoClass.create(this); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 237 | |
| 238 | // There is an element to append |
| 239 | if (this._element === undefined || |
| 240 | this._element === null) |
| 241 | return this._info; |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 242 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 243 | // Info is already activated |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 244 | if (this._info._element !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 245 | return this._info; |
| 246 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 247 | return this._info; |
| 248 | }, |
| 249 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 250 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 251 | /** |
| 252 | * Get match element. |
| 253 | */ |
| 254 | element : function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 255 | return this._element; // May be null |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 258 | }); |