Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Hint menu item based on MenuItem |
| 3 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 4 | "use strict"; |
| 5 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 6 | define(['menu/item', 'util'], function (itemClass) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 7 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * Create new menu item object. |
| 11 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 12 | create : function (params) { |
| 13 | return Object.create(itemClass) |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 14 | .upgradeTo(this) |
| 15 | ._init(params); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 16 | }, |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 17 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 18 | // Initialize menu item object |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | _init : function (params) { |
| 20 | if (params[0] === undefined || |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 21 | params[1] === undefined) |
| 22 | throw new Error("Missing parameters"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 23 | |
| 24 | this._name = params[0]; |
| 25 | this._action = params[1]; |
| 26 | this._lcField = ' ' + this._name.toLowerCase(); |
| 27 | |
| 28 | if (params.length > 2) { |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 29 | this._desc = params[2]; |
| 30 | this._lcField += " " + this._desc.toLowerCase(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | return this; |
| 34 | }, |
| 35 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Get or set the content of the item. |
| 38 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | content : function (content) { |
| 40 | if (arguments.length === 1) { |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 41 | this._content = content; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 42 | }; |
| 43 | return this._content; |
| 44 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 45 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Override the click action |
| 48 | * of the menu item. |
| 49 | */ |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 50 | onclick : function (e) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 51 | var m = this.menu(); |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 52 | // m.hide(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 53 | |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 54 | // Reset prefix and update the input field |
| 55 | m.reset(this._action); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 56 | |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 57 | e.halt(); |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 58 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 59 | // show alt |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 60 | m.hint().show(true); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 61 | }, |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 62 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 63 | /** |
| 64 | * The name of the menu entry. |
| 65 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 66 | name : function () { |
| 67 | return this._name; |
| 68 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * The action (the string inserted on click) |
| 72 | * of the menu item. |
| 73 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 74 | action : function () { |
| 75 | return this._action; |
| 76 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * The description of the menu item. |
| 80 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 81 | desc : function () { |
| 82 | return this._desc; |
| 83 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * The associated dom element of the |
| 87 | * menu item. |
| 88 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 89 | element : function () { |
| 90 | // already defined |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 91 | if (this._el !== undefined) |
| 92 | return this._el; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | |
| 94 | // Create list item |
| 95 | var li = document.createElement("li"); |
| 96 | |
| 97 | if (this.onclick !== undefined) { |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 98 | li["onclick"] = this.onclick.bind(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // Create title |
| 102 | var name = document.createElement("span"); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 103 | name.addT(this._name); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 104 | |
| 105 | li.appendChild(name); |
| 106 | |
| 107 | // Create description |
| 108 | if (this._desc !== undefined) { |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 109 | var desc = document.createElement("span"); |
| 110 | desc.classList.add('desc'); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 111 | desc.addT(this._desc); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 112 | li.appendChild(desc); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 113 | }; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 114 | return this._el = li; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | }); |