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(); |
| 51 | m.hide(); |
| 52 | |
| 53 | // Update input field |
| 54 | var input = h.inputField(); |
| 55 | input.insert(this._action); |
| 56 | input.update(); |
| 57 | |
| 58 | h.active = false; |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 59 | |
| 60 | e.halt(); |
| 61 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | h.show(true); |
| 63 | }, |
Nils Diewald | 47f366b | 2015-04-15 20:06:35 +0000 | [diff] [blame] | 64 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 65 | /** |
| 66 | * The name of the menu entry. |
| 67 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 68 | name : function () { |
| 69 | return this._name; |
| 70 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * The action (the string inserted on click) |
| 74 | * of the menu item. |
| 75 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 76 | action : function () { |
| 77 | return this._action; |
| 78 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * The description of the menu item. |
| 82 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | desc : function () { |
| 84 | return this._desc; |
| 85 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * The associated dom element of the |
| 89 | * menu item. |
| 90 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | element : function () { |
| 92 | // already defined |
| 93 | if (this._element !== undefined) |
| 94 | return this._element; |
| 95 | |
| 96 | // Create list item |
| 97 | var li = document.createElement("li"); |
| 98 | |
| 99 | if (this.onclick !== undefined) { |
| 100 | li["onclick"] = this.onclick.bind(this); |
| 101 | }; |
| 102 | |
| 103 | // Create title |
| 104 | var name = document.createElement("span"); |
| 105 | name.appendChild(document.createTextNode(this._name)); |
| 106 | |
| 107 | li.appendChild(name); |
| 108 | |
| 109 | // Create description |
| 110 | if (this._desc !== undefined) { |
| 111 | var desc = document.createElement("span"); |
| 112 | desc.classList.add('desc'); |
| 113 | desc.appendChild(document.createTextNode(this._desc)); |
| 114 | li.appendChild(desc); |
| 115 | }; |
| 116 | return this._element = li; |
| 117 | } |
| 118 | }; |
| 119 | }); |