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 | |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 54 | // Reset prefix |
55 | m.prefix(""); | ||||
56 | |||||
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 57 | var h = m.hint(); |
58 | |||||
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 59 | // Update input field |
60 | var input = h.inputField(); | ||||
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 61 | input.insert(this._action).update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 63 | e.halt(); |
Akron | 95abaf4 | 2018-04-26 15:33:22 +0200 | [diff] [blame] | 64 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 65 | // show alt |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 66 | h.show(true); |
67 | }, | ||||
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 68 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 69 | /** |
70 | * The name of the menu entry. | ||||
71 | */ | ||||
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 72 | name : function () { |
73 | return this._name; | ||||
74 | }, | ||||
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 75 | |
76 | /** | ||||
77 | * The action (the string inserted on click) | ||||
78 | * of the menu item. | ||||
79 | */ | ||||
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 80 | action : function () { |
81 | return this._action; | ||||
82 | }, | ||||
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 83 | |
84 | /** | ||||
85 | * The description of the menu item. | ||||
86 | */ | ||||
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 87 | desc : function () { |
88 | return this._desc; | ||||
89 | }, | ||||
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 90 | |
91 | /** | ||||
92 | * The associated dom element of the | ||||
93 | * menu item. | ||||
94 | */ | ||||
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 95 | element : function () { |
96 | // already defined | ||||
97 | if (this._element !== undefined) | ||||
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 98 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 99 | |
100 | // Create list item | ||||
101 | var li = document.createElement("li"); | ||||
102 | |||||
103 | if (this.onclick !== undefined) { | ||||
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 104 | li["onclick"] = this.onclick.bind(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 105 | }; |
106 | |||||
107 | // Create title | ||||
108 | var name = document.createElement("span"); | ||||
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 109 | name.addT(this._name); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 110 | |
111 | li.appendChild(name); | ||||
112 | |||||
113 | // Create description | ||||
114 | if (this._desc !== undefined) { | ||||
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 115 | var desc = document.createElement("span"); |
116 | desc.classList.add('desc'); | ||||
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 117 | desc.addT(this._desc); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 118 | li.appendChild(desc); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 119 | }; |
120 | return this._element = li; | ||||
121 | } | ||||
122 | }; | ||||
123 | }); |