blob: e98b92a5f136a3dd9bc7fe720135786243238bb1 [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);
Akronb759ee92024-11-19 18:02:56 +0100142 expect(inputField.selectionRange()[0]).toEqual(5);
143 expect(inputField.selectionRange()[1]).toEqual(5);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000144 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000145
Nils Diewald7c8ced22015-04-15 19:21:00 +0000146 it('should have a correct context', function () {
147 expect(input.value).toEqual('abcdefghijklmno');
148 var inputField = inputClass.create(input);
149 expect(inputField.value()).toEqual("abcdefghijklmno");
150 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000151 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000152 expect(inputField.context()).toEqual("abcde");
153 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000154
Leo Repp46903bf2021-12-18 16:05:53 +0100155
Nils Diewald7c8ced22015-04-15 19:21:00 +0000156 it('should be correctly triggerable', function () {
157 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
hebastae66b62b2022-01-19 18:03:56 +0100158 var hint = hintClass.create({ "inputField" : input });
Leo Repp46903bf2021-12-18 16:05:53 +0100159 emitKeyboardEvent(hint.inputField()._el, "keypress", "E", 69);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000160 });
Leo Repp46903bf2021-12-18 16:05:53 +0100161
Nils Diewald19ccee92014-12-08 11:30:08 +0000162 });
163
Nils Diewald1c546922015-04-13 01:56:19 +0000164
Nils Diewald7c8ced22015-04-15 19:21:00 +0000165 describe('KorAP.ContextAnalyzer', function () {
Akronfac16472018-07-26 16:47:21 +0200166 beforeAll(beforeAllFunc);
167 afterAll(afterAllFunc);
168
Nils Diewald7c8ced22015-04-15 19:21:00 +0000169 it('should be initializable', function () {
170 var analyzer = contextClass.create(")");
171 expect(analyzer).toBe(undefined);
172 analyzer = contextClass.create(".+?");
173 expect(analyzer).not.toBe(undefined);
174 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000175
Nils Diewald7c8ced22015-04-15 19:21:00 +0000176 it('should check correctly', function () {
Akronfac16472018-07-26 16:47:21 +0200177
178 // Intialize KorAP.context
179 hintClass.create();
180
Akron954c6a52020-11-10 14:26:29 +0100181 const analyzer = contextClass.create(KorAP.context);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000182 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
183 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
184 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
185 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
186 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
187 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akronfac16472018-07-26 16:47:21 +0200188 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100189 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
190 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
191 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
192 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100193 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
194 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
195 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200196 expect(analyzer.test("")).toEqual(undefined);
197 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000198 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000199 });
200
Nils Diewald19ccee92014-12-08 11:30:08 +0000201
Nils Diewald7c8ced22015-04-15 19:21:00 +0000202 describe('KorAP.Hint', function () {
Akron954c6a52020-11-10 14:26:29 +0100203
204 let input;
205
Akronfac16472018-07-26 16:47:21 +0200206 beforeAll(beforeAllFunc);
207 afterAll(afterAllFunc);
Nils Diewald19ccee92014-12-08 11:30:08 +0000208
Nils Diewald7c8ced22015-04-15 19:21:00 +0000209 beforeEach(function () {
210 input = document.createElement("input");
211 input.setAttribute("type", "text");
212 input.setAttribute("value", "abcdefghijklmno");
213 input.style.position = 'absolute';
214 input.style.top = "20px";
215 input.style.left = "30px";
216 input.focus();
217 input.selectionStart = 5;
218 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000219
Nils Diewald7c8ced22015-04-15 19:21:00 +0000220 it('should be initializable', function () {
Akronfac16472018-07-26 16:47:21 +0200221
Nils Diewald7c8ced22015-04-15 19:21:00 +0000222 // Supports: context, searchField
223 var hint = hintClass.create({
Akronfac16472018-07-26 16:47:21 +0200224 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000225 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000226
Nils Diewald7c8ced22015-04-15 19:21:00 +0000227 expect(hint).toBeTruthy();
228 });
Akron00cd4d12016-05-31 21:01:11 +0200229
Akron1a5a5872016-09-05 20:17:14 +0200230
Akron00cd4d12016-05-31 21:01:11 +0200231 it('should alert at char pos', function () {
232 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200233 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200234 });
235
236 expect(hint.active()).toBeFalsy();
237
238 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
239
240 expect(hint.active()).toBeTruthy();
241
242 var container = hint.inputField().container();
243 expect(container.firstChild.classList.contains('hint')).toBe(true);
244 expect(container.firstChild.classList.contains('alert')).toBe(true);
245 expect(container.firstChild.textContent).toEqual('That does not work!');
246 expect(hint.inputField().mirrorValue()).toEqual('abcd');
247
248 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
249
250 // Update - meaning: hide alert
251 hint.update();
252
253 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200254 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200255
256 // Show again
257 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
258 expect(hint.inputField().mirrorValue()).toEqual('abcde');
259 expect(hint.alert().active).toBeTruthy();
260 expect(hint.active()).toBeTruthy();
261
262 // Show menu, hide alert!
263 hint.show(false);
264 expect(hint.active()).toBeTruthy();
265 expect(hint.inputField().mirrorValue()).toEqual('abcde');
266 expect(hint.alert().active).toBeFalsy();
267
Akronfac16472018-07-26 16:47:21 +0200268 // Show again
Akron1a5a5872016-09-05 20:17:14 +0200269 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
270 expect(hint.inputField().mirrorValue()).toEqual('abcde');
271 expect(hint.alert().active).toBeTruthy();
272 expect(hint.active()).toBeTruthy();
273
274 // Show menu, hide alert!
275 hint.show(false);
Akronee9ef4a2016-06-03 12:50:08 +0200276 });
Akron00cd4d12016-05-31 21:01:11 +0200277
Akronda5bd3a2020-10-16 17:37:49 +0200278 it('should work both in Chrome and Firefox', function () {
279 var hint = hintClass.create({
280 inputField : input
281 });
282 hint.show(false);
283 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
284
285 // Show menu, hide alert!
286 hint.show(false);
287
288 expect(hint.active()).toBeFalsy();
289 });
290
291
Akronee9ef4a2016-06-03 12:50:08 +0200292 it('should view main menu on default', function () {
293 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200294 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200295 });
296
297 expect(hint.active()).toBeFalsy();
298
299 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200300
Akron8eaeb2e2016-08-29 18:26:28 +0200301 var cont = hint.inputField().container();
Akron8eaeb2e2016-08-29 18:26:28 +0200302 expect(cont.getElementsByTagName('div').length).toBe(1);
303 expect(cont.getElementsByTagName('ul').length).toBe(0);
304 expect(cont.firstChild).toEqual(cont.firstChild);
305
306 // Show menu, if a relevant context exists
307 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200308 hint.show(false);
309
Leo Repp57997402021-08-18 16:37:52 +0200310 expect(cont.getElementsByTagName('ul').length).toEqual(1+1); //+1 from containermenu (see container/container.js)
311 expect(cont.getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200312
Akron8eaeb2e2016-08-29 18:26:28 +0200313 // Hide the menu and focus on the input
314 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200315
Leo Repp57997402021-08-18 16:37:52 +0200316 expect(cont.getElementsByTagName('div').length).toEqual(1);
317 expect(cont.getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200318
Akron02360e42016-06-07 13:41:12 +0200319 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200320
Akronee9ef4a2016-06-03 12:50:08 +0200321 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200322
Leo Repp57997402021-08-18 16:37:52 +0200323 // show with context if possible
Akron02360e42016-06-07 13:41:12 +0200324 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200325
Leo Repp57997402021-08-18 16:37:52 +0200326 expect(cont.getElementsByTagName('div').length).toEqual(4);
327 expect(cont.getElementsByTagName('ul').length).toEqual(1+1);//+1 from containermenu (see container/container.js)
328 expect(cont.getElementsByTagName('li').length).toEqual(2);
Akron02360e42016-06-07 13:41:12 +0200329
Akronee9ef4a2016-06-03 12:50:08 +0200330 hint.unshow();
331 hint.inputField().insert(' aaaa/');
332
Leo Repp57997402021-08-18 16:37:52 +0200333 // show with context necessarily
Akronee9ef4a2016-06-03 12:50:08 +0200334 hint.show(true);
335
Leo Repp57997402021-08-18 16:37:52 +0200336 expect(cont.getElementsByTagName('div').length).toEqual(1);
337 expect(cont.getElementsByTagName('ul').length).toEqual(0); //here not +1: context doesnt fit
Akron1a5a5872016-09-05 20:17:14 +0200338 });
339
Akron95abaf42018-04-26 15:33:22 +0200340
341 it('should open menus depending on the context', function () {
342 var hint = hintClass.create({
343 inputField : input
344 });
345 hint.inputField().reset();
346
347 expect(hint.active()).toBeFalsy();
348
349 // show with context
350 hint.show(false);
351
352 expect(hint.active()).toBeTruthy();
Leo Repp57997402021-08-18 16:37:52 +0200353 var cont = hint.inputField().container();
354 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
Akron95abaf42018-04-26 15:33:22 +0200355
356 // Type in prefix
357 hint.active().prefix("cor").show();
358 expect(hint.active().prefix()).toEqual("cor");
359
360 // Click first step
Leo Repp57997402021-08-18 16:37:52 +0200361 expect(cont.getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
362 cont.getElementsByTagName('li')[0].click();
Akron95abaf42018-04-26 15:33:22 +0200363
364 expect(hint.active()).toBeTruthy();
365
366 // Click second step
Leo Repp57997402021-08-18 16:37:52 +0200367 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
368 cont.getElementsByTagName('li')[0].click()
Akron95abaf42018-04-26 15:33:22 +0200369
370 // Invisible menu
Leo Repp57997402021-08-18 16:37:52 +0200371 expect(cont.getElementsByTagName('li')[0]).toBeUndefined();
Akron95abaf42018-04-26 15:33:22 +0200372
373 // Inactive menu
374 expect(hint.active()).toBeFalsy();
375
376 // show with context
377 hint.show(false);
378
379 // No prefix
380 expect(hint.active().prefix()).toEqual("");
381 });
382
383
Akron1a5a5872016-09-05 20:17:14 +0200384 it('should not view main menu if context is mandatory', function () {
385 var hint = hintClass.create({
386 inputField : input
387 });
388
389 expect(hint.active()).toBeFalsy();
390
391 // Fine
392 hint.inputField().insert('der Baum corenlp/');
393 hint.show(true);
394 expect(hint.active()).toBeTruthy();
395
396 // Not analyzable
397 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
398 hint.show(true);
399 expect(hint.active()).toBeFalsy();
400
401 // Not available
402 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
403 hint.show(true);
404 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200405 });
Akron5746ecf2018-06-23 10:57:24 +0200406
407
408 it('should show the assistant bar on blur', function () {
409 var hint = hintClass.create({
410 inputField : input
411 });
412 // Fine
413 hint.inputField().insert('der Baum corenlp/');
414 hint.show(true);
415 expect(hint.active()).toBeTruthy();
416
417 // Blur
Akrone0789112018-08-31 14:32:04 +0200418 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200419 expect(hint.active()).toBeFalsy();
420 });
Akron954c6a52020-11-10 14:26:29 +0100421
422 it('should support prefix', function () {
423 const hint = hintClass.create({
424 inputField : input
425 });
426 hint.inputField().reset();
427
428 expect(hint.active()).toBeFalsy();
429
430 // show with context
431 hint.show(false);
432
433 expect(hint.active()).toBeTruthy();
434
435 const menu = hint.active();
436
437 expect(menu.element().nodeName).toEqual('UL');
438
439 menu.limit(8);
440
441 // view
442 menu.show();
443
444 expect(menu.prefix()).toBe('');
445 expect(hint.active()).toBeTruthy();
446
447 // Type in prefix
448 hint.active().prefix("cor").show();
Leo Repp57997402021-08-18 16:37:52 +0200449 expect(hint.active()._prefix.value()).toBe("cor");
Akron954c6a52020-11-10 14:26:29 +0100450 expect(hint.active().prefix()).toEqual("cor");
Akron954c6a52020-11-10 14:26:29 +0100451 expect(input.value).toEqual("");
Leo Repp57997402021-08-18 16:37:52 +0200452 expect(hint.active()._prefix["isSelectable"]).not.toBeNull();
453 expect(hint._menuCollection['-']._prefix["isSelectable"]).not.toBeNull();
454 expect(hint.active()._prefix).toBe(hint._menuCollection['-']._prefix);
455 expect(hint.active()._prefix.element()).toBe(hint._menuCollection['-']._prefix.element());
Akron954c6a52020-11-10 14:26:29 +0100456 hint.active()._prefix.element().click();
Leo Repp57997402021-08-18 16:37:52 +0200457
458
Akron954c6a52020-11-10 14:26:29 +0100459 expect(input.value).toEqual("cor");
460 expect(hint.active()).toBeFalsy();
461
462 // view
463 menu.show();
464 expect(menu.prefix()).toBe('');
465
466 });
467
Leo Repp46903bf2021-12-18 16:05:53 +0100468
469 it('should highlight the prefix if no item matches.', function () {
470 const hint = hintClass.create({
471 inputField : input
472 });
473 hint.inputField().reset();
474
475 expect(hint.active()).toBeFalsy();
476
477 // show with context
478 hint.show(false);
479
480 expect(hint.active()).toBeTruthy();
481
482 const menu = hint.active();
483 expect(menu.limit(3).show(3)).toBe(true);
484 menu.element().focus();
485
486
487
488 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
489 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
490 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
491 expect(menu.container()._cItemPrefix.active()).toBeTruthy();
492 expect(menu.prefix()).toEqual("EEE");
493 expect(menu.element().classList.contains("visible")).toBeTruthy();
494 expect(menu.container().element().classList.contains("visible")).toBeTruthy();
495
496 emitKeyboardEvent(menu.element(),'keydown'," ",13);
497 //Should call reset() and hide()
498 // hint containermenu should do something different.
499 expect(menu.prefix()).toEqual("");
500 expect(menu.element().classList.contains("visible")).toBeFalsy();
501 expect(menu.container().element().classList.contains("visible")).toBeFalsy();
502 });
Leo Repp57997402021-08-18 16:37:52 +0200503
Akron65c74352016-09-02 17:23:39 +0200504
Akron02360e42016-06-07 13:41:12 +0200505 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000506 });
507
Akron65c74352016-09-02 17:23:39 +0200508
Nils Diewald7c8ced22015-04-15 19:21:00 +0000509 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200510 beforeAll(beforeAllFunc);
511 afterAll(afterAllFunc);
512
Nils Diewald7c8ced22015-04-15 19:21:00 +0000513 it('should be initializable', function () {
514 expect(
Akronfac16472018-07-26 16:47:21 +0200515 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000516 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000517
Nils Diewald7c8ced22015-04-15 19:21:00 +0000518 expect(
Akronfac16472018-07-26 16:47:21 +0200519 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000520 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000521
Nils Diewald7c8ced22015-04-15 19:21:00 +0000522 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
523 expect(menuItem.name()).toEqual('CoreNLP');
524 expect(menuItem.action()).toEqual('corenlp/');
525 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000526
Nils Diewald7c8ced22015-04-15 19:21:00 +0000527 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200528 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000529 );
530 expect(menuItem.name()).toEqual('CoreNLP');
531 expect(menuItem.action()).toEqual('corenlp/');
532 expect(menuItem.desc()).not.toBeUndefined();
533 expect(menuItem.desc()).toEqual('It\'s funny');
534 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000535
Akronfac16472018-07-26 16:47:21 +0200536
Nils Diewald7c8ced22015-04-15 19:21:00 +0000537 it('should have an element', function () {
538 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
539 expect(menuItem.element()).not.toBe(undefined);
540 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000541
Nils Diewald7c8ced22015-04-15 19:21:00 +0000542 var title = menuItem.element().firstChild;
543 expect(title.nodeName).toEqual("SPAN");
544 expect(title.firstChild.nodeType).toEqual(3);
545 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
546 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
547 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000548
Nils Diewald7c8ced22015-04-15 19:21:00 +0000549 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200550 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000551 );
552 expect(menuItem.element()).not.toBe(undefined);
553 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000554
Nils Diewald7c8ced22015-04-15 19:21:00 +0000555 title = menuItem.element().firstChild;
556 expect(title.nodeName).toEqual("SPAN");
557 expect(title.firstChild.nodeType).toEqual(3); // TextNode
558 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000559
Nils Diewald7c8ced22015-04-15 19:21:00 +0000560 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
561 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000562
Nils Diewald7c8ced22015-04-15 19:21:00 +0000563 var desc = menuItem.element().lastChild;
564 expect(desc.nodeName).toEqual("SPAN");
565 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
566 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
567 });
Nils Diewald1c546922015-04-13 01:56:19 +0000568
Nils Diewald19ccee92014-12-08 11:30:08 +0000569
Nils Diewald7c8ced22015-04-15 19:21:00 +0000570 it('should be activatable and deactivateable by class', function () {
571 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
572 expect(menuItem.active()).toBe(false);
573 expect(menuItem.element().getAttribute("class")).toBe(null);
574 menuItem.active(true);
575 expect(menuItem.active()).toBe(true);
576 expect(menuItem.element().getAttribute("class")).toEqual("active");
577 menuItem.active(false); // Is active
578 expect(menuItem.active()).toBe(false);
579 expect(menuItem.element().getAttribute("class")).toEqual("");
580 menuItem.active(true);
581 expect(menuItem.active()).toBe(true);
582 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000583
Nils Diewald7c8ced22015-04-15 19:21:00 +0000584 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
585 expect(menuItem.active()).toBe(false);
586 expect(menuItem.element().getAttribute("class")).toBe(null);
587 menuItem.active(false); // Is not active
588 expect(menuItem.active()).toBe(false);
589 expect(menuItem.element().getAttribute("class")).toBe(null);
590 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000591
Akronfac16472018-07-26 16:47:21 +0200592
Nils Diewald7c8ced22015-04-15 19:21:00 +0000593 it('should be set to boundary', function () {
594 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
595 expect(menuItem.active()).toBe(false);
596 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000597
Nils Diewald7c8ced22015-04-15 19:21:00 +0000598 // Set active
599 menuItem.active(true);
600 expect(menuItem.active()).toBe(true);
601 expect(menuItem.noMore()).toBe(false);
602 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000603
Nils Diewald7c8ced22015-04-15 19:21:00 +0000604 // Set no more
605 menuItem.noMore(true);
606 expect(menuItem.active()).toBe(true);
607 expect(menuItem.noMore()).toBe(true);
608 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000609
Nils Diewald7c8ced22015-04-15 19:21:00 +0000610 // No no more
611 menuItem.noMore(false);
612 expect(menuItem.active()).toBe(true);
613 expect(menuItem.noMore()).toBe(false);
614 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000615
Nils Diewald7c8ced22015-04-15 19:21:00 +0000616 // Set no more, deactivate
617 menuItem.noMore(true);
618 menuItem.active(false);
619 expect(menuItem.active()).toBe(false);
620 expect(menuItem.noMore()).toBe(true);
621 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000622
Nils Diewald7c8ced22015-04-15 19:21:00 +0000623 // Set active
624 menuItem.active(true);
625 expect(menuItem.active()).toBe(true);
626 expect(menuItem.noMore()).toBe(true);
627 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
628 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000629
Akronfac16472018-07-26 16:47:21 +0200630
Nils Diewald7c8ced22015-04-15 19:21:00 +0000631 it('should be highlightable', function () {
632 // Highlight in the middle
633 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
634 menuItem.highlight("ren");
635 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000636
Nils Diewald7c8ced22015-04-15 19:21:00 +0000637 menuItem.lowlight();
638 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000639
Nils Diewald7c8ced22015-04-15 19:21:00 +0000640 // Starting highlight
641 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
642 menuItem.highlight("cor");
643 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000644
Nils Diewald7c8ced22015-04-15 19:21:00 +0000645 menuItem.lowlight();
646 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
647
648 // Starting highlight - short
649 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
650 menuItem.highlight("c");
651 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
652
653 menuItem.lowlight();
654 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
655
656 // Highlight at the end
657 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
658 menuItem.highlight("nlp");
659 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
660
661 menuItem.lowlight();
662 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
663
664 // Highlight at the end - short
665 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
666 menuItem.highlight("p");
667 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
668
669 menuItem.lowlight();
670 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
671
672 // No highlight
673 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
674 menuItem.highlight("xp");
675 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
676
677 menuItem.lowlight();
678 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
679
680 // Highlight in the middle - first
681 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
682
683 menuItem.highlight("ren");
684 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
685
686 menuItem.lowlight();
687 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
688
689 // Highlight in the middle - second
690 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
691 menuItem.highlight("ampl");
692 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
693
694 menuItem.lowlight();
695 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
696
697 // Highlight in the middle - both
698 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
699
700 menuItem.highlight("e");
701 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>');
702
703 menuItem.lowlight();
704 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
705
706 // Highlight in the end - second
707 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
708 menuItem.highlight("le");
709 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
710
711 menuItem.lowlight();
712 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
713
714 // Highlight at the beginning - second
715 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
716 menuItem.highlight("this");
717 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
718
719 menuItem.lowlight();
720 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
721 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000722 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000723
Akronfac16472018-07-26 16:47:21 +0200724
Nils Diewald7c8ced22015-04-15 19:21:00 +0000725 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200726 beforeAll(beforeAllFunc);
727 afterAll(afterAllFunc);
728
Nils Diewald7c8ced22015-04-15 19:21:00 +0000729 var list = [
730 ["Constituency", "c=", "Example 1"],
731 ["Lemma", "l="],
732 ["Morphology", "m=", "Example 2"],
733 ["Part-of-Speech", "p="],
734 ["Syntax", "syn="]
735 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000736
Nils Diewald7c8ced22015-04-15 19:21:00 +0000737 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000738 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000739 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200740 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000741
Nils Diewald7c8ced22015-04-15 19:21:00 +0000742 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000743
Nils Diewald7c8ced22015-04-15 19:21:00 +0000744 // view
745 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200746 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000747
Nils Diewald7c8ced22015-04-15 19:21:00 +0000748 // First element in list
749 expect(menu.item(0).active()).toBe(true);
750 expect(menu.item(0).noMore()).toBe(true);
751
752 // Middle element in list
753 expect(menu.item(2).active()).toBe(false);
754 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000755
Nils Diewald7c8ced22015-04-15 19:21:00 +0000756 // Last element in list
757 expect(menu.item(menu.length() - 1).active()).toBe(false);
758 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200759
760 expect(menu.shownItem(0).active()).toBeTruthy();
761 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
762 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
763 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
764
765 menu.next();
766 expect(menu.shownItem(1).active()).toBeTruthy();
767
768 menu.next();
769 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000770 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000771 });
772});