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