blob: 3b07bd925eaa7d4580681b270b5f7484d25e3762 [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
Akronbe2f9a02025-01-14 09:36:55 +0100274 // Show again with the same message
275 expect(hint.alert().active).toBeTruthy();
276 hint.update();
277 expect(hint.alert().active).toBeFalsy();
278 hint.alert().show();
279 expect(hint.alert().active).toBeTruthy();
280 expect(hint.alert().element().textContent).toEqual("That does not work!");
281
Akron1a5a5872016-09-05 20:17:14 +0200282 // Show menu, hide alert!
283 hint.show(false);
Akronee9ef4a2016-06-03 12:50:08 +0200284 });
Akron00cd4d12016-05-31 21:01:11 +0200285
Akronda5bd3a2020-10-16 17:37:49 +0200286 it('should work both in Chrome and Firefox', function () {
287 var hint = hintClass.create({
288 inputField : input
289 });
290 hint.show(false);
291 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
292
293 // Show menu, hide alert!
294 hint.show(false);
295
296 expect(hint.active()).toBeFalsy();
297 });
298
299
Akronee9ef4a2016-06-03 12:50:08 +0200300 it('should view main menu on default', function () {
301 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200302 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200303 });
304
305 expect(hint.active()).toBeFalsy();
306
307 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200308
Akron8eaeb2e2016-08-29 18:26:28 +0200309 var cont = hint.inputField().container();
Akron8eaeb2e2016-08-29 18:26:28 +0200310 expect(cont.getElementsByTagName('div').length).toBe(1);
311 expect(cont.getElementsByTagName('ul').length).toBe(0);
312 expect(cont.firstChild).toEqual(cont.firstChild);
313
314 // Show menu, if a relevant context exists
315 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200316 hint.show(false);
317
Leo Repp57997402021-08-18 16:37:52 +0200318 expect(cont.getElementsByTagName('ul').length).toEqual(1+1); //+1 from containermenu (see container/container.js)
319 expect(cont.getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200320
Akron8eaeb2e2016-08-29 18:26:28 +0200321 // Hide the menu and focus on the input
322 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200323
Leo Repp57997402021-08-18 16:37:52 +0200324 expect(cont.getElementsByTagName('div').length).toEqual(1);
325 expect(cont.getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200326
Akron02360e42016-06-07 13:41:12 +0200327 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200328
Akronee9ef4a2016-06-03 12:50:08 +0200329 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200330
Leo Repp57997402021-08-18 16:37:52 +0200331 // show with context if possible
Akron02360e42016-06-07 13:41:12 +0200332 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200333
Leo Repp57997402021-08-18 16:37:52 +0200334 expect(cont.getElementsByTagName('div').length).toEqual(4);
335 expect(cont.getElementsByTagName('ul').length).toEqual(1+1);//+1 from containermenu (see container/container.js)
336 expect(cont.getElementsByTagName('li').length).toEqual(2);
Akron02360e42016-06-07 13:41:12 +0200337
Akronee9ef4a2016-06-03 12:50:08 +0200338 hint.unshow();
339 hint.inputField().insert(' aaaa/');
340
Leo Repp57997402021-08-18 16:37:52 +0200341 // show with context necessarily
Akronee9ef4a2016-06-03 12:50:08 +0200342 hint.show(true);
343
Leo Repp57997402021-08-18 16:37:52 +0200344 expect(cont.getElementsByTagName('div').length).toEqual(1);
345 expect(cont.getElementsByTagName('ul').length).toEqual(0); //here not +1: context doesnt fit
Akron1a5a5872016-09-05 20:17:14 +0200346 });
347
Akron95abaf42018-04-26 15:33:22 +0200348
349 it('should open menus depending on the context', function () {
350 var hint = hintClass.create({
351 inputField : input
352 });
353 hint.inputField().reset();
354
355 expect(hint.active()).toBeFalsy();
356
357 // show with context
358 hint.show(false);
359
360 expect(hint.active()).toBeTruthy();
Leo Repp57997402021-08-18 16:37:52 +0200361 var cont = hint.inputField().container();
362 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
Akron95abaf42018-04-26 15:33:22 +0200363
364 // Type in prefix
365 hint.active().prefix("cor").show();
366 expect(hint.active().prefix()).toEqual("cor");
367
368 // Click first step
Leo Repp57997402021-08-18 16:37:52 +0200369 expect(cont.getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
370 cont.getElementsByTagName('li')[0].click();
Akron95abaf42018-04-26 15:33:22 +0200371
372 expect(hint.active()).toBeTruthy();
373
374 // Click second step
Leo Repp57997402021-08-18 16:37:52 +0200375 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
376 cont.getElementsByTagName('li')[0].click()
Akron95abaf42018-04-26 15:33:22 +0200377
378 // Invisible menu
Leo Repp57997402021-08-18 16:37:52 +0200379 expect(cont.getElementsByTagName('li')[0]).toBeUndefined();
Akron95abaf42018-04-26 15:33:22 +0200380
381 // Inactive menu
382 expect(hint.active()).toBeFalsy();
383
384 // show with context
385 hint.show(false);
386
387 // No prefix
388 expect(hint.active().prefix()).toEqual("");
389 });
390
391
Akron1a5a5872016-09-05 20:17:14 +0200392 it('should not view main menu if context is mandatory', function () {
393 var hint = hintClass.create({
394 inputField : input
395 });
396
397 expect(hint.active()).toBeFalsy();
398
399 // Fine
400 hint.inputField().insert('der Baum corenlp/');
401 hint.show(true);
402 expect(hint.active()).toBeTruthy();
403
404 // Not analyzable
405 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
406 hint.show(true);
407 expect(hint.active()).toBeFalsy();
408
409 // Not available
410 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
411 hint.show(true);
412 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200413 });
Akron5746ecf2018-06-23 10:57:24 +0200414
415
416 it('should show the assistant bar on blur', function () {
417 var hint = hintClass.create({
418 inputField : input
419 });
420 // Fine
421 hint.inputField().insert('der Baum corenlp/');
422 hint.show(true);
423 expect(hint.active()).toBeTruthy();
424
425 // Blur
Akrone0789112018-08-31 14:32:04 +0200426 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200427 expect(hint.active()).toBeFalsy();
428 });
Akron954c6a52020-11-10 14:26:29 +0100429
430 it('should support prefix', function () {
431 const hint = hintClass.create({
432 inputField : input
433 });
434 hint.inputField().reset();
435
436 expect(hint.active()).toBeFalsy();
437
438 // show with context
439 hint.show(false);
440
441 expect(hint.active()).toBeTruthy();
442
443 const menu = hint.active();
444
445 expect(menu.element().nodeName).toEqual('UL');
446
447 menu.limit(8);
448
449 // view
450 menu.show();
451
452 expect(menu.prefix()).toBe('');
453 expect(hint.active()).toBeTruthy();
454
455 // Type in prefix
456 hint.active().prefix("cor").show();
Leo Repp57997402021-08-18 16:37:52 +0200457 expect(hint.active()._prefix.value()).toBe("cor");
Akron954c6a52020-11-10 14:26:29 +0100458 expect(hint.active().prefix()).toEqual("cor");
Akron954c6a52020-11-10 14:26:29 +0100459 expect(input.value).toEqual("");
Leo Repp57997402021-08-18 16:37:52 +0200460 expect(hint.active()._prefix["isSelectable"]).not.toBeNull();
461 expect(hint._menuCollection['-']._prefix["isSelectable"]).not.toBeNull();
462 expect(hint.active()._prefix).toBe(hint._menuCollection['-']._prefix);
463 expect(hint.active()._prefix.element()).toBe(hint._menuCollection['-']._prefix.element());
Akron954c6a52020-11-10 14:26:29 +0100464 hint.active()._prefix.element().click();
Leo Repp57997402021-08-18 16:37:52 +0200465
466
Akron954c6a52020-11-10 14:26:29 +0100467 expect(input.value).toEqual("cor");
468 expect(hint.active()).toBeFalsy();
469
470 // view
471 menu.show();
472 expect(menu.prefix()).toBe('');
473
474 });
475
Leo Repp46903bf2021-12-18 16:05:53 +0100476
477 it('should highlight the prefix if no item matches.', function () {
478 const hint = hintClass.create({
479 inputField : input
480 });
481 hint.inputField().reset();
482
483 expect(hint.active()).toBeFalsy();
484
485 // show with context
486 hint.show(false);
487
488 expect(hint.active()).toBeTruthy();
489
490 const menu = hint.active();
491 expect(menu.limit(3).show(3)).toBe(true);
492 menu.element().focus();
493
494
495
496 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
497 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
498 emitKeyboardEvent(menu.element(), 'keypress', "E", 69);
499 expect(menu.container()._cItemPrefix.active()).toBeTruthy();
500 expect(menu.prefix()).toEqual("EEE");
501 expect(menu.element().classList.contains("visible")).toBeTruthy();
502 expect(menu.container().element().classList.contains("visible")).toBeTruthy();
503
504 emitKeyboardEvent(menu.element(),'keydown'," ",13);
505 //Should call reset() and hide()
506 // hint containermenu should do something different.
507 expect(menu.prefix()).toEqual("");
508 expect(menu.element().classList.contains("visible")).toBeFalsy();
509 expect(menu.container().element().classList.contains("visible")).toBeFalsy();
510 });
Leo Repp57997402021-08-18 16:37:52 +0200511
Akron65c74352016-09-02 17:23:39 +0200512
Akron02360e42016-06-07 13:41:12 +0200513 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000514 });
515
Akron65c74352016-09-02 17:23:39 +0200516
Nils Diewald7c8ced22015-04-15 19:21:00 +0000517 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200518 beforeAll(beforeAllFunc);
519 afterAll(afterAllFunc);
520
Nils Diewald7c8ced22015-04-15 19:21:00 +0000521 it('should be initializable', function () {
522 expect(
Akronfac16472018-07-26 16:47:21 +0200523 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000524 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000525
Nils Diewald7c8ced22015-04-15 19:21:00 +0000526 expect(
Akronfac16472018-07-26 16:47:21 +0200527 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000528 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000529
Nils Diewald7c8ced22015-04-15 19:21:00 +0000530 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
531 expect(menuItem.name()).toEqual('CoreNLP');
532 expect(menuItem.action()).toEqual('corenlp/');
533 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000534
Nils Diewald7c8ced22015-04-15 19:21:00 +0000535 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200536 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000537 );
538 expect(menuItem.name()).toEqual('CoreNLP');
539 expect(menuItem.action()).toEqual('corenlp/');
540 expect(menuItem.desc()).not.toBeUndefined();
541 expect(menuItem.desc()).toEqual('It\'s funny');
542 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000543
Akronfac16472018-07-26 16:47:21 +0200544
Nils Diewald7c8ced22015-04-15 19:21:00 +0000545 it('should have an element', function () {
546 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
547 expect(menuItem.element()).not.toBe(undefined);
548 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000549
Nils Diewald7c8ced22015-04-15 19:21:00 +0000550 var title = menuItem.element().firstChild;
551 expect(title.nodeName).toEqual("SPAN");
552 expect(title.firstChild.nodeType).toEqual(3);
553 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
554 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
555 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000556
Nils Diewald7c8ced22015-04-15 19:21:00 +0000557 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200558 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000559 );
560 expect(menuItem.element()).not.toBe(undefined);
561 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000562
Nils Diewald7c8ced22015-04-15 19:21:00 +0000563 title = menuItem.element().firstChild;
564 expect(title.nodeName).toEqual("SPAN");
565 expect(title.firstChild.nodeType).toEqual(3); // TextNode
566 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000567
Nils Diewald7c8ced22015-04-15 19:21:00 +0000568 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
569 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000570
Nils Diewald7c8ced22015-04-15 19:21:00 +0000571 var desc = menuItem.element().lastChild;
572 expect(desc.nodeName).toEqual("SPAN");
573 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
574 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
575 });
Nils Diewald1c546922015-04-13 01:56:19 +0000576
Nils Diewald19ccee92014-12-08 11:30:08 +0000577
Nils Diewald7c8ced22015-04-15 19:21:00 +0000578 it('should be activatable and deactivateable by class', function () {
579 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
580 expect(menuItem.active()).toBe(false);
581 expect(menuItem.element().getAttribute("class")).toBe(null);
582 menuItem.active(true);
583 expect(menuItem.active()).toBe(true);
584 expect(menuItem.element().getAttribute("class")).toEqual("active");
585 menuItem.active(false); // Is active
586 expect(menuItem.active()).toBe(false);
587 expect(menuItem.element().getAttribute("class")).toEqual("");
588 menuItem.active(true);
589 expect(menuItem.active()).toBe(true);
590 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000591
Nils Diewald7c8ced22015-04-15 19:21:00 +0000592 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
593 expect(menuItem.active()).toBe(false);
594 expect(menuItem.element().getAttribute("class")).toBe(null);
595 menuItem.active(false); // Is not active
596 expect(menuItem.active()).toBe(false);
597 expect(menuItem.element().getAttribute("class")).toBe(null);
598 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000599
Akronfac16472018-07-26 16:47:21 +0200600
Nils Diewald7c8ced22015-04-15 19:21:00 +0000601 it('should be set to boundary', function () {
602 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
603 expect(menuItem.active()).toBe(false);
604 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000605
Nils Diewald7c8ced22015-04-15 19:21:00 +0000606 // Set active
607 menuItem.active(true);
608 expect(menuItem.active()).toBe(true);
609 expect(menuItem.noMore()).toBe(false);
610 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000611
Nils Diewald7c8ced22015-04-15 19:21:00 +0000612 // Set no more
613 menuItem.noMore(true);
614 expect(menuItem.active()).toBe(true);
615 expect(menuItem.noMore()).toBe(true);
616 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000617
Nils Diewald7c8ced22015-04-15 19:21:00 +0000618 // No no more
619 menuItem.noMore(false);
620 expect(menuItem.active()).toBe(true);
621 expect(menuItem.noMore()).toBe(false);
622 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000623
Nils Diewald7c8ced22015-04-15 19:21:00 +0000624 // Set no more, deactivate
625 menuItem.noMore(true);
626 menuItem.active(false);
627 expect(menuItem.active()).toBe(false);
628 expect(menuItem.noMore()).toBe(true);
629 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000630
Nils Diewald7c8ced22015-04-15 19:21:00 +0000631 // Set active
632 menuItem.active(true);
633 expect(menuItem.active()).toBe(true);
634 expect(menuItem.noMore()).toBe(true);
635 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
636 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000637
Akronfac16472018-07-26 16:47:21 +0200638
Nils Diewald7c8ced22015-04-15 19:21:00 +0000639 it('should be highlightable', function () {
640 // Highlight in the middle
641 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
642 menuItem.highlight("ren");
643 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</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>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000647
Nils Diewald7c8ced22015-04-15 19:21:00 +0000648 // Starting highlight
649 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
650 menuItem.highlight("cor");
651 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000652
Nils Diewald7c8ced22015-04-15 19:21:00 +0000653 menuItem.lowlight();
654 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
655
656 // Starting highlight - short
657 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
658 menuItem.highlight("c");
659 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
660
661 menuItem.lowlight();
662 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
663
664 // Highlight at the end
665 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
666 menuItem.highlight("nlp");
667 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
668
669 menuItem.lowlight();
670 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
671
672 // Highlight at the end - short
673 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
674 menuItem.highlight("p");
675 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
676
677 menuItem.lowlight();
678 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
679
680 // No highlight
681 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
682 menuItem.highlight("xp");
683 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
684
685 menuItem.lowlight();
686 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
687
688 // Highlight in the middle - first
689 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
690
691 menuItem.highlight("ren");
692 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</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 - second
698 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
699 menuItem.highlight("ampl");
700 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
701
702 menuItem.lowlight();
703 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
704
705 // Highlight in the middle - both
706 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
707
708 menuItem.highlight("e");
709 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>');
710
711 menuItem.lowlight();
712 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
713
714 // Highlight in the end - second
715 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
716 menuItem.highlight("le");
717 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
718
719 menuItem.lowlight();
720 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
721
722 // Highlight at the beginning - second
723 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
724 menuItem.highlight("this");
725 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
726
727 menuItem.lowlight();
728 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
729 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000730 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000731
Akronfac16472018-07-26 16:47:21 +0200732
Nils Diewald7c8ced22015-04-15 19:21:00 +0000733 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200734 beforeAll(beforeAllFunc);
735 afterAll(afterAllFunc);
736
Nils Diewald7c8ced22015-04-15 19:21:00 +0000737 var list = [
738 ["Constituency", "c=", "Example 1"],
739 ["Lemma", "l="],
740 ["Morphology", "m=", "Example 2"],
741 ["Part-of-Speech", "p="],
742 ["Syntax", "syn="]
743 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000744
Nils Diewald7c8ced22015-04-15 19:21:00 +0000745 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000746 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000747 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200748 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000749
Nils Diewald7c8ced22015-04-15 19:21:00 +0000750 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000751
Nils Diewald7c8ced22015-04-15 19:21:00 +0000752 // view
753 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200754 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000755
Nils Diewald7c8ced22015-04-15 19:21:00 +0000756 // First element in list
757 expect(menu.item(0).active()).toBe(true);
758 expect(menu.item(0).noMore()).toBe(true);
759
760 // Middle element in list
761 expect(menu.item(2).active()).toBe(false);
762 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000763
Nils Diewald7c8ced22015-04-15 19:21:00 +0000764 // Last element in list
765 expect(menu.item(menu.length() - 1).active()).toBe(false);
766 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200767
768 expect(menu.shownItem(0).active()).toBeTruthy();
769 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
770 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
771 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
772
773 menu.next();
774 expect(menu.shownItem(1).active()).toBeTruthy();
775
776 menu.next();
777 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000778 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000779 });
780});