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