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