Skip to content

Commit

Permalink
the-graph: Move component merging logic out of Polymer element
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnor committed Jan 6, 2017
1 parent b5ce8fb commit bc828e8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 59 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ require("./the-graph/font-awesome-unicode-map.js").register(g);
g.TheGraph.thumb = require('./the-graph-thumb/the-graph-thumb.js');
g.TheGraph.nav = require('./the-graph-nav/the-graph-nav.js');
g.TheGraph.autolayout = require('./the-graph/the-graph-autolayout.js');
g.TheGraph.library = require('./the-graph/the-graph-library.js');

module.exports = g.TheGraph;
63 changes: 63 additions & 0 deletions the-graph/the-graph-library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Component library functionality
function mergeComponentDefinition(component, definition) {
// In cases where a component / subgraph ports change,
// we don't want the connections hanging in middle of node
// TODO visually indicate that port is a ghost
if (component === definition) {
return definition;
}
var _i, _j, _len, _len1, exists;
var cInports = component.inports;
var dInports = definition.inports;

if (cInports !== dInports) {
for (_i = 0, _len = cInports.length; _i < _len; _i++) {
var cInport = cInports[_i];
exists = false;
for (_j = 0, _len1 = dInports.length; _j < _len1; _j++) {
var dInport = dInports[_j];
if (cInport.name === dInport.name) {
exists = true;
}
}
if (!exists) {
dInports.push(cInport);
}
}
}

var cOutports = component.outports;
var dOutports = definition.outports;

if (cOutports !== dOutports) {
for (_i = 0, _len = cOutports.length; _i < _len; _i++) {
var cOutport = cOutports[_i];
exists = false;
for (_j = 0, _len1 = dOutports.length; _j < _len1; _j++) {
var dOutport = dOutports[_j];
if (cOutport.name === dOutport.name) {
exists = true;
}
}
if (!exists) {
dOutports.push(cOutport);
}
}
}

if (definition.icon !== 'cog') {
// Use the latest icon given
component.icon = definition.icon;
} else {
// we should use the icon from the library
definition.icon = component.icon;
}
// a component could also define a svg icon
definition.iconsvg = component.iconsvg;

return definition;
}

module.exports = {
mergeComponentDefinition: mergeComponentDefinition,
};
60 changes: 1 addition & 59 deletions the-graph/the-graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,64 +312,6 @@
this.rerender({libraryDirty:true});
}.bind(this), 200);
},
mergeComponentDefinition: function (component, definition) {
// In cases where a component / subgraph ports change,
// we don't want the connections hanging in middle of node
// TODO visually indicate that port is a ghost
if (component === definition) {
return definition;
}
var _i, _j, _len, _len1, exists;
var cInports = component.inports;
var dInports = definition.inports;

if (cInports !== dInports) {
for (_i = 0, _len = cInports.length; _i < _len; _i++) {
var cInport = cInports[_i];
exists = false;
for (_j = 0, _len1 = dInports.length; _j < _len1; _j++) {
var dInport = dInports[_j];
if (cInport.name === dInport.name) {
exists = true;
}
}
if (!exists) {
dInports.push(cInport);
}
}
}

var cOutports = component.outports;
var dOutports = definition.outports;

if (cOutports !== dOutports) {
for (_i = 0, _len = cOutports.length; _i < _len; _i++) {
var cOutport = cOutports[_i];
exists = false;
for (_j = 0, _len1 = dOutports.length; _j < _len1; _j++) {
var dOutport = dOutports[_j];
if (cOutport.name === dOutport.name) {
exists = true;
}
}
if (!exists) {
dOutports.push(cOutport);
}
}
}

if (definition.icon !== 'cog') {
// Use the latest icon given
component.icon = definition.icon;
} else {
// we should use the icon from the library
definition.icon = component.icon;
}
// a component could also define a svg icon
definition.iconsvg = component.iconsvg;

return definition;
},
registerComponent: function (definition, generated) {
var component = this.library[definition.name];
var def = definition;
Expand All @@ -378,7 +320,7 @@
// Don't override real one with generated dummy
return;
}
def = this.mergeComponentDefinition(component, definition);
def = TheGraph.library.mergeComponentDefinition(component, definition);
}
this.library[definition.name] = def;
// So changes are rendered
Expand Down

0 comments on commit bc828e8

Please sign in to comment.