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