blob: ecd0b51262fe168d6e8569e9e93fe597f2fe7d59 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Hint menu
3 */
Akronda5bd3a2020-10-16 17:37:49 +02004
5"use strict";
6
Akroncae907d2018-08-20 17:22:15 +02007define([
Leo Repp57997402021-08-18 16:37:52 +02008 'containermenu',
Akron1ff3ac22016-04-28 16:30:45 +02009 'hint/item',
10 'hint/prefix',
Akroncae907d2018-08-20 17:22:15 +020011 'hint/lengthField'
12], function (
Leo Repp57997402021-08-18 16:37:52 +020013 containerMenuClass,
Akroncae907d2018-08-20 17:22:15 +020014 itemClass,
15 prefixClass,
16 lengthFieldClass) {
Nils Diewald7148c6f2015-05-04 15:07:53 +000017
Akroncae907d2018-08-20 17:22:15 +020018 return {
19
Nils Diewald7148c6f2015-05-04 15:07:53 +000020 /**
21 * Create new hint helper menu.
22 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 create : function (hint, context, params) {
Leo Repp57997402021-08-18 16:37:52 +020024 const obj = containerMenuClass.create(params, {
25 itemClass : itemClass,
26 prefixClass : prefixClass,
27 lengthFieldClass : lengthFieldClass})
28 .upgradeTo(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 obj._context = context;
Akron24aa0052020-11-10 11:00:34 +010030 obj._el.classList.add('hint');
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 obj._hint = hint;
32
Nils Diewald20f7ace2015-05-07 12:51:34 +000033 // Make the top item always active
34 obj._firstActive = true;
35
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 obj.element().addEventListener('blur', function (e) {
Akrone0789112018-08-31 14:32:04 +020037 this.menu.hide(); // WithoutDestruction();
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 });
Leo Repp57997402021-08-18 16:37:52 +020039 // Fix the containeritems not being clickable. Add this to the containers element.
40 obj.container().element().addEventListener("mousedown", function (e) {
41 // see https://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event
42 e.preventDefault();
43 // It used to be, that clicking on a item within the container (see container.js) would cause the container to gain focus
44 // thanks to mousedown default behaviour, which would mean the actual menu (ul menu roll containermenu hint) would not be in focus (I think? containermenu ul is its child
45 // afterall?). This would cause blur to be called, which (see hint/menu.js) would hide the current menu and its container, causing click to target a location
46 // the containeritem USED to be.
47 //https://w3c.github.io/uievents/#event-type-mousedown
48 //These default actions are thus not supported anymore.
49
50 }.bind(obj));
51 obj.container().element().addEventListener("click", function (e) {
52 this.reset("");
53 this.element().blur();
54 this.hint().unshow(); //hide the containermenu, not with hide but with blur, because blur would usually happen in default mousedown behaviour
55 e.halt(); // Question: my impression is that this click event handler is called after all the others and thus this should be absolutely no problem.
56 // Are we sure there are no things that do not happen now thanks to this?
57
58 //by default, click focuses its target. Maybe that is why e.halt() is necessary? (https://w3c.github.io/uievents/#event-type-click)
59 }.bind(obj));
Nils Diewald0e6992a2015-04-14 20:13:52 +000060
61 // Focus on input field on hide
62 obj.onHide = function () {
Akron954c6a52020-11-10 14:26:29 +010063 const h = this._hint;
Akroncae907d2018-08-20 17:22:15 +020064 h._inputField.element().focus();
65 if (h.active() !== null) {
66 if (h._alert.active) {
67 h._unshowAlert();
68 };
69 h.active(null);
70 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 };
72
73 return obj;
74 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000075
76 /**
77 * The hint helper object,
78 * the menu is attached to.
79 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000080 hint : function () {
81 return this._hint;
Leo Repp57997402021-08-18 16:37:52 +020082 },
83
84 /**
85 * Reset the prefix, inputField and hide the menu. Called by hint/item.
86 */
87 reset : function (action) {
88 this.prefix("");
89 this.hint().inputField().insert(action).update();
90 },
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 };
92});