blob: f6fbd41f4d569a835b31ae647d9ffc62a7c3061f [file] [log] [blame]
Nils Diewalde8518f82015-03-18 22:41:49 +00001/**
Nils Diewalda297f062015-04-02 00:23:46 +00002 * Get information on matches,
3 * generate annotation tables and trees.
Nils Diewalde8518f82015-03-18 22:41:49 +00004 *
5 * @author Nils Diewald
6 */
7/*
Nils Diewald6e43ffd2015-03-25 18:55:39 +00008 * - Highlight (at least mark as bold) the match
9 * - Scroll to match vertically per default
Nils Diewalde8518f82015-03-18 22:41:49 +000010 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000011define([
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 'match/info',
Akron30ee5142015-06-26 01:50:14 +020013 'match/reference',
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 'util'
Akron30ee5142015-06-26 01:50:14 +020015], function (infoClass, refClass) {
Nils Diewald4f6521a2015-03-20 21:30:13 +000016
Nils Diewald6e43ffd2015-03-25 18:55:39 +000017 // Localization values
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 var loc = KorAP.Locale;
Nils Diewalda297f062015-04-02 00:23:46 +000019 loc.ADDTREE = loc.ADDTREE || 'Add tree view';
20 loc.SHOWINFO = loc.SHOWINFO || 'Show information';
21 loc.CLOSE = loc.CLOSE || 'Close';
Nils Diewald0e6992a2015-04-14 20:13:52 +000022
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 var _matchTerms = ['corpusID', 'docID', 'textID', 'matchID', 'available'];
Nils Diewalda297f062015-04-02 00:23:46 +000024
25 /**
26 * Match object
27 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000029
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 Diewalda297f062015-04-02 00:23:46 +000035 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000037 },
38
Nils Diewald7c8ced22015-04-15 19:21:00 +000039
Nils Diewald6e43ffd2015-03-25 18:55:39 +000040 /**
Nils Diewalda297f062015-04-02 00:23:46 +000041 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000042 */
Nils Diewalda297f062015-04-02 00:23:46 +000043 _init : function (match) {
44 this._element = null;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000045
Nils Diewalda297f062015-04-02 00:23:46 +000046 // No match defined
47 if (arguments.length < 1 ||
48 match === null ||
49 match === undefined) {
50 throw new Error('Missing parameters');
51 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000052
Nils Diewalda297f062015-04-02 00:23:46 +000053 // Match defined as a node
54 else if (match instanceof Node) {
55 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000056
Nils Diewalda297f062015-04-02 00:23:46 +000057 // 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 Diewald0e6992a2015-04-14 20:13:52 +000073 for (var i in _matchTerms) {
74 var term = _matchTerms[i];
Nils Diewald7c8ced22015-04-15 19:21:00 +000075 this[term] = match[term] !== undefined ? match[term] : undefined;
Nils Diewalda297f062015-04-02 00:23:46 +000076 };
77 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000078
Nils Diewald7c8ced22015-04-15 19:21:00 +000079 this._avail = {
Nils Diewalde8518f82015-03-18 22:41:49 +000080 tokens : [],
Nils Diewalda297f062015-04-02 00:23:46 +000081 spans : [],
82 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000083 };
Nils Diewalda297f062015-04-02 00:23:46 +000084
85 // Iterate over info layers
86 for (var i = 0; i < this.available.length; i++) {
87 var term = this.available[i];
88
Nils Diewalde8518f82015-03-18 22:41:49 +000089 // Create info layer objects
90 try {
Nils Diewald7c8ced22015-04-15 19:21:00 +000091 var layer = require('match/infolayer').create(term);
92 this._avail[layer.type].push(layer);
Nils Diewalde8518f82015-03-18 22:41:49 +000093 }
94 catch (e) {
95 continue;
96 };
97 };
Nils Diewalda297f062015-04-02 00:23:46 +000098
Nils Diewalde8518f82015-03-18 22:41:49 +000099 return this;
100 },
101
102
103 /**
104 * Return a list of parseable tree annotations.
105 */
106 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000107 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000108 },
109
110
111 /**
112 * Return a list of parseable token annotations.
113 */
114 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000115 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000116 },
117
118
119 /**
120 * Return a list of parseable relation annotations.
121 */
122 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000123 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000124 },
125
Nils Diewald7c8ced22015-04-15 19:21:00 +0000126
Nils Diewalda297f062015-04-02 00:23:46 +0000127 /**
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 Diewald7c8ced22015-04-15 19:21:00 +0000146 // Already there
147 if (element.classList.contains('action'))
148 return true;
149
Nils Diewalda297f062015-04-02 00:23:46 +0000150 // Create action buttons
151 var ul = document.createElement('ul');
152 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000153
Nils Diewald7c8ced22015-04-15 19:21:00 +0000154 element.appendChild(ul);
155 element.classList.add('action');
156
157 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000158
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 Diewald0e6992a2015-04-14 20:13:52 +0000165
Nils Diewalda297f062015-04-02 00:23:46 +0000166 // 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 Diewald5c5a7472015-04-02 22:13:38 +0000184 that.info().toggle();
Nils Diewalda297f062015-04-02 00:23:46 +0000185 });
186
187 ul.appendChild(close);
188 ul.appendChild(info);
189
190 return true;
191 },
192
Nils Diewald7c8ced22015-04-15 19:21:00 +0000193
Nils Diewald8bc7e412015-03-19 22:08:27 +0000194 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000195 * Close info view
196 */
197 close : function () {
198 this._element.classList.remove('active');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000199 /* if (this._info !== undefined) {
200 * this._info.destroy();
201 * };
202 */
Nils Diewalda297f062015-04-02 00:23:46 +0000203 },
204
205
Nils Diewalda297f062015-04-02 00:23:46 +0000206 /**
207 * Get and open associated match info.
208 */
209 info : function () {
210
211 // Create match info
212 if (this._info === undefined)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000213 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000214
215 // There is an element to append
216 if (this._element === undefined ||
217 this._element === null)
218 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000219
Nils Diewalda297f062015-04-02 00:23:46 +0000220 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000221 if (this._info._element !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000222 return this._info;
223
Nils Diewalda297f062015-04-02 00:23:46 +0000224 return this._info;
225 },
226
Nils Diewald7c8ced22015-04-15 19:21:00 +0000227
Nils Diewalda297f062015-04-02 00:23:46 +0000228 /**
229 * Get match element.
230 */
231 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000232 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000233 }
234 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000235});