blob: 5c7a1c7f6335f28d30077fda0e65d3562915e4b9 [file] [log] [blame]
Akron954c6a52020-11-10 14:26:29 +01001"use strict";
2
Akronfac16472018-07-26 16:47:21 +02003define(['hint', 'hint/input', 'hint/contextanalyzer', 'hint/menu', 'hint/item'], function (hintClass, inputClass, contextClass, menuClass, menuItemClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +00004
Leo Repp46903bf2021-12-18 16:05:53 +01005 function emitKeyboardEvent (element, type, letter, keyCode) {
Akronfac16472018-07-26 16:47:21 +02006 // event type : keydown, keyup, keypress
7 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
8 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
Leo Repp46903bf2021-12-18 16:05:53 +01009 /**
10 * Nils Version. Does not work due to bug noted below
11 var keyboardEvent = document.createEvent("KeyboardEvent",);
Akronfac16472018-07-26 16:47:21 +020012 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
13 "initKeyboardEvent" : "initKeyEvent";
14 keyboardEvent[initMethod](
Leo Repp46903bf2021-12-18 16:05:53 +010015
16
17
Akronfac16472018-07-26 16:47:21 +020018 type,
19 true, // bubbles
20 true, // cancelable
21 window, // viewArg: should be window
22 false, // ctrlKeyArg
23 false, // altKeyArg
24 false, // shiftKeyArg
25 false, // metaKeyArg
26 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
Leo Repp46903bf2021-12-18 16:05:53 +010027 charCode || 0 // charCodeArgs : unsigned long the Unicode character
Akronfac16472018-07-26 16:47:21 +020028 // associated with the depressed key, else 0
Leo Repp46903bf2021-12-18 16:05:53 +010029
Akronfac16472018-07-26 16:47:21 +020030 );
31 element.dispatchEvent(keyboardEvent);
Leo Repp46903bf2021-12-18 16:05:53 +010032 */
33 //Leos Version
34 //https://stackoverflow.com/a/59113178
35
36 //might not work on Chromium
37 element.dispatchEvent(new KeyboardEvent(type, {
38 key: letter,
39 keyCode: keyCode,
40 code: "Key"+letter,
41 which: keyCode, //This is a hack
42 shiftKey: false,
43 ctrlKey: false,
44 metaKey: false,
45 bubbles: true,
46 view: window,
47 charCode: keyCode //This is a hack https://bugs.webkit.org/show_bug.cgi?id=16735
48 // charCodeArgs : unsigned long the Unicode character
49 // associated with the depressed key, else 0
50 }));
Akronfac16472018-07-26 16:47:21 +020051 };
Nils Diewald19ccee92014-12-08 11:30:08 +000052
Akronfac16472018-07-26 16:47:21 +020053 var afterAllFunc = function () {
54 try {
55 var mirrors = document.querySelectorAll(".hint.mirror");
56 for (var i in mirrors) {
57 mirrors[i].parentNode.removeChild(mirrors[i])
58 };
59 }
60 catch (e) {};
61
62 var body = document.body;
63 for (var i in body.children) {
64 if (body.children[i].nodeType && body.children[i].nodeType === 1) {
65 if (!body.children[i].classList.contains("jasmine_html-reporter")) {
66 body.removeChild(body.children[i]);
67 };
68 };
69 };
70 KorAP.API.getMatchInfo = undefined;
71 KorAP.context = undefined;
72 // KorAP.annotationHelper = undefined;
73 };
74
75 var beforeAllFunc = function () {
76 KorAP.annotationHelper = KorAP.annotationHelper || {};
77 KorAP.annotationHelper["-"] = [
78 ["Base Annotation", "base/s=", "Structure"],
79 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"]
80 ];
81 KorAP.annotationHelper["corenlp/"] = [
82 ["Named Entity", "ne=" , "Combined"],
83 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
84 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
85 ];
86 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000087
Nils Diewald7c8ced22015-04-15 19:21:00 +000088 describe('KorAP.InputField', function () {
Akronfac16472018-07-26 16:47:21 +020089 beforeAll(beforeAllFunc);
90 afterAll(afterAllFunc);
Akron5336fd42020-10-09 18:13:51 +020091 let input;
Nils Diewald1c546922015-04-13 01:56:19 +000092
Nils Diewald7c8ced22015-04-15 19:21:00 +000093 beforeEach(function () {
94 input = document.createElement("input");
95 input.setAttribute('type', "text");
96 input.setAttribute("value", "abcdefghijklmno");
97 input.style.position = 'absolute';
98 document.getElementsByTagName('body')[0].appendChild(input);
99 input.style.top = "20px";
100 input.style.left = "30px";
101 input.focus();
102 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +0000103 });
104
Nils Diewald7c8ced22015-04-15 19:21:00 +0000105 afterEach(function () {
106 document.getElementsByTagName("body")[0].removeChild(
Akron95abaf42018-04-26 15:33:22 +0200107 input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000108 );
109 });
Nils Diewald5c5a7472015-04-02 22:13:38 +0000110
Nils Diewald7c8ced22015-04-15 19:21:00 +0000111 it('should be initializable', function () {
112 // Supports: context, searchField
113 var inputField = inputClass.create(input);
Akron24aa0052020-11-10 11:00:34 +0100114 expect(inputField._el).not.toBe(undefined);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000115 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000116
Nils Diewald7c8ced22015-04-15 19:21:00 +0000117 it('should have text', function () {
118 expect(input.value).toEqual('abcdefghijklmno');
119 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +0000120
Nils Diewald7c8ced22015-04-15 19:21:00 +0000121 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000122
Nils Diewald7c8ced22015-04-15 19:21:00 +0000123 expect(input.selectionStart).toEqual(5);
124 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000125 expect(inputField._split()[0]).toEqual("abcde");
126 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000127
Nils Diewald7c8ced22015-04-15 19:21:00 +0000128 inputField.insert("xyz");
129 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000130 expect(inputField._split()[0]).toEqual("abcdexyz");
131 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000132 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000133
Nils Diewald7c8ced22015-04-15 19:21:00 +0000134 it('should be correctly positioned', function () {
135 expect(input.value).toEqual('abcdefghijklmno');
136 var inputField = inputClass.create(input);
137 document.getElementsByTagName("body")[0].appendChild(input);
138 inputField.reposition();
139 expect(input.style.left).toEqual("30px");
140 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
141 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
142 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000143
Nils Diewald7c8ced22015-04-15 19:21:00 +0000144 it('should have a correct context', function () {
145 expect(input.value).toEqual('abcdefghijklmno');
146 var inputField = inputClass.create(input);
147 expect(inputField.value()).toEqual("abcdefghijklmno");
148 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000149 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000150 expect(inputField.context()).toEqual("abcde");
151 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000152
Leo Repp46903bf2021-12-18 16:05:53 +0100153
Nils Diewald7c8ced22015-04-15 19:21:00 +0000154 it('should be correctly triggerable', function () {
155 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
156 var hint = KorAP.Hint.create({ "inputField" : input });
Leo Repp46903bf2021-12-18 16:05:53 +0100157 emitKeyboardEvent(hint.inputField()._el, "keypress", "E", 69);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000158 });
Leo Repp46903bf2021-12-18 16:05:53 +0100159
Nils Diewald19ccee92014-12-08 11:30:08 +0000160 });
161
Nils Diewald1c546922015-04-13 01:56:19 +0000162
Nils Diewald7c8ced22015-04-15 19:21:00 +0000163 describe('KorAP.ContextAnalyzer', function () {
Akronfac16472018-07-26 16:47:21 +0200164 beforeAll(beforeAllFunc);
165 afterAll(afterAllFunc);
166
Nils Diewald7c8ced22015-04-15 19:21:00 +0000167 it('should be initializable', function () {
168 var analyzer = contextClass.create(")");
169 expect(analyzer).toBe(undefined);
170 analyzer = contextClass.create(".+?");
171 expect(analyzer).not.toBe(undefined);
172 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000173
Nils Diewald7c8ced22015-04-15 19:21:00 +0000174 it('should check correctly', function () {
Akronfac16472018-07-26 16:47:21 +0200175
176 // Intialize KorAP.context
177 hintClass.create();
178
Akron954c6a52020-11-10 14:26:29 +0100179 const analyzer = contextClass.create(KorAP.context);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000180 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
181 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
182 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
183 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
184 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
185 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akronfac16472018-07-26 16:47:21 +0200186 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100187 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
188 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
189 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
190 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100191 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
192 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
193 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200194 expect(analyzer.test("")).toEqual(undefined);
195 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000196 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000197 });
198
Nils Diewald19ccee92014-12-08 11:30:08 +0000199
Nils Diewald7c8ced22015-04-15 19:21:00 +0000200 describe('KorAP.Hint', function () {
Akron954c6a52020-11-10 14:26:29 +0100201
202 let input;
203
Akronfac16472018-07-26 16:47:21 +0200204 beforeAll(beforeAllFunc);
205 afterAll(afterAllFunc);
Nils Diewald19ccee92014-12-08 11:30:08 +0000206
Nils Diewald7c8ced22015-04-15 19:21:00 +0000207 beforeEach(function () {
208 input = document.createElement("input");
209 input.setAttribute("type", "text");
210 input.setAttribute("value", "abcdefghijklmno");
211 input.style.position = 'absolute';
212 input.style.top = "20px";
213 input.style.left = "30px";
214 input.focus();
215 input.selectionStart = 5;
216 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000217
Nils Diewald7c8ced22015-04-15 19:21:00 +0000218 it('should be initializable', function () {
Akronfac16472018-07-26 16:47:21 +0200219
Nils Diewald7c8ced22015-04-15 19:21:00 +0000220 // Supports: context, searchField
221 var hint = hintClass.create({
Akronfac16472018-07-26 16:47:21 +0200222 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000223 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000224
Nils Diewald7c8ced22015-04-15 19:21:00 +0000225 expect(hint).toBeTruthy();
226 });
Akron00cd4d12016-05-31 21:01:11 +0200227
Akron1a5a5872016-09-05 20:17:14 +0200228
Akron00cd4d12016-05-31 21:01:11 +0200229 it('should alert at char pos', function () {
230 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200231 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200232 });
233
234 expect(hint.active()).toBeFalsy();
235
236 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
237
238 expect(hint.active()).toBeTruthy();
239
240 var container = hint.inputField().container();
241 expect(container.firstChild.classList.contains('hint')).toBe(true);
242 expect(container.firstChild.classList.contains('alert')).toBe(true);
243 expect(container.firstChild.textContent).toEqual('That does not work!');
244 expect(hint.inputField().mirrorValue()).toEqual('abcd');
245
246 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
247
248 // Update - meaning: hide alert
249 hint.update();
250
251 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200252 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200253
254 // Show again
255 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
256 expect(hint.inputField().mirrorValue()).toEqual('abcde');
257 expect(hint.alert().active).toBeTruthy();
258 expect(hint.active()).toBeTruthy();
259
260 // Show menu, hide alert!
261 hint.show(false);
262 expect(hint.active()).toBeTruthy();
263 expect(hint.inputField().mirrorValue()).toEqual('abcde');
264 expect(hint.alert().active).toBeFalsy();
265
Akronfac16472018-07-26 16:47:21 +0200266 // Show again
Akron1a5a5872016-09-05 20:17:14 +0200267 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
268 expect(hint.inputField().mirrorValue()).toEqual('abcde');
269 expect(hint.alert().active).toBeTruthy();
270 expect(hint.active()).toBeTruthy();
271
272 // Show menu, hide alert!
273 hint.show(false);
Akronee9ef4a2016-06-03 12:50:08 +0200274 });
Akron00cd4d12016-05-31 21:01:11 +0200275
Akronda5bd3a2020-10-16 17:37:49 +0200276 it('should work both in Chrome and Firefox', function () {
277 var hint = hintClass.create({
278 inputField : input
279 });
280 hint.show(false);
281 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
282
283 // Show menu, hide alert!
284 hint.show(false);
285
286 expect(hint.active()).toBeFalsy();
287 });
288
289
Akronee9ef4a2016-06-03 12:50:08 +0200290 it('should view main menu on default', function () {
291 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200292 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200293 });
294
295 expect(hint.active()).toBeFalsy();
296
297 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200298
Akron8eaeb2e2016-08-29 18:26:28 +0200299 var cont = hint.inputField().container();
Akron8eaeb2e2016-08-29 18:26:28 +0200300 expect(cont.getElementsByTagName('div').length).toBe(1);
301 expect(cont.getElementsByTagName('ul').length).toBe(0);
302 expect(cont.firstChild).toEqual(cont.firstChild);
303
304 // Show menu, if a relevant context exists
305 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200306 hint.show(false);
307
Leo Repp57997402021-08-18 16:37:52 +0200308 expect(cont.getElementsByTagName('ul').length).toEqual(1+1); //+1 from containermenu (see container/container.js)
309 expect(cont.getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200310
Akron8eaeb2e2016-08-29 18:26:28 +0200311 // Hide the menu and focus on the input
312 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200313
Leo Repp57997402021-08-18 16:37:52 +0200314 expect(cont.getElementsByTagName('div').length).toEqual(1);
315 expect(cont.getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200316
Akron02360e42016-06-07 13:41:12 +0200317 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200318
Akronee9ef4a2016-06-03 12:50:08 +0200319 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200320
Leo Repp57997402021-08-18 16:37:52 +0200321 // show with context if possible
Akron02360e42016-06-07 13:41:12 +0200322 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200323
Leo Repp57997402021-08-18 16:37:52 +0200324 expect(cont.getElementsByTagName('div').length).toEqual(4);
325 expect(cont.getElementsByTagName('ul').length).toEqual(1+1);//+1 from containermenu (see container/container.js)
326 expect(cont.getElementsByTagName('li').length).toEqual(2);
Akron02360e42016-06-07 13:41:12 +0200327
Akronee9ef4a2016-06-03 12:50:08 +0200328 hint.unshow();
329 hint.inputField().insert(' aaaa/');
330
Leo Repp57997402021-08-18 16:37:52 +0200331 // show with context necessarily
Akronee9ef4a2016-06-03 12:50:08 +0200332 hint.show(true);
333
Leo Repp57997402021-08-18 16:37:52 +0200334 expect(cont.getElementsByTagName('div').length).toEqual(1);
335 expect(cont.getElementsByTagName('ul').length).toEqual(0); //here not +1: context doesnt fit
Akron1a5a5872016-09-05 20:17:14 +0200336 });
337
Akron95abaf42018-04-26 15:33:22 +0200338
339 it('should open menus depending on the context', function () {
340 var hint = hintClass.create({
341 inputField : input
342 });
343 hint.inputField().reset();
344
345 expect(hint.active()).toBeFalsy();
346
347 // show with context
348 hint.show(false);
349
350 expect(hint.active()).toBeTruthy();
Leo Repp57997402021-08-18 16:37:52 +0200351 var cont = hint.inputField().container();
352 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
Akron95abaf42018-04-26 15:33:22 +0200353
354 // Type in prefix
355 hint.active().prefix("cor").show();
356 expect(hint.active().prefix()).toEqual("cor");
357
358 // Click first step
Leo Repp57997402021-08-18 16:37:52 +0200359 expect(cont.getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
360 cont.getElementsByTagName('li')[0].click();
Akron95abaf42018-04-26 15:33:22 +0200361
362 expect(hint.active()).toBeTruthy();
363
364 // Click second step
Leo Repp57997402021-08-18 16:37:52 +0200365 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
366 cont.getElementsByTagName('li')[0].click()
Akron95abaf42018-04-26 15:33:22 +0200367
368 // Invisible menu
Leo Repp57997402021-08-18 16:37:52 +0200369 expect(cont.getElementsByTagName('li')[0]).toBeUndefined();
Akron95abaf42018-04-26 15:33:22 +0200370
371 // Inactive menu
372 expect(hint.active()).toBeFalsy();
373
374 // show with context
375 hint.show(false);
376
377 // No prefix
378 expect(hint.active().prefix()).toEqual("");
379 });
380
381
Akron1a5a5872016-09-05 20:17:14 +0200382 it('should not view main menu if context is mandatory', function () {
383 var hint = hintClass.create({
384 inputField : input
385 });
386
387 expect(hint.active()).toBeFalsy();
388
389 // Fine
390 hint.inputField().insert('der Baum corenlp/');
391 hint.show(true);
392 expect(hint.active()).toBeTruthy();
393
394 // Not analyzable
395 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
396 hint.show(true);
397 expect(hint.active()).toBeFalsy();
398
399 // Not available
400 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
401 hint.show(true);
402 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200403 });
Akron5746ecf2018-06-23 10:57:24 +0200404
405
406 it('should show the assistant bar on blur', function () {
407 var hint = hintClass.create({
408 inputField : input
409 });
410 // Fine
411 hint.inputField().insert('der Baum corenlp/');
412 hint.show(true);
413 expect(hint.active()).toBeTruthy();
414
415 // Blur
Akrone0789112018-08-31 14:32:04 +0200416 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200417 expect(hint.active()).toBeFalsy();
418 });
Akron954c6a52020-11-10 14:26:29 +0100419
420 it('should support prefix', function () {
421 const hint = hintClass.create({
422 inputField : input
423 });
424 hint.inputField().reset();
425
426 expect(hint.active()).toBeFalsy();
427
428 // show with context
429 hint.show(false);
430
431 expect(hint.active()).toBeTruthy();
432
433 const menu = hint.active();
434
435 expect(menu.element().nodeName).toEqual('UL');
436
437 menu.limit(8);
438
439 // view
440 menu.show();
441
442 expect(menu.prefix()).toBe('');
443 expect(hint.active()).toBeTruthy();
444
445 // Type in prefix
446 hint.active().prefix("cor").show();
Leo Repp57997402021-08-18 16:37:52 +0200447 expect(hint.active()._prefix.value()).toBe("cor");
Akron954c6a52020-11-10 14:26:29 +0100448 expect(hint.active().prefix()).toEqual("cor");
Akron954c6a52020-11-10 14:26:29 +0100449 expect(input.value).toEqual("");
Leo Repp57997402021-08-18 16:37:52 +0200450 expect(hint.active()._prefix["isSelectable"]).not.toBeNull();
451 expect(hint._menuCollection['-']._prefix["isSelectable"]).not.toBeNull();
452 expect(hint.active()._prefix).toBe(hint._menuCollection['-']._prefix);
453 expect(hint.active()._prefix.element()).toBe(hint._menuCollection['-']._prefix.element());
Akron954c6a52020-11-10 14:26:29 +0100454 hint.active()._prefix.element().click();
Leo Repp57997402021-08-18 16:37:52 +0200455
456
Akron954c6a52020-11-10 14:26:29 +0100457 expect(input.value).toEqual("cor");
458 expect(hint.active()).toBeFalsy();
459
460 // view
461 menu.show();
462 expect(menu.prefix()).toBe('');
463
464 });
465
Leo Repp46903bf2021-12-18 16:05:53 +0100466
467 it('should highlight the prefix if no item matches.', function () {
468 const hint = hintClass.create({
469 inputField : input
470 });
471 hint.inputField().reset();
472
473 expect(hint.active()).toBeFalsy();
474
475 // show with context
476 hint.show(false);
477
478 expect(hint.active()).toBeTruthy();
479
480 const menu = hint.active();
481 expect(menu.limit(3).show(3)).toBe(true);
482 menu.element().focus();
483
484
485
486 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
487 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
488 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
489 expect(menu.container()._cItemPrefix.active()).toBeTruthy();
490 expect(menu.prefix()).toEqual("EEE");
491 expect(menu.element().classList.contains("visible")).toBeTruthy();
492 expect(menu.container().element().classList.contains("visible")).toBeTruthy();
493
494 emitKeyboardEvent(menu.element(),'keydown'," ",13);
495 //Should call reset() and hide()
496 // hint containermenu should do something different.
497 expect(menu.prefix()).toEqual("");
498 expect(menu.element().classList.contains("visible")).toBeFalsy();
499 expect(menu.container().element().classList.contains("visible")).toBeFalsy();
500 });
Leo Repp57997402021-08-18 16:37:52 +0200501
Akron65c74352016-09-02 17:23:39 +0200502
Akron02360e42016-06-07 13:41:12 +0200503 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000504 });
505
Akron65c74352016-09-02 17:23:39 +0200506
Nils Diewald7c8ced22015-04-15 19:21:00 +0000507 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200508 beforeAll(beforeAllFunc);
509 afterAll(afterAllFunc);
510
Nils Diewald7c8ced22015-04-15 19:21:00 +0000511 it('should be initializable', function () {
512 expect(
Akronfac16472018-07-26 16:47:21 +0200513 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000514 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000515
Nils Diewald7c8ced22015-04-15 19:21:00 +0000516 expect(
Akronfac16472018-07-26 16:47:21 +0200517 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000518 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000519
Nils Diewald7c8ced22015-04-15 19:21:00 +0000520 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
521 expect(menuItem.name()).toEqual('CoreNLP');
522 expect(menuItem.action()).toEqual('corenlp/');
523 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000524
Nils Diewald7c8ced22015-04-15 19:21:00 +0000525 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200526 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000527 );
528 expect(menuItem.name()).toEqual('CoreNLP');
529 expect(menuItem.action()).toEqual('corenlp/');
530 expect(menuItem.desc()).not.toBeUndefined();
531 expect(menuItem.desc()).toEqual('It\'s funny');
532 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000533
Akronfac16472018-07-26 16:47:21 +0200534
Nils Diewald7c8ced22015-04-15 19:21:00 +0000535 it('should have an element', function () {
536 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
537 expect(menuItem.element()).not.toBe(undefined);
538 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000539
Nils Diewald7c8ced22015-04-15 19:21:00 +0000540 var title = menuItem.element().firstChild;
541 expect(title.nodeName).toEqual("SPAN");
542 expect(title.firstChild.nodeType).toEqual(3);
543 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
544 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
545 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000546
Nils Diewald7c8ced22015-04-15 19:21:00 +0000547 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200548 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000549 );
550 expect(menuItem.element()).not.toBe(undefined);
551 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000552
Nils Diewald7c8ced22015-04-15 19:21:00 +0000553 title = menuItem.element().firstChild;
554 expect(title.nodeName).toEqual("SPAN");
555 expect(title.firstChild.nodeType).toEqual(3); // TextNode
556 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000557
Nils Diewald7c8ced22015-04-15 19:21:00 +0000558 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
559 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000560
Nils Diewald7c8ced22015-04-15 19:21:00 +0000561 var desc = menuItem.element().lastChild;
562 expect(desc.nodeName).toEqual("SPAN");
563 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
564 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
565 });
Nils Diewald1c546922015-04-13 01:56:19 +0000566
Nils Diewald19ccee92014-12-08 11:30:08 +0000567
Nils Diewald7c8ced22015-04-15 19:21:00 +0000568 it('should be activatable and deactivateable by class', function () {
569 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
570 expect(menuItem.active()).toBe(false);
571 expect(menuItem.element().getAttribute("class")).toBe(null);
572 menuItem.active(true);
573 expect(menuItem.active()).toBe(true);
574 expect(menuItem.element().getAttribute("class")).toEqual("active");
575 menuItem.active(false); // Is active
576 expect(menuItem.active()).toBe(false);
577 expect(menuItem.element().getAttribute("class")).toEqual("");
578 menuItem.active(true);
579 expect(menuItem.active()).toBe(true);
580 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000581
Nils Diewald7c8ced22015-04-15 19:21:00 +0000582 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
583 expect(menuItem.active()).toBe(false);
584 expect(menuItem.element().getAttribute("class")).toBe(null);
585 menuItem.active(false); // Is not active
586 expect(menuItem.active()).toBe(false);
587 expect(menuItem.element().getAttribute("class")).toBe(null);
588 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000589
Akronfac16472018-07-26 16:47:21 +0200590
Nils Diewald7c8ced22015-04-15 19:21:00 +0000591 it('should be set to boundary', function () {
592 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
593 expect(menuItem.active()).toBe(false);
594 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000595
Nils Diewald7c8ced22015-04-15 19:21:00 +0000596 // Set active
597 menuItem.active(true);
598 expect(menuItem.active()).toBe(true);
599 expect(menuItem.noMore()).toBe(false);
600 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000601
Nils Diewald7c8ced22015-04-15 19:21:00 +0000602 // Set no more
603 menuItem.noMore(true);
604 expect(menuItem.active()).toBe(true);
605 expect(menuItem.noMore()).toBe(true);
606 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000607
Nils Diewald7c8ced22015-04-15 19:21:00 +0000608 // No no more
609 menuItem.noMore(false);
610 expect(menuItem.active()).toBe(true);
611 expect(menuItem.noMore()).toBe(false);
612 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000613
Nils Diewald7c8ced22015-04-15 19:21:00 +0000614 // Set no more, deactivate
615 menuItem.noMore(true);
616 menuItem.active(false);
617 expect(menuItem.active()).toBe(false);
618 expect(menuItem.noMore()).toBe(true);
619 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000620
Nils Diewald7c8ced22015-04-15 19:21:00 +0000621 // Set active
622 menuItem.active(true);
623 expect(menuItem.active()).toBe(true);
624 expect(menuItem.noMore()).toBe(true);
625 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
626 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000627
Akronfac16472018-07-26 16:47:21 +0200628
Nils Diewald7c8ced22015-04-15 19:21:00 +0000629 it('should be highlightable', function () {
630 // Highlight in the middle
631 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
632 menuItem.highlight("ren");
633 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000634
Nils Diewald7c8ced22015-04-15 19:21:00 +0000635 menuItem.lowlight();
636 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000637
Nils Diewald7c8ced22015-04-15 19:21:00 +0000638 // Starting highlight
639 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
640 menuItem.highlight("cor");
641 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000642
Nils Diewald7c8ced22015-04-15 19:21:00 +0000643 menuItem.lowlight();
644 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
645
646 // Starting highlight - short
647 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
648 menuItem.highlight("c");
649 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
650
651 menuItem.lowlight();
652 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
653
654 // Highlight at the end
655 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
656 menuItem.highlight("nlp");
657 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
658
659 menuItem.lowlight();
660 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
661
662 // Highlight at the end - short
663 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
664 menuItem.highlight("p");
665 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
666
667 menuItem.lowlight();
668 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
669
670 // No highlight
671 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
672 menuItem.highlight("xp");
673 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
674
675 menuItem.lowlight();
676 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
677
678 // Highlight in the middle - first
679 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
680
681 menuItem.highlight("ren");
682 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
683
684 menuItem.lowlight();
685 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
686
687 // Highlight in the middle - second
688 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
689 menuItem.highlight("ampl");
690 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
691
692 menuItem.lowlight();
693 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
694
695 // Highlight in the middle - both
696 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
697
698 menuItem.highlight("e");
699 expect(menuItem.element().innerHTML).toEqual('<span>Cor<mark>e</mark>NLP</span><span class="desc">This is my <mark>E</mark>xampl<mark>e</mark></span>');
700
701 menuItem.lowlight();
702 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
703
704 // Highlight in the end - second
705 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
706 menuItem.highlight("le");
707 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
708
709 menuItem.lowlight();
710 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
711
712 // Highlight at the beginning - second
713 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
714 menuItem.highlight("this");
715 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
716
717 menuItem.lowlight();
718 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
719 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000720 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000721
Akronfac16472018-07-26 16:47:21 +0200722
Nils Diewald7c8ced22015-04-15 19:21:00 +0000723 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200724 beforeAll(beforeAllFunc);
725 afterAll(afterAllFunc);
726
Nils Diewald7c8ced22015-04-15 19:21:00 +0000727 var list = [
728 ["Constituency", "c=", "Example 1"],
729 ["Lemma", "l="],
730 ["Morphology", "m=", "Example 2"],
731 ["Part-of-Speech", "p="],
732 ["Syntax", "syn="]
733 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000734
Nils Diewald7c8ced22015-04-15 19:21:00 +0000735 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000736 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000737 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200738 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000739
Nils Diewald7c8ced22015-04-15 19:21:00 +0000740 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000741
Nils Diewald7c8ced22015-04-15 19:21:00 +0000742 // view
743 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200744 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000745
Nils Diewald7c8ced22015-04-15 19:21:00 +0000746 // First element in list
747 expect(menu.item(0).active()).toBe(true);
748 expect(menu.item(0).noMore()).toBe(true);
749
750 // Middle element in list
751 expect(menu.item(2).active()).toBe(false);
752 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000753
Nils Diewald7c8ced22015-04-15 19:21:00 +0000754 // Last element in list
755 expect(menu.item(menu.length() - 1).active()).toBe(false);
756 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200757
758 expect(menu.shownItem(0).active()).toBeTruthy();
759 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
760 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
761 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
762
763 menu.next();
764 expect(menu.shownItem(1).active()).toBeTruthy();
765
766 menu.next();
767 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000768 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000769 });
770});