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