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