blob: f9267e8bff262fa744585b1dd7a87e7e3a24fd82 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001// Field menu item
Nils Diewald4c221252015-04-21 20:19:25 +00002define(['menu/item', 'util'], function (itemClass) {
3
Akron0b489ad2018-02-02 16:49:32 +01004 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +00005
Nils Diewald0e6992a2015-04-14 20:13:52 +00006 return {
Nils Diewald4c221252015-04-21 20:19:25 +00007
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 Diewald0e6992a2015-04-14 20:13:52 +000015 create : function (params) {
16 return Object.create(itemClass)
Akron0b489ad2018-02-02 16:49:32 +010017 .upgradeTo(this)
18 ._init(params);
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 },
20
Nils Diewald4c221252015-04-21 20:19:25 +000021 // Initialize item object
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 _init : function (params) {
23 if (params[0] === undefined)
Akron0b489ad2018-02-02 16:49:32 +010024 throw new Error("Missing parameters");
Nils Diewald0e6992a2015-04-14 20:13:52 +000025
Nils Diewald4c221252015-04-21 20:19:25 +000026 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 Diewald0e6992a2015-04-14 20:13:52 +000031
32 this._lcField = ' ' + this._name.toLowerCase();
33
34 return this;
35 },
36
Nils Diewald4c221252015-04-21 20:19:25 +000037 /**
38 * Override click event by passing all clicks
39 * to the menu object.
40 */
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000041 onclick : function (e) {
42 this.menu().release(
Akron0b489ad2018-02-02 16:49:32 +010043 this._key,
44 this._type
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000045 );
46 e.halt();
47 },
48
Nils Diewald4c221252015-04-21 20:19:25 +000049 /**
50 * Get the name of the item.
51 * This is a potential localized version
52 * of the value.
53 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 name : function () {
55 return this._name;
56 },
57
Nils Diewald4c221252015-04-21 20:19:25 +000058 /**
59 * Get the type of the item.
60 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 type : function () {
62 return this._type;
63 },
64
Nils Diewald4c221252015-04-21 20:19:25 +000065 /**
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 Diewald0e6992a2015-04-14 20:13:52 +000075 element : function () {
Nils Diewald4c221252015-04-21 20:19:25 +000076
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 // already defined
78 if (this._element !== undefined)
Akron0b489ad2018-02-02 16:49:32 +010079 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
81 // Create list item
82 var li = document.createElement("li");
Akrone65a88a2018-04-05 19:14:20 +020083 if (this._type)
84 li.setAttribute("data-type", this._type);
Nils Diewald4c221252015-04-21 20:19:25 +000085 li.setAttribute("data-key", this._key);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000086
87 // Connect action
88 li["onclick"] = this.onclick.bind(this);
89
Akron0b489ad2018-02-02 16:49:32 +010090 li.addT(this._name);
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 return this._element = li;
92 }
93 };
94});