Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Hint menu |
| 3 | */ |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 4 | define([ |
| 5 | 'menu', |
Akron | 1ff3ac2 | 2016-04-28 16:30:45 +0200 | [diff] [blame] | 6 | 'hint/item', |
| 7 | 'hint/prefix', |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 8 | 'hint/lengthField' |
| 9 | ], function ( |
| 10 | menuClass, |
| 11 | itemClass, |
| 12 | prefixClass, |
| 13 | lengthFieldClass) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 14 | |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 15 | return { |
| 16 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Create new hint helper menu. |
| 19 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 20 | create : function (hint, context, params) { |
| 21 | var obj = Object.create(menuClass) |
Akron | 72f7357 | 2017-12-05 12:31:09 +0100 | [diff] [blame] | 22 | .upgradeTo(this) |
| 23 | ._init(params, { |
| 24 | itemClass : itemClass, |
| 25 | prefixClass : prefixClass, |
| 26 | lengthFieldClass : lengthFieldClass |
| 27 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | obj._context = context; |
| 29 | obj._element.classList.add('hint'); |
| 30 | obj._hint = hint; |
| 31 | |
Nils Diewald | 20f7ace | 2015-05-07 12:51:34 +0000 | [diff] [blame] | 32 | // Make the top item always active |
| 33 | obj._firstActive = true; |
| 34 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 35 | obj.element().addEventListener('blur', function (e) { |
Akron | e39cc86 | 2018-04-25 15:16:11 +0200 | [diff] [blame] | 36 | this.menu.hideWithoutDestruction(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 37 | }); |
| 38 | |
| 39 | // Focus on input field on hide |
| 40 | obj.onHide = function () { |
Akron | cae907d | 2018-08-20 17:22:15 +0200 | [diff] [blame] | 41 | var h = this._hint; |
| 42 | h._inputField.element().focus(); |
| 43 | if (h.active() !== null) { |
| 44 | if (h._alert.active) { |
| 45 | h._unshowAlert(); |
| 46 | }; |
| 47 | h.active(null); |
| 48 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | return obj; |
| 52 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * The hint helper object, |
| 56 | * the menu is attached to. |
| 57 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 58 | hint : function () { |
| 59 | return this._hint; |
Akron | e39cc86 | 2018-04-25 15:16:11 +0200 | [diff] [blame] | 60 | }, |
| 61 | |
| 62 | /** |
| 63 | * Hide the menu just for the moment, |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 64 | * without cleaning up anything, |
| 65 | /* but resetting the prefix. |
Akron | e39cc86 | 2018-04-25 15:16:11 +0200 | [diff] [blame] | 66 | */ |
| 67 | hideWithoutDestruction : function () { |
| 68 | this.element().classList.remove("visible"); |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 69 | if (this._hint) { |
Akron | e39cc86 | 2018-04-25 15:16:11 +0200 | [diff] [blame] | 70 | this._hint.inputField().element().focus(); |
Akron | 5746ecf | 2018-06-23 10:57:24 +0200 | [diff] [blame] | 71 | this.onHide(); |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 72 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | }); |