blob: 317dae789752e82a19060fbf844c419c54e7fc93 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Hint menu item based on MenuItem
3 */
4define(['menu/item'], function (itemClass) {
5 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +00006
7 /**
8 * Create new menu item object.
9 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000010 create : function (params) {
11 return Object.create(itemClass)
12 .upgradeTo(this)
13 ._init(params);
14 },
Nils Diewald47f366b2015-04-15 20:06:35 +000015
Nils Diewald7148c6f2015-05-04 15:07:53 +000016 // Initialize menu item object
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 _init : function (params) {
18 if (params[0] === undefined ||
19 params[1] === undefined)
20 throw new Error("Missing parameters");
21
22 this._name = params[0];
23 this._action = params[1];
24 this._lcField = ' ' + this._name.toLowerCase();
25
26 if (params.length > 2) {
27 this._desc = params[2];
28 this._lcField += " " + this._desc.toLowerCase();
29 };
30
31 return this;
32 },
33
Nils Diewald7148c6f2015-05-04 15:07:53 +000034 /**
35 * Get or set the content of the item.
36 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 content : function (content) {
38 if (arguments.length === 1) {
39 this._content = content;
40 };
41 return this._content;
42 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000043
44 /**
45 * Override the click action
46 * of the menu item.
47 */
Nils Diewald47f366b2015-04-15 20:06:35 +000048 onclick : function (e) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 var m = this.menu();
50 var h = m.hint();
51 m.hide();
52
53 // Update input field
54 var input = h.inputField();
Akron308db382016-05-30 22:34:07 +020055 input.insert(this._action).update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000056
Nils Diewald47f366b2015-04-15 20:06:35 +000057 e.halt();
58
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 h.show(true);
60 },
Nils Diewald47f366b2015-04-15 20:06:35 +000061
Nils Diewald7148c6f2015-05-04 15:07:53 +000062 /**
63 * The name of the menu entry.
64 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000065 name : function () {
66 return this._name;
67 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000068
69 /**
70 * The action (the string inserted on click)
71 * of the menu item.
72 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 action : function () {
74 return this._action;
75 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000076
77 /**
78 * The description of the menu item.
79 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000080 desc : function () {
81 return this._desc;
82 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000083
84 /**
85 * The associated dom element of the
86 * menu item.
87 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 element : function () {
89 // already defined
90 if (this._element !== undefined)
91 return this._element;
92
93 // Create list item
94 var li = document.createElement("li");
95
96 if (this.onclick !== undefined) {
97 li["onclick"] = this.onclick.bind(this);
98 };
99
100 // Create title
101 var name = document.createElement("span");
102 name.appendChild(document.createTextNode(this._name));
103
104 li.appendChild(name);
105
106 // Create description
107 if (this._desc !== undefined) {
108 var desc = document.createElement("span");
109 desc.classList.add('desc');
110 desc.appendChild(document.createTextNode(this._desc));
111 li.appendChild(desc);
112 };
113 return this._element = li;
114 }
115 };
116});