This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.js
278 lines (244 loc) · 9.18 KB
/
Widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* The base class for all widgets. Handles common things such as NetworkTables and the right click menu
* Import functions to override:
* - render() - creates the DOM for the widget. Append to this.root. this.saveData is an object that gets
* saved and restored to the layout file and can be used to store widget properties
* - update() - called whenever a NT update is recieved. Use this.key and this.val
* - destroy() - handle cleanup actions here
* - createContextMenu(menu) - Add items to the right click menu here
* - createPropertiesView(win) - Add items to the properties page for the widget here. See blank.html
* - _update(key, value) and attachListeners() - Use these to override the default NT listener behavior.
* this.table is the NetworkTable object. All listener functions must also be widget object properties
* otherwise node will garbage collect them.
*/
class Widget extends DraggableElement {
constructor(table, key, saveData) {
super();
this.dom = document.createElement("div");
this.dom.classList.add("widget");
this.dom.classList.add("widget-" + this.constructor.name);
var self = this;
this.root = this.dom;
this.table = typeof table == "string" ? ntcore.getTable(table) : table;
this.key = key;
this.val = null;
this.saveData = saveData || {};
if(!this.saveData.label){
this.saveData.label = "left";
}
this.root.dataset.label = this.saveData.label;
this.editable = false;
this.decorators = [];
this.attachListeners();
this._render();
super._registerDom(this.dom);
this._update(this.key, this.table.get(this.key, null));
}
_createContextMenu() {
var self = this;
var menu = new gui.Menu();
menu.append(new gui.MenuItem({
label: this.constructor.name,
enabled: false
}));
menu.append(new gui.MenuItem({
type: "separator"
}));
menu.append(new gui.MenuItem({
label: "Reset Size",
click: function () {
self.resetSize();
}
}));
menu.append(new gui.MenuItem({
label: "Delete",
click: function () {
self.remove();
}
}));
var morphMenu = new gui.Menu();
var self = this;
for (var widgetType in SmartDashboard.widgetTypes) {
if (!SmartDashboard.widgetTypes.hasOwnProperty(widgetType)) continue;
if(widgetType == this.constructor.name) continue;
if(SmartDashboard.widgetTypes[widgetType].dataType != SmartDashboard.widgetTypes[this.constructor.name].dataType) continue;
(function (widgetType) {
morphMenu.append(new gui.MenuItem({
label: widgetType,
click: function () {
setTimeout(function () {
var pos = self.getPosition();
var widget = new SmartDashboard.widgetTypes[widgetType].widget(self.table.getTablePath(), self.key);
widget.setPosition(pos.x, pos.y, pos.w, pos.h);
SmartDashboard.removeWidget(self);
SmartDashboard.addWidget(widget);
widget.resetSize();
widget.setPosition(pos.x + pos.w / 2 - widget._w / 2, pos.y + pos.h / 2 - widget._h / 2, widget._w, widget._h);
if(self.parent){
self.parent.addChild(widget, true);
}
}, 1);
}
}));
})(widgetType);
}
menu.append(new gui.MenuItem({
label: "Morph",
submenu: morphMenu
}));
menu.append(new gui.MenuItem({
label: "Change Variable Name",
click: function () {
SmartDashboard.prompt("New name:", self.table.getTablePath() + "/" + self.key, function(nameRaw){
if (nameRaw == null || nameRaw == "")
return;
var pos = self.getPosition();
var widget = new SmartDashboard.widgetTypes[self.constructor.name].widget(nameRaw.substring(0, nameRaw.lastIndexOf("/")),
nameRaw.substring(nameRaw.lastIndexOf("/") + 1));
SmartDashboard.removeWidget(self);
widget.setPosition(pos.x, pos.y, pos.w, pos.h);
widget.saveData = self.saveData;
SmartDashboard.addWidget(widget);
if(self.parent){
self.parent.addChild(widget, true);
}
});
}
}));
menu.append(new gui.MenuItem({
type: "separator"
}));
menu.append(new gui.MenuItem({
label: "Label",
enabled: false
}));
menu.append(new gui.MenuItem({
label: "Left",
type: "checkbox",
checked: this.saveData.label == "left",
click: function () {
self.root.dataset.label = self.saveData.label = "left";
}
}));
menu.append(new gui.MenuItem({
label: "Top",
type: "checkbox",
checked: this.saveData.label == "top",
click: function () {
self.root.dataset.label = self.saveData.label = "top";
}
}));
menu.append(new gui.MenuItem({
label: "None",
type: "checkbox",
checked: this.saveData.label == "none",
click: function () {
self.root.dataset.label = self.saveData.label = "none";
}
}));
menu.append(new gui.MenuItem({
type: "separator"
}));
ContextMenu.append(menu, [
{
label: "To Front",
click: this.changeOrder.bind(this, "to front")
},
{
label: "Forward",
click: this.changeOrder.bind(this, "forward")
},
{
label: "Backward",
click: this.changeOrder.bind(this, "backward")
},
{
label: "To Back",
click: this.changeOrder.bind(this, "to back")
}
]);
menu.append(new gui.MenuItem({
type: "separator"
}));
this.createContextMenu(menu);
menu.append(new gui.MenuItem({
type: "separator"
}));
menu.append(new gui.MenuItem({
label: "Properties",
click: this._propertyMenuCallback.bind(this)
}));
return menu;
}
_createPropertiesView(win){
var self = this;
win.addField("Override Robot", "checkbox", self.saveData.clientOverride || false, function(k, v){
self.saveData.clientOverride = v;
});
win.addSeparator();
this.createPropertiesView(win);
}
createPropertiesView(win){
}
createContextMenu(menu) {
}
remove() {
super.remove();
SmartDashboard.removeWidget(this);
}
attachListeners() {
this._notOverridenAttachListeners = true;
// get around the ntcore addon losing context on callbacks
var self = this;
this._mainListener = function (k, v) {
if(self.saveData.clientOverride){
// prevent robot connect from overwriting values
self.table.put(k, self.val);
} else {
self._update(k, v);
}
};
this.table.onChange(this.key, this._mainListener);
}
_update(key, val) {
if(val == null) return;
this.val = val;
this.update();
}
update() {
this.root.textContent = this.key + ": " + this.val;
}
_render() {
this.dom.innerHTML = "";
for (var decorator of this.decorators) {
decorator.decorate(this);
}
this._dom_id = this.constructor.name + "-" + uniqueId();
var label = document.createElement("label");
label.innerHTML = this.key + ": ";
label.setAttribute("for", this._dom_id);
this.root.appendChild(label);
this.render();
var inputs = this.root.querySelectorAll("input[type=text],input[type=number],textarea");
for(var i = 0; i < inputs.length; i++){
inputs[i].addEventListener("focus", function(e){
DomUtils.showUpdateButton(e.target);
});
inputs[i].addEventListener("blur", function(){
DomUtils.hideUpdateButton();
});
}
}
render() {
this.root.textContent = this.key + ": " + this.val;
}
destroy() {
}
_destroy(){
if(this._notOverridenAttachListeners) {
this.table.offChange(this.key, this._mainListener);
}
this.destroy();
}
}
global.Widget = Widget;