blob: 5c9a9a7de8d39a3a8bd7e284b1571fa89004ed14 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Hint menu item based on MenuItem
3 */
Akrone51eaa32020-11-10 09:35:53 +01004"use strict";
5
Akron0b489ad2018-02-02 16:49:32 +01006define(['menu/item', 'util'], function (itemClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +00007 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +00008
9 /**
10 * Create new menu item object.
11 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 create : function (params) {
13 return Object.create(itemClass)
Akron65c74352016-09-02 17:23:39 +020014 .upgradeTo(this)
15 ._init(params);
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 },
Nils Diewald47f366b2015-04-15 20:06:35 +000017
Nils Diewald7148c6f2015-05-04 15:07:53 +000018 // Initialize menu item object
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 _init : function (params) {
20 if (params[0] === undefined ||
Akron65c74352016-09-02 17:23:39 +020021 params[1] === undefined)
22 throw new Error("Missing parameters");
Nils Diewald0e6992a2015-04-14 20:13:52 +000023
24 this._name = params[0];
25 this._action = params[1];
26 this._lcField = ' ' + this._name.toLowerCase();
27
28 if (params.length > 2) {
Akron65c74352016-09-02 17:23:39 +020029 this._desc = params[2];
30 this._lcField += " " + this._desc.toLowerCase();
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 };
32
33 return this;
34 },
35
Nils Diewald7148c6f2015-05-04 15:07:53 +000036 /**
37 * Get or set the content of the item.
38 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 content : function (content) {
40 if (arguments.length === 1) {
Akron65c74352016-09-02 17:23:39 +020041 this._content = content;
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 };
43 return this._content;
44 },
Akron52ed22d2018-07-11 17:05:19 +020045
Nils Diewald7148c6f2015-05-04 15:07:53 +000046 /**
47 * Override the click action
48 * of the menu item.
49 */
Nils Diewald47f366b2015-04-15 20:06:35 +000050 onclick : function (e) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000051 var m = this.menu();
Akron02360e42016-06-07 13:41:12 +020052 // m.hide();
Nils Diewald0e6992a2015-04-14 20:13:52 +000053
Akron95abaf42018-04-26 15:33:22 +020054 // Reset prefix
55 m.prefix("");
56
Akron52ed22d2018-07-11 17:05:19 +020057 var h = m.hint();
58
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 // Update input field
60 var input = h.inputField();
Akron308db382016-05-30 22:34:07 +020061 input.insert(this._action).update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000062
Nils Diewald47f366b2015-04-15 20:06:35 +000063 e.halt();
Akron95abaf42018-04-26 15:33:22 +020064
Akron02360e42016-06-07 13:41:12 +020065 // show alt
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 h.show(true);
67 },
Nils Diewald47f366b2015-04-15 20:06:35 +000068
Nils Diewald7148c6f2015-05-04 15:07:53 +000069 /**
70 * The name of the menu entry.
71 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 name : function () {
73 return this._name;
74 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000075
76 /**
77 * The action (the string inserted on click)
78 * of the menu item.
79 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000080 action : function () {
81 return this._action;
82 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000083
84 /**
85 * The description of the menu item.
86 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 desc : function () {
88 return this._desc;
89 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000090
91 /**
92 * The associated dom element of the
93 * menu item.
94 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 element : function () {
96 // already defined
Akron24aa0052020-11-10 11:00:34 +010097 if (this._el !== undefined)
98 return this._el;
Nils Diewald0e6992a2015-04-14 20:13:52 +000099
100 // Create list item
101 var li = document.createElement("li");
102
103 if (this.onclick !== undefined) {
Akron65c74352016-09-02 17:23:39 +0200104 li["onclick"] = this.onclick.bind(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000105 };
106
107 // Create title
108 var name = document.createElement("span");
Akron0b489ad2018-02-02 16:49:32 +0100109 name.addT(this._name);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000110
111 li.appendChild(name);
112
113 // Create description
114 if (this._desc !== undefined) {
Akron65c74352016-09-02 17:23:39 +0200115 var desc = document.createElement("span");
116 desc.classList.add('desc');
Akron0b489ad2018-02-02 16:49:32 +0100117 desc.addT(this._desc);
Akron65c74352016-09-02 17:23:39 +0200118 li.appendChild(desc);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000119 };
Akron24aa0052020-11-10 11:00:34 +0100120 return this._el = li;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000121 }
122 };
123});