blob: 59589d8039f2292c40f62efa6936a16c125d9a7f [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001// Field menu item
Akron88d237e2020-10-21 08:05:18 +02002"use strict";
3
Nils Diewald4c221252015-04-21 20:19:25 +00004define(['menu/item', 'util'], function (itemClass) {
5
Akron0b489ad2018-02-02 16:49:32 +01006 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +00007
Nils Diewald0e6992a2015-04-14 20:13:52 +00008 return {
Nils Diewald4c221252015-04-21 20:19:25 +00009
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 Diewald0e6992a2015-04-14 20:13:52 +000017 create : function (params) {
18 return Object.create(itemClass)
Akron0b489ad2018-02-02 16:49:32 +010019 .upgradeTo(this)
20 ._init(params);
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 },
22
Akron88d237e2020-10-21 08:05:18 +020023
Nils Diewald4c221252015-04-21 20:19:25 +000024 // Initialize item object
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 _init : function (params) {
Akron88d237e2020-10-21 08:05:18 +020026 const t = this;
27
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 if (params[0] === undefined)
Akron0b489ad2018-02-02 16:49:32 +010029 throw new Error("Missing parameters");
Nils Diewald0e6992a2015-04-14 20:13:52 +000030
Akron88d237e2020-10-21 08:05:18 +020031 t._key = params[0];
32 t._type = params[1];
Nils Diewald4c221252015-04-21 20:19:25 +000033
Akron88d237e2020-10-21 08:05:18 +020034 const k = t._key;
35 t._name = loc["VC_" + k] ? loc["VC_" + k] : k;
Nils Diewald0e6992a2015-04-14 20:13:52 +000036
Akron88d237e2020-10-21 08:05:18 +020037 t._lcField = ' ' + t._name.toLowerCase();
Nils Diewald0e6992a2015-04-14 20:13:52 +000038
Akron88d237e2020-10-21 08:05:18 +020039 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000040 },
41
Akron88d237e2020-10-21 08:05:18 +020042
Nils Diewald4c221252015-04-21 20:19:25 +000043 /**
44 * Override click event by passing all clicks
45 * to the menu object.
46 */
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000047 onclick : function (e) {
48 this.menu().release(
Akron0b489ad2018-02-02 16:49:32 +010049 this._key,
50 this._type
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000051 );
52 e.halt();
53 },
54
Akron88d237e2020-10-21 08:05:18 +020055
Nils Diewald4c221252015-04-21 20:19:25 +000056 /**
57 * Get the name of the item.
58 * This is a potential localized version
59 * of the value.
60 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 name : function () {
62 return this._name;
63 },
64
Akron88d237e2020-10-21 08:05:18 +020065
Nils Diewald4c221252015-04-21 20:19:25 +000066 /**
67 * Get the type of the item.
68 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 type : function () {
70 return this._type;
71 },
72
Akron88d237e2020-10-21 08:05:18 +020073
Nils Diewald4c221252015-04-21 20:19:25 +000074 /**
75 * Get the key of the item.
76 */
77 key : function () {
78 return this._key;
79 },
80
Akron88d237e2020-10-21 08:05:18 +020081
Nils Diewald4c221252015-04-21 20:19:25 +000082 /**
83 * Get the HTML element associated with the item.
84 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 element : function () {
Akron88d237e2020-10-21 08:05:18 +020086 const t = this;
Nils Diewald4c221252015-04-21 20:19:25 +000087
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 // already defined
Akron88d237e2020-10-21 08:05:18 +020089 if (t._element !== undefined)
90 return t._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +000091
92 // Create list item
93 var li = document.createElement("li");
Akron88d237e2020-10-21 08:05:18 +020094 if (t._type)
95 li.setAttribute("data-type", t._type);
96
97 li.setAttribute("data-key", t._key);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000098
99 // Connect action
Akron88d237e2020-10-21 08:05:18 +0200100 li["onclick"] = t.onclick.bind(t);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000101
Akron88d237e2020-10-21 08:05:18 +0200102 li.addT(t._name);
103 return t._element = li;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000104 }
105 };
106});