blob: 07d4550dd0dae78c02eb7c3c3949c6d72d618cf6 [file] [log] [blame]
Akron99713ef2017-06-28 18:19:28 +02001/**
2 * Information about a match.
3 */
Nils Diewald7c8ced22015-04-15 19:21:00 +00004define([
5 'match/infolayer',
6 'match/table',
7 'match/tree',
Akron151bc872018-02-02 14:04:15 +01008 'match/meta',
Akron0988d882017-11-10 16:13:12 +01009 'match/relations',
Akron99713ef2017-06-28 18:19:28 +020010 'match/querycreator',
Nils Diewald7c8ced22015-04-15 19:21:00 +000011 'util'
12], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010013 matchTableClass,
14 matchTreeClass,
Akron151bc872018-02-02 14:04:15 +010015 matchMetaClass,
Akron0988d882017-11-10 16:13:12 +010016 matchRelClass,
Akron0988d882017-11-10 16:13:12 +010017 matchQueryCreator) {
Akron3bb91bc2016-12-02 16:43:17 +010018
Nils Diewald7148c6f2015-05-04 15:07:53 +000019 // Override
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () {
21 KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented')
22 return {};
23 };
24
25 var loc = KorAP.Locale;
26
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000028
29 /**
30 * Create new match object
31 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 create : function (match) {
33 return Object.create(this)._init(match);
34 },
35
Akron151bc872018-02-02 14:04:15 +010036
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 /**
38 * Initialize object
39 */
40 _init : function (match) {
41 this._match = match;
42 this.opened = false;
43 return this;
44 },
45
Akron151bc872018-02-02 14:04:15 +010046
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 /**
48 * Get match object
49 */
50 match : function () {
51 return this._match;
52 },
53
Nils Diewald7148c6f2015-05-04 15:07:53 +000054
55 /**
56 * Open the information view,
57 * if closed, otherwise close.
58 */
Akronbd342982018-01-25 18:01:46 +010059 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010061
Akron08b82d62016-12-05 15:06:05 +010062 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010063
64 if (this.opened == true) {
Akrond67d45b2017-05-18 21:47:38 +020065 elem.removeChild(
66 this.element()
67 );
68 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 }
70 else {
Akrond67d45b2017-05-18 21:47:38 +020071 // Append element to match
Akron3bb91bc2016-12-02 16:43:17 +010072 elem.appendChild(
Akrond67d45b2017-05-18 21:47:38 +020073 this.element()
74 );
75 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 };
77
78 return this.opened;
79 },
Akronbd342982018-01-25 18:01:46 +010080 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000081
82
83 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 * Retrieve and parse snippet for table
85 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 */
Akron151bc872018-02-02 14:04:15 +010087 getTableData : function (tokens, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 var focus = [];
89
90 // Get all tokens
91 if (tokens === undefined) {
Akrond67d45b2017-05-18 21:47:38 +020092 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 }
94
95 // Get only some tokens
96 else {
Akrond67d45b2017-05-18 21:47:38 +020097
98 // Push newly to focus array
99 for (var i = 0; i < tokens.length; i++) {
100 var term = tokens[i];
101 try {
102 // Create info layer objects
103 var layer = infoLayerClass.create(term);
104 layer.type = "tokens";
105 focus.push(layer);
106 }
107 catch (e) {
108 continue;
109 };
110 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000111 };
112
113 // No tokens chosen
114 if (focus.length == 0)
Akrond67d45b2017-05-18 21:47:38 +0200115 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000116
117 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200119 this._match,
120 { 'spans' : false, 'layer' : focus },
121
122 // Callback for retrieval
123 function (matchResponse) {
Akron3bb91bc2016-12-02 16:43:17 +0100124
Akron515851a2017-05-02 12:53:17 +0200125 if (matchResponse === undefined)
126 cb(null);
127
Akrond67d45b2017-05-18 21:47:38 +0200128 // Get snippet from match info
129 if (matchResponse["snippet"] !== undefined) {
130 this._table = matchTableClass.create(matchResponse["snippet"]);
131 cb(this._table);
132 };
133 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000134 );
135
136 /*
137 // Todo: Store the table as a hash of the focus
138 return null;
139 */
140 },
Akronbd342982018-01-25 18:01:46 +0100141
142
Akron151bc872018-02-02 14:04:15 +0100143 getMetaData : function (metaInfo, cb) {
144 // ...
Akronbd342982018-01-25 18:01:46 +0100145 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146
147
148 /**
149 * Retrieve and parse snippet for tree representation
150 */
Akron151bc872018-02-02 14:04:15 +0100151 getTreeData : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000152 var focus = [];
153
154 // TODO: Support and cache multiple trees
155 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200156 this._match, {
157 'spans' : true,
158 'foundry' : foundry,
159 'layer' : layer
160 },
161 function (matchResponse) {
162 // Get snippet from match info
163 if (matchResponse["snippet"] !== undefined) {
164 // Todo: This should be cached somehow
Akronc56cf2d2016-11-09 22:02:38 +0100165
Akron0988d882017-11-10 16:13:12 +0100166 if (type === "spans") {
167 cb(matchTreeClass.create(matchResponse["snippet"]));
168 }
169 else if (type === "rels") {
170 cb(matchRelClass.create(matchResponse["snippet"]));
171 }
172
173 // Unknown tree type
174 else {
175 cb(null);
176 };
Akrond67d45b2017-05-18 21:47:38 +0200177 }
178 else {
179 cb(null);
180 };
181 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000182 );
183 },
184
Akron151bc872018-02-02 14:04:15 +0100185
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186 /**
187 * Destroy this match information view.
188 */
189 destroy : function () {
190
191 // Remove circular reference
Akron8b592d42018-01-26 18:33:06 +0100192 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +0000193 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200194 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000195
196 this._treeMenu.destroy();
197 this._treeMenu = undefined;
Akron8b592d42018-01-26 18:33:06 +0100198 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000199 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200200 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000201 // Element destroy
202 },
203
Akron151bc872018-02-02 14:04:15 +0100204
Nils Diewald0e6992a2015-04-14 20:13:52 +0000205 /**
206 * Add a new tree view to the list
207 */
Akron151bc872018-02-02 14:04:15 +0100208 showTree : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000209 var matchtree = document.createElement('div');
210 matchtree.classList.add('matchtree');
Akrond67d45b2017-05-18 21:47:38 +0200211
Akron151bc872018-02-02 14:04:15 +0100212 // Add title line
213 var h6 = matchtree.addE('h6');
214 h6.addE('span').addT(foundry);
215 h6.addE('span').addT(layer);
216
217 var tree = matchtree.addE('div');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000218
219 this._element.insertBefore(matchtree, this._element.lastChild);
220
Akron151bc872018-02-02 14:04:15 +0100221 // Add close action button
222 var actions = tree.addE('ul');
Akronc56cf2d2016-11-09 22:02:38 +0100223 actions.classList.add('action', 'image');
Akron151bc872018-02-02 14:04:15 +0100224 var close = actions.addE('li');
Akronc56cf2d2016-11-09 22:02:38 +0100225 close.className = 'close';
Akron151bc872018-02-02 14:04:15 +0100226 close.addE('span');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000227 close.addEventListener(
Akrond67d45b2017-05-18 21:47:38 +0200228 'click', function (e) {
229 matchtree.parentNode.removeChild(matchtree);
230 e.halt();
231 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000232 );
233
Nils Diewald0ec142f2015-05-05 00:29:23 +0000234 tree.classList.add('loading');
235
Nils Diewald0e6992a2015-04-14 20:13:52 +0000236 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100237 this.getTreeData(foundry, layer, type, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000238
Akrond67d45b2017-05-18 21:47:38 +0200239 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000240
Akrond67d45b2017-05-18 21:47:38 +0200241 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000242
Akrond67d45b2017-05-18 21:47:38 +0200243 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100244 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200245 }
246 else {
247 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100248 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100249
Akrond67d45b2017-05-18 21:47:38 +0200250 // Reposition the view to the center
251 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100252 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100253
Akron0988d882017-11-10 16:13:12 +0100254 // This is currently not supported by relations
255 if (type === "spans") {
256 var dl = document.createElement('li');
257 dl.className = 'download';
258 dl.addEventListener(
259 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100260 var a = treeObj.downloadLink();
Akron0988d882017-11-10 16:13:12 +0100261 document.body.appendChild(a);
262 a.click();
263 document.body.removeChild(a)
264 e.halt();
265 }
266 );
267
268 actions.appendChild(dl);
269 };
270
Akronc56cf2d2016-11-09 22:02:38 +0100271 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200272 };
273
274 if (cb !== undefined)
275 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000276 });
277 },
Akronbd342982018-01-25 18:01:46 +0100278
279
Akrone7679692018-01-26 12:06:33 +0100280 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100281 showMeta : function () {
Akronbd342982018-01-25 18:01:46 +0100282 var matchmeta = document.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100283
284 // TODO: This is part of the getMeta!
285 var metaInfo = this._match.element().getAttribute('data-info');
286
287 if (metaInfo)
288 metaInfo = JSON.parse(metaInfo);
289
290 // There is metainfo
291 if (metaInfo) {
292
293 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100294 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akrone7679692018-01-26 12:06:33 +0100295 var elem = this.element();
Akronbd342982018-01-25 18:01:46 +0100296
Akrone7679692018-01-26 12:06:33 +0100297 elem.insertBefore(
298 metaElem,
299 elem.firstChild
300 );
Akronbd342982018-01-25 18:01:46 +0100301 };
302 },
303
Akron151bc872018-02-02 14:04:15 +0100304
Akronbd342982018-01-25 18:01:46 +0100305 // Add table
Akron151bc872018-02-02 14:04:15 +0100306 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100307
308 var info = this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309
310 // Append default table
311 var matchtable = document.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000312 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000313 info.appendChild(matchtable);
314
315 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100316 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100317
Akrond67d45b2017-05-18 21:47:38 +0200318 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100319 matchtable.appendChild(table.element());
320 };
Akron151bc872018-02-02 14:04:15 +0100321
322 // Load data
323 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200324
325 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200326 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000327 });
Akronbd342982018-01-25 18:01:46 +0100328 },
329
Akron151bc872018-02-02 14:04:15 +0100330
Akronbd342982018-01-25 18:01:46 +0100331 /**
332 * Create match information view.
333 */
334 element : function () {
335
336 if (this._element !== undefined)
337 return this._element;
338
339 // Create info table
340 var info = document.createElement('div');
341 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000342
343 this._element = info;
344
Akronbd342982018-01-25 18:01:46 +0100345 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000346 }
347 };
348});