From b6ca97f2b1f6c3ec0be919070850a2f0df81f0b2 Mon Sep 17 00:00:00 2001 From: tamat Date: Wed, 8 May 2019 15:15:57 +0200 Subject: [PATCH] build --- build/litegraph.js | 7374 +++++++++++++++++++++++++++------------- build/litegraph.min.js | 24 +- 2 files changed, 5006 insertions(+), 2392 deletions(-) diff --git a/build/litegraph.js b/build/litegraph.js index 592ea840e..a80c6d825 100644 --- a/build/litegraph.js +++ b/build/litegraph.js @@ -99,11 +99,14 @@ */ registerNodeType: function(type, base_class) { - if (!base_class.prototype) + if (!base_class.prototype) { throw "Cannot register a simple object, it must be a class with a prototype"; + } base_class.type = type; - if (LiteGraph.debug) console.log("Node registered: " + type); + if (LiteGraph.debug) { + console.log("Node registered: " + type); + } var categories = type.split("/"); var classname = base_class.name; @@ -111,15 +114,20 @@ var pos = type.lastIndexOf("/"); base_class.category = type.substr(0, pos); - if (!base_class.title) base_class.title = classname; + if (!base_class.title) { + base_class.title = classname; + } //info.name = name.substr(pos+1,name.length - pos); //extend class - if (base_class.prototype) + if (base_class.prototype) { //is a class - for (var i in LGraphNode.prototype) - if (!base_class.prototype[i]) + for (var i in LGraphNode.prototype) { + if (!base_class.prototype[i]) { base_class.prototype[i] = LGraphNode.prototype[i]; + } + } + } Object.defineProperty(base_class.prototype, "shape", { set: function(v) { @@ -149,27 +157,33 @@ enumerable: true }); - var prev = this.registered_node_types[type]; + var prev = this.registered_node_types[type]; this.registered_node_types[type] = base_class; - if (base_class.constructor.name) this.Nodes[classname] = base_class; - if(LiteGraph.onNodeTypeRegistered) - LiteGraph.onNodeTypeRegistered(type,base_class); - if( prev && LiteGraph.onNodeTypeReplaced) - LiteGraph.onNodeTypeReplaced(type,base_class,prev); + if (base_class.constructor.name) { + this.Nodes[classname] = base_class; + } + if (LiteGraph.onNodeTypeRegistered) { + LiteGraph.onNodeTypeRegistered(type, base_class); + } + if (prev && LiteGraph.onNodeTypeReplaced) { + LiteGraph.onNodeTypeReplaced(type, base_class, prev); + } //warnings - if (base_class.prototype.onPropertyChange) + if (base_class.prototype.onPropertyChange) { console.warn( "LiteGraph node class " + type + " has onPropertyChange method, it must be called onPropertyChanged with d at the end" ); + } if (base_class.supported_extensions) { - for (var i in base_class.supported_extensions) + for (var i in base_class.supported_extensions) { this.node_types_by_file_extension[ base_class.supported_extensions[i].toLowerCase() ] = base_class; + } } }, @@ -193,7 +207,7 @@ var params = Array(func.length); var code = ""; var names = LiteGraph.getParameterNames(func); - for (var i = 0; i < names.length; ++i) + for (var i = 0; i < names.length; ++i) { code += "this.addInput('" + names[i] + @@ -202,19 +216,22 @@ ? "'" + param_types[i] + "'" : "0") + ");\n"; + } code += "this.addOutput('out'," + (return_type ? "'" + return_type + "'" : 0) + ");\n"; - if (properties) + if (properties) { code += "this.properties = " + JSON.stringify(properties) + ";\n"; + } var classobj = Function(code); classobj.title = name.split("/").pop(); classobj.desc = "Generated from " + func.name; classobj.prototype.onExecute = function onExecute() { - for (var i = 0; i < params.length; ++i) + for (var i = 0; i < params.length; ++i) { params[i] = this.getInputData(i); + } var r = func.apply(this, params); this.setOutputData(0, r); }; @@ -231,8 +248,9 @@ LGraphNode.prototype[name] = func; for (var i in this.registered_node_types) { var type = this.registered_node_types[i]; - if (type.prototype[name]) - type.prototype["_" + name] = type.prototype[name]; //keep old in case of replacing + if (type.prototype[name]) { + type.prototype["_" + name] = type.prototype[name]; + } //keep old in case of replacing type.prototype[name] = func; } }, @@ -248,10 +266,11 @@ createNode: function(type, title, options) { var base_class = this.registered_node_types[type]; if (!base_class) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log( 'GraphNode type "' + type + '" not registered.' ); + } return null; } @@ -268,21 +287,39 @@ console.error(err); return null; } - } else node = new base_class(title); + } else { + node = new base_class(title); + } node.type = type; - if (!node.title && title) node.title = title; - if (!node.properties) node.properties = {}; - if (!node.properties_info) node.properties_info = []; - if (!node.flags) node.flags = {}; - if (!node.size) node.size = node.computeSize(); - if (!node.pos) node.pos = LiteGraph.DEFAULT_POSITION.concat(); - if (!node.mode) node.mode = LiteGraph.ALWAYS; + if (!node.title && title) { + node.title = title; + } + if (!node.properties) { + node.properties = {}; + } + if (!node.properties_info) { + node.properties_info = []; + } + if (!node.flags) { + node.flags = {}; + } + if (!node.size) { + node.size = node.computeSize(); + } + if (!node.pos) { + node.pos = LiteGraph.DEFAULT_POSITION.concat(); + } + if (!node.mode) { + node.mode = LiteGraph.ALWAYS; + } //extra options if (options) { - for (var i in options) node[i] = options[i]; + for (var i in options) { + node[i] = options[i]; + } } return node; @@ -309,11 +346,17 @@ var r = []; for (var i in this.registered_node_types) { var type = this.registered_node_types[i]; - if (filter && type.filter && type.filter != filter) continue; + if (filter && type.filter && type.filter != filter) { + continue; + } if (category == "") { - if (type.category == null) r.push(type); - } else if (type.category == category) r.push(type); + if (type.category == null) { + r.push(type); + } + } else if (type.category == category) { + r.push(type); + } } return r; @@ -327,14 +370,18 @@ getNodeTypesCategories: function() { var categories = { "": 1 }; - for (var i in this.registered_node_types) + for (var i in this.registered_node_types) { if ( this.registered_node_types[i].category && !this.registered_node_types[i].skip_list - ) + ) { categories[this.registered_node_types[i].category] = 1; + } + } var result = []; - for (var i in categories) result.push(i); + for (var i in categories) { + result.push(i); + } return result; }, @@ -343,7 +390,9 @@ var tmp = document.getElementsByTagName("script"); //weird, this array changes by its own, so we use a copy var script_files = []; - for (var i in tmp) script_files.push(tmp[i]); + for (var i in tmp) { + script_files.push(tmp[i]); + } var docHeadObj = document.getElementsByTagName("head")[0]; folder_wildcard = document.location.href + folder_wildcard; @@ -353,33 +402,47 @@ if ( !src || src.substr(0, folder_wildcard.length) != folder_wildcard - ) + ) { continue; + } try { - if (LiteGraph.debug) console.log("Reloading: " + src); + if (LiteGraph.debug) { + console.log("Reloading: " + src); + } var dynamicScript = document.createElement("script"); dynamicScript.type = "text/javascript"; dynamicScript.src = src; docHeadObj.appendChild(dynamicScript); docHeadObj.removeChild(script_files[i]); } catch (err) { - if (LiteGraph.throw_errors) throw err; - if (LiteGraph.debug) + if (LiteGraph.throw_errors) { + throw err; + } + if (LiteGraph.debug) { console.log("Error while reloading " + src); + } } } - if (LiteGraph.debug) console.log("Nodes reloaded"); + if (LiteGraph.debug) { + console.log("Nodes reloaded"); + } }, //separated just to improve if it doesn't work cloneObject: function(obj, target) { - if (obj == null) return null; + if (obj == null) { + return null; + } var r = JSON.parse(JSON.stringify(obj)); - if (!target) return r; + if (!target) { + return r; + } - for (var i in r) target[i] = r[i]; + for (var i in r) { + target[i] = r[i]; + } return target; }, @@ -389,8 +452,9 @@ !type_b || //generic input type_a == type_b || //same type (is valid for triggers) (type_a == LiteGraph.EVENT && type_b == LiteGraph.ACTION) - ) + ) { return true; + } // Enforce string type to handle toLowerCase call (-1 number not ok) type_a = String(type_a); @@ -399,16 +463,20 @@ type_b = type_b.toLowerCase(); // For nodes supporting multiple connection types - if (type_a.indexOf(",") == -1 && type_b.indexOf(",") == -1) + if (type_a.indexOf(",") == -1 && type_b.indexOf(",") == -1) { return type_a == type_b; + } // Check all permutations to see if one is valid var supported_types_a = type_a.split(","); var supported_types_b = type_b.split(","); - for (var i = 0; i < supported_types_a.length; ++i) - for (var j = 0; j < supported_types_b.length; ++j) - if (supported_types_a[i] == supported_types_b[j]) + for (var i = 0; i < supported_types_a.length; ++i) { + for (var j = 0; j < supported_types_b.length; ++j) { + if (supported_types_a[i] == supported_types_b[j]) { return true; + } + } + } return false; }, @@ -423,19 +491,20 @@ }); //timer that works everywhere - if (typeof performance != "undefined") + if (typeof performance != "undefined") { LiteGraph.getTime = performance.now.bind(performance); - else if (typeof Date != "undefined" && Date.now) + } else if (typeof Date != "undefined" && Date.now) { LiteGraph.getTime = Date.now.bind(Date); - else if (typeof process != "undefined") + } else if (typeof process != "undefined") { LiteGraph.getTime = function() { var t = process.hrtime(); return t[0] * 0.001 + t[1] * 1e-6; }; - else + } else { LiteGraph.getTime = function getTime() { return new Date().getTime(); }; + } //********************************************************************************* // LGraph CLASS @@ -450,11 +519,15 @@ */ function LGraph(o) { - if (LiteGraph.debug) console.log("Graph created"); + if (LiteGraph.debug) { + console.log("Graph created"); + } this.list_of_graphcanvas = null; this.clear(); - if (o) this.configure(o); + if (o) { + this.configure(o); + } } global.LGraph = LiteGraph.LGraph = LGraph; @@ -485,11 +558,14 @@ this._version = -1; //used to detect changes //safe clear - if (this._nodes) + if (this._nodes) { for (var i = 0; i < this._nodes.length; ++i) { var node = this._nodes[i]; - if (node.onRemoved) node.onRemoved(); + if (node.onRemoved) { + node.onRemoved(); + } } + } //nodes this._nodes = []; @@ -537,13 +613,17 @@ */ LGraph.prototype.attachCanvas = function(graphcanvas) { - if (graphcanvas.constructor != LGraphCanvas) + if (graphcanvas.constructor != LGraphCanvas) { throw "attachCanvas expects a LGraphCanvas instance"; - if (graphcanvas.graph && graphcanvas.graph != this) + } + if (graphcanvas.graph && graphcanvas.graph != this) { graphcanvas.graph.detachCanvas(graphcanvas); + } graphcanvas.graph = this; - if (!this.list_of_graphcanvas) this.list_of_graphcanvas = []; + if (!this.list_of_graphcanvas) { + this.list_of_graphcanvas = []; + } this.list_of_graphcanvas.push(graphcanvas); }; @@ -553,10 +633,14 @@ * @param {GraphCanvas} graph_canvas */ LGraph.prototype.detachCanvas = function(graphcanvas) { - if (!this.list_of_graphcanvas) return; + if (!this.list_of_graphcanvas) { + return; + } var pos = this.list_of_graphcanvas.indexOf(graphcanvas); - if (pos == -1) return; + if (pos == -1) { + return; + } graphcanvas.graph = null; this.list_of_graphcanvas.splice(pos, 1); }; @@ -568,10 +652,14 @@ */ LGraph.prototype.start = function(interval) { - if (this.status == LGraph.STATUS_RUNNING) return; + if (this.status == LGraph.STATUS_RUNNING) { + return; + } this.status = LGraph.STATUS_RUNNING; - if (this.onPlayEvent) this.onPlayEvent(); + if (this.onPlayEvent) { + this.onPlayEvent(); + } this.sendEventToAllNodes("onStart"); @@ -587,17 +675,20 @@ window.requestAnimationFrame ) { function on_frame() { - if (that.execution_timer_id != -1) return; + if (that.execution_timer_id != -1) { + return; + } window.requestAnimationFrame(on_frame); that.runStep(1, !this.catch_errors); } this.execution_timer_id = -1; on_frame(); - } else + } else { this.execution_timer_id = setInterval(function() { //execute that.runStep(1, !this.catch_errors); }, interval); + } }; /** @@ -606,15 +697,20 @@ */ LGraph.prototype.stop = function() { - if (this.status == LGraph.STATUS_STOPPED) return; + if (this.status == LGraph.STATUS_STOPPED) { + return; + } this.status = LGraph.STATUS_STOPPED; - if (this.onStopEvent) this.onStopEvent(); + if (this.onStopEvent) { + this.onStopEvent(); + } if (this.execution_timer_id != null) { - if (this.execution_timer_id != -1) + if (this.execution_timer_id != -1) { clearInterval(this.execution_timer_id); + } this.execution_timer_id = null; } @@ -636,50 +732,67 @@ var nodes = this._nodes_executable ? this._nodes_executable : this._nodes; - if (!nodes) return; + if (!nodes) { + return; + } if (do_not_catch_errors) { //iterations for (var i = 0; i < num; i++) { for (var j = 0, l = nodes.length; j < l; ++j) { var node = nodes[j]; - if (node.mode == LiteGraph.ALWAYS && node.onExecute) + if (node.mode == LiteGraph.ALWAYS && node.onExecute) { node.onExecute(); + } } this.fixedtime += this.fixedtime_lapse; - if (this.onExecuteStep) this.onExecuteStep(); + if (this.onExecuteStep) { + this.onExecuteStep(); + } } - if (this.onAfterExecute) this.onAfterExecute(); + if (this.onAfterExecute) { + this.onAfterExecute(); + } } else { try { //iterations for (var i = 0; i < num; i++) { for (var j = 0, l = nodes.length; j < l; ++j) { var node = nodes[j]; - if (node.mode == LiteGraph.ALWAYS && node.onExecute) + if (node.mode == LiteGraph.ALWAYS && node.onExecute) { node.onExecute(); + } } this.fixedtime += this.fixedtime_lapse; - if (this.onExecuteStep) this.onExecuteStep(); + if (this.onExecuteStep) { + this.onExecuteStep(); + } } - if (this.onAfterExecute) this.onAfterExecute(); + if (this.onAfterExecute) { + this.onAfterExecute(); + } this.errors_in_execution = false; } catch (err) { this.errors_in_execution = true; - if (LiteGraph.throw_errors) throw err; - if (LiteGraph.debug) + if (LiteGraph.throw_errors) { + throw err; + } + if (LiteGraph.debug) { console.log("Error during execution: " + err); + } this.stop(); } } var now = LiteGraph.getTime(); var elapsed = now - start; - if (elapsed == 0) elapsed = 1; + if (elapsed == 0) { + elapsed = 1; + } this.execution_time = 0.001 * elapsed; this.globaltime += 0.001 * elapsed; this.iteration += 1; @@ -695,9 +808,11 @@ LGraph.prototype.updateExecutionOrder = function() { this._nodes_in_order = this.computeExecutionOrder(false); this._nodes_executable = []; - for (var i = 0; i < this._nodes_in_order.length; ++i) - if (this._nodes_in_order[i].onExecute) + for (var i = 0; i < this._nodes_in_order.length; ++i) { + if (this._nodes_in_order[i].onExecute) { this._nodes_executable.push(this._nodes_in_order[i]); + } + } }; //This is more internal, it computes the executable nodes in order and returns it @@ -714,35 +829,49 @@ //search for the nodes without inputs (starting nodes) for (var i = 0, l = this._nodes.length; i < l; ++i) { var node = this._nodes[i]; - if (only_onExecute && !node.onExecute) continue; + if (only_onExecute && !node.onExecute) { + continue; + } M[node.id] = node; //add to pending nodes var num = 0; //num of input connections - if (node.inputs) - for (var j = 0, l2 = node.inputs.length; j < l2; j++) - if (node.inputs[j] && node.inputs[j].link != null) num += 1; + if (node.inputs) { + for (var j = 0, l2 = node.inputs.length; j < l2; j++) { + if (node.inputs[j] && node.inputs[j].link != null) { + num += 1; + } + } + } if (num == 0) { //is a starting node S.push(node); - if (set_level) node._level = 1; + if (set_level) { + node._level = 1; + } } //num of input links else { - if (set_level) node._level = 0; + if (set_level) { + node._level = 0; + } remaining_links[node.id] = num; } } while (true) { - if (S.length == 0) break; + if (S.length == 0) { + break; + } //get an starting node var node = S.shift(); L.push(node); //add to ordered list delete M[node.id]; //remove from the pending nodes - if (!node.outputs) continue; + if (!node.outputs) { + continue; + } //for every output for (var i = 0; i < node.outputs.length; i++) { @@ -752,17 +881,22 @@ output == null || output.links == null || output.links.length == 0 - ) + ) { continue; + } //for every connection for (var j = 0; j < output.links.length; j++) { var link_id = output.links[j]; var link = this.links[link_id]; - if (!link) continue; + if (!link) { + continue; + } //already visited link (ignore it) - if (visited_links[link.id]) continue; + if (visited_links[link.id]) { + continue; + } var target_node = this.getNodeById(link.target_id); if (target_node == null) { @@ -774,40 +908,50 @@ set_level && (!target_node._level || target_node._level <= node._level) - ) + ) { target_node._level = node._level + 1; + } visited_links[link.id] = true; //mark as visited remaining_links[target_node.id] -= 1; //reduce the number of links remaining - if (remaining_links[target_node.id] == 0) - S.push(target_node); //if no more links, then add to starters array + if (remaining_links[target_node.id] == 0) { + S.push(target_node); + } //if no more links, then add to starters array } } } //the remaining ones (loops) - for (var i in M) L.push(M[i]); + for (var i in M) { + L.push(M[i]); + } - if (L.length != this._nodes.length && LiteGraph.debug) + if (L.length != this._nodes.length && LiteGraph.debug) { console.warn("something went wrong, nodes missing"); + } var l = L.length; //save order number in the node - for (var i = 0; i < l; ++i) L[i].order = i; + for (var i = 0; i < l; ++i) { + L[i].order = i; + } //sort now by priority L = L.sort(function(A, B) { var Ap = A.constructor.priority || A.priority || 0; var Bp = B.constructor.priority || B.priority || 0; - if (Ap == Bp) + if (Ap == Bp) { //if same priority, sort by order return A.order - B.order; + } return Ap - Bp; //sort by priority }); //save order number in the node, again... - for (var i = 0; i < l; ++i) L[i].order = i; + for (var i = 0; i < l; ++i) { + L[i].order = i; + } return L; }; @@ -825,7 +969,9 @@ while (pending.length) { var current = pending.shift(); - if (!current.inputs) continue; + if (!current.inputs) { + continue; + } if (!visited[current.id] && current != node) { visited[current.id] = true; ancestors.push(current); @@ -857,7 +1003,9 @@ for (var i = 0; i < nodes.length; ++i) { var node = nodes[i]; var col = node._level || 1; - if (!columns[col]) columns[col] = []; + if (!columns[col]) { + columns[col] = []; + } columns[col].push(node); } @@ -865,14 +1013,18 @@ for (var i = 0; i < columns.length; ++i) { var column = columns[i]; - if (!column) continue; + if (!column) { + continue; + } var max_size = 100; var y = margin; for (var j = 0; j < column.length; ++j) { var node = column[j]; node.pos[0] = x; node.pos[1] = y; - if (node.size[0] > max_size) max_size = node.size[0]; + if (node.size[0] > max_size) { + max_size = node.size[0]; + } y += node.size[1] + margin; } x += max_size + margin; @@ -921,7 +1073,9 @@ mode = mode || LiteGraph.ALWAYS; var nodes = this._nodes_in_order ? this._nodes_in_order : this._nodes; - if (!nodes) return; + if (!nodes) { + return; + } for (var j = 0, l = nodes.length; j < l; ++j) { var node = nodes[j]; @@ -930,25 +1084,35 @@ node.constructor === LiteGraph.Subgraph && eventname != "onExecute" ) { - if (node.mode == mode) + if (node.mode == mode) { node.sendEventToAllNodes(eventname, params, mode); + } continue; } - if (!node[eventname] || node.mode != mode) continue; - if (params === undefined) node[eventname](); - else if (params && params.constructor === Array) + if (!node[eventname] || node.mode != mode) { + continue; + } + if (params === undefined) { + node[eventname](); + } else if (params && params.constructor === Array) { node[eventname].apply(node, params); - else node[eventname](params); + } else { + node[eventname](params); + } } }; LGraph.prototype.sendActionToCanvas = function(action, params) { - if (!this.list_of_graphcanvas) return; + if (!this.list_of_graphcanvas) { + return; + } for (var i = 0; i < this.list_of_graphcanvas.length; ++i) { var c = this.list_of_graphcanvas[i]; - if (c[action]) c[action].apply(c, params); + if (c[action]) { + c[action].apply(c, params); + } } }; @@ -959,7 +1123,9 @@ */ LGraph.prototype.add = function(node, skip_compute_order) { - if (!node) return; + if (!node) { + return; + } //groups if (node.constructor === LGraphGroup) { @@ -979,12 +1145,16 @@ node.id = ++this.last_node_id; } - if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) + if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) { throw "LiteGraph: max number of nodes in a graph reached"; + } //give him an id - if (node.id == null || node.id == -1) node.id = ++this.last_node_id; - else if (this.last_node_id < node.id) this.last_node_id = node.id; + if (node.id == null || node.id == -1) { + node.id = ++this.last_node_id; + } else if (this.last_node_id < node.id) { + this.last_node_id = node.id; + } node.graph = this; this._version++; @@ -992,13 +1162,21 @@ this._nodes.push(node); this._nodes_by_id[node.id] = node; - if (node.onAdded) node.onAdded(this); + if (node.onAdded) { + node.onAdded(this); + } - if (this.config.align_to_grid) node.alignToGrid(); + if (this.config.align_to_grid) { + node.alignToGrid(); + } - if (!skip_compute_order) this.updateExecutionOrder(); + if (!skip_compute_order) { + this.updateExecutionOrder(); + } - if (this.onNodeAdded) this.onNodeAdded(node); + if (this.onNodeAdded) { + this.onNodeAdded(node); + } this.setDirtyCanvas(true); this.change(); @@ -1015,7 +1193,9 @@ LGraph.prototype.remove = function(node) { if (node.constructor === LiteGraph.LGraphGroup) { var index = this._groups.indexOf(node); - if (index != -1) this._groups.splice(index, 1); + if (index != -1) { + this._groups.splice(index, 1); + } node.graph = null; this._version++; this.setDirtyCanvas(true, true); @@ -1023,29 +1203,40 @@ return; } - if (this._nodes_by_id[node.id] == null) return; //not found + if (this._nodes_by_id[node.id] == null) { + return; + } //not found - if (node.ignore_remove) return; //cannot be removed + if (node.ignore_remove) { + return; + } //cannot be removed //disconnect inputs - if (node.inputs) + if (node.inputs) { for (var i = 0; i < node.inputs.length; i++) { var slot = node.inputs[i]; - if (slot.link != null) node.disconnectInput(i); + if (slot.link != null) { + node.disconnectInput(i); + } } + } //disconnect outputs - if (node.outputs) + if (node.outputs) { for (var i = 0; i < node.outputs.length; i++) { var slot = node.outputs[i]; - if (slot.links != null && slot.links.length) + if (slot.links != null && slot.links.length) { node.disconnectOutput(i); + } } + } //node.id = -1; //why? //callback - if (node.onRemoved) node.onRemoved(); + if (node.onRemoved) { + node.onRemoved(); + } node.graph = null; this._version++; @@ -1054,18 +1245,25 @@ if (this.list_of_graphcanvas) { for (var i = 0; i < this.list_of_graphcanvas.length; ++i) { var canvas = this.list_of_graphcanvas[i]; - if (canvas.selected_nodes[node.id]) + if (canvas.selected_nodes[node.id]) { delete canvas.selected_nodes[node.id]; - if (canvas.node_dragged == node) canvas.node_dragged = null; + } + if (canvas.node_dragged == node) { + canvas.node_dragged = null; + } } } //remove from containers var pos = this._nodes.indexOf(node); - if (pos != -1) this._nodes.splice(pos, 1); + if (pos != -1) { + this._nodes.splice(pos, 1); + } delete this._nodes_by_id[node.id]; - if (this.onNodeRemoved) this.onNodeRemoved(node); + if (this.onNodeRemoved) { + this.onNodeRemoved(node); + } this.setDirtyCanvas(true, true); this.change(); @@ -1080,7 +1278,9 @@ */ LGraph.prototype.getNodeById = function(id) { - if (id == null) return null; + if (id == null) { + return null; + } return this._nodes_by_id[id]; }; @@ -1093,9 +1293,11 @@ LGraph.prototype.findNodesByClass = function(classObject, result) { result = result || []; result.length = 0; - for (var i = 0, l = this._nodes.length; i < l; ++i) - if (this._nodes[i].constructor === classObject) + for (var i = 0, l = this._nodes.length; i < l; ++i) { + if (this._nodes[i].constructor === classObject) { result.push(this._nodes[i]); + } + } return result; }; @@ -1109,9 +1311,11 @@ var type = type.toLowerCase(); result = result || []; result.length = 0; - for (var i = 0, l = this._nodes.length; i < l; ++i) - if (this._nodes[i].type.toLowerCase() == type) + for (var i = 0, l = this._nodes.length; i < l; ++i) { + if (this._nodes[i].type.toLowerCase() == type) { result.push(this._nodes[i]); + } + } return result; }; @@ -1122,8 +1326,11 @@ * @return {Node} the node or null */ LGraph.prototype.findNodeByTitle = function(title) { - for (var i = 0, l = this._nodes.length; i < l; ++i) - if (this._nodes[i].title == title) return this._nodes[i]; + for (var i = 0, l = this._nodes.length; i < l; ++i) { + if (this._nodes[i].title == title) { + return this._nodes[i]; + } + } return null; }; @@ -1135,8 +1342,11 @@ */ LGraph.prototype.findNodesByTitle = function(title) { var result = []; - for (var i = 0, l = this._nodes.length; i < l; ++i) - if (this._nodes[i].title == title) result.push(this._nodes[i]); + for (var i = 0, l = this._nodes.length; i < l; ++i) { + if (this._nodes[i].title == title) { + result.push(this._nodes[i]); + } + } return result; }; @@ -1152,7 +1362,9 @@ nodes_list = nodes_list || this._nodes; for (var i = nodes_list.length - 1; i >= 0; i--) { var n = nodes_list[i]; - if (n.isPointInside(x, y, margin)) return n; + if (n.isPointInside(x, y, margin)) { + return n; + } } return null; }; @@ -1167,39 +1379,43 @@ LGraph.prototype.getGroupOnPos = function(x, y) { for (var i = this._groups.length - 1; i >= 0; i--) { var g = this._groups[i]; - if (g.isPointInside(x, y, 2, true)) return g; + if (g.isPointInside(x, y, 2, true)) { + return g; + } } return null; }; /** * Checks that the node type matches the node type registered, used when replacing a nodetype by a newer version during execution - * this replaces the ones using the old version with the new version + * this replaces the ones using the old version with the new version * @method checkNodeTypes */ LGraph.prototype.checkNodeTypes = function() { - var changes = false; + var changes = false; for (var i = 0; i < this._nodes.length; i++) { var node = this._nodes[i]; - var ctor = LiteGraph.registered_node_types[ node.type ]; - if(node.constructor == ctor) - continue; - console.log("node being replaced by newer version: " + node.type ); - var newnode = LiteGraph.createNode( node.type ); - changes = true; - this._nodes[i] = newnode; - newnode.configure( node.serialize() ); - newnode.graph = this; - this._nodes_by_id[newnode.id] = newnode; - if(node.inputs) - newnode.inputs = node.inputs.concat(); - if(node.outputs) - newnode.outputs = node.outputs.concat(); + var ctor = LiteGraph.registered_node_types[node.type]; + if (node.constructor == ctor) { + continue; + } + console.log("node being replaced by newer version: " + node.type); + var newnode = LiteGraph.createNode(node.type); + changes = true; + this._nodes[i] = newnode; + newnode.configure(node.serialize()); + newnode.graph = this; + this._nodes_by_id[newnode.id] = newnode; + if (node.inputs) { + newnode.inputs = node.inputs.concat(); + } + if (node.outputs) { + newnode.outputs = node.outputs.concat(); + } } - this.updateExecutionOrder(); + this.updateExecutionOrder(); }; - // ********** GLOBALS ***************** LGraph.prototype.onAction = function(action, param) { @@ -1209,14 +1425,18 @@ ); for (var i = 0; i < this._input_nodes.length; ++i) { var node = this._input_nodes[i]; - if (node.properties.name != action) continue; + if (node.properties.name != action) { + continue; + } node.onAction(action, param); break; } }; LGraph.prototype.trigger = function(action, param) { - if (this.onTrigger) this.onTrigger(action, param); + if (this.onTrigger) { + this.onTrigger(action, param); + } }; /** @@ -1228,16 +1448,21 @@ */ LGraph.prototype.addInput = function(name, type, value) { var input = this.inputs[name]; - if (input) + if (input) { //already exist return; + } this.inputs[name] = { name: name, type: type, value: value }; this._version++; - if (this.onInputAdded) this.onInputAdded(name, type); + if (this.onInputAdded) { + this.onInputAdded(name, type); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } }; /** @@ -1248,7 +1473,9 @@ */ LGraph.prototype.setInputData = function(name, data) { var input = this.inputs[name]; - if (!input) return; + if (!input) { + return; + } input.value = data; }; @@ -1260,7 +1487,9 @@ */ LGraph.prototype.getInputData = function(name) { var input = this.inputs[name]; - if (!input) return null; + if (!input) { + return null; + } return input.value; }; @@ -1271,9 +1500,13 @@ * @param {String} new_name */ LGraph.prototype.renameInput = function(old_name, name) { - if (name == old_name) return; + if (name == old_name) { + return; + } - if (!this.inputs[old_name]) return false; + if (!this.inputs[old_name]) { + return false; + } if (this.inputs[name]) { console.error("there is already one input with that name"); @@ -1284,9 +1517,13 @@ delete this.inputs[old_name]; this._version++; - if (this.onInputRenamed) this.onInputRenamed(old_name, name); + if (this.onInputRenamed) { + this.onInputRenamed(old_name, name); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } }; /** @@ -1296,18 +1533,23 @@ * @param {String} type */ LGraph.prototype.changeInputType = function(name, type) { - if (!this.inputs[name]) return false; + if (!this.inputs[name]) { + return false; + } if ( this.inputs[name].type && String(this.inputs[name].type).toLowerCase() == String(type).toLowerCase() - ) + ) { return; + } this.inputs[name].type = type; this._version++; - if (this.onInputTypeChanged) this.onInputTypeChanged(name, type); + if (this.onInputTypeChanged) { + this.onInputTypeChanged(name, type); + } }; /** @@ -1317,14 +1559,20 @@ * @param {String} type */ LGraph.prototype.removeInput = function(name) { - if (!this.inputs[name]) return false; + if (!this.inputs[name]) { + return false; + } delete this.inputs[name]; this._version++; - if (this.onInputRemoved) this.onInputRemoved(name); + if (this.onInputRemoved) { + this.onInputRemoved(name); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } return true; }; @@ -1339,9 +1587,13 @@ this.outputs[name] = { name: name, type: type, value: value }; this._version++; - if (this.onOutputAdded) this.onOutputAdded(name, type); + if (this.onOutputAdded) { + this.onOutputAdded(name, type); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } }; /** @@ -1352,7 +1604,9 @@ */ LGraph.prototype.setOutputData = function(name, value) { var output = this.outputs[name]; - if (!output) return; + if (!output) { + return; + } output.value = value; }; @@ -1364,7 +1618,9 @@ */ LGraph.prototype.getOutputData = function(name) { var output = this.outputs[name]; - if (!output) return null; + if (!output) { + return null; + } return output.value; }; @@ -1375,7 +1631,9 @@ * @param {String} new_name */ LGraph.prototype.renameOutput = function(old_name, name) { - if (!this.outputs[old_name]) return false; + if (!this.outputs[old_name]) { + return false; + } if (this.outputs[name]) { console.error("there is already one output with that name"); @@ -1386,9 +1644,13 @@ delete this.outputs[old_name]; this._version++; - if (this.onOutputRenamed) this.onOutputRenamed(old_name, name); + if (this.onOutputRenamed) { + this.onOutputRenamed(old_name, name); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } }; /** @@ -1398,18 +1660,23 @@ * @param {String} type */ LGraph.prototype.changeOutputType = function(name, type) { - if (!this.outputs[name]) return false; + if (!this.outputs[name]) { + return false; + } if ( this.outputs[name].type && String(this.outputs[name].type).toLowerCase() == String(type).toLowerCase() - ) + ) { return; + } this.outputs[name].type = type; this._version++; - if (this.onOutputTypeChanged) this.onOutputTypeChanged(name, type); + if (this.onOutputTypeChanged) { + this.onOutputTypeChanged(name, type); + } }; /** @@ -1418,29 +1685,41 @@ * @param {String} name */ LGraph.prototype.removeOutput = function(name) { - if (!this.outputs[name]) return false; + if (!this.outputs[name]) { + return false; + } delete this.outputs[name]; this._version++; - if (this.onOutputRemoved) this.onOutputRemoved(name); + if (this.onOutputRemoved) { + this.onOutputRemoved(name); + } - if (this.onInputsOutputsChange) this.onInputsOutputsChange(); + if (this.onInputsOutputsChange) { + this.onInputsOutputsChange(); + } return true; }; LGraph.prototype.triggerInput = function(name, value) { var nodes = this.findNodesByTitle(name); - for (var i = 0; i < nodes.length; ++i) nodes[i].onTrigger(value); + for (var i = 0; i < nodes.length; ++i) { + nodes[i].onTrigger(value); + } }; LGraph.prototype.setCallback = function(name, func) { var nodes = this.findNodesByTitle(name); - for (var i = 0; i < nodes.length; ++i) nodes[i].setTrigger(func); + for (var i = 0; i < nodes.length; ++i) { + nodes[i].setTrigger(func); + } }; LGraph.prototype.connectionChange = function(node, link_info) { this.updateExecutionOrder(); - if (this.onConnectionChange) this.onConnectionChange(node); + if (this.onConnectionChange) { + this.onConnectionChange(node); + } this._version++; this.sendActionToCanvas("onConnectionChange"); }; @@ -1451,11 +1730,15 @@ */ LGraph.prototype.isLive = function() { - if (!this.list_of_graphcanvas) return false; + if (!this.list_of_graphcanvas) { + return false; + } for (var i = 0; i < this.list_of_graphcanvas.length; ++i) { var c = this.list_of_graphcanvas[i]; - if (c.live_mode) return true; + if (c.live_mode) { + return true; + } } return false; }; @@ -1467,16 +1750,24 @@ LGraph.prototype.clearTriggeredSlots = function() { for (var i in this.links) { var link_info = this.links[i]; - if (!link_info) continue; - if (link_info._last_time) link_info._last_time = 0; + if (!link_info) { + continue; + } + if (link_info._last_time) { + link_info._last_time = 0; + } } }; /* Called when something visually changed (not the graph!) */ LGraph.prototype.change = function() { - if (LiteGraph.debug) console.log("Graph changed"); + if (LiteGraph.debug) { + console.log("Graph changed"); + } this.sendActionToCanvas("setDirty", [true, true]); - if (this.on_change) this.on_change(this); + if (this.on_change) { + this.on_change(this); + } }; LGraph.prototype.setDirtyCanvas = function(fg, bg) { @@ -1490,9 +1781,13 @@ */ LGraph.prototype.removeLink = function(link_id) { var link = this.links[link_id]; - if (!link) return; + if (!link) { + return; + } var node = this.getNodeById(link.target_id); - if (node) node.disconnectInput(link.target_slot); + if (node) { + node.disconnectInput(link.target_slot); + } }; //save and recover app state *************************************** @@ -1503,30 +1798,35 @@ */ LGraph.prototype.serialize = function() { var nodes_info = []; - for (var i = 0, l = this._nodes.length; i < l; ++i) + for (var i = 0, l = this._nodes.length; i < l; ++i) { nodes_info.push(this._nodes[i].serialize()); + } //pack link info into a non-verbose format var links = []; for (var i in this.links) { //links is an OBJECT var link = this.links[i]; - if(!link.serialize) //weird bug I havent solved yet - { - console.warn("weird LLink bug, link info is not a LLink but a regular object"); - var link2 = new LLink(); - for(var i in link) - link2[i] = link[i]; - this.links[i] = link2; - link = link2; - } + if (!link.serialize) { + //weird bug I havent solved yet + console.warn( + "weird LLink bug, link info is not a LLink but a regular object" + ); + var link2 = new LLink(); + for (var i in link) { + link2[i] = link[i]; + } + this.links[i] = link2; + link = link2; + } links.push(link.serialize()); } var groups_info = []; - for (var i = 0; i < this._groups.length; ++i) + for (var i = 0; i < this._groups.length; ++i) { groups_info.push(this._groups[i].serialize()); + } var data = { last_node_id: this.last_node_id, @@ -1548,9 +1848,13 @@ * @param {Boolean} returns if there was any error parsing */ LGraph.prototype.configure = function(data, keep_old) { - if (!data) return; + if (!data) { + return; + } - if (!keep_old) this.clear(); + if (!keep_old) { + this.clear(); + } var nodes = data.nodes; @@ -1567,7 +1871,9 @@ } //copy all stored fields - for (var i in data) this[i] = data[i]; + for (var i in data) { + this[i] = data[i]; + } var error = false; @@ -1578,10 +1884,11 @@ var n_info = nodes[i]; //stored info var node = LiteGraph.createNode(n_info.type, n_info.title); if (!node) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log( "Node not found or has errors: " + n_info.type ); + } //in case of error we create a replacement node to avoid losing info node = new LGraphNode(); @@ -1599,18 +1906,21 @@ for (var i = 0, l = nodes.length; i < l; ++i) { var n_info = nodes[i]; var node = this.getNodeById(n_info.id); - if (node) node.configure(n_info); + if (node) { + node.configure(n_info); + } } } //groups this._groups.length = 0; - if (data.groups) + if (data.groups) { for (var i = 0; i < data.groups.length; ++i) { var group = new LiteGraph.LGraphGroup(); group.configure(data.groups[i]); this.add(group); } + } this.updateExecutionOrder(); this._version++; @@ -1760,7 +2070,9 @@ Object.defineProperty(this, "pos", { set: function(v) { - if (!v || v.length < 2) return; + if (!v || v.length < 2) { + return; + } this._pos[0] = v[0]; this._pos[1] = v[1]; }, @@ -1790,32 +2102,42 @@ * @method configure */ LGraphNode.prototype.configure = function(info) { - if (this.graph) this.graph._version++; - + if (this.graph) { + this.graph._version++; + } for (var j in info) { if (j == "properties") { //i don't want to clone properties, I want to reuse the old container for (var k in info.properties) { this.properties[k] = info.properties[k]; - if (this.onPropertyChanged) + if (this.onPropertyChanged) { this.onPropertyChanged(k, info.properties[k]); + } } continue; } - if (info[j] == null) continue; - else if (typeof info[j] == "object") { + if (info[j] == null) { + continue; + } else if (typeof info[j] == "object") { //object - if (this[j] && this[j].configure) this[j].configure(info[j]); - else this[j] = LiteGraph.cloneObject(info[j], this[j]); + if (this[j] && this[j].configure) { + this[j].configure(info[j]); + } else { + this[j] = LiteGraph.cloneObject(info[j], this[j]); + } } //value - else this[j] = info[j]; + else { + this[j] = info[j]; + } } - if (!info.title) this.title = this.constructor.title; + if (!info.title) { + this.title = this.constructor.title; + } if (this.onConnectionsChange) { - if (this.inputs) + if (this.inputs) { for (var i = 0; i < this.inputs.length; ++i) { var input = this.inputs[i]; var link_info = this.graph @@ -1829,11 +2151,14 @@ input ); //link_info has been created now, so its updated } + } - if (this.outputs) + if (this.outputs) { for (var i = 0; i < this.outputs.length; ++i) { var output = this.outputs[i]; - if (!output.links) continue; + if (!output.links) { + continue; + } for (var j = 0; j < output.links.length; ++j) { var link_info = this.graph ? this.graph.links[output.links[j]] @@ -1847,15 +2172,20 @@ ); //link_info has been created now, so its updated } } + } } if (info.widgets_values && this.widgets) { - for (var i = 0; i < info.widgets_values.length; ++i) - if (this.widgets[i]) + for (var i = 0; i < info.widgets_values.length; ++i) { + if (this.widgets[i]) { this.widgets[i].value = info.widgets_values[i]; + } + } } - if (this.onConfigure) this.onConfigure(info); + if (this.onConfigure) { + this.onConfigure(info); + } }; /** @@ -1875,42 +2205,60 @@ }; //special case for when there were errors - if (this.constructor === LGraphNode && this.last_serialization) + if (this.constructor === LGraphNode && this.last_serialization) { return this.last_serialization; + } - if (this.inputs) o.inputs = this.inputs; + if (this.inputs) { + o.inputs = this.inputs; + } if (this.outputs) { //clear outputs last data (because data in connections is never serialized but stored inside the outputs info) - for (var i = 0; i < this.outputs.length; i++) + for (var i = 0; i < this.outputs.length; i++) { delete this.outputs[i]._data; + } o.outputs = this.outputs; } - if (this.title && this.title != this.constructor.title) + if (this.title && this.title != this.constructor.title) { o.title = this.title; + } - if (this.properties) + if (this.properties) { o.properties = LiteGraph.cloneObject(this.properties); + } if (this.widgets && this.serialize_widgets) { o.widgets_values = []; - for (var i = 0; i < this.widgets.length; ++i) + for (var i = 0; i < this.widgets.length; ++i) { o.widgets_values[i] = this.widgets[i].value; + } } - if (!o.type) o.type = this.constructor.type; + if (!o.type) { + o.type = this.constructor.type; + } - if (this.color) o.color = this.color; - if (this.bgcolor) o.bgcolor = this.bgcolor; - if (this.boxcolor) o.boxcolor = this.boxcolor; - if (this.shape) o.shape = this.shape; + if (this.color) { + o.color = this.color; + } + if (this.bgcolor) { + o.bgcolor = this.bgcolor; + } + if (this.boxcolor) { + o.boxcolor = this.boxcolor; + } + if (this.shape) { + o.shape = this.shape; + } if (this.onSerialize) { - if (this.onSerialize(o)) + if (this.onSerialize(o)) { console.warn( "node onSerialize shouldnt return anything, data should be stored in the object pass in the first parameter" ); + } } return o; @@ -1919,20 +2267,27 @@ /* Creates a clone of this node */ LGraphNode.prototype.clone = function() { var node = LiteGraph.createNode(this.type); - if (!node) return null; + if (!node) { + return null; + } //we clone it because serialize returns shared containers var data = LiteGraph.cloneObject(this.serialize()); //remove links - if (data.inputs) - for (var i = 0; i < data.inputs.length; ++i) + if (data.inputs) { + for (var i = 0; i < data.inputs.length; ++i) { data.inputs[i].link = null; - - if (data.outputs) - for (var i = 0; i < data.outputs.length; ++i) { - if (data.outputs[i].links) data.outputs[i].links.length = 0; } + } + + if (data.outputs) { + for (var i = 0; i < data.outputs.length; ++i) { + if (data.outputs[i].links) { + data.outputs[i].links.length = 0; + } + } + } delete data["id"]; //remove links @@ -1967,12 +2322,14 @@ * @param {*} value */ LGraphNode.prototype.setProperty = function(name, value) { - if(!this.properties) - this.properties = {}; - this.properties[name] = value; - if (this.onPropertyChanged) - this.onPropertyChanged(name, value); - }; + if (!this.properties) { + this.properties = {}; + } + this.properties[name] = value; + if (this.onPropertyChanged) { + this.onPropertyChanged(name, value); + } + }; // Execution ************************* /** @@ -1982,16 +2339,22 @@ * @param {*} data */ LGraphNode.prototype.setOutputData = function(slot, data) { - if (!this.outputs) return; + if (!this.outputs) { + return; + } //this maybe slow and a niche case //if(slot && slot.constructor === String) // slot = this.findOutputSlot(slot); - if (slot == -1 || slot >= this.outputs.length) return; + if (slot == -1 || slot >= this.outputs.length) { + return; + } var output_info = this.outputs[slot]; - if (!output_info) return; + if (!output_info) { + return; + } //store data in the output itself in case we want to debug output_info._data = data; @@ -2012,10 +2375,16 @@ * @param {String} datatype */ LGraphNode.prototype.setOutputDataType = function(slot, type) { - if (!this.outputs) return; - if (slot == -1 || slot >= this.outputs.length) return; + if (!this.outputs) { + return; + } + if (slot == -1 || slot >= this.outputs.length) { + return; + } var output_info = this.outputs[slot]; - if (!output_info) return; + if (!output_info) { + return; + } //store data in the output itself in case we want to debug output_info.type = type; @@ -2036,25 +2405,36 @@ * @return {*} data or if it is not connected returns undefined */ LGraphNode.prototype.getInputData = function(slot, force_update) { - if (!this.inputs) return; //undefined; - - if (slot >= this.inputs.length || this.inputs[slot].link == null) + if (!this.inputs) { return; + } //undefined; + + if (slot >= this.inputs.length || this.inputs[slot].link == null) { + return; + } var link_id = this.inputs[slot].link; var link = this.graph.links[link_id]; - if (!link) + if (!link) { //bug: weird case but it happens sometimes return null; + } - if (!force_update) return link.data; + if (!force_update) { + return link.data; + } //special case: used to extract data from the incoming connection before the graph has been executed var node = this.graph.getNodeById(link.origin_id); - if (!node) return link.data; + if (!node) { + return link.data; + } - if (node.updateOutputData) node.updateOutputData(link.origin_slot); - else if (node.onExecute) node.onExecute(); + if (node.updateOutputData) { + node.updateOutputData(link.origin_slot); + } else if (node.onExecute) { + node.onExecute(); + } return link.data; }; @@ -2066,19 +2446,27 @@ * @return {String} datatype in string format */ LGraphNode.prototype.getInputDataType = function(slot) { - if (!this.inputs) return null; //undefined; - - if (slot >= this.inputs.length || this.inputs[slot].link == null) + if (!this.inputs) { return null; + } //undefined; + + if (slot >= this.inputs.length || this.inputs[slot].link == null) { + return null; + } var link_id = this.inputs[slot].link; var link = this.graph.links[link_id]; - if (!link) + if (!link) { //bug: weird case but it happens sometimes return null; + } var node = this.graph.getNodeById(link.origin_id); - if (!node) return link.type; + if (!node) { + return link.type; + } var output_info = node.outputs[link.origin_slot]; - if (output_info) return output_info.type; + if (output_info) { + return output_info.type; + } return null; }; @@ -2094,7 +2482,9 @@ force_update ) { var slot = this.findInputSlot(slot_name); - if (slot == -1) return null; + if (slot == -1) { + return null; + } return this.getInputData(slot, force_update); }; @@ -2105,7 +2495,9 @@ * @return {boolean} */ LGraphNode.prototype.isInputConnected = function(slot) { - if (!this.inputs) return false; + if (!this.inputs) { + return false; + } return slot < this.inputs.length && this.inputs[slot].link != null; }; @@ -2116,8 +2508,12 @@ * @return {Object} object or null { link: id, name: string, type: string or 0 } */ LGraphNode.prototype.getInputInfo = function(slot) { - if (!this.inputs) return null; - if (slot < this.inputs.length) return this.inputs[slot]; + if (!this.inputs) { + return null; + } + if (slot < this.inputs.length) { + return this.inputs[slot]; + } return null; }; @@ -2128,12 +2524,20 @@ * @return {LGraphNode} node or null */ LGraphNode.prototype.getInputNode = function(slot) { - if (!this.inputs) return null; - if (slot >= this.inputs.length) return null; + if (!this.inputs) { + return null; + } + if (slot >= this.inputs.length) { + return null; + } var input = this.inputs[slot]; - if (!input || input.link === null) return null; + if (!input || input.link === null) { + return null; + } var link_info = this.graph.links[input.link]; - if (!link_info) return null; + if (!link_info) { + return null; + } return this.graph.getNodeById(link_info.origin_id); }; @@ -2144,14 +2548,17 @@ * @return {*} value */ LGraphNode.prototype.getInputOrProperty = function(name) { - if (!this.inputs || !this.inputs.length) + if (!this.inputs || !this.inputs.length) { return this.properties ? this.properties[name] : null; + } for (var i = 0, l = this.inputs.length; i < l; ++i) { var input_info = this.inputs[i]; if (name == input_info.name && input_info.link != null) { var link = this.graph.links[input_info.link]; - if (link) return link.data; + if (link) { + return link.data; + } } } return this.properties[name]; @@ -2164,8 +2571,12 @@ * @return {Object} object or null */ LGraphNode.prototype.getOutputData = function(slot) { - if (!this.outputs) return null; - if (slot >= this.outputs.length) return null; + if (!this.outputs) { + return null; + } + if (slot >= this.outputs.length) { + return null; + } var info = this.outputs[slot]; return info._data; @@ -2178,8 +2589,12 @@ * @return {Object} object or null { name: string, type: string, links: [ ids of links in number ] } */ LGraphNode.prototype.getOutputInfo = function(slot) { - if (!this.outputs) return null; - if (slot < this.outputs.length) return this.outputs[slot]; + if (!this.outputs) { + return null; + } + if (slot < this.outputs.length) { + return this.outputs[slot]; + } return null; }; @@ -2190,7 +2605,9 @@ * @return {boolean} */ LGraphNode.prototype.isOutputConnected = function(slot) { - if (!this.outputs) return false; + if (!this.outputs) { + return false; + } return ( slot < this.outputs.length && this.outputs[slot].links && @@ -2204,10 +2621,14 @@ * @return {boolean} */ LGraphNode.prototype.isAnyOutputConnected = function() { - if (!this.outputs) return false; - for (var i = 0; i < this.outputs.length; ++i) - if (this.outputs[i].links && this.outputs[i].links.length) + if (!this.outputs) { + return false; + } + for (var i = 0; i < this.outputs.length; ++i) { + if (this.outputs[i].links && this.outputs[i].links.length) { return true; + } + } return false; }; @@ -2218,12 +2639,18 @@ * @return {array} */ LGraphNode.prototype.getOutputNodes = function(slot) { - if (!this.outputs || this.outputs.length == 0) return null; + if (!this.outputs || this.outputs.length == 0) { + return null; + } - if (slot >= this.outputs.length) return null; + if (slot >= this.outputs.length) { + return null; + } var output = this.outputs[slot]; - if (!output.links || output.links.length == 0) return null; + if (!output.links || output.links.length == 0) { + return null; + } var r = []; for (var i = 0; i < output.links.length; i++) { @@ -2231,7 +2658,9 @@ var link = this.graph.links[link_id]; if (link) { var target_node = this.graph.getNodeById(link.target_id); - if (target_node) r.push(target_node); + if (target_node) { + r.push(target_node); + } } } return r; @@ -2244,9 +2673,13 @@ * @param {*} param */ LGraphNode.prototype.trigger = function(action, param) { - if (!this.outputs || !this.outputs.length) return; + if (!this.outputs || !this.outputs.length) { + return; + } - if (this.graph) this.graph._last_trigger_time = LiteGraph.getTime(); + if (this.graph) { + this.graph._last_trigger_time = LiteGraph.getTime(); + } for (var i = 0; i < this.outputs.length; ++i) { var output = this.outputs[i]; @@ -2254,8 +2687,9 @@ !output || output.type !== LiteGraph.EVENT || (action && output.name != action) - ) + ) { continue; + } this.triggerSlot(i, param); } }; @@ -2268,38 +2702,52 @@ * @param {Number} link_id [optional] in case you want to trigger and specific output link in a slot */ LGraphNode.prototype.triggerSlot = function(slot, param, link_id) { - if (!this.outputs) return; + if (!this.outputs) { + return; + } var output = this.outputs[slot]; - if (!output) return; + if (!output) { + return; + } var links = output.links; - if (!links || !links.length) return; + if (!links || !links.length) { + return; + } - if (this.graph) this.graph._last_trigger_time = LiteGraph.getTime(); + if (this.graph) { + this.graph._last_trigger_time = LiteGraph.getTime(); + } //for every link attached here for (var k = 0; k < links.length; ++k) { var id = links[k]; - if (link_id != null && link_id != id) + if (link_id != null && link_id != id) { //to skip links continue; + } var link_info = this.graph.links[links[k]]; - if (!link_info) + if (!link_info) { //not connected continue; + } link_info._last_time = LiteGraph.getTime(); var node = this.graph.getNodeById(link_info.target_id); - if (!node) + if (!node) { //node not found? continue; + } //used to mark events in graph var target_connection = node.inputs[link_info.target_slot]; - if (node.onAction) node.onAction(target_connection.name, param); - else if (node.mode === LiteGraph.ON_TRIGGER) { - if (node.onExecute) node.onExecute(param); + if (node.onAction) { + node.onAction(target_connection.name, param); + } else if (node.mode === LiteGraph.ON_TRIGGER) { + if (node.onExecute) { + node.onExecute(param); + } } } }; @@ -2311,24 +2759,32 @@ * @param {Number} link_id [optional] in case you want to trigger and specific output link in a slot */ LGraphNode.prototype.clearTriggeredSlot = function(slot, link_id) { - if (!this.outputs) return; + if (!this.outputs) { + return; + } var output = this.outputs[slot]; - if (!output) return; + if (!output) { + return; + } var links = output.links; - if (!links || !links.length) return; + if (!links || !links.length) { + return; + } //for every link attached here for (var k = 0; k < links.length; ++k) { var id = links[k]; - if (link_id != null && link_id != id) + if (link_id != null && link_id != id) { //to skip links continue; + } var link_info = this.graph.links[links[k]]; - if (!link_info) + if (!link_info) { //not connected continue; + } link_info._last_time = 0; } }; @@ -2348,10 +2804,18 @@ extra_info ) { var o = { name: name, type: type, default_value: default_value }; - if (extra_info) for (var i in extra_info) o[i] = extra_info[i]; - if (!this.properties_info) this.properties_info = []; + if (extra_info) { + for (var i in extra_info) { + o[i] = extra_info[i]; + } + } + if (!this.properties_info) { + this.properties_info = []; + } this.properties_info.push(o); - if (!this.properties) this.properties = {}; + if (!this.properties) { + this.properties = {}; + } this.properties[name] = default_value; return o; }; @@ -2367,11 +2831,19 @@ */ LGraphNode.prototype.addOutput = function(name, type, extra_info) { var o = { name: name, type: type, links: null }; - if (extra_info) for (var i in extra_info) o[i] = extra_info[i]; + if (extra_info) { + for (var i in extra_info) { + o[i] = extra_info[i]; + } + } - if (!this.outputs) this.outputs = []; + if (!this.outputs) { + this.outputs = []; + } this.outputs.push(o); - if (this.onOutputAdded) this.onOutputAdded(o); + if (this.onOutputAdded) { + this.onOutputAdded(o); + } this.size = this.computeSize(); this.setDirtyCanvas(true, true); return o; @@ -2386,11 +2858,19 @@ for (var i = 0; i < array.length; ++i) { var info = array[i]; var o = { name: info[0], type: info[1], link: null }; - if (array[2]) for (var j in info[2]) o[j] = info[2][j]; + if (array[2]) { + for (var j in info[2]) { + o[j] = info[2][j]; + } + } - if (!this.outputs) this.outputs = []; + if (!this.outputs) { + this.outputs = []; + } this.outputs.push(o); - if (this.onOutputAdded) this.onOutputAdded(o); + if (this.onOutputAdded) { + this.onOutputAdded(o); + } } this.size = this.computeSize(); @@ -2406,17 +2886,23 @@ this.disconnectOutput(slot); this.outputs.splice(slot, 1); for (var i = slot; i < this.outputs.length; ++i) { - if (!this.outputs[i] || !this.outputs[i].links) continue; + if (!this.outputs[i] || !this.outputs[i].links) { + continue; + } var links = this.outputs[i].links; for (var j = 0; j < links.length; ++j) { var link = this.graph.links[links[j]]; - if (!link) continue; + if (!link) { + continue; + } link.origin_slot -= 1; } } this.size = this.computeSize(); - if (this.onOutputRemoved) this.onOutputRemoved(slot); + if (this.onOutputRemoved) { + this.onOutputRemoved(slot); + } this.setDirtyCanvas(true, true); }; @@ -2430,12 +2916,20 @@ LGraphNode.prototype.addInput = function(name, type, extra_info) { type = type || 0; var o = { name: name, type: type, link: null }; - if (extra_info) for (var i in extra_info) o[i] = extra_info[i]; + if (extra_info) { + for (var i in extra_info) { + o[i] = extra_info[i]; + } + } - if (!this.inputs) this.inputs = []; + if (!this.inputs) { + this.inputs = []; + } this.inputs.push(o); this.size = this.computeSize(); - if (this.onInputAdded) this.onInputAdded(o); + if (this.onInputAdded) { + this.onInputAdded(o); + } this.setDirtyCanvas(true, true); return o; }; @@ -2449,11 +2943,19 @@ for (var i = 0; i < array.length; ++i) { var info = array[i]; var o = { name: info[0], type: info[1], link: null }; - if (array[2]) for (var j in info[2]) o[j] = info[2][j]; + if (array[2]) { + for (var j in info[2]) { + o[j] = info[2][j]; + } + } - if (!this.inputs) this.inputs = []; + if (!this.inputs) { + this.inputs = []; + } this.inputs.push(o); - if (this.onInputAdded) this.onInputAdded(o); + if (this.onInputAdded) { + this.onInputAdded(o); + } } this.size = this.computeSize(); @@ -2469,13 +2971,19 @@ this.disconnectInput(slot); this.inputs.splice(slot, 1); for (var i = slot; i < this.inputs.length; ++i) { - if (!this.inputs[i]) continue; + if (!this.inputs[i]) { + continue; + } var link = this.graph.links[this.inputs[i].link]; - if (!link) continue; + if (!link) { + continue; + } link.target_slot -= 1; } this.size = this.computeSize(); - if (this.onInputRemoved) this.onInputRemoved(slot); + if (this.onInputRemoved) { + this.onInputRemoved(slot); + } this.setDirtyCanvas(true, true); }; @@ -2506,7 +3014,9 @@ * @return {number} the total size */ LGraphNode.prototype.computeSize = function(minHeight, out) { - if (this.constructor.size) return this.constructor.size.concat(); + if (this.constructor.size) { + return this.constructor.size.concat(); + } var rows = Math.max( this.inputs ? this.inputs.length : 1, @@ -2519,50 +3029,66 @@ (this.constructor.slot_start_y || 0) + rows * LiteGraph.NODE_SLOT_HEIGHT; var widgets_height = 0; - if (this.widgets && this.widgets.length) + if (this.widgets && this.widgets.length) { widgets_height = this.widgets.length * (LiteGraph.NODE_WIDGET_HEIGHT + 4) + 8; - if (this.widgets_up) size[1] = Math.max(size[1], widgets_height); - else size[1] += widgets_height; + } + if (this.widgets_up) { + size[1] = Math.max(size[1], widgets_height); + } else { + size[1] += widgets_height; + } var font_size = font_size; var title_width = compute_text_size(this.title); var input_width = 0; var output_width = 0; - if (this.inputs) + if (this.inputs) { for (var i = 0, l = this.inputs.length; i < l; ++i) { var input = this.inputs[i]; var text = input.label || input.name || ""; var text_width = compute_text_size(text); - if (input_width < text_width) input_width = text_width; + if (input_width < text_width) { + input_width = text_width; + } } + } - if (this.outputs) + if (this.outputs) { for (var i = 0, l = this.outputs.length; i < l; ++i) { var output = this.outputs[i]; var text = output.label || output.name || ""; var text_width = compute_text_size(text); - if (output_width < text_width) output_width = text_width; + if (output_width < text_width) { + output_width = text_width; + } } + } size[0] = Math.max(input_width + output_width + 10, title_width); size[0] = Math.max(size[0], LiteGraph.NODE_WIDTH); - if (this.widgets && this.widgets.length) + if (this.widgets && this.widgets.length) { size[0] = Math.max(size[0], LiteGraph.NODE_WIDTH * 1.5); + } - if (this.onResize) this.onResize(size); + if (this.onResize) { + this.onResize(size); + } function compute_text_size(text) { - if (!text) return 0; + if (!text) { + return 0; + } return font_size * text.length * 0.6; } if ( this.constructor.min_height && size[1] < this.constructor.min_height - ) + ) { size[1] = this.constructor.min_height; + } size[1] += 6; //margin @@ -2582,7 +3108,9 @@ callback, options ) { - if (!this.widgets) this.widgets = []; + if (!this.widgets) { + this.widgets = []; + } var w = { type: type.toLowerCase(), name: name, @@ -2591,18 +3119,24 @@ options: options || {} }; - if (w.options.y !== undefined) w.y = w.options.y; + if (w.options.y !== undefined) { + w.y = w.options.y; + } - if (!callback) + if (!callback) { console.warn("LiteGraph addWidget(...) without a callback"); - if (type == "combo" && !w.options.values) + } + if (type == "combo" && !w.options.values) { throw "LiteGraph addWidget('combo',...) requires to pass values in options: { values:['red','blue'] }"; + } this.widgets.push(w); return w; }; LGraphNode.prototype.addCustomWidget = function(custom_widget) { - if (!this.widgets) this.widgets = []; + if (!this.widgets) { + this.widgets = []; + } this.widgets.push(custom_widget); return custom_widget; }; @@ -2620,7 +3154,9 @@ out[2] = this.size[0] + 4; out[3] = this.size[1] + LiteGraph.NODE_TITLE_HEIGHT; - if (this.onBounding) this.onBounding(out); + if (this.onBounding) { + this.onBounding(out); + } return out; }; @@ -2634,8 +3170,10 @@ LGraphNode.prototype.isPointInside = function(x, y, margin, skip_title) { margin = margin || 0; - var margin_top = this.graph && this.graph.isLive() ? 0 : 20; - if (skip_title) margin_top = 0; + var margin_top = this.graph && this.graph.isLive() ? 0 : LiteGraph.NODE_TITLE_HEIGHT; + if (skip_title) { + margin_top = 0; + } if (this.flags && this.flags.collapsed) { //if ( distance([x,y], [this.pos[0] + this.size[0]*0.5, this.pos[1] + this.size[1]*0.5]) < LiteGraph.NODE_COLLAPSED_RADIUS) if ( @@ -2648,15 +3186,17 @@ 2 * margin, LiteGraph.NODE_TITLE_HEIGHT + 2 * margin ) - ) + ) { return true; + } } else if ( this.pos[0] - 4 - margin < x && this.pos[0] + this.size[0] + 4 + margin > x && this.pos[1] - margin_top - margin < y && this.pos[1] + this.size[1] + margin > y - ) + ) { return true; + } return false; }; @@ -2670,7 +3210,7 @@ LGraphNode.prototype.getSlotInPosition = function(x, y) { //search for inputs var link_pos = new Float32Array(2); - if (this.inputs) + if (this.inputs) { for (var i = 0, l = this.inputs.length; i < l; ++i) { var input = this.inputs[i]; this.getConnectionPos(true, i, link_pos); @@ -2683,11 +3223,13 @@ 20, 10 ) - ) + ) { return { input: input, slot: i, link_pos: link_pos }; + } } + } - if (this.outputs) + if (this.outputs) { for (var i = 0, l = this.outputs.length; i < l; ++i) { var output = this.outputs[i]; this.getConnectionPos(false, i, link_pos); @@ -2700,9 +3242,11 @@ 20, 10 ) - ) + ) { return { output: output, slot: i, link_pos: link_pos }; + } } + } return null; }; @@ -2714,9 +3258,14 @@ * @return {number} the slot (-1 if not found) */ LGraphNode.prototype.findInputSlot = function(name) { - if (!this.inputs) return -1; - for (var i = 0, l = this.inputs.length; i < l; ++i) - if (name == this.inputs[i].name) return i; + if (!this.inputs) { + return -1; + } + for (var i = 0, l = this.inputs.length; i < l; ++i) { + if (name == this.inputs[i].name) { + return i; + } + } return -1; }; @@ -2727,9 +3276,14 @@ * @return {number} the slot (-1 if not found) */ LGraphNode.prototype.findOutputSlot = function(name) { - if (!this.outputs) return -1; - for (var i = 0, l = this.outputs.length; i < l; ++i) - if (name == this.outputs[i].name) return i; + if (!this.outputs) { + return -1; + } + for (var i = 0, l = this.outputs.length; i < l; ++i) { + if (name == this.outputs[i].name) { + return i; + } + } return -1; }; @@ -2756,31 +3310,39 @@ if (slot.constructor === String) { slot = this.findOutputSlot(slot); if (slot == -1) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, no slot of name " + slot); + } return null; } } else if (!this.outputs || slot >= this.outputs.length) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, slot number not found"); + } return null; } - if (target_node && target_node.constructor === Number) + if (target_node && target_node.constructor === Number) { target_node = this.graph.getNodeById(target_node); - if (!target_node) throw "target node is null"; + } + if (!target_node) { + throw "target node is null"; + } //avoid loopback - if (target_node == this) return null; + if (target_node == this) { + return null; + } //you can specify the slot by name if (target_slot.constructor === String) { target_slot = target_node.findInputSlot(target_slot); if (target_slot == -1) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log( "Connect: Error, no slot of name " + target_slot ); + } return null; } } else if (target_slot === LiteGraph.EVENT) { @@ -2796,14 +3358,16 @@ !target_node.inputs || target_slot >= target_node.inputs.length ) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, slot number not found"); + } return null; } //if there is something already plugged there, disconnect - if (target_node.inputs[target_slot].link != null) + if (target_node.inputs[target_slot].link != null) { target_node.disconnectInput(target_slot); + } //why here?? //this.setDirtyCanvas(false,true); @@ -2812,12 +3376,14 @@ var output = this.outputs[slot]; //allows nodes to block connection - if (target_node.onConnectInput) + if (target_node.onConnectInput) { if ( target_node.onConnectInput(target_slot, output.type, output) === false - ) + ) { return null; + } + } var input = target_node.inputs[target_slot]; var link_info = null; @@ -2836,20 +3402,25 @@ this.graph.links[link_info.id] = link_info; //connect in output - if (output.links == null) output.links = []; + if (output.links == null) { + output.links = []; + } output.links.push(link_info.id); //connect in input target_node.inputs[target_slot].link = link_info.id; - if (this.graph) this.graph._version++; - if (this.onConnectionsChange) + if (this.graph) { + this.graph._version++; + } + if (this.onConnectionsChange) { this.onConnectionsChange( LiteGraph.OUTPUT, slot, true, link_info, output - ); //link_info has been created now, so its updated - if (target_node.onConnectionsChange) + ); + } //link_info has been created now, so its updated + if (target_node.onConnectionsChange) { target_node.onConnectionsChange( LiteGraph.INPUT, target_slot, @@ -2857,6 +3428,7 @@ link_info, input ); + } if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.INPUT, @@ -2892,25 +3464,32 @@ if (slot.constructor === String) { slot = this.findOutputSlot(slot); if (slot == -1) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, no slot of name " + slot); + } return false; } } else if (!this.outputs || slot >= this.outputs.length) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, slot number not found"); + } return false; } //get output slot var output = this.outputs[slot]; - if (!output || !output.links || output.links.length == 0) return false; + if (!output || !output.links || output.links.length == 0) { + return false; + } //one of the output links in this slot if (target_node) { - if (target_node.constructor === Number) + if (target_node.constructor === Number) { target_node = this.graph.getNodeById(target_node); - if (!target_node) throw "Target Node not found"; + } + if (!target_node) { + throw "Target Node not found"; + } for (var i = 0, l = output.links.length; i < l; i++) { var link_id = output.links[i]; @@ -2922,16 +3501,19 @@ var input = target_node.inputs[link_info.target_slot]; input.link = null; //remove there delete this.graph.links[link_id]; //remove the link from the links pool - if (this.graph) this.graph._version++; - if (target_node.onConnectionsChange) + if (this.graph) { + this.graph._version++; + } + if (target_node.onConnectionsChange) { target_node.onConnectionsChange( LiteGraph.INPUT, link_info.target_slot, false, link_info, input - ); //link_info hasn't been modified so its ok - if (this.onConnectionsChange) + ); + } //link_info hasn't been modified so its ok + if (this.onConnectionsChange) { this.onConnectionsChange( LiteGraph.OUTPUT, slot, @@ -2939,12 +3521,14 @@ link_info, output ); - if (this.graph && this.graph.onNodeConnectionChange) + } + if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.OUTPUT, this, slot ); + } if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.OUTPUT, @@ -2965,33 +3549,38 @@ for (var i = 0, l = output.links.length; i < l; i++) { var link_id = output.links[i]; var link_info = this.graph.links[link_id]; - if (!link_info) + if (!link_info) { //bug: it happens sometimes continue; + } var target_node = this.graph.getNodeById(link_info.target_id); var input = null; - if (this.graph) this.graph._version++; + if (this.graph) { + this.graph._version++; + } if (target_node) { input = target_node.inputs[link_info.target_slot]; input.link = null; //remove other side link - if (target_node.onConnectionsChange) + if (target_node.onConnectionsChange) { target_node.onConnectionsChange( LiteGraph.INPUT, link_info.target_slot, false, link_info, input - ); //link_info hasn't been modified so its ok - if (this.graph && this.graph.onNodeConnectionChange) + ); + } //link_info hasn't been modified so its ok + if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.INPUT, target_node, link_info.target_slot ); + } } delete this.graph.links[link_id]; //remove the link from the links pool - if (this.onConnectionsChange) + if (this.onConnectionsChange) { this.onConnectionsChange( LiteGraph.OUTPUT, slot, @@ -2999,6 +3588,7 @@ link_info, output ); + } if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.OUTPUT, @@ -3031,18 +3621,22 @@ if (slot.constructor === String) { slot = this.findInputSlot(slot); if (slot == -1) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, no slot of name " + slot); + } return false; } } else if (!this.inputs || slot >= this.inputs.length) { - if (LiteGraph.debug) + if (LiteGraph.debug) { console.log("Connect: Error, slot number not found"); + } return false; } var input = this.inputs[slot]; - if (!input) return false; + if (!input) { + return false; + } var link_id = this.inputs[slot].link; this.inputs[slot].link = null; @@ -3051,11 +3645,14 @@ var link_info = this.graph.links[link_id]; if (link_info) { var target_node = this.graph.getNodeById(link_info.origin_id); - if (!target_node) return false; + if (!target_node) { + return false; + } var output = target_node.outputs[link_info.origin_slot]; - if (!output || !output.links || output.links.length == 0) + if (!output || !output.links || output.links.length == 0) { return false; + } //search in the inputs list for this link for (var i = 0, l = output.links.length; i < l; i++) { @@ -3066,8 +3663,10 @@ } delete this.graph.links[link_id]; //remove from the pool - if (this.graph) this.graph._version++; - if (this.onConnectionsChange) + if (this.graph) { + this.graph._version++; + } + if (this.onConnectionsChange) { this.onConnectionsChange( LiteGraph.INPUT, slot, @@ -3075,7 +3674,8 @@ link_info, input ); - if (target_node.onConnectionsChange) + } + if (target_node.onConnectionsChange) { target_node.onConnectionsChange( LiteGraph.OUTPUT, i, @@ -3083,6 +3683,7 @@ link_info, output ); + } if (this.graph && this.graph.onNodeConnectionChange) { this.graph.onNodeConnectionChange( LiteGraph.OUTPUT, @@ -3113,8 +3714,12 @@ ) { out = out || new Float32Array(2); var num_slots = 0; - if (is_input && this.inputs) num_slots = this.inputs.length; - if (!is_input && this.outputs) num_slots = this.outputs.length; + if (is_input && this.inputs) { + num_slots = this.inputs.length; + } + if (!is_input && this.outputs) { + num_slots = this.outputs.length; + } var offset = LiteGraph.NODE_SLOT_HEIGHT * 0.5; @@ -3122,12 +3727,17 @@ var w = this._collapsed_width || LiteGraph.NODE_COLLAPSED_WIDTH; if (this.horizontal) { out[0] = this.pos[0] + w * 0.5; - if (is_input) + if (is_input) { out[1] = this.pos[1] - LiteGraph.NODE_TITLE_HEIGHT; - else out[1] = this.pos[1]; + } else { + out[1] = this.pos[1]; + } } else { - if (is_input) out[0] = this.pos[0]; - else out[0] = this.pos[0] + w; + if (is_input) { + out[0] = this.pos[0]; + } else { + out[0] = this.pos[0] + w; + } out[1] = this.pos[1] - LiteGraph.NODE_TITLE_HEIGHT * 0.5; } return out; @@ -3163,14 +3773,20 @@ if (this.horizontal) { out[0] = this.pos[0] + (slot_number + 0.5) * (this.size[0] / num_slots); - if (is_input) out[1] = this.pos[1] - LiteGraph.NODE_TITLE_HEIGHT; - else out[1] = this.pos[1] + this.size[1]; + if (is_input) { + out[1] = this.pos[1] - LiteGraph.NODE_TITLE_HEIGHT; + } else { + out[1] = this.pos[1] + this.size[1]; + } return out; } //default vertical slots - if (is_input) out[0] = this.pos[0] + offset; - else out[0] = this.pos[0] + this.size[0] + 1 - offset; + if (is_input) { + out[0] = this.pos[0] + offset; + } else { + out[0] = this.pos[0] + this.size[0] + 1 - offset; + } out[1] = this.pos[1] + (slot_number + 0.7) * LiteGraph.NODE_SLOT_HEIGHT + @@ -3190,9 +3806,13 @@ /* Console output */ LGraphNode.prototype.trace = function(msg) { - if (!this.console) this.console = []; + if (!this.console) { + this.console = []; + } this.console.push(msg); - if (this.console.length > LGraphNode.MAX_CONSOLE) this.console.shift(); + if (this.console.length > LGraphNode.MAX_CONSOLE) { + this.console.shift(); + } this.graph.onNodeTrace(this, msg); }; @@ -3202,7 +3822,9 @@ dirty_foreground, dirty_background ) { - if (!this.graph) return; + if (!this.graph) { + return; + } this.graph.sendActionToCanvas("setDirty", [ dirty_foreground, dirty_background @@ -3263,14 +3885,18 @@ LGraphNode.prototype.executeAction = function(action) /* Allows to get onMouseMove and onMouseUp events even if the mouse is out of focus */ LGraphNode.prototype.captureInput = function(v) { - if (!this.graph || !this.graph.list_of_graphcanvas) return; + if (!this.graph || !this.graph.list_of_graphcanvas) { + return; + } var list = this.graph.list_of_graphcanvas; for (var i = 0; i < list.length; ++i) { var c = list[i]; //releasing somebody elses capture?! - if (!v && c.node_capturing_input != this) continue; + if (!v && c.node_capturing_input != this) { + continue; + } //change c.node_capturing_input = v ? this : null; @@ -3283,9 +3909,14 @@ LGraphNode.prototype.executeAction = function(action) **/ LGraphNode.prototype.collapse = function(force) { this.graph._version++; - if (this.constructor.collapsable === false && !force) return; - if (!this.flags.collapsed) this.flags.collapsed = true; - else this.flags.collapsed = false; + if (this.constructor.collapsable === false && !force) { + return; + } + if (!this.flags.collapsed) { + this.flags.collapsed = true; + } else { + this.flags.collapsed = false; + } this.setDirtyCanvas(true, true); }; @@ -3296,8 +3927,11 @@ LGraphNode.prototype.executeAction = function(action) LGraphNode.prototype.pin = function(v) { this.graph._version++; - if (v === undefined) this.flags.pinned = !this.flags.pinned; - else this.flags.pinned = v; + if (v === undefined) { + this.flags.pinned = !this.flags.pinned; + } else { + this.flags.pinned = v; + } }; LGraphNode.prototype.localToScreen = function(x, y, graphcanvas) { @@ -3327,7 +3961,9 @@ LGraphNode.prototype.executeAction = function(action) Object.defineProperty(this, "pos", { set: function(v) { - if (!v || v.length < 2) return; + if (!v || v.length < 2) { + return; + } this._pos[0] = v[0]; this._pos[1] = v[1]; }, @@ -3339,7 +3975,9 @@ LGraphNode.prototype.executeAction = function(action) Object.defineProperty(this, "size", { set: function(v) { - if (!v || v.length < 2) return; + if (!v || v.length < 2) { + return; + } this._size[0] = Math.max(140, v[0]); this._size[1] = Math.max(80, v[1]); }, @@ -3375,7 +4013,9 @@ LGraphNode.prototype.executeAction = function(action) LGraphGroup.prototype.move = function(deltax, deltay, ignore_nodes) { this._pos[0] += deltax; this._pos[1] += deltay; - if (ignore_nodes) return; + if (ignore_nodes) { + return; + } for (var i = 0; i < this._nodes.length; ++i) { var node = this._nodes[i]; node.pos[0] += deltax; @@ -3391,7 +4031,9 @@ LGraphNode.prototype.executeAction = function(action) for (var i = 0; i < nodes.length; ++i) { var node = nodes[i]; node.getBounding(node_bounding); - if (!overlapBounding(this._bounding, node_bounding)) continue; //out of the visible area + if (!overlapBounding(this._bounding, node_bounding)) { + continue; + } //out of the visible area this._nodes.push(node); } }; @@ -3415,7 +4057,9 @@ LGraphNode.prototype.executeAction = function(action) if (element) { this.element = element; - if (!skip_events) this.bindEvents(element); + if (!skip_events) { + this.bindEvents(element); + } } } @@ -3455,7 +4099,9 @@ LGraphNode.prototype.executeAction = function(action) }; DragAndScale.prototype.onMouse = function(e) { - if (!this.enabled) return; + if (!this.enabled) { + return; + } var canvas = this.element; var rect = canvas.getBoundingClientRect(); @@ -3466,7 +4112,9 @@ LGraphNode.prototype.executeAction = function(action) e.dragging = this.dragging; var ignore = false; - if (this.onmouse) ignore = this.onmouse(e); + if (this.onmouse) { + ignore = this.onmouse(e); + } if (e.type == "mousedown") { this.dragging = true; @@ -3486,7 +4134,9 @@ LGraphNode.prototype.executeAction = function(action) if (!ignore) { var deltax = x - this.last_mouse[0]; var deltay = y - this.last_mouse[1]; - if (this.dragging) this.mouseDrag(deltax, deltay); + if (this.dragging) { + this.mouseDrag(deltax, deltay); + } } } else if (e.type == "mouseup") { this.dragging = false; @@ -3505,10 +4155,12 @@ LGraphNode.prototype.executeAction = function(action) e.type == "DOMMouseScroll" ) { e.eventType = "mousewheel"; - if (e.type == "wheel") e.wheel = -e.deltaY; - else + if (e.type == "wheel") { + e.wheel = -e.deltaY; + } else { e.wheel = e.wheelDeltaY != null ? e.wheelDeltaY : e.detail * -60; + } //from stack overflow e.delta = e.wheelDelta @@ -3551,19 +4203,30 @@ LGraphNode.prototype.executeAction = function(action) this.offset[0] += x / this.scale; this.offset[1] += y / this.scale; - if (this.onredraw) this.onredraw(this); + if (this.onredraw) { + this.onredraw(this); + } }; DragAndScale.prototype.changeScale = function(value, zooming_center) { - if (value < this.min_scale) value = this.min_scale; - else if (value > this.max_scale) value = this.max_scale; + if (value < this.min_scale) { + value = this.min_scale; + } else if (value > this.max_scale) { + value = this.max_scale; + } - if (value == this.scale) return; + if (value == this.scale) { + return; + } - if (!this.element) return; + if (!this.element) { + return; + } var rect = this.element.getBoundingClientRect(); - if (!rect) return; + if (!rect) { + return; + } zooming_center = zooming_center || [ rect.width * 0.5, @@ -3571,7 +4234,9 @@ LGraphNode.prototype.executeAction = function(action) ]; var center = this.convertCanvasToOffset(zooming_center); this.scale = value; - if (Math.abs(this.scale - 1) < 0.01) this.scale = 1; + if (Math.abs(this.scale - 1) < 0.01) { + this.scale = 1; + } var new_center = this.convertCanvasToOffset(zooming_center); var delta_offset = [ @@ -3582,7 +4247,9 @@ LGraphNode.prototype.executeAction = function(action) this.offset[0] += delta_offset[0]; this.offset[1] += delta_offset[1]; - if (this.onredraw) this.onredraw(this); + if (this.onredraw) { + this.onredraw(this); + } }; DragAndScale.prototype.changeDeltaScale = function(value, zooming_center) { @@ -3617,8 +4284,9 @@ LGraphNode.prototype.executeAction = function(action) this.background_image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQBJREFUeNrs1rEKwjAUhlETUkj3vP9rdmr1Ysammk2w5wdxuLgcMHyptfawuZX4pJSWZTnfnu/lnIe/jNNxHHGNn//HNbbv+4dr6V+11uF527arU7+u63qfa/bnmh8sWLBgwYJlqRf8MEptXPBXJXa37BSl3ixYsGDBMliwFLyCV/DeLIMFCxYsWLBMwSt4Be/NggXLYMGCBUvBK3iNruC9WbBgwYJlsGApeAWv4L1ZBgsWLFiwYJmCV/AK3psFC5bBggULloJX8BpdwXuzYMGCBctgwVLwCl7Be7MMFixYsGDBsu8FH1FaSmExVfAxBa/gvVmwYMGCZbBg/W4vAQYA5tRF9QYlv/QAAAAASUVORK5CYII="; - if (canvas && canvas.constructor === String) + if (canvas && canvas.constructor === String) { canvas = document.querySelector(canvas); + } this.ds = new DragAndScale(); this.zoom_modify_alpha = true; //otherwise it generates ugly patterns when scaling down too much @@ -3690,12 +4358,16 @@ LGraphNode.prototype.executeAction = function(action) this.visible_links = []; //link canvas and graph - if (graph) graph.attachCanvas(this); + if (graph) { + graph.attachCanvas(this); + } this.setCanvas(canvas); this.clear(); - if (!options.skip_render) this.startRendering(); + if (!options.skip_render) { + this.startRendering(); + } this.autoresize = options.autoresize; } @@ -3746,7 +4418,9 @@ LGraphNode.prototype.executeAction = function(action) this.last_mouseclick = 0; this.visible_area.set([0, 0, 0, 0]); - if (this.onClear) this.onClear(); + if (this.onClear) { + this.onClear(); + } }; /** @@ -3756,9 +4430,13 @@ LGraphNode.prototype.executeAction = function(action) * @param {LGraph} graph */ LGraphCanvas.prototype.setGraph = function(graph, skip_clear) { - if (this.graph == graph) return; + if (this.graph == graph) { + return; + } - if (!skip_clear) this.clear(); + if (!skip_clear) { + this.clear(); + } if (!graph && this.graph) { this.graph.detachCanvas(this); @@ -3783,14 +4461,20 @@ LGraphNode.prototype.executeAction = function(action) * @param {LGraph} graph */ LGraphCanvas.prototype.openSubgraph = function(graph) { - if (!graph) throw "graph cannot be null"; + if (!graph) { + throw "graph cannot be null"; + } - if (this.graph == graph) throw "graph cannot be the same"; + if (this.graph == graph) { + throw "graph cannot be the same"; + } this.clear(); if (this.graph) { - if (!this._graph_stack) this._graph_stack = []; + if (!this._graph_stack) { + this._graph_stack = []; + } this._graph_stack.push(this.graph); } @@ -3805,7 +4489,9 @@ LGraphNode.prototype.executeAction = function(action) * @param {LGraph} assigns a graph */ LGraphCanvas.prototype.closeSubgraph = function() { - if (!this._graph_stack || this._graph_stack.length == 0) return; + if (!this._graph_stack || this._graph_stack.length == 0) { + return; + } var subgraph_node = this.graph._subgraph_node; var graph = this._graph_stack.pop(); this.selected_nodes = {}; @@ -3824,8 +4510,8 @@ LGraphNode.prototype.executeAction = function(action) * @return {LGraph} the active graph */ LGraphCanvas.prototype.getCurrentGraph = function() { - return this.graph; - } + return this.graph; + }; /** * assigns a canvas @@ -3839,22 +4525,29 @@ LGraphNode.prototype.executeAction = function(action) if (canvas) { if (canvas.constructor === String) { canvas = document.getElementById(canvas); - if (!canvas) + if (!canvas) { throw "Error creating LiteGraph canvas: Canvas not found"; + } } } - if (canvas === this.canvas) return; + if (canvas === this.canvas) { + return; + } if (!canvas && this.canvas) { //maybe detach events from old_canvas - if (!skip_events) this.unbindEvents(); + if (!skip_events) { + this.unbindEvents(); + } } this.canvas = canvas; this.ds.element = canvas; - if (!canvas) return; + if (!canvas) { + return; + } //this.canvas.tabindex = "1000"; canvas.className += " lgraphcanvas"; @@ -3870,18 +4563,20 @@ LGraphNode.prototype.executeAction = function(action) } if (canvas.getContext == null) { - if (canvas.localName != "canvas") + if (canvas.localName != "canvas") { throw "Element supplied for LGraphCanvas must be a element, you passed a " + canvas.localName; + } throw "This browser doesn't support Canvas"; } var ctx = (this.ctx = canvas.getContext("2d")); if (ctx == null) { - if (!canvas.webgl_enabled) + if (!canvas.webgl_enabled) { console.warn( "This canvas seems to be WebGL, enabling WebGL renderer" ); + } this.enableWebGL(); } @@ -3889,7 +4584,9 @@ LGraphNode.prototype.executeAction = function(action) this._mousemove_callback = this.processMouseMove.bind(this); this._mouseup_callback = this.processMouseUp.bind(this); - if (!skip_events) this.bindEvents(); + if (!skip_events) { + this.bindEvents(); + } }; //used in some events to capture them @@ -3999,9 +4696,13 @@ LGraphNode.prototype.executeAction = function(action) LGraphCanvas.getFileExtension = function(url) { var question = url.indexOf("?"); - if (question != -1) url = url.substr(0, question); + if (question != -1) { + url = url.substr(0, question); + } var point = url.lastIndexOf("."); - if (point == -1) return ""; + if (point == -1) { + return ""; + } return url.substr(point + 1).toLowerCase(); }; @@ -4011,10 +4712,12 @@ LGraphNode.prototype.executeAction = function(action) * @method enableWebGL **/ LGraphCanvas.prototype.enableWebGL = function() { - if (typeof GL === undefined) + if (typeof GL === undefined) { throw "litegl.js must be included to use a WebGL canvas"; - if (typeof enableWebGLCanvas === undefined) + } + if (typeof enableWebGLCanvas === undefined) { throw "webglCanvas.js must be included to use this feature"; + } this.gl = this.ctx = enableWebGLCanvas(this.canvas); this.ctx.webgl = true; @@ -4038,8 +4741,12 @@ LGraphNode.prototype.executeAction = function(action) * @param {bool} bgcanvas if the background canvas is dirty (the one containing the wires) */ LGraphCanvas.prototype.setDirty = function(fgcanvas, bgcanvas) { - if (fgcanvas) this.dirty_canvas = true; - if (bgcanvas) this.dirty_bgcanvas = true; + if (fgcanvas) { + this.dirty_canvas = true; + } + if (bgcanvas) { + this.dirty_bgcanvas = true; + } }; /** @@ -4049,7 +4756,9 @@ LGraphNode.prototype.executeAction = function(action) * @return {window} returns the window where the canvas is attached (the DOM root node) */ LGraphCanvas.prototype.getCanvasWindow = function() { - if (!this.canvas) return window; + if (!this.canvas) { + return window; + } var doc = this.canvas.ownerDocument; return doc.defaultView || doc.parentWindow; }; @@ -4060,17 +4769,22 @@ LGraphNode.prototype.executeAction = function(action) * @method startRendering */ LGraphCanvas.prototype.startRendering = function() { - if (this.is_rendering) return; //already rendering + if (this.is_rendering) { + return; + } //already rendering this.is_rendering = true; renderFrame.call(this); function renderFrame() { - if (!this.pause_rendering) this.draw(); + if (!this.pause_rendering) { + this.draw(); + } var window = this.getCanvasWindow(); - if (this.is_rendering) + if (this.is_rendering) { window.requestAnimationFrame(renderFrame.bind(this)); + } } }; @@ -4093,7 +4807,9 @@ LGraphNode.prototype.executeAction = function(action) /* LiteGraphCanvas input */ LGraphCanvas.prototype.processMouseDown = function(e) { - if (!this.graph) return; + if (!this.graph) { + return; + } this.adjustMouseEvent(e); @@ -4133,7 +4849,9 @@ LGraphNode.prototype.executeAction = function(action) LiteGraph.closeAllContextMenus(ref_window); if (this.onMouse) { - if (this.onMouse(e) == true) return; + if (this.onMouse(e) == true) { + return; + } } if (e.which == 1) { @@ -4152,8 +4870,9 @@ LGraphNode.prototype.executeAction = function(action) //when clicked on top of a node //and it is not interactive if (node && this.allow_interaction && !skip_action) { - if (!this.live_mode && !node.flags.pinned) - this.bringToFront(node); //if it wasn't selected? + if (!this.live_mode && !node.flags.pinned) { + this.bringToFront(node); + } //if it wasn't selected? //not dragging mouse to connect two slots if ( @@ -4179,7 +4898,7 @@ LGraphNode.prototype.executeAction = function(action) skip_action = true; } else { //search for outputs - if (node.outputs) + if (node.outputs) { for ( var i = 0, l = node.outputs.length; i < l; @@ -4205,23 +4924,28 @@ LGraphNode.prototype.executeAction = function(action) ); this.connecting_slot = i; - if (e.shiftKey) node.disconnectOutput(i); + if (e.shiftKey) { + node.disconnectOutput(i); + } if (is_double_click) { - if (node.onOutputDblClick) + if (node.onOutputDblClick) { node.onOutputDblClick(i, e); + } } else { - if (node.onOutputClick) + if (node.onOutputClick) { node.onOutputClick(i, e); + } } skip_action = true; break; } } + } //search for inputs - if (node.inputs) + if (node.inputs) { for ( var i = 0, l = node.inputs.length; i < l; @@ -4240,11 +4964,13 @@ LGraphNode.prototype.executeAction = function(action) ) ) { if (is_double_click) { - if (node.onInputDblClick) + if (node.onInputDblClick) { node.onInputDblClick(i, e); + } } else { - if (node.onInputClick) + if (node.onInputClick) { node.onInputClick(i, e); + } } if (input.link !== null) { @@ -4276,6 +5002,7 @@ LGraphNode.prototype.executeAction = function(action) } } } + } } //not resizing } @@ -4306,7 +5033,7 @@ LGraphNode.prototype.executeAction = function(action) //double clicking if (is_double_click && this.selected_nodes[node.id]) { //double click node - if (node.onDblClick) + if (node.onDblClick) { node.onDblClick( e, [ @@ -4315,6 +5042,7 @@ LGraphNode.prototype.executeAction = function(action) ], this ); + } this.processNodeDblClicked(node); block_drag_node = true; } @@ -4335,9 +5063,12 @@ LGraphNode.prototype.executeAction = function(action) } if (!block_drag_node) { - if (this.allow_dragnodes) this.node_dragged = node; - if (!this.selected_nodes[node.id]) + if (this.allow_dragnodes) { + this.node_dragged = node; + } + if (!this.selected_nodes[node.id]) { this.processNodeSelected(node, e); + } } this.dirty_canvas = true; @@ -4354,8 +5085,9 @@ LGraphNode.prototype.executeAction = function(action) e.canvasX > center[0] + 4 || e.canvasY < center[1] - 4 || e.canvasY > center[1] + 4 - ) + ) { continue; + } //link clicked this.showLinkMenu(link, e); break; @@ -4367,7 +5099,9 @@ LGraphNode.prototype.executeAction = function(action) ); this.selected_group_resizing = false; if (this.selected_group) { - if (e.ctrlKey) this.dragging_rectangle = null; + if (e.ctrlKey) { + this.dragging_rectangle = null; + } var dist = distance( [e.canvasX, e.canvasY], @@ -4378,12 +5112,16 @@ LGraphNode.prototype.executeAction = function(action) this.selected_group.size[1] ] ); - if (dist * this.ds.scale < 10) + if (dist * this.ds.scale < 10) { this.selected_group_resizing = true; - else this.selected_group.recomputeInsideNodes(); + } else { + this.selected_group.recomputeInsideNodes(); + } } - if (is_double_click) this.showSearchBox(e); + if (is_double_click) { + this.showSearchBox(e); + } clicking_canvas_bg = true; } @@ -4421,11 +5159,14 @@ LGraphNode.prototype.executeAction = function(action) "input" && ref_window.document.activeElement.nodeName.toLowerCase() != "textarea") - ) + ) { e.preventDefault(); + } e.stopPropagation(); - if (this.onMouseDown) this.onMouseDown(e); + if (this.onMouseDown) { + this.onMouseDown(e); + } return false; }; @@ -4435,9 +5176,13 @@ LGraphNode.prototype.executeAction = function(action) * @method processMouseMove **/ LGraphCanvas.prototype.processMouseMove = function(e) { - if (this.autoresize) this.resize(); + if (this.autoresize) { + this.resize(); + } - if (!this.graph) return; + if (!this.graph) { + return; + } LGraphCanvas.active_canvas = this; this.adjustMouseEvent(e); @@ -4467,16 +5212,18 @@ LGraphNode.prototype.executeAction = function(action) this.dirty_canvas = true; } else if (this.selected_group) { //moving/resizing a group - if (this.selected_group_resizing) + if (this.selected_group_resizing) { this.selected_group.size = [ e.canvasX - this.selected_group.pos[0], e.canvasY - this.selected_group.pos[1] ]; - else { + } else { var deltax = delta[0] / this.ds.scale; var deltay = delta[1] / this.ds.scale; this.selected_group.move(deltax, deltay, e.ctrlKey); - if (this.selected_group._nodes.length) this.dirty_canvas = true; + if (this.selected_group._nodes.length) { + this.dirty_canvas = true; + } } this.dirty_bgcanvas = true; } else if (this.dragging_canvas) { @@ -4485,7 +5232,9 @@ LGraphNode.prototype.executeAction = function(action) this.dirty_canvas = true; this.dirty_bgcanvas = true; } else if (this.allow_interaction) { - if (this.connecting_node) this.dirty_canvas = true; + if (this.connecting_node) { + this.dirty_canvas = true; + } //get node over var node = this.graph.getNodeOnPos( @@ -4502,8 +5251,9 @@ LGraphNode.prototype.executeAction = function(action) ) { //mouse leave this.graph._nodes[i].mouseOver = false; - if (this.node_over && this.node_over.onMouseLeave) + if (this.node_over && this.node_over.onMouseLeave) { this.node_over.onMouseLeave(e); + } this.node_over = null; this.dirty_canvas = true; } @@ -4518,16 +5268,19 @@ LGraphNode.prototype.executeAction = function(action) this.node_over = node; this.dirty_canvas = true; - if (node.onMouseEnter) node.onMouseEnter(e); + if (node.onMouseEnter) { + node.onMouseEnter(e); + } } //in case the node wants to do something - if (node.onMouseMove) + if (node.onMouseMove) { node.onMouseMove( e, [e.canvasX - node.pos[0], e.canvasY - node.pos[1]], this ); + } //if dragging a link if (this.connecting_node) { @@ -4551,9 +5304,12 @@ LGraphNode.prototype.executeAction = function(action) this.connecting_output.type, slot_type ) - ) + ) { this._highlight_input = pos; - } else this._highlight_input = null; + } + } else { + this._highlight_input = null; + } } } @@ -4568,11 +5324,15 @@ LGraphNode.prototype.executeAction = function(action) 5, 5 ) - ) + ) { this.canvas.style.cursor = "se-resize"; - else this.canvas.style.cursor = "crosshair"; + } else { + this.canvas.style.cursor = "crosshair"; + } } - } else if (this.canvas) this.canvas.style.cursor = ""; + } else if (this.canvas) { + this.canvas.style.cursor = ""; + } if ( this.node_capturing_input && @@ -4616,10 +5376,12 @@ LGraphNode.prototype.executeAction = function(action) : 0) * (LiteGraph.NODE_WIDGET_HEIGHT + 4) + 4; - if (this.resizing_node.size[1] < min_height) + if (this.resizing_node.size[1] < min_height) { this.resizing_node.size[1] = min_height; - if (this.resizing_node.size[0] < LiteGraph.NODE_MIN_WIDTH) + } + if (this.resizing_node.size[0] < LiteGraph.NODE_MIN_WIDTH) { this.resizing_node.size[0] = LiteGraph.NODE_MIN_WIDTH; + } this.canvas.style.cursor = "se-resize"; this.dirty_canvas = true; @@ -4636,7 +5398,9 @@ LGraphNode.prototype.executeAction = function(action) * @method processMouseUp **/ LGraphCanvas.prototype.processMouseUp = function(e) { - if (!this.graph) return; + if (!this.graph) { + return; + } var window = this.getCanvasWindow(); var document = window.document; @@ -4678,7 +5442,9 @@ LGraphNode.prototype.executeAction = function(action) this.selected_group.pos[1] = Math.round( this.selected_group.pos[1] ); - if (this.selected_group._nodes.length) this.dirty_canvas = true; + if (this.selected_group._nodes.length) { + this.dirty_canvas = true; + } this.selected_group = null; } this.selected_group_resizing = false; @@ -4714,11 +5480,14 @@ LGraphNode.prototype.executeAction = function(action) this.dragging_rectangle, node_bounding ) - ) - continue; //out of the visible area + ) { + continue; + } //out of the visible area to_select.push(node); } - if (to_select.length) this.selectNodes(to_select); + if (to_select.length) { + this.selectNodes(to_select); + } } this.dragging_rectangle = null; } else if (this.connecting_node) { @@ -4760,24 +5529,27 @@ LGraphNode.prototype.executeAction = function(action) //not on top of an input var input = node.getInputInfo(0); //auto connect - if (this.connecting_output.type == LiteGraph.EVENT) + if ( + this.connecting_output.type == LiteGraph.EVENT + ) { this.connecting_node.connect( this.connecting_slot, node, LiteGraph.EVENT ); - else if ( + } else if ( input && !input.link && LiteGraph.isValidConnection( input.type && this.connecting_output.type ) - ) + ) { this.connecting_node.connect( this.connecting_slot, node, 0 ); + } } } } @@ -4805,15 +5577,17 @@ LGraphNode.prototype.executeAction = function(action) LiteGraph.NODE_TITLE_HEIGHT, LiteGraph.NODE_TITLE_HEIGHT ) - ) + ) { node.collapse(); + } this.dirty_canvas = true; this.dirty_bgcanvas = true; this.node_dragged.pos[0] = Math.round(this.node_dragged.pos[0]); this.node_dragged.pos[1] = Math.round(this.node_dragged.pos[1]); - if (this.graph.config.align_to_grid) + if (this.graph.config.align_to_grid) { this.node_dragged.alignToGrid(); + } this.node_dragged = null; } //no node being dragged else { @@ -4824,12 +5598,14 @@ LGraphNode.prototype.executeAction = function(action) this.visible_nodes ); - if (!node && e.click_time < 300) this.deselectAllNodes(); + if (!node && e.click_time < 300) { + this.deselectAllNodes(); + } this.dirty_canvas = true; this.dragging_canvas = false; - if (this.node_over && this.node_over.onMouseUp) + if (this.node_over && this.node_over.onMouseUp) { this.node_over.onMouseUp( e, [ @@ -4838,14 +5614,16 @@ LGraphNode.prototype.executeAction = function(action) ], this ); + } if ( this.node_capturing_input && this.node_capturing_input.onMouseUp - ) + ) { this.node_capturing_input.onMouseUp(e, [ e.canvasX - this.node_capturing_input.pos[0], e.canvasY - this.node_capturing_input.pos[1] ]); + } } } else if (e.which == 2) { //middle button @@ -4876,7 +5654,9 @@ LGraphNode.prototype.executeAction = function(action) * @method processMouseWheel **/ LGraphCanvas.prototype.processMouseWheel = function(e) { - if (!this.graph || !this.allow_dragcanvas) return; + if (!this.graph || !this.allow_dragcanvas) { + return; + } var delta = e.wheelDeltaY != null ? e.wheelDeltaY : e.detail * -60; @@ -4884,8 +5664,11 @@ LGraphNode.prototype.executeAction = function(action) var scale = this.ds.scale; - if (delta > 0) scale *= 1.1; - else if (delta < 0) scale *= 1 / 1.1; + if (delta > 0) { + scale *= 1.1; + } else if (delta < 0) { + scale *= 1 / 1.1; + } //this.setZoom( scale, [ e.localX, e.localY ] ); this.ds.changeScale(scale, [e.localX, e.localY]); @@ -4911,8 +5694,9 @@ LGraphNode.prototype.executeAction = function(action) title_height - 4, title_height - 4 ) - ) + ) { return true; + } return false; }; @@ -4926,12 +5710,12 @@ LGraphNode.prototype.executeAction = function(action) canvasy, slot_pos ) { - if (node.inputs) + if (node.inputs) { for (var i = 0, l = node.inputs.length; i < l; ++i) { var input = node.inputs[i]; var link_pos = node.getConnectionPos(true, i); var is_inside = false; - if (node.horizontal) + if (node.horizontal) { is_inside = isInsideRectangle( canvasx, canvasy, @@ -4940,7 +5724,7 @@ LGraphNode.prototype.executeAction = function(action) 10, 20 ); - else + } else { is_inside = isInsideRectangle( canvasx, canvasy, @@ -4949,6 +5733,7 @@ LGraphNode.prototype.executeAction = function(action) 40, 10 ); + } if (is_inside) { if (slot_pos) { slot_pos[0] = link_pos[0]; @@ -4957,6 +5742,7 @@ LGraphNode.prototype.executeAction = function(action) return i; } } + } return -1; }; @@ -4965,12 +5751,16 @@ LGraphNode.prototype.executeAction = function(action) * @method processKey **/ LGraphCanvas.prototype.processKey = function(e) { - if (!this.graph) return; + if (!this.graph) { + return; + } var block_default = false; //console.log(e); //debug - if (e.target.localName == "input") return; + if (e.target.localName == "input") { + return; + } if (e.type == "keydown") { if (e.keyCode == 32) { @@ -5013,17 +5803,25 @@ LGraphNode.prototype.executeAction = function(action) //... //TODO - if (this.selected_nodes) - for (var i in this.selected_nodes) - if (this.selected_nodes[i].onKeyDown) + if (this.selected_nodes) { + for (var i in this.selected_nodes) { + if (this.selected_nodes[i].onKeyDown) { this.selected_nodes[i].onKeyDown(e); + } + } + } } else if (e.type == "keyup") { - if (e.keyCode == 32) this.dragging_canvas = false; + if (e.keyCode == 32) { + this.dragging_canvas = false; + } - if (this.selected_nodes) - for (var i in this.selected_nodes) - if (this.selected_nodes[i].onKeyUp) + if (this.selected_nodes) { + for (var i in this.selected_nodes) { + if (this.selected_nodes[i].onKeyUp) { this.selected_nodes[i].onKeyUp(e); + } + } + } } this.graph.change(); @@ -5052,18 +5850,23 @@ LGraphNode.prototype.executeAction = function(action) for (var i = 0; i < selected_nodes_array.length; ++i) { var node = selected_nodes_array[i]; clipboard_info.nodes.push(node.clone().serialize()); - if (node.inputs && node.inputs.length) + if (node.inputs && node.inputs.length) { for (var j = 0; j < node.inputs.length; ++j) { var input = node.inputs[j]; - if (!input || input.link == null) continue; + if (!input || input.link == null) { + continue; + } var link_info = this.graph.links[input.link]; - if (!link_info) continue; + if (!link_info) { + continue; + } var target_node = this.graph.getNodeById( link_info.origin_id ); - if (!target_node || !this.selected_nodes[target_node.id]) + if (!target_node || !this.selected_nodes[target_node.id]) { //improve this by allowing connections to non-selected nodes - continue; //not selected + continue; + } //not selected clipboard_info.links.push([ target_node._relative_id, j, @@ -5071,6 +5874,7 @@ LGraphNode.prototype.executeAction = function(action) link_info.target_slot ]); } + } } localStorage.setItem( "litegrapheditor_clipboard", @@ -5080,7 +5884,9 @@ LGraphNode.prototype.executeAction = function(action) LGraphCanvas.prototype.pasteFromClipboard = function() { var data = localStorage.getItem("litegrapheditor_clipboard"); - if (!data) return; + if (!data) { + return; + } //create nodes var clipboard_info = JSON.parse(data); @@ -5121,8 +5927,12 @@ LGraphNode.prototype.executeAction = function(action) if (!node) { var r = null; - if (this.onDropItem) r = this.onDropItem(event); - if (!r) this.checkDropItem(e); + if (this.onDropItem) { + r = this.onDropItem(event); + } + if (!r) { + this.checkDropItem(e); + } return; } @@ -5135,7 +5945,9 @@ LGraphNode.prototype.executeAction = function(action) var ext = LGraphCanvas.getFileExtension(filename); //console.log(file); - if (node.onDropFile) node.onDropFile(file); + if (node.onDropFile) { + node.onDropFile(file); + } if (node.onDropData) { //prepare reader @@ -5148,20 +5960,27 @@ LGraphNode.prototype.executeAction = function(action) //read data var type = file.type.split("/")[0]; - if (type == "text" || type == "") + if (type == "text" || type == "") { reader.readAsText(file); - else if (type == "image") reader.readAsDataURL(file); - else reader.readAsArrayBuffer(file); + } else if (type == "image") { + reader.readAsDataURL(file); + } else { + reader.readAsArrayBuffer(file); + } } } } } if (node.onDropItem) { - if (node.onDropItem(event)) return true; + if (node.onDropItem(event)) { + return true; + } } - if (this.onDropItem) return this.onDropItem(event); + if (this.onDropItem) { + return this.onDropItem(event); + } return false; }; @@ -5176,27 +5995,37 @@ LGraphNode.prototype.executeAction = function(action) var node = LiteGraph.createNode(nodetype.type); node.pos = [e.canvasX, e.canvasY]; this.graph.add(node); - if (node.onDropFile) node.onDropFile(file); + if (node.onDropFile) { + node.onDropFile(file); + } } } }; LGraphCanvas.prototype.processNodeDblClicked = function(n) { - if (this.onShowNodePanel) this.onShowNodePanel(n); + if (this.onShowNodePanel) { + this.onShowNodePanel(n); + } - if (this.onNodeDblClicked) this.onNodeDblClicked(n); + if (this.onNodeDblClicked) { + this.onNodeDblClicked(n); + } this.setDirty(true); }; LGraphCanvas.prototype.processNodeSelected = function(node, e) { this.selectNode(node, e && e.shiftKey); - if (this.onNodeSelected) this.onNodeSelected(node); + if (this.onNodeSelected) { + this.onNodeSelected(node); + } }; LGraphCanvas.prototype.processNodeDeselected = function(node) { this.deselectNode(node); - if (this.onNodeDeselected) this.onNodeDeselected(node); + if (this.onNodeDeselected) { + this.onNodeDeselected(node); + } }; /** @@ -5207,8 +6036,11 @@ LGraphNode.prototype.executeAction = function(action) node, add_to_current_selection ) { - if (node == null) this.deselectAllNodes(); - else this.selectNodes([node], add_to_current_selection); + if (node == null) { + this.deselectAllNodes(); + } else { + this.selectNodes([node], add_to_current_selection); + } }; /** @@ -5219,27 +6051,38 @@ LGraphNode.prototype.executeAction = function(action) nodes, add_to_current_selection ) { - if (!add_to_current_selection) this.deselectAllNodes(); + if (!add_to_current_selection) { + this.deselectAllNodes(); + } nodes = nodes || this.graph._nodes; for (var i = 0; i < nodes.length; ++i) { var node = nodes[i]; - if (node.is_selected) continue; + if (node.is_selected) { + continue; + } - if (!node.is_selected && node.onSelected) node.onSelected(); + if (!node.is_selected && node.onSelected) { + node.onSelected(); + } node.is_selected = true; this.selected_nodes[node.id] = node; - if (node.inputs) - for (var j = 0; j < node.inputs.length; ++j) + if (node.inputs) { + for (var j = 0; j < node.inputs.length; ++j) { this.highlighted_links[node.inputs[j].link] = true; - if (node.outputs) + } + } + if (node.outputs) { for (var j = 0; j < node.outputs.length; ++j) { var out = node.outputs[j]; - if (out.links) - for (var k = 0; k < out.links.length; ++k) + if (out.links) { + for (var k = 0; k < out.links.length; ++k) { this.highlighted_links[out.links[k]] = true; + } + } } + } } this.setDirty(true); @@ -5250,21 +6093,30 @@ LGraphNode.prototype.executeAction = function(action) * @method deselectNode **/ LGraphCanvas.prototype.deselectNode = function(node) { - if (!node.is_selected) return; - if (node.onDeselected) node.onDeselected(); + if (!node.is_selected) { + return; + } + if (node.onDeselected) { + node.onDeselected(); + } node.is_selected = false; //remove highlighted - if (node.inputs) - for (var i = 0; i < node.inputs.length; ++i) + if (node.inputs) { + for (var i = 0; i < node.inputs.length; ++i) { delete this.highlighted_links[node.inputs[i].link]; - if (node.outputs) + } + } + if (node.outputs) { for (var i = 0; i < node.outputs.length; ++i) { var out = node.outputs[i]; - if (out.links) - for (var j = 0; j < out.links.length; ++j) + if (out.links) { + for (var j = 0; j < out.links.length; ++j) { delete this.highlighted_links[out.links[j]]; + } + } } + } }; /** @@ -5272,12 +6124,18 @@ LGraphNode.prototype.executeAction = function(action) * @method deselectAllNodes **/ LGraphCanvas.prototype.deselectAllNodes = function() { - if (!this.graph) return; + if (!this.graph) { + return; + } var nodes = this.graph._nodes; for (var i = 0, l = nodes.length; i < l; ++i) { var node = nodes[i]; - if (!node.is_selected) continue; - if (node.onDeselected) node.onDeselected(); + if (!node.is_selected) { + continue; + } + if (node.onDeselected) { + node.onDeselected(); + } node.is_selected = false; } this.selected_nodes = {}; @@ -5403,7 +6261,9 @@ LGraphNode.prototype.executeAction = function(action) **/ LGraphCanvas.prototype.bringToFront = function(node) { var i = this.graph._nodes.indexOf(node); - if (i == -1) return; + if (i == -1) { + return; + } this.graph._nodes.splice(i, 1); this.graph._nodes.push(node); @@ -5415,7 +6275,9 @@ LGraphNode.prototype.executeAction = function(action) **/ LGraphCanvas.prototype.sendToBack = function(node) { var i = this.graph._nodes.indexOf(node); - if (i == -1) return; + if (i == -1) { + return; + } this.graph._nodes.splice(i, 1); this.graph._nodes.unshift(node); @@ -5438,11 +6300,13 @@ LGraphNode.prototype.executeAction = function(action) var n = nodes[i]; //skip rendering nodes in live mode - if (this.live_mode && !n.onDrawBackground && !n.onDrawForeground) + if (this.live_mode && !n.onDrawBackground && !n.onDrawForeground) { continue; + } - if (!overlapBounding(this.visible_area, n.getBounding(temp))) - continue; //out of the visible area + if (!overlapBounding(this.visible_area, n.getBounding(temp))) { + continue; + } //out of the visible area visible_nodes.push(n); } @@ -5454,14 +6318,18 @@ LGraphNode.prototype.executeAction = function(action) * @method draw **/ LGraphCanvas.prototype.draw = function(force_canvas, force_bgcanvas) { - if (!this.canvas) return; + if (!this.canvas) { + return; + } //fps counting var now = LiteGraph.getTime(); this.render_time = (now - this.last_draw_time) * 0.001; this.last_draw_time = now; - if (this.graph) this.ds.computeVisibleArea(); + if (this.graph) { + this.ds.computeVisibleArea(); + } if ( this.dirty_bgcanvas || @@ -5470,10 +6338,13 @@ LGraphNode.prototype.executeAction = function(action) (this.graph && this.graph._last_trigger_time && now - this.graph._last_trigger_time < 1000) - ) + ) { this.drawBackCanvas(); + } - if (this.dirty_canvas || force_canvas) this.drawFrontCanvas(); + if (this.dirty_canvas || force_canvas) { + this.drawFrontCanvas(); + } this.fps = this.render_time ? 1.0 / this.render_time : 0; this.frame += 1; @@ -5486,13 +6357,18 @@ LGraphNode.prototype.executeAction = function(action) LGraphCanvas.prototype.drawFrontCanvas = function() { this.dirty_canvas = false; - if (!this.ctx) this.ctx = this.bgcanvas.getContext("2d"); + if (!this.ctx) { + this.ctx = this.bgcanvas.getContext("2d"); + } var ctx = this.ctx; - if (!ctx) + if (!ctx) { //maybe is using webgl... return; + } - if (ctx.start2D) ctx.start2D(); + if (ctx.start2D) { + ctx.start2D(); + } var canvas = this.canvas; @@ -5515,18 +6391,26 @@ LGraphNode.prototype.executeAction = function(action) //clear //canvas.width = canvas.width; - if (this.clear_background) + if (this.clear_background) { ctx.clearRect(0, 0, canvas.width, canvas.height); + } //draw bg canvas - if (this.bgcanvas == this.canvas) this.drawBackCanvas(); - else ctx.drawImage(this.bgcanvas, 0, 0); + if (this.bgcanvas == this.canvas) { + this.drawBackCanvas(); + } else { + ctx.drawImage(this.bgcanvas, 0, 0); + } //rendering - if (this.onRender) this.onRender(canvas, ctx); + if (this.onRender) { + this.onRender(canvas, ctx); + } //info widget - if (this.show_info) this.renderInfo(ctx); + if (this.show_info) { + this.renderInfo(ctx); + } if (this.graph) { //apply transformations @@ -5556,11 +6440,16 @@ LGraphNode.prototype.executeAction = function(action) } //on top (debug) - if (this.render_execution_order) this.drawExecutionOrder(ctx); + if (this.render_execution_order) { + this.drawExecutionOrder(ctx); + } //connections ontop? - if (this.graph.config.links_ontop) - if (!this.live_mode) this.drawConnections(ctx); + if (this.graph.config.links_ontop) { + if (!this.live_mode) { + this.drawConnections(ctx); + } + } //current connection (the one being dragged by the mouse) if (this.connecting_pos != null) { @@ -5595,14 +6484,14 @@ LGraphNode.prototype.executeAction = function(action) if ( this.connecting_output.type === LiteGraph.EVENT || this.connecting_output.shape === LiteGraph.BOX_SHAPE - ) + ) { ctx.rect( this.connecting_pos[0] - 6 + 0.5, this.connecting_pos[1] - 5 + 0.5, 14, 10 ); - else + } else { ctx.arc( this.connecting_pos[0], this.connecting_pos[1], @@ -5610,6 +6499,7 @@ LGraphNode.prototype.executeAction = function(action) 0, Math.PI * 2 ); + } ctx.fill(); ctx.fillStyle = "#ffcc00"; @@ -5636,22 +6526,26 @@ LGraphNode.prototype.executeAction = function(action) ); } - if (this.onDrawForeground) + if (this.onDrawForeground) { this.onDrawForeground(ctx, this.visible_rect); + } ctx.restore(); } - if (this.onDrawOverlay) this.onDrawOverlay(ctx); + if (this.onDrawOverlay) { + this.onDrawOverlay(ctx); + } if (this.dirty_area) { ctx.restore(); //this.dirty_area = null; } - if (ctx.finish2D) + if (ctx.finish2D) { //this is a function I use in webgl renderer ctx.finish2D(); + } }; /** @@ -5685,7 +6579,9 @@ LGraphNode.prototype.executeAction = function(action) ); ctx.fillText("V: " + this.graph._version, 5, 13 * 4); ctx.fillText("FPS:" + this.fps.toFixed(2), 5, 13 * 5); - } else ctx.fillText("No graph selected", 5, 13 * 1); + } else { + ctx.fillText("No graph selected", 5, 13 * 1); + } ctx.restore(); }; @@ -5703,13 +6599,18 @@ LGraphNode.prototype.executeAction = function(action) canvas.height = this.canvas.height; } - if (!this.bgctx) this.bgctx = this.bgcanvas.getContext("2d"); + if (!this.bgctx) { + this.bgctx = this.bgcanvas.getContext("2d"); + } var ctx = this.bgctx; - if (ctx.start) ctx.start(); + if (ctx.start) { + ctx.start(); + } //clear - if (this.clear_background) + if (this.clear_background) { ctx.clearRect(0, 0, canvas.width, canvas.height); + } if (this._graph_stack && this._graph_stack.length) { ctx.save(); @@ -5723,9 +6624,10 @@ LGraphNode.prototype.executeAction = function(action) ctx.textAlign = "center"; ctx.fillStyle = subgraph_node.bgcolor || "#AAA"; var title = ""; - for (var i = 1; i < this._graph_stack.length; ++i) + for (var i = 1; i < this._graph_stack.length; ++i) { title += this._graph_stack[i]._subgraph_node.getTitle() + " >> "; + } ctx.fillText( title + subgraph_node.getTitle(), canvas.width * 0.5, @@ -5735,8 +6637,9 @@ LGraphNode.prototype.executeAction = function(action) } var bg_already_painted = false; - if (this.onRenderBackground) + if (this.onRenderBackground) { bg_already_painted = this.onRenderBackground(canvas, ctx); + } //reset in case of error ctx.restore(); @@ -5754,10 +6657,12 @@ LGraphNode.prototype.executeAction = function(action) this.ds.scale > 0.5 && !bg_already_painted ) { - if (this.zoom_modify_alpha) + if (this.zoom_modify_alpha) { ctx.globalAlpha = (1.0 - 0.5 / this.ds.scale) * this.editor_alpha; - else ctx.globalAlpha = this.editor_alpha; + } else { + ctx.globalAlpha = this.editor_alpha; + } ctx.imageSmoothingEnabled = ctx.mozImageSmoothingEnabled = ctx.imageSmoothingEnabled = false; if ( !this._bg_img || @@ -5777,7 +6682,9 @@ LGraphNode.prototype.executeAction = function(action) pattern = ctx.createPattern(this._bg_img, "repeat"); this._pattern_img = this._bg_img; this._pattern = pattern; - } else pattern = this._pattern; + } else { + pattern = this._pattern; + } if (pattern) { ctx.fillStyle = pattern; ctx.fillRect( @@ -5794,11 +6701,13 @@ LGraphNode.prototype.executeAction = function(action) } //groups - if (this.graph._groups.length && !this.live_mode) + if (this.graph._groups.length && !this.live_mode) { this.drawGroups(canvas, ctx); + } - if (this.onDrawBackground) + if (this.onDrawBackground) { this.onDrawBackground(ctx, this.visible_area); + } if (this.onBackgroundRender) { //LEGACY console.error( @@ -5822,10 +6731,14 @@ LGraphNode.prototype.executeAction = function(action) ctx.shadowOffsetX = 0; ctx.shadowOffsetY = 0; ctx.shadowBlur = 6; - } else ctx.shadowColor = "rgba(0,0,0,0)"; + } else { + ctx.shadowColor = "rgba(0,0,0,0)"; + } //draw connections - if (!this.live_mode) this.drawConnections(ctx); + if (!this.live_mode) { + this.drawConnections(ctx); + } ctx.shadowColor = "rgba(0,0,0,0)"; @@ -5833,7 +6746,9 @@ LGraphNode.prototype.executeAction = function(action) ctx.restore(); } - if (ctx.finish) ctx.finish(); + if (ctx.finish) { + ctx.finish(); + } this.dirty_bgcanvas = false; this.dirty_canvas = true; //to force to repaint the front canvas with the bgcanvas @@ -5859,14 +6774,17 @@ LGraphNode.prototype.executeAction = function(action) LiteGraph.NODE_DEFAULT_BGCOLOR; //shadow and glow - if (node.mouseOver) glow = true; + if (node.mouseOver) { + glow = true; + } //only render if it forces it to do it if (this.live_mode) { if (!node.flags.collapsed) { ctx.shadowColor = "transparent"; - if (node.onDrawForeground) + if (node.onDrawForeground) { node.onDrawForeground(ctx, this, this.canvas); + } } return; } @@ -5879,15 +6797,18 @@ LGraphNode.prototype.executeAction = function(action) ctx.shadowOffsetX = 2 * this.ds.scale; ctx.shadowOffsetY = 2 * this.ds.scale; ctx.shadowBlur = 3 * this.ds.scale; - } else ctx.shadowColor = "transparent"; + } else { + ctx.shadowColor = "transparent"; + } //custom draw collapsed method (draw after shadows because they are affected) if ( node.flags.collapsed && node.onDrawCollapsed && node.onDrawCollapsed(ctx, this) == true - ) + ) { return; + } //clip if required (mask) var shape = node._shape || LiteGraph.BOX_SHAPE; @@ -5913,10 +6834,11 @@ LGraphNode.prototype.executeAction = function(action) //Start clipping ctx.save(); ctx.beginPath(); - if (shape == LiteGraph.BOX_SHAPE) ctx.rect(0, 0, size[0], size[1]); - else if (shape == LiteGraph.ROUND_SHAPE) + if (shape == LiteGraph.BOX_SHAPE) { + ctx.rect(0, 0, size[0], size[1]); + } else if (shape == LiteGraph.ROUND_SHAPE) { ctx.roundRect(0, 0, size[0], size[1], 10); - else if (shape == LiteGraph.CIRCLE_SHAPE) + } else if (shape == LiteGraph.CIRCLE_SHAPE) { ctx.arc( size[0] * 0.5, size[1] * 0.5, @@ -5924,11 +6846,14 @@ LGraphNode.prototype.executeAction = function(action) 0, Math.PI * 2 ); + } ctx.clip(); } //draw shape - if (node.has_errors) bgcolor = "red"; + if (node.has_errors) { + bgcolor = "red"; + } this.drawNodeShape( node, ctx, @@ -5941,8 +6866,9 @@ LGraphNode.prototype.executeAction = function(action) ctx.shadowColor = "transparent"; //draw foreground - if (node.onDrawForeground) + if (node.onDrawForeground) { node.onDrawForeground(ctx, this, this.canvas); + } //connection slots ctx.textAlign = horizontal ? "center" : "left"; @@ -5959,7 +6885,7 @@ LGraphNode.prototype.executeAction = function(action) //render inputs and outputs if (!node.flags.collapsed) { //input connection slots - if (node.inputs) + if (node.inputs) { for (var i = 0; i < node.inputs.length; i++) { var slot = node.inputs[i]; @@ -5968,8 +6894,9 @@ LGraphNode.prototype.executeAction = function(action) if ( this.connecting_node && LiteGraph.isValidConnection(slot.type && out_slot.type) - ) + ) { ctx.globalAlpha = 0.4 * editor_alpha; + } ctx.fillStyle = slot.link != null @@ -5981,8 +6908,9 @@ LGraphNode.prototype.executeAction = function(action) var pos = node.getConnectionPos(true, i, slot_pos); pos[0] -= node.pos[0]; pos[1] -= node.pos[1]; - if (max_y < pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5) + if (max_y < pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5) { max_y = pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5; + } ctx.beginPath(); @@ -5990,20 +6918,21 @@ LGraphNode.prototype.executeAction = function(action) slot.type === LiteGraph.EVENT || slot.shape === LiteGraph.BOX_SHAPE ) { - if (horizontal) + if (horizontal) { ctx.rect( pos[0] - 5 + 0.5, pos[1] - 8 + 0.5, 10, 14 ); - else + } else { ctx.rect( pos[0] - 6 + 0.5, pos[1] - 5 + 0.5, 14, 10 ); + } } else if (slot.shape === LiteGraph.ARROW_SHAPE) { ctx.moveTo(pos[0] + 8, pos[1] + 0.5); ctx.lineTo(pos[0] - 4, pos[1] + 6 + 0.5); @@ -6020,27 +6949,33 @@ LGraphNode.prototype.executeAction = function(action) var text = slot.label != null ? slot.label : slot.name; if (text) { ctx.fillStyle = LiteGraph.NODE_TEXT_COLOR; - if (horizontal || slot.dir == LiteGraph.UP) + if (horizontal || slot.dir == LiteGraph.UP) { ctx.fillText(text, pos[0], pos[1] - 10); - else ctx.fillText(text, pos[0] + 10, pos[1] + 5); + } else { + ctx.fillText(text, pos[0] + 10, pos[1] + 5); + } } } } + } //output connection slots - if (this.connecting_node) ctx.globalAlpha = 0.4 * editor_alpha; + if (this.connecting_node) { + ctx.globalAlpha = 0.4 * editor_alpha; + } ctx.textAlign = horizontal ? "center" : "right"; ctx.strokeStyle = "black"; - if (node.outputs) + if (node.outputs) { for (var i = 0; i < node.outputs.length; i++) { var slot = node.outputs[i]; var pos = node.getConnectionPos(false, i, slot_pos); pos[0] -= node.pos[0]; pos[1] -= node.pos[1]; - if (max_y < pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5) + if (max_y < pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5) { max_y = pos[1] + LiteGraph.NODE_SLOT_HEIGHT * 0.5; + } ctx.fillStyle = slot.links && slot.links.length @@ -6055,20 +6990,21 @@ LGraphNode.prototype.executeAction = function(action) slot.type === LiteGraph.EVENT || slot.shape === LiteGraph.BOX_SHAPE ) { - if (horizontal) + if (horizontal) { ctx.rect( pos[0] - 5 + 0.5, pos[1] - 8 + 0.5, 10, 14 ); - else + } else { ctx.rect( pos[0] - 6 + 0.5, pos[1] - 5 + 0.5, 14, 10 ); + } } else if (slot.shape === LiteGraph.ARROW_SHAPE) { ctx.moveTo(pos[0] + 8, pos[1] + 0.5); ctx.lineTo(pos[0] - 4, pos[1] + 6 + 0.5); @@ -6091,18 +7027,23 @@ LGraphNode.prototype.executeAction = function(action) var text = slot.label != null ? slot.label : slot.name; if (text) { ctx.fillStyle = LiteGraph.NODE_TEXT_COLOR; - if (horizontal || slot.dir == LiteGraph.DOWN) + if (horizontal || slot.dir == LiteGraph.DOWN) { ctx.fillText(text, pos[0], pos[1] - 8); - else ctx.fillText(text, pos[0] - 10, pos[1] + 5); + } else { + ctx.fillText(text, pos[0] - 10, pos[1] + 5); + } } } } + } ctx.textAlign = "left"; ctx.globalAlpha = 1; if (node.widgets) { - if (horizontal || node.widgets_up) max_y = 2; + if (horizontal || node.widgets_up) { + max_y = 2; + } this.drawNodeWidgets( node, max_y, @@ -6121,7 +7062,9 @@ LGraphNode.prototype.executeAction = function(action) if (node.inputs) { for (var i = 0; i < node.inputs.length; i++) { var slot = node.inputs[i]; - if (slot.link == null) continue; + if (slot.link == null) { + continue; + } input_slot = slot; break; } @@ -6129,7 +7072,9 @@ LGraphNode.prototype.executeAction = function(action) if (node.outputs) { for (var i = 0; i < node.outputs.length; i++) { var slot = node.outputs[i]; - if (!slot.links || !slot.links.length) continue; + if (!slot.links || !slot.links.length) { + continue; + } output_slot = slot; } } @@ -6187,7 +7132,9 @@ LGraphNode.prototype.executeAction = function(action) } } - if (node.clip_area) ctx.restore(); + if (node.clip_area) { + ctx.restore(); + } ctx.globalAlpha = 1.0; }; @@ -6221,9 +7168,11 @@ LGraphNode.prototype.executeAction = function(action) var title_mode = node.constructor.title_mode; var render_title = true; - if (title_mode == LiteGraph.TRANSPARENT_TITLE) render_title = false; - else if (title_mode == LiteGraph.AUTOHIDE_TITLE && mouse_over) + if (title_mode == LiteGraph.TRANSPARENT_TITLE) { + render_title = false; + } else if (title_mode == LiteGraph.AUTOHIDE_TITLE && mouse_over) { render_title = true; + } var area = tmp_area; area[0] = 0; //x @@ -6237,12 +7186,12 @@ LGraphNode.prototype.executeAction = function(action) //if(node.flags.collapsed) { ctx.beginPath(); - if (shape == LiteGraph.BOX_SHAPE || low_quality) + if (shape == LiteGraph.BOX_SHAPE || low_quality) { ctx.fillRect(area[0], area[1], area[2], area[3]); - else if ( + } else if ( shape == LiteGraph.ROUND_SHAPE || shape == LiteGraph.CARD_SHAPE - ) + ) { ctx.roundRect( area[0], area[1], @@ -6251,7 +7200,7 @@ LGraphNode.prototype.executeAction = function(action) this.round_radius, shape == LiteGraph.CARD_SHAPE ? 0 : this.round_radius ); - else if (shape == LiteGraph.CIRCLE_SHAPE) + } else if (shape == LiteGraph.CIRCLE_SHAPE) { ctx.arc( size[0] * 0.5, size[1] * 0.5, @@ -6259,6 +7208,7 @@ LGraphNode.prototype.executeAction = function(action) 0, Math.PI * 2 ); + } ctx.fill(); ctx.shadowColor = "transparent"; @@ -6267,8 +7217,9 @@ LGraphNode.prototype.executeAction = function(action) } ctx.shadowColor = "transparent"; - if (node.onDrawBackground) + if (node.onDrawBackground) { node.onDrawBackground(ctx, this, this.canvas); + } //title bg (remember, it is rendered ABOVE the node) if (render_title || title_mode == LiteGraph.TRANSPARENT_TITLE) { @@ -6287,8 +7238,9 @@ LGraphNode.prototype.executeAction = function(action) ) { var title_color = node.constructor.title_color || fgcolor; - if (node.flags.collapsed) + if (node.flags.collapsed) { ctx.shadowColor = LiteGraph.DEFAULT_SHADOW_COLOR; + } //* gradient test if (this.use_gradients) { @@ -6301,16 +7253,18 @@ LGraphNode.prototype.executeAction = function(action) grad.addColorStop(1, "#000"); } ctx.fillStyle = grad; - } else ctx.fillStyle = title_color; + } else { + ctx.fillStyle = title_color; + } //ctx.globalAlpha = 0.5 * old_alpha; ctx.beginPath(); - if (shape == LiteGraph.BOX_SHAPE || low_quality) + if (shape == LiteGraph.BOX_SHAPE || low_quality) { ctx.rect(0, -title_height, size[0] + 1, title_height); - else if ( + } else if ( shape == LiteGraph.ROUND_SHAPE || shape == LiteGraph.CARD_SHAPE - ) + ) { ctx.roundRect( 0, -title_height, @@ -6319,6 +7273,7 @@ LGraphNode.prototype.executeAction = function(action) this.round_radius, node.flags.collapsed ? this.round_radius : 0 ); + } ctx.fill(); ctx.shadowColor = "transparent"; } @@ -6392,11 +7347,13 @@ LGraphNode.prototype.executeAction = function(action) ctx.font = this.title_text_font; var title = node.getTitle(); if (title) { - if (selected) ctx.fillStyle = "white"; - else + if (selected) { + ctx.fillStyle = "white"; + } else { ctx.fillStyle = node.constructor.title_text_color || this.node_title_color; + } if (node.flags.collapsed) { ctx.textAlign = "center"; var measure = ctx.measureText(title); @@ -6417,12 +7374,16 @@ LGraphNode.prototype.executeAction = function(action) } } - if (node.onDrawTitle) node.onDrawTitle(ctx); + if (node.onDrawTitle) { + node.onDrawTitle(ctx); + } } //render selection marker if (selected) { - if (node.onBounding) node.onBounding(area); + if (node.onBounding) { + node.onBounding(area); + } if (title_mode == LiteGraph.TRANSPARENT_TITLE) { area[1] -= title_height; @@ -6431,17 +7392,17 @@ LGraphNode.prototype.executeAction = function(action) ctx.lineWidth = 1; ctx.globalAlpha = 0.8; ctx.beginPath(); - if (shape == LiteGraph.BOX_SHAPE) + if (shape == LiteGraph.BOX_SHAPE) { ctx.rect( -6 + area[0], -6 + area[1], 12 + area[2], 12 + area[3] ); - else if ( + } else if ( shape == LiteGraph.ROUND_SHAPE || (shape == LiteGraph.CARD_SHAPE && node.flags.collapsed) - ) + ) { ctx.roundRect( -6 + area[0], -6 + area[1], @@ -6449,7 +7410,7 @@ LGraphNode.prototype.executeAction = function(action) 12 + area[3], this.round_radius * 2 ); - else if (shape == LiteGraph.CARD_SHAPE) + } else if (shape == LiteGraph.CARD_SHAPE) { ctx.roundRect( -6 + area[0], -6 + area[1], @@ -6458,7 +7419,7 @@ LGraphNode.prototype.executeAction = function(action) this.round_radius * 2, 2 ); - else if (shape == LiteGraph.CIRCLE_SHAPE) + } else if (shape == LiteGraph.CIRCLE_SHAPE) { ctx.arc( size[0] * 0.5, size[1] * 0.5, @@ -6466,6 +7427,7 @@ LGraphNode.prototype.executeAction = function(action) 0, Math.PI * 2 ); + } ctx.strokeStyle = "#FFF"; ctx.stroke(); ctx.strokeStyle = fgcolor; @@ -6502,31 +7464,40 @@ LGraphNode.prototype.executeAction = function(action) for (var n = 0, l = nodes.length; n < l; ++n) { var node = nodes[n]; //for every input (we render just inputs because it is easier as every slot can only have one input) - if (!node.inputs || !node.inputs.length) continue; + if (!node.inputs || !node.inputs.length) { + continue; + } for (var i = 0; i < node.inputs.length; ++i) { var input = node.inputs[i]; - if (!input || input.link == null) continue; + if (!input || input.link == null) { + continue; + } var link_id = input.link; var link = this.graph.links[link_id]; - if (!link) continue; + if (!link) { + continue; + } //find link info var start_node = this.graph.getNodeById(link.origin_id); - if (start_node == null) continue; + if (start_node == null) { + continue; + } var start_node_slot = link.origin_slot; var start_node_slotpos = null; - if (start_node_slot == -1) + if (start_node_slot == -1) { start_node_slotpos = [ start_node.pos[0] + 10, start_node.pos[1] + 10 ]; - else + } else { start_node_slotpos = start_node.getConnectionPos( false, start_node_slot, tempA ); + } var end_node_slotpos = node.getConnectionPos(true, i, tempB); //compute link bounding @@ -6544,11 +7515,15 @@ LGraphNode.prototype.executeAction = function(action) } //skip links outside of the visible area of the canvas - if (!overlapBounding(link_bounding, margin_area)) continue; + if (!overlapBounding(link_bounding, margin_area)) { + continue; + } var start_slot = start_node.outputs[start_node_slot]; var end_slot = node.inputs[i]; - if (!start_slot || !end_slot) continue; + if (!start_slot || !end_slot) { + continue; + } var start_dir = start_slot.dir || (start_node.horizontal ? LiteGraph.DOWN : LiteGraph.RIGHT); @@ -6616,24 +7591,34 @@ LGraphNode.prototype.executeAction = function(action) end_dir, num_sublines ) { - if (link) this.visible_links.push(link); + if (link) { + this.visible_links.push(link); + } //choose color - if (!color && link) + if (!color && link) { color = link.color || LGraphCanvas.link_type_colors[link.type]; - if (!color) color = this.default_link_color; - if (link != null && this.highlighted_links[link.id]) color = "#FFF"; + } + if (!color) { + color = this.default_link_color; + } + if (link != null && this.highlighted_links[link.id]) { + color = "#FFF"; + } start_dir = start_dir || LiteGraph.RIGHT; end_dir = end_dir || LiteGraph.LEFT; var dist = distance(a, b); - if (this.render_connections_border && this.ds.scale > 0.6) + if (this.render_connections_border && this.ds.scale > 0.6) { ctx.lineWidth = this.connections_width + 4; + } ctx.lineJoin = "round"; num_sublines = num_sublines || 1; - if (num_sublines > 1) ctx.lineWidth = 0.5; + if (num_sublines > 1) { + ctx.lineWidth = 0.5; + } //begin line shape ctx.beginPath(); @@ -6732,16 +7717,24 @@ LGraphNode.prototype.executeAction = function(action) var start_y = a[1]; var end_x = b[0]; var end_y = b[1]; - if (start_dir == LiteGraph.RIGHT) start_x += 10; - else start_y += 10; - if (end_dir == LiteGraph.LEFT) end_x -= 10; - else end_y -= 10; + if (start_dir == LiteGraph.RIGHT) { + start_x += 10; + } else { + start_y += 10; + } + if (end_dir == LiteGraph.LEFT) { + end_x -= 10; + } else { + end_y -= 10; + } ctx.lineTo(start_x, start_y); ctx.lineTo((start_x + end_x) * 0.5, start_y); ctx.lineTo((start_x + end_x) * 0.5, end_y); ctx.lineTo(end_x, end_y); ctx.lineTo(b[0], b[1]); - } else return; //unknown + } else { + return; + } //unknown } //rendering the outline of the connection can be a little bit slow @@ -6809,7 +7802,9 @@ LGraphNode.prototype.executeAction = function(action) if (this.render_curved_connections) { angleA = -Math.atan2(posB[0] - posA[0], posB[1] - posA[1]); angleB = -Math.atan2(posD[0] - posC[0], posD[1] - posC[1]); - } else angleB = angleA = b[1] > a[1] ? 0 : Math.PI; + } else { + angleB = angleA = b[1] > a[1] ? 0 : Math.PI; + } //render arrow ctx.save(); @@ -6931,13 +7926,14 @@ LGraphNode.prototype.executeAction = function(action) LiteGraph.NODE_TITLE_HEIGHT, LiteGraph.NODE_TITLE_HEIGHT ); - if (node.order == 0) + if (node.order == 0) { ctx.strokeRect( node.pos[0] - LiteGraph.NODE_TITLE_HEIGHT + 0.5, node.pos[1] - LiteGraph.NODE_TITLE_HEIGHT + 0.5, LiteGraph.NODE_TITLE_HEIGHT, LiteGraph.NODE_TITLE_HEIGHT ); + } ctx.fillStyle = "#FFF"; ctx.fillText( node.order, @@ -6958,7 +7954,9 @@ LGraphNode.prototype.executeAction = function(action) ctx, active_widget ) { - if (!node.widgets || !node.widgets.length) return 0; + if (!node.widgets || !node.widgets.length) { + return 0; + } var width = node.size[0]; var widgets = node.widgets; posY += 2; @@ -6973,7 +7971,9 @@ LGraphNode.prototype.executeAction = function(action) for (var i = 0; i < widgets.length; ++i) { var w = widgets[i]; var y = posY; - if (w.y) y = w.y; + if (w.y) { + y = w.y; + } w.last_y = y; ctx.strokeStyle = outline_color; ctx.fillStyle = "#222"; @@ -7014,8 +8014,9 @@ LGraphNode.prototype.executeAction = function(action) ctx.fill(); if (show_text) { ctx.fillStyle = "#999"; - if (w.name != null) + if (w.name != null) { ctx.fillText(w.name, margin * 2, y + H * 0.7); + } ctx.fillStyle = w.value ? "#DDD" : "#888"; ctx.textAlign = "right"; ctx.fillText( @@ -7078,7 +8079,7 @@ LGraphNode.prototype.executeAction = function(action) ctx.fillText(w.name, margin * 2 + 5, y + H * 0.7); ctx.fillStyle = "#DDD"; ctx.textAlign = "right"; - if (w.type == "number") + if (w.type == "number") { ctx.fillText( Number(w.value).toFixed( w.options.precision !== undefined @@ -7088,12 +8089,13 @@ LGraphNode.prototype.executeAction = function(action) width - margin * 2 - 20, y + H * 0.7 ); - else + } else { ctx.fillText( w.value, width - margin * 2 - 20, y + H * 0.7 ); + } } break; case "string": @@ -7107,15 +8109,18 @@ LGraphNode.prototype.executeAction = function(action) ctx.stroke(); if (show_text) { ctx.fillStyle = "#999"; - if (w.name != null) + if (w.name != null) { ctx.fillText(w.name, margin * 2, y + H * 0.7); + } ctx.fillStyle = "#DDD"; ctx.textAlign = "right"; ctx.fillText(w.value, width - margin * 2, y + H * 0.7); } break; default: - if (w.draw) w.draw(ctx, node, w, y, H); + if (w.draw) { + w.draw(ctx, node, w, y, H); + } break; } posY += H + 4; @@ -7133,7 +8138,9 @@ LGraphNode.prototype.executeAction = function(action) event, active_widget ) { - if (!node.widgets || !node.widgets.length) return null; + if (!node.widgets || !node.widgets.length) { + return null; + } var x = pos[0] - node.pos[0]; var y = pos[1] - node.pos[1]; @@ -7156,10 +8163,11 @@ LGraphNode.prototype.executeAction = function(action) if (event.type === "mousemove") { break; } - if (w.callback) + if (w.callback) { setTimeout(function() { w.callback(w, that, node, pos); }, 20); + } w.clicked = true; this.dirty_canvas = true; break; @@ -7169,10 +8177,11 @@ LGraphNode.prototype.executeAction = function(action) w.value = w.options.min + (w.options.max - w.options.min) * nvalue; - if (w.callback) + if (w.callback) { setTimeout(function() { inner_value_change(w, w.value); }, 20); + } this.dirty_canvas = true; break; case "number": @@ -7183,17 +8192,20 @@ LGraphNode.prototype.executeAction = function(action) if ( w.options.min != null && w.value < w.options.min - ) + ) { w.value = w.options.min; + } if ( w.options.max != null && w.value > w.options.max - ) + ) { w.value = w.options.max; + } } else if (event.type == "mousedown") { var values = w.options.values; - if (values && values.constructor === Function) + if (values && values.constructor === Function) { values = w.options.values(w, node); + } var delta = x < 40 ? -1 : x > width - 40 ? 1 : 0; if (w.type == "number") { @@ -7201,17 +8213,23 @@ LGraphNode.prototype.executeAction = function(action) if ( w.options.min != null && w.value < w.options.min - ) + ) { w.value = w.options.min; + } if ( w.options.max != null && w.value > w.options.max - ) + ) { w.value = w.options.max; + } } else if (delta) { var index = values.indexOf(w.value) + delta; - if (index >= values.length) index = 0; - if (index < 0) index = values.length - 1; + if (index >= values.length) { + index = 0; + } + if (index < 0) { + index = values.length - 1; + } w.value = values[index]; } else { var menu = new LiteGraph.ContextMenu( @@ -7243,15 +8261,16 @@ LGraphNode.prototype.executeAction = function(action) case "toggle": if (event.type == "mousedown") { w.value = !w.value; - if (w.callback) + if (w.callback) { setTimeout(function() { inner_value_change(w, w.value); }, 20); + } } break; case "string": case "text": - if (event.type == "mousedown") + if (event.type == "mousedown") { this.prompt( "Value", w.value, @@ -7261,9 +8280,12 @@ LGraphNode.prototype.executeAction = function(action) }.bind(w), event ); + } break; default: - if (w.mouse) w.mouse(ctx, event, [x, y], node); + if (w.mouse) { + w.mouse(ctx, event, [x, y], node); + } break; } @@ -7276,10 +8298,12 @@ LGraphNode.prototype.executeAction = function(action) if ( widget.property && node.properties[widget.property] !== undefined - ) + ) { node.properties[widget.property] = value; - if (widget.callback) + } + if (widget.callback) { widget.callback(widget.value, that, node, pos, event); + } } return null; @@ -7290,7 +8314,9 @@ LGraphNode.prototype.executeAction = function(action) * @method drawGroups **/ LGraphCanvas.prototype.drawGroups = function(canvas, ctx) { - if (!this.graph) return; + if (!this.graph) { + return; + } var groups = this.graph._groups; @@ -7300,7 +8326,9 @@ LGraphNode.prototype.executeAction = function(action) for (var i = 0; i < groups.length; ++i) { var group = groups[i]; - if (!overlapBounding(this.visible_area, group._bounding)) continue; //out of the visible area + if (!overlapBounding(this.visible_area, group._bounding)) { + continue; + } //out of the visible area ctx.fillStyle = group.color || "#335"; ctx.strokeStyle = group.color || "#335"; @@ -7330,8 +8358,9 @@ LGraphNode.prototype.executeAction = function(action) LGraphCanvas.prototype.adjustNodesSize = function() { var nodes = this.graph._nodes; - for (var i = 0; i < nodes.length; ++i) + for (var i = 0; i < nodes.length; ++i) { nodes[i].size = nodes[i].computeSize(); + } this.setDirty(true, true); }; @@ -7346,7 +8375,9 @@ LGraphNode.prototype.executeAction = function(action) height = parent.offsetHeight; } - if (this.canvas.width == width && this.canvas.height == height) return; + if (this.canvas.width == width && this.canvas.height == height) { + return; + } this.canvas.width = width; this.canvas.height = height; @@ -7382,7 +8413,9 @@ LGraphNode.prototype.executeAction = function(action) if (delta < 1 && self.editor_alpha < 0.01) { clearInterval(t); - if (delta < 1) self.live_mode = true; + if (delta < 1) { + self.live_mode = true; + } } if (delta > 1 && self.editor_alpha > 0.99) { clearInterval(t); @@ -7461,13 +8494,15 @@ LGraphNode.prototype.executeAction = function(action) var values = LiteGraph.getNodeTypesCategories(); var entries = []; - for (var i in values) - if (values[i]) + for (var i in values) { + if (values[i]) { entries.push({ value: values[i], content: values[i], has_submenu: true }); + } + } //show categories var menu = new LiteGraph.ContextMenu( @@ -7483,12 +8518,14 @@ LGraphNode.prototype.executeAction = function(action) canvas.filter ); var values = []; - for (var i in node_types) - if (!node_types[i].skip_list) + for (var i in node_types) { + if (!node_types[i].skip_list) { values.push({ content: node_types[i].title, value: node_types[i].type }); + } + } new LiteGraph.ContextMenu( values, @@ -7521,17 +8558,21 @@ LGraphNode.prototype.executeAction = function(action) prev_menu, node ) { - if (!node) return; + if (!node) { + return; + } var that = this; var canvas = LGraphCanvas.active_canvas; var ref_window = canvas.getCanvasWindow(); var options = node.optional_inputs; - if (node.onGetInputs) options = node.onGetInputs(); + if (node.onGetInputs) { + options = node.onGetInputs(); + } var entries = []; - if (options) + if (options) { for (var i in options) { var entry = options[i]; if (!entry) { @@ -7539,15 +8580,24 @@ LGraphNode.prototype.executeAction = function(action) continue; } var label = entry[0]; - if (entry[2] && entry[2].label) label = entry[2].label; + if (entry[2] && entry[2].label) { + label = entry[2].label; + } var data = { content: label, value: entry }; - if (entry[1] == LiteGraph.ACTION) data.className = "event"; + if (entry[1] == LiteGraph.ACTION) { + data.className = "event"; + } entries.push(data); } + } - if (this.onMenuNodeInputs) entries = this.onMenuNodeInputs(entries); + if (this.onMenuNodeInputs) { + entries = this.onMenuNodeInputs(entries); + } - if (!entries.length) return; + if (!entries.length) { + return; + } var menu = new LiteGraph.ContextMenu( entries, @@ -7561,9 +8611,13 @@ LGraphNode.prototype.executeAction = function(action) ); function inner_clicked(v, e, prev) { - if (!node) return; + if (!node) { + return; + } - if (v.callback) v.callback.call(that, node, v, e, prev); + if (v.callback) { + v.callback.call(that, node, v, e, prev); + } if (v.value) { node.addInput(v.value[0], v.value[1], v.value[2]); @@ -7581,17 +8635,21 @@ LGraphNode.prototype.executeAction = function(action) prev_menu, node ) { - if (!node) return; + if (!node) { + return; + } var that = this; var canvas = LGraphCanvas.active_canvas; var ref_window = canvas.getCanvasWindow(); var options = node.optional_outputs; - if (node.onGetOutputs) options = node.onGetOutputs(); + if (node.onGetOutputs) { + options = node.onGetOutputs(); + } var entries = []; - if (options) + if (options) { for (var i in options) { var entry = options[i]; if (!entry) { @@ -7604,18 +8662,28 @@ LGraphNode.prototype.executeAction = function(action) node.flags && node.flags.skip_repeated_outputs && node.findOutputSlot(entry[0]) != -1 - ) - continue; //skip the ones already on + ) { + continue; + } //skip the ones already on var label = entry[0]; - if (entry[2] && entry[2].label) label = entry[2].label; + if (entry[2] && entry[2].label) { + label = entry[2].label; + } var data = { content: label, value: entry }; - if (entry[1] == LiteGraph.EVENT) data.className = "event"; + if (entry[1] == LiteGraph.EVENT) { + data.className = "event"; + } entries.push(data); } + } - if (this.onMenuNodeOutputs) entries = this.onMenuNodeOutputs(entries); + if (this.onMenuNodeOutputs) { + entries = this.onMenuNodeOutputs(entries); + } - if (!entries.length) return; + if (!entries.length) { + return; + } var menu = new LiteGraph.ContextMenu( entries, @@ -7629,11 +8697,17 @@ LGraphNode.prototype.executeAction = function(action) ); function inner_clicked(v, e, prev) { - if (!node) return; + if (!node) { + return; + } - if (v.callback) v.callback.call(that, node, v, e, prev); + if (v.callback) { + v.callback.call(that, node, v, e, prev); + } - if (!v.value) return; + if (!v.value) { + return; + } var value = v.value[1]; @@ -7643,8 +8717,9 @@ LGraphNode.prototype.executeAction = function(action) ) { //submenu why? var entries = []; - for (var i in value) + for (var i in value) { entries.push({ content: i, value: value[i] }); + } new LiteGraph.ContextMenu(entries, { event: e, callback: inner_clicked, @@ -7668,7 +8743,9 @@ LGraphNode.prototype.executeAction = function(action) prev_menu, node ) { - if (!node || !node.properties) return; + if (!node || !node.properties) { + return; + } var that = this; var canvas = LGraphCanvas.active_canvas; @@ -7691,7 +8768,9 @@ LGraphNode.prototype.executeAction = function(action) value: i }); } - if (!entries.length) return; + if (!entries.length) { + return; + } var menu = new LiteGraph.ContextMenu( entries, @@ -7706,7 +8785,9 @@ LGraphNode.prototype.executeAction = function(action) ); function inner_clicked(v, options, e, prev) { - if (!node) return; + if (!node) { + return; + } var rect = this.getBoundingClientRect(); canvas.showEditPropertyValue(node, v.value, { position: [rect.left, rect.top] @@ -7723,7 +8804,9 @@ LGraphNode.prototype.executeAction = function(action) }; LGraphCanvas.onResizeNode = function(value, options, e, menu, node) { - if (!node) return; + if (!node) { + return; + } node.size = node.computeSize(); node.setDirtyCanvas(true, true); }; @@ -7766,7 +8849,9 @@ LGraphNode.prototype.executeAction = function(action) this.focus(); }); input.addEventListener("keydown", function(e) { - if (e.keyCode != 13) return; + if (e.keyCode != 13) { + return; + } inner(); e.preventDefault(); e.stopPropagation(); @@ -7801,10 +8886,15 @@ LGraphNode.prototype.executeAction = function(action) } function setValue(value) { - if (item.type == "Number") value = Number(value); - else if (item.type == "Boolean") value = Boolean(value); + if (item.type == "Number") { + value = Number(value); + } else if (item.type == "Boolean") { + value = Boolean(value); + } node[property] = value; - if (dialog.parentNode) dialog.parentNode.removeChild(dialog); + if (dialog.parentNode) { + dialog.parentNode.removeChild(dialog); + } node.setDirtyCanvas(true, true); } }; @@ -7822,17 +8912,24 @@ LGraphNode.prototype.executeAction = function(action) " "; dialog.close = function() { that.prompt_box = null; - if (dialog.parentNode) dialog.parentNode.removeChild(dialog); + if (dialog.parentNode) { + dialog.parentNode.removeChild(dialog); + } }; - if (this.ds.scale > 1) + if (this.ds.scale > 1) { dialog.style.transform = "scale(" + this.ds.scale + ")"; + } dialog.addEventListener("mouseleave", function(e) { - if (!modified) dialog.close(); + if (!modified) { + dialog.close(); + } }); - if (that.prompt_box) that.prompt_box.close(); + if (that.prompt_box) { + that.prompt_box.close(); + } that.prompt_box = dialog; var first = null; @@ -7847,20 +8944,26 @@ LGraphNode.prototype.executeAction = function(action) var input = dialog.querySelector("input"); input.addEventListener("keydown", function(e) { modified = true; - if (e.keyCode == 27) + if (e.keyCode == 27) { //ESC dialog.close(); - else if (e.keyCode == 13) { - if (callback) callback(this.value); + } else if (e.keyCode == 13) { + if (callback) { + callback(this.value); + } dialog.close(); - } else return; + } else { + return; + } e.preventDefault(); e.stopPropagation(); }); var button = dialog.querySelector("button"); button.addEventListener("click", function(e) { - if (callback) callback(input.value); + if (callback) { + callback(input.value); + } that.setDirty(true); dialog.close(); }); @@ -7907,13 +9010,16 @@ LGraphNode.prototype.executeAction = function(action) setTimeout(function() { that.canvas.focus(); }, 20); //important, if canvas loses focus keys wont be captured - if (dialog.parentNode) dialog.parentNode.removeChild(dialog); + if (dialog.parentNode) { + dialog.parentNode.removeChild(dialog); + } }; var timeout_close = null; - if (this.ds.scale > 1) + if (this.ds.scale > 1) { dialog.style.transform = "scale(" + this.ds.scale + ")"; + } dialog.addEventListener("mouseenter", function(e) { if (timeout_close) { @@ -7929,7 +9035,9 @@ LGraphNode.prototype.executeAction = function(action) }, 500); }); - if (that.search_box) that.search_box.close(); + if (that.search_box) { + that.search_box.close(); + } that.search_box = dialog; var helper = dialog.querySelector(".helper"); @@ -7944,21 +9052,27 @@ LGraphNode.prototype.executeAction = function(action) this.focus(); }); input.addEventListener("keydown", function(e) { - if (e.keyCode == 38) + if (e.keyCode == 38) { //UP changeSelection(false); - else if (e.keyCode == 40) + } else if (e.keyCode == 40) { //DOWN changeSelection(true); - else if (e.keyCode == 27) + } else if (e.keyCode == 27) { //ESC dialog.close(); - else if (e.keyCode == 13) { - if (selected) select(selected.innerHTML); - else if (first) select(first); - else dialog.close(); + } else if (e.keyCode == 13) { + if (selected) { + select(selected.innerHTML); + } else if (first) { + select(first); + } else { + dialog.close(); + } } else { - if (timeout) clearInterval(timeout); + if (timeout) { + clearInterval(timeout); + } timeout = setTimeout(refreshHelper, 10); return; } @@ -7991,11 +9105,13 @@ LGraphNode.prototype.executeAction = function(action) function select(name) { if (name) { - if (that.onSearchBoxSelection) + if (that.onSearchBoxSelection) { that.onSearchBoxSelection(name, event, graphcanvas); - else { + } else { var extra = LiteGraph.searchbox_extras[name]; - if (extra) name = extra.type; + if (extra) { + name = extra.type; + } var node = LiteGraph.createNode(name); if (node) { @@ -8006,30 +9122,38 @@ LGraphNode.prototype.executeAction = function(action) } if (extra && extra.data) { - if (extra.data.properties) - for (var i in extra.data.properties) + if (extra.data.properties) { + for (var i in extra.data.properties) { node.addProperty( extra.data.properties[i][0], extra.data.properties[i][0] ); + } + } if (extra.data.inputs) { node.inputs = []; - for (var i in extra.data.inputs) + for (var i in extra.data.inputs) { node.addOutput( extra.data.inputs[i][0], extra.data.inputs[i][1] ); + } } if (extra.data.outputs) { node.outputs = []; - for (var i in extra.data.outputs) + for (var i in extra.data.outputs) { node.addOutput( extra.data.outputs[i][0], extra.data.outputs[i][1] ); + } + } + if (extra.data.title) { + node.title = extra.data.title; + } + if (extra.data.json) { + node.configure(extra.data.json); } - if (extra.data.title) node.title = extra.data.title; - if (extra.data.json) node.configure(extra.data.json); } } } @@ -8039,18 +9163,24 @@ LGraphNode.prototype.executeAction = function(action) function changeSelection(forward) { var prev = selected; - if (selected) selected.classList.remove("selected"); - if (!selected) + if (selected) { + selected.classList.remove("selected"); + } + if (!selected) { selected = forward ? helper.childNodes[0] : helper.childNodes[helper.childNodes.length]; - else { + } else { selected = forward ? selected.nextSibling : selected.previousSibling; - if (!selected) selected = prev; + if (!selected) { + selected = prev; + } + } + if (!selected) { + return; } - if (!selected) return; selected.classList.add("selected"); selected.scrollIntoView(); } @@ -8060,25 +9190,33 @@ LGraphNode.prototype.executeAction = function(action) var str = input.value; first = null; helper.innerHTML = ""; - if (!str) return; + if (!str) { + return; + } if (that.onSearchBox) { var list = that.onSearchBox(helper, str, graphcanvas); - if (list) - for (var i = 0; i < list.length; ++i) addResult(list[i]); + if (list) { + for (var i = 0; i < list.length; ++i) { + addResult(list[i]); + } + } } else { var c = 0; str = str.toLowerCase(); //extras for (var i in LiteGraph.searchbox_extras) { var extra = LiteGraph.searchbox_extras[i]; - if (extra.desc.toLowerCase().indexOf(str) === -1) continue; + if (extra.desc.toLowerCase().indexOf(str) === -1) { + continue; + } addResult(extra.desc, "searchbox_extra"); if ( LGraphCanvas.search_limit !== -1 && c++ > LGraphCanvas.search_limit - ) + ) { break; + } } if (Array.prototype.filter) { @@ -8093,8 +9231,9 @@ LGraphNode.prototype.executeAction = function(action) if ( LGraphCanvas.search_limit !== -1 && c++ > LGraphCanvas.search_limit - ) + ) { break; + } } } else { for (var i in LiteGraph.registered_node_types) { @@ -8103,8 +9242,9 @@ LGraphNode.prototype.executeAction = function(action) if ( LGraphCanvas.search_limit !== -1 && c++ > LGraphCanvas.search_limit - ) + ) { break; + } } } } @@ -8112,11 +9252,15 @@ LGraphNode.prototype.executeAction = function(action) function addResult(type, className) { var help = document.createElement("div"); - if (!first) first = type; + if (!first) { + first = type; + } help.innerText = type; help.dataset["type"] = escape(type); help.className = "litegraph lite-search-item"; - if (className) help.className += " " + className; + if (className) { + help.className += " " + className; + } help.addEventListener("click", function(e) { select(unescape(this.dataset["type"])); }); @@ -8132,23 +9276,30 @@ LGraphNode.prototype.executeAction = function(action) property, options ) { - if (!node || node.properties[property] === undefined) return; + if (!node || node.properties[property] === undefined) { + return; + } options = options || {}; var that = this; var type = "string"; - if (node.properties[property] !== null) + if (node.properties[property] !== null) { type = typeof node.properties[property]; + } //for arrays if (type == "object") { - if (node.properties[property].length) type = "array"; + if (node.properties[property].length) { + type = "array"; + } } var info = null; - if (node.getPropertyInfo) info = node.getPropertyInfo(property); + if (node.getPropertyInfo) { + info = node.getPropertyInfo(property); + } if (node.properties_info) { for (var i = 0; i < node.properties_info.length; ++i) { if (node.properties_info[i].name == property) { @@ -8158,13 +9309,15 @@ LGraphNode.prototype.executeAction = function(action) } } - if (info !== undefined && info !== null && info.type) type = info.type; + if (info !== undefined && info !== null && info.type) { + type = info.type; + } var input_html = ""; - if (type == "string" || type == "number" || type == "array") + if (type == "string" || type == "number" || type == "array") { input_html = ""; - else if (type == "enum" && info.values) { + } else if (type == "enum" && info.values) { input_html = "