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