Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | // Field menu item |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 2 | define(['menu/item', 'util'], function (itemClass) { |
| 3 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 4 | const loc = KorAP.Locale; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 5 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 6 | return { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * Create new menu item. |
| 10 | * Pass two parameters: value and type. |
| 11 | * the value may be localized by a name in |
| 12 | * KorAP.Locale with the prefix 'VC_', |
| 13 | * e.g. 'VC_subTitle'. |
| 14 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | create : function (params) { |
| 16 | return Object.create(itemClass) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 17 | .upgradeTo(this) |
| 18 | ._init(params); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | }, |
| 20 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 21 | // Initialize item object |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 22 | _init : function (params) { |
| 23 | if (params[0] === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 24 | throw new Error("Missing parameters"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 25 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 26 | this._key = params[0]; |
| 27 | this._type = params[1]; |
| 28 | |
| 29 | var k = this._key; |
| 30 | this._name = loc["VC_" + k] ? loc["VC_" + k] : k; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 31 | |
| 32 | this._lcField = ' ' + this._name.toLowerCase(); |
| 33 | |
| 34 | return this; |
| 35 | }, |
| 36 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Override click event by passing all clicks |
| 39 | * to the menu object. |
| 40 | */ |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 41 | onclick : function (e) { |
| 42 | this.menu().release( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 43 | this._key, |
| 44 | this._type |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 45 | ); |
| 46 | e.halt(); |
| 47 | }, |
| 48 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Get the name of the item. |
| 51 | * This is a potential localized version |
| 52 | * of the value. |
| 53 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | name : function () { |
| 55 | return this._name; |
| 56 | }, |
| 57 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Get the type of the item. |
| 60 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 61 | type : function () { |
| 62 | return this._type; |
| 63 | }, |
| 64 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Get the key of the item. |
| 67 | */ |
| 68 | key : function () { |
| 69 | return this._key; |
| 70 | }, |
| 71 | |
| 72 | /** |
| 73 | * Get the HTML element associated with the item. |
| 74 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 75 | element : function () { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 76 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 77 | // already defined |
| 78 | if (this._element !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 79 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 80 | |
| 81 | // Create list item |
| 82 | var li = document.createElement("li"); |
| 83 | li.setAttribute("data-type", this._type); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 84 | li.setAttribute("data-key", this._key); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 85 | |
| 86 | // Connect action |
| 87 | li["onclick"] = this.onclick.bind(this); |
| 88 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 89 | li.addT(this._name); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 90 | return this._element = li; |
| 91 | } |
| 92 | }; |
| 93 | }); |