blob: c05fbad6342fe0dc1efb4926833affc4fd7e8957 [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
4 var loc = KorAP.Locale;
5
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)
17 .upgradeTo(this)
18 ._init(params);
19 },
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)
24 throw new Error("Missing parameters");
25
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(
Nils Diewald4c221252015-04-21 20:19:25 +000043 this._key,
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000044 this._type
45 );
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)
79 return this._element;
80
81 // Create list item
82 var li = document.createElement("li");
83 li.setAttribute("data-type", this._type);
Nils Diewald4c221252015-04-21 20:19:25 +000084 li.setAttribute("data-key", this._key);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000085
86 // Connect action
87 li["onclick"] = this.onclick.bind(this);
88
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 li.appendChild(document.createTextNode(this._name));
90 return this._element = li;
91 }
92 };
93});