blob: b9a1bc8b71be7593d9f43fbd2cc3a960adadfac6 [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();
55 input.insert(this._action);
56 input.update();
57
58 h.active = false;
Nils Diewald47f366b2015-04-15 20:06:35 +000059
60 e.halt();
61
Nils Diewald0e6992a2015-04-14 20:13:52 +000062 h.show(true);
63 },
Nils Diewald47f366b2015-04-15 20:06:35 +000064
Nils Diewald7148c6f2015-05-04 15:07:53 +000065 /**
66 * The name of the menu entry.
67 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 name : function () {
69 return this._name;
70 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000071
72 /**
73 * The action (the string inserted on click)
74 * of the menu item.
75 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 action : function () {
77 return this._action;
78 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000079
80 /**
81 * The description of the menu item.
82 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 desc : function () {
84 return this._desc;
85 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000086
87 /**
88 * The associated dom element of the
89 * menu item.
90 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 element : function () {
92 // already defined
93 if (this._element !== undefined)
94 return this._element;
95
96 // Create list item
97 var li = document.createElement("li");
98
99 if (this.onclick !== undefined) {
100 li["onclick"] = this.onclick.bind(this);
101 };
102
103 // Create title
104 var name = document.createElement("span");
105 name.appendChild(document.createTextNode(this._name));
106
107 li.appendChild(name);
108
109 // Create description
110 if (this._desc !== undefined) {
111 var desc = document.createElement("span");
112 desc.classList.add('desc');
113 desc.appendChild(document.createTextNode(this._desc));
114 li.appendChild(desc);
115 };
116 return this._element = li;
117 }
118 };
119});