Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Hint menu |
| 3 | */ |
Akron | da5bd3a | 2020-10-16 17:37:49 +0200 | [diff] [blame] | 4 | |
| 5 | "use strict"; |
| 6 | |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 7 | define([ |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 8 | 'containermenu', |
Akron | 1ff3ac2 | 2016-04-28 16:30:45 +0200 | [diff] [blame] | 9 | 'hint/item', |
| 10 | 'hint/prefix', |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 11 | 'hint/lengthField' |
| 12 | ], function ( |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 13 | containerMenuClass, |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 14 | itemClass, |
| 15 | prefixClass, |
| 16 | lengthFieldClass) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 17 | |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 18 | return { |
| 19 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Create new hint helper menu. |
| 22 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 23 | create : function (hint, context, params) { |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 24 | const obj = containerMenuClass.create(params, { |
| 25 | itemClass : itemClass, |
| 26 | prefixClass : prefixClass, |
| 27 | lengthFieldClass : lengthFieldClass}) |
| 28 | .upgradeTo(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | obj._context = context; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 30 | obj._el.classList.add('hint'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 31 | obj._hint = hint; |
| 32 | |
Nils Diewald | 20f7ace | 2015-05-07 12:51:34 +0000 | [diff] [blame] | 33 | // Make the top item always active |
| 34 | obj._firstActive = true; |
| 35 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 36 | obj.element().addEventListener('blur', function (e) { |
Akron | e078911 | 2018-08-31 14:32:04 +0200 | [diff] [blame] | 37 | this.menu.hide(); // WithoutDestruction(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | }); |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 39 | // 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 60 | |
| 61 | // Focus on input field on hide |
| 62 | obj.onHide = function () { |
Akron | 954c6a5 | 2020-11-10 14:26:29 +0100 | [diff] [blame] | 63 | const h = this._hint; |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 64 | h._inputField.element().focus(); |
| 65 | if (h.active() !== null) { |
| 66 | if (h._alert.active) { |
| 67 | h._unshowAlert(); |
| 68 | }; |
| 69 | h.active(null); |
| 70 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | return obj; |
| 74 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * The hint helper object, |
| 78 | * the menu is attached to. |
| 79 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 80 | hint : function () { |
| 81 | return this._hint; |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 82 | }, |
| 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | }; |
| 92 | }); |