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