blob: 80ac78b08cbec9858845d079b602fa213e5fbee1 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Hint menu item based on MenuItem
3 */
Akron0b489ad2018-02-02 16:49:32 +01004define(['menu/item', 'util'], function (itemClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +00005 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)
Akron65c74352016-09-02 17:23:39 +020012 .upgradeTo(this)
13 ._init(params);
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 },
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 ||
Akron65c74352016-09-02 17:23:39 +020019 params[1] === undefined)
20 throw new Error("Missing parameters");
Nils Diewald0e6992a2015-04-14 20:13:52 +000021
22 this._name = params[0];
23 this._action = params[1];
24 this._lcField = ' ' + this._name.toLowerCase();
25
26 if (params.length > 2) {
Akron65c74352016-09-02 17:23:39 +020027 this._desc = params[2];
28 this._lcField += " " + this._desc.toLowerCase();
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 };
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) {
Akron65c74352016-09-02 17:23:39 +020039 this._content = content;
Nils Diewald0e6992a2015-04-14 20:13:52 +000040 };
41 return this._content;
42 },
Akron52ed22d2018-07-11 17:05:19 +020043
Nils Diewald7148c6f2015-05-04 15:07:53 +000044 /**
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();
Akron02360e42016-06-07 13:41:12 +020050 // m.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000051
Akron95abaf42018-04-26 15:33:22 +020052 // Reset prefix
53 m.prefix("");
54
Akron52ed22d2018-07-11 17:05:19 +020055 var h = m.hint();
56
Nils Diewald0e6992a2015-04-14 20:13:52 +000057 // Update input field
58 var input = h.inputField();
Akron308db382016-05-30 22:34:07 +020059 input.insert(this._action).update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000060
Nils Diewald47f366b2015-04-15 20:06:35 +000061 e.halt();
Akron95abaf42018-04-26 15:33:22 +020062
Akron02360e42016-06-07 13:41:12 +020063 // show alt
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 h.show(true);
65 },
Nils Diewald47f366b2015-04-15 20:06:35 +000066
Nils Diewald7148c6f2015-05-04 15:07:53 +000067 /**
68 * The name of the menu entry.
69 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 name : function () {
71 return this._name;
72 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000073
74 /**
75 * The action (the string inserted on click)
76 * of the menu item.
77 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 action : function () {
79 return this._action;
80 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000081
82 /**
83 * The description of the menu item.
84 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 desc : function () {
86 return this._desc;
87 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000088
89 /**
90 * The associated dom element of the
91 * menu item.
92 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 element : function () {
94 // already defined
95 if (this._element !== undefined)
Akron65c74352016-09-02 17:23:39 +020096 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +000097
98 // Create list item
99 var li = document.createElement("li");
100
101 if (this.onclick !== undefined) {
Akron65c74352016-09-02 17:23:39 +0200102 li["onclick"] = this.onclick.bind(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000103 };
104
105 // Create title
106 var name = document.createElement("span");
Akron0b489ad2018-02-02 16:49:32 +0100107 name.addT(this._name);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108
109 li.appendChild(name);
110
111 // Create description
112 if (this._desc !== undefined) {
Akron65c74352016-09-02 17:23:39 +0200113 var desc = document.createElement("span");
114 desc.classList.add('desc');
Akron0b489ad2018-02-02 16:49:32 +0100115 desc.addT(this._desc);
Akron65c74352016-09-02 17:23:39 +0200116 li.appendChild(desc);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117 };
118 return this._element = li;
119 }
120 };
121});