blob: 039764cd2af73a437bfc5535c1189eb3b3da96f4 [file] [log] [blame]
Akron88d237e2020-10-21 08:05:18 +02001"use strict";
2
Akron48b1e4d2015-06-17 18:47:01 +02003define(['menu/item', 'util'], function (itemClass) {
4
Akron88d237e2020-10-21 08:05:18 +02005 const loc = KorAP.Locale;
Akron48b1e4d2015-06-17 18:47:01 +02006
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)
Akrone4961b12017-05-10 21:04:46 +020018 .upgradeTo(this)
19 ._init(params);
Akron48b1e4d2015-06-17 18:47:01 +020020 },
21
Akron88d237e2020-10-21 08:05:18 +020022
Akron48b1e4d2015-06-17 18:47:01 +020023 // Initialize item object
24 _init : function (params) {
Akron88d237e2020-10-21 08:05:18 +020025 const t = this;
26
Akron48b1e4d2015-06-17 18:47:01 +020027 if (params[0] === undefined)
Akrone4961b12017-05-10 21:04:46 +020028 throw new Error("Missing parameters");
Akron48b1e4d2015-06-17 18:47:01 +020029
Akron88d237e2020-10-21 08:05:18 +020030 t._id = params[0];
31 t._name = params[1];
32 t._desc = params[2];
Akron48b1e4d2015-06-17 18:47:01 +020033
Akron88d237e2020-10-21 08:05:18 +020034 t._lcField = ' ' + t._name.toLowerCase();
35 t._lcField += ' ' + t._desc.toLowerCase();
Akron48b1e4d2015-06-17 18:47:01 +020036
37 return this;
38 },
39
Akron88d237e2020-10-21 08:05:18 +020040
Akron48b1e4d2015-06-17 18:47:01 +020041 /**
42 * Override click event by passing all clicks
43 * to the menu object.
44 */
45 onclick : function (e) {
46 this.menu().release(
Akrone4961b12017-05-10 21:04:46 +020047 this._id,
48 this._name
Akron48b1e4d2015-06-17 18:47:01 +020049 );
50 e.halt();
51 },
52
Akron88d237e2020-10-21 08:05:18 +020053
Akron48b1e4d2015-06-17 18:47:01 +020054 /**
55 * Get the name of the item.
56 */
57 name : function () {
58 return this._name;
59 },
60
Akron88d237e2020-10-21 08:05:18 +020061
Akron48b1e4d2015-06-17 18:47:01 +020062 /**
63 * Get the identifier of the item.
64 */
65 id : function () {
66 return this._id;
67 },
68
Akron88d237e2020-10-21 08:05:18 +020069
Akron48b1e4d2015-06-17 18:47:01 +020070 /**
71 * Get the description of the item.
72 */
73 desc : function () {
74 return this._desc;
75 },
76
Akron88d237e2020-10-21 08:05:18 +020077
Akron48b1e4d2015-06-17 18:47:01 +020078 /**
79 * Get the HTML element associated with the item.
80 */
81 element : function () {
Akron88d237e2020-10-21 08:05:18 +020082 const t = this;
Akron48b1e4d2015-06-17 18:47:01 +020083
84 // already defined
Akron88d237e2020-10-21 08:05:18 +020085 if (t._element !== undefined)
86 return t._element;
Akron48b1e4d2015-06-17 18:47:01 +020087
88 // Create list item
Akron88d237e2020-10-21 08:05:18 +020089 const li = document.createElement("li");
90 li.setAttribute("data-type", t._type);
91 li.setAttribute("data-key", t._key);
Akron48b1e4d2015-06-17 18:47:01 +020092
93 // Connect action
Akron88d237e2020-10-21 08:05:18 +020094 li["onclick"] = t.onclick.bind(t);
Akron48b1e4d2015-06-17 18:47:01 +020095
Akron88d237e2020-10-21 08:05:18 +020096 li.addT(t._name);
97 return t._element = li;
Akron48b1e4d2015-06-17 18:47:01 +020098 }
99 }
100});