Skip to content

Commit

Permalink
Merge pull request jerosoler#117 from pavlyuts/id-uuid
Browse files Browse the repository at this point in the history
Use of UUID as node id instead of integer index
  • Loading branch information
jerosoler authored Feb 19, 2021
2 parents 2c0305d + 317cc32 commit d71f7b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Parameter | Type | Default | Description
`zoom_value` | Number | 0.1 | Default zoom value update
`zoom_last_value` | Number | 1 | Default zoom last value
`draggable_inputs` | Boolean | true | Drag nodes on click inputs
`useuuid` | Boolean | false | Use UUID as node ID instead of integer index. Only affect newly created nodes, do not affect imported nodes

### Reroute
Active reroute connections. Use before `start` or `import`.
Expand Down
36 changes: 29 additions & 7 deletions src/drawflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class Drawflow {
this.first_click = null;
this.force_first_input = false;
this.draggable_inputs = true;
this.useuuid = false;



Expand Down Expand Up @@ -1324,12 +1325,17 @@ export default class Drawflow {
}

addNode (name, num_in, num_out, ele_pos_x, ele_pos_y, classoverride, data, html, typenode = false) {
if (this.useuuid) {
var newNodeId = this.getUuid();
} else {
var newNodeId = this.nodeId;
}
const parent = document.createElement('div');
parent.classList.add("parent-node");

const node = document.createElement('div');
node.innerHTML = "";
node.setAttribute("id", "node-"+this.nodeId);
node.setAttribute("id", "node-"+newNodeId);
node.classList.add("drawflow-node");
if(classoverride != '') {
node.classList.add(classoverride);
Expand Down Expand Up @@ -1421,7 +1427,7 @@ export default class Drawflow {
parent.appendChild(node);
this.precanvas.appendChild(parent);
var json = {
id: this.nodeId,
id: newNodeId,
name: name,
data: data,
class: classoverride,
Expand All @@ -1432,11 +1438,12 @@ export default class Drawflow {
pos_x: ele_pos_x,
pos_y: ele_pos_y,
}
this.drawflow.drawflow[this.module].data[this.nodeId] = json;
this.dispatch('nodeCreated', this.nodeId);
var nodeId = this.nodeId;
this.nodeId++;
return nodeId;
this.drawflow.drawflow[this.module].data[newNodeId] = json;
this.dispatch('nodeCreated', newNodeId);
if (!this.useuuid) {
this.nodeId++;
}
return newNodeId;
}

addNodeImport (dataNode, precanvas) {
Expand Down Expand Up @@ -2050,5 +2057,20 @@ export default class Drawflow {
listener(details);
});
}

getUuid() {
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";

var uuid = s.join("");
return uuid;
}

}

0 comments on commit d71f7b1

Please sign in to comment.