Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Hint menu item based on MenuItem |
| 3 | */ |
| 4 | define(['menu/item'], function (itemClass) { |
| 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) |
| 12 | .upgradeTo(this) |
| 13 | ._init(params); |
| 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 || |
| 19 | params[1] === undefined) |
| 20 | throw new Error("Missing parameters"); |
| 21 | |
| 22 | this._name = params[0]; |
| 23 | this._action = params[1]; |
| 24 | this._lcField = ' ' + this._name.toLowerCase(); |
| 25 | |
| 26 | if (params.length > 2) { |
| 27 | this._desc = params[2]; |
| 28 | this._lcField += " " + this._desc.toLowerCase(); |
| 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) { |
| 39 | this._content = content; |
| 40 | }; |
| 41 | return this._content; |
| 42 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 43 | |
| 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(); |
| 50 | var h = m.hint(); |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 51 | // m.hide(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | |
| 53 | // Update input field |
| 54 | var input = h.inputField(); |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 55 | input.insert(this._action).update(); |
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(); |
| 58 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 59 | // show alt |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 60 | h.show(true); |
| 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 |
| 91 | if (this._element !== undefined) |
| 92 | return this._element; |
| 93 | |
| 94 | // Create list item |
| 95 | var li = document.createElement("li"); |
| 96 | |
| 97 | if (this.onclick !== undefined) { |
| 98 | li["onclick"] = this.onclick.bind(this); |
| 99 | }; |
| 100 | |
| 101 | // Create title |
| 102 | var name = document.createElement("span"); |
| 103 | name.appendChild(document.createTextNode(this._name)); |
| 104 | |
| 105 | li.appendChild(name); |
| 106 | |
| 107 | // Create description |
| 108 | if (this._desc !== undefined) { |
| 109 | var desc = document.createElement("span"); |
| 110 | desc.classList.add('desc'); |
| 111 | desc.appendChild(document.createTextNode(this._desc)); |
| 112 | li.appendChild(desc); |
| 113 | }; |
| 114 | return this._element = li; |
| 115 | } |
| 116 | }; |
| 117 | }); |