Skip to content

Commit 7670771

Browse files
committed
feat: improve this-binding #3
- Address remaining instances of xx = this;
1 parent 5da9f05 commit 7670771

File tree

6 files changed

+55
-70
lines changed

6 files changed

+55
-70
lines changed

apps/editor/src/Editor.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,14 @@ export default class Editor {
150150
this.root
151151
.querySelector<HTMLDivElement>(".header .tools-left")!
152152
.appendChild(this.meter);
153-
var self = this;
154-
155153
setInterval(() => {
156154
this.meter!.querySelector<HTMLDivElement>(
157155
".cpuload .fgload",
158-
)!.style.width = 2 * self.graph.execution_time * 90 + "px";
159-
if (self.graph.status == LGraphStatus.STATUS_RUNNING) {
156+
)!.style.width = 2 * this.graph.execution_time * 90 + "px";
157+
if (this.graph.status == LGraphStatus.STATUS_RUNNING) {
160158
this.meter!.querySelector<HTMLDivElement>(
161159
".gpuload .fgload",
162-
)!.style.width = self.graphCanvas.render_time * 10 * 90 + "px";
160+
)!.style.width = this.graphCanvas.render_time * 10 * 90 + "px";
163161
} else {
164162
this.meter!.querySelector<HTMLDivElement>(
165163
".gpuload .fgload",
@@ -265,9 +263,8 @@ export default class Editor {
265263
throw "Fullscreen not supported";
266264
}
267265

268-
var self = this;
269-
setTimeout(function () {
270-
self.graphCanvas.resize();
266+
setTimeout(() => {
267+
this.graphCanvas.resize();
271268
}, 100);
272269
}
273270

packages/core/src/ContextMenu.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export default class ContextMenu {
387387
//menu option clicked
388388
const inner_onclick = (_e: MouseEvent) => {
389389
let index = parseInt(element.dataset["value"]);
390-
var value = ctxMenu.values[index];
390+
var value = this.values[index];
391391
if (LiteGraph.debug)
392392
console.debug("ContextMenu inner_onclick", index, value);
393393

@@ -466,8 +466,6 @@ export default class ContextMenu {
466466
LiteGraph.pointerListenerAdd(element, "enter", inner_over);
467467
}
468468

469-
let ctxMenu = this;
470-
471469
function inner_over(e: MouseEvent) {
472470
var value = this.value;
473471
if (!value || !value.has_submenu) {
@@ -527,7 +525,6 @@ export default class ContextMenu {
527525
}
528526

529527
protected setupComboFilter() {
530-
const menuThis = this;
531528
const { values, options, root: $root } = this;
532529
const is_long_combo =
533530
this.options.className?.includes("combo-menu") &&
@@ -649,7 +646,7 @@ export default class ContextMenu {
649646
selectedItem?.click();
650647
break;
651648
case "Escape":
652-
menuThis.close();
649+
this.close();
653650
break;
654651
}
655652
});

packages/core/src/LGraphCanvas.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,20 @@ export default class LGraphCanvas
20302030
var width = node.size[0];
20312031
var ref_window = this.getCanvasWindow();
20322032

2033+
const inner_value_change = (widget: IWidget, value: any) => {
2034+
widget.value = value;
2035+
if (
2036+
widget.options &&
2037+
widget.options.property &&
2038+
node.properties[widget.options.property] !== undefined
2039+
) {
2040+
node.setProperty(widget.options.property, value);
2041+
}
2042+
if (widget.callback) {
2043+
widget.callback(widget.value, this, node, pos, event);
2044+
}
2045+
};
2046+
20332047
for (var i = 0; i < node.widgets.length; ++i) {
20342048
var w = node.widgets[i];
20352049
if (!w || w.disabled) continue;
@@ -2263,21 +2277,6 @@ export default class LGraphCanvas
22632277
return w;
22642278
} //end for
22652279

2266-
const thisCanvas = this;
2267-
function inner_value_change(widget: IWidget, value: any) {
2268-
widget.value = value;
2269-
if (
2270-
widget.options &&
2271-
widget.options.property &&
2272-
node.properties[widget.options.property] !== undefined
2273-
) {
2274-
node.setProperty(widget.options.property, value);
2275-
}
2276-
if (widget.callback) {
2277-
widget.callback(widget.value, thisCanvas, node, pos, event);
2278-
}
2279-
}
2280-
22812280
return null;
22822281
}
22832282

@@ -2341,27 +2340,26 @@ export default class LGraphCanvas
23412340
return;
23422341
}
23432342

2344-
var self = this;
23452343
var delta = this.live_mode ? 1.1 : 0.9;
23462344
if (this.live_mode) {
23472345
this.live_mode = false;
23482346
this.editor_alpha = 0.1;
23492347
}
23502348

2351-
var t = setInterval(function () {
2352-
self.editor_alpha *= delta;
2353-
self.dirty_canvas = true;
2354-
self.dirty_bgcanvas = true;
2349+
var t = setInterval(() => {
2350+
this.editor_alpha *= delta;
2351+
this.dirty_canvas = true;
2352+
this.dirty_bgcanvas = true;
23552353

2356-
if (delta < 1 && self.editor_alpha < 0.01) {
2354+
if (delta < 1 && this.editor_alpha < 0.01) {
23572355
clearInterval(t);
23582356
if (delta < 1) {
2359-
self.live_mode = true;
2357+
this.live_mode = true;
23602358
}
23612359
}
2362-
if (delta > 1 && self.editor_alpha > 0.99) {
2360+
if (delta > 1 && this.editor_alpha > 0.99) {
23632361
clearInterval(t);
2364-
self.editor_alpha = 1;
2362+
this.editor_alpha = 1;
23652363
}
23662364
}, 1);
23672365
}

packages/core/src/LGraphCanvas_UI.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,24 +2148,23 @@ export default class LGraphCanvas_UI {
21482148
// this.SELECTED_NODE = node;
21492149
this.closePanels();
21502150
var ref_window = this.getCanvasWindow();
2151-
var graphcanvas = this;
21522151
var panel = this.createPanel(node.title || "", {
21532152
closable: true,
21542153
window: ref_window,
21552154
onOpen: function () {
21562155
// graphcanvas.NODEPANEL_IS_OPEN = true;
21572156
},
2158-
onClose: function () {
2157+
onClose: () => {
21592158
// graphcanvas.NODEPANEL_IS_OPEN = false;
2160-
graphcanvas.node_panel = null;
2159+
this.node_panel = null;
21612160
},
21622161
}) as INodePanel;
2163-
graphcanvas.node_panel = panel;
2162+
this.node_panel = panel;
21642163
panel.id = "node-panel";
21652164
panel.node = node;
21662165
panel.classList.add("settings");
21672166

2168-
function inner_refresh() {
2167+
const inner_refresh = () => {
21692168
panel.content.innerHTML = ""; //clear
21702169
panel.addHTML(
21712170
"<span class='node_type'>" +
@@ -2177,8 +2176,8 @@ export default class LGraphCanvas_UI {
21772176

21782177
panel.addHTML("<h3>Properties</h3>");
21792178

2180-
var fUpdate = function (name: string, value: any) {
2181-
graphcanvas.graph.beforeChange(node);
2179+
var fUpdate = (name: string, value: any) => {
2180+
this.graph.beforeChange(node);
21822181
switch (name) {
21832182
case "Title":
21842183
node.title = value;
@@ -2206,8 +2205,8 @@ export default class LGraphCanvas_UI {
22062205
node.setProperty(name, value);
22072206
break;
22082207
}
2209-
graphcanvas.graph.afterChange();
2210-
graphcanvas.dirty_canvas = true;
2208+
this.graph.afterChange();
2209+
this.dirty_canvas = true;
22112210
};
22122211

22132212
panel.addWidget("string", "Title", node.title, {}, fUpdate);
@@ -2270,9 +2269,9 @@ export default class LGraphCanvas_UI {
22702269
panel.close();
22712270
})
22722271
.classList.add("delete");
2273-
}
2272+
};
22742273

2275-
panel.inner_showCodePad = function (propname: string): void {
2274+
panel.inner_showCodePad = (propname: string): void => {
22762275
panel.classList.remove("settings");
22772276
panel.classList.add("centered");
22782277

@@ -2289,7 +2288,7 @@ export default class LGraphCanvas_UI {
22892288
{*/
22902289
panel.alt_content.innerHTML = "<textarea class='code'></textarea>";
22912290
var textarea = panel.alt_content.querySelector("textarea");
2292-
var fDoneWith = function () {
2291+
var fDoneWith = () => {
22932292
panel.toggleAltContent(false); //if(node_prop_div) node_prop_div.style.display = "block"; // panel.close();
22942293
panel.toggleFooterVisibility(true);
22952294
textarea.parentNode.removeChild(textarea);
@@ -2298,7 +2297,7 @@ export default class LGraphCanvas_UI {
22982297
inner_refresh();
22992298
};
23002299
textarea.value = node.properties[propname];
2301-
textarea.addEventListener("keydown", function (e) {
2300+
textarea.addEventListener("keydown", (e) => {
23022301
if (e.code == "Enter" && e.ctrlKey) {
23032302
node.setProperty(propname, textarea.value);
23042303
fDoneWith();
@@ -2308,7 +2307,7 @@ export default class LGraphCanvas_UI {
23082307
panel.toggleFooterVisibility(false);
23092308
textarea.style.height = "calc(100% - 40px)";
23102309
/*}*/
2311-
var assign = panel.addButton("Assign", function () {
2310+
var assign = panel.addButton("Assign", () => {
23122311
node.setProperty(propname, textarea.value);
23132312
fDoneWith();
23142313
});
@@ -3851,7 +3850,6 @@ export default class LGraphCanvas_UI {
38513850
}
38523851
let parentNode = this.parentNode as HTMLElement;
38533852
var propname = parentNode.dataset["property"];
3854-
var elem_that = this;
38553853
let choices = Array.from(values).map((v) => {
38563854
return { content: v };
38573855
});
@@ -3860,21 +3858,20 @@ export default class LGraphCanvas_UI {
38603858
{
38613859
event: event as MouseEventExt,
38623860
className: "dark combo-menu",
3863-
callback: inner_clicked,
3861+
callback: (
3862+
v: IContextMenuItem,
3863+
option: IContextMenuOptions,
3864+
event: MouseEventExt,
3865+
) => {
3866+
//node.setProperty(propname,v);
3867+
//graphcanvas.dirty_canvas = true;
3868+
this.innerText = v.content;
3869+
innerChange(propname, v.content);
3870+
return false;
3871+
},
38643872
},
38653873
ref_window,
38663874
);
3867-
function inner_clicked(
3868-
v: IContextMenuItem,
3869-
option: IContextMenuOptions,
3870-
event: MouseEventExt,
3871-
) {
3872-
//node.setProperty(propname,v);
3873-
//graphcanvas.dirty_canvas = true;
3874-
elem_that.innerText = v.content;
3875-
innerChange(propname, v.content);
3876-
return false;
3877-
}
38783875
},
38793876
);
38803877
}

packages/core/src/nodes/GraphInput.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export default class GraphInput extends LGraphNode {
6161
constructor(title?: string) {
6262
super(title);
6363

64-
let that = this;
65-
6664
this.nameWidget = this.addWidget(
6765
"text",
6866
"Name",
@@ -91,8 +89,8 @@ export default class GraphInput extends LGraphNode {
9189
"number",
9290
"Value",
9391
this.properties.value,
94-
function (v) {
95-
that.setProperty("value", v);
92+
(v) => {
93+
this.setProperty("value", v);
9694
},
9795
);
9896

packages/core/src/nodes/GraphOutput.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ export default class GraphOutput extends LGraphNode {
5555
constructor(title?: string) {
5656
super(title);
5757

58-
let that = this;
59-
6058
this.nameWidget = this.addWidget(
6159
"text",
6260
"Name",

0 commit comments

Comments
 (0)