mirror of
https://github.com/fluxscape/fluxscape.git
synced 2026-01-12 07:12:54 +01:00
Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
This commit is contained in:
42
packages/noodl-runtime/src/variants.js
Normal file
42
packages/noodl-runtime/src/variants.js
Normal file
@@ -0,0 +1,42 @@
|
||||
class Variants {
|
||||
|
||||
constructor({graphModel, getNodeScope}) {
|
||||
this.getNodeScope = getNodeScope;
|
||||
|
||||
if(graphModel) {
|
||||
this.graphModel = graphModel;
|
||||
graphModel.on('variantUpdated', variant => this.onVariantUpdated(variant));
|
||||
}
|
||||
}
|
||||
|
||||
getVariant(typename, name) {
|
||||
if(!this.graphModel) return undefined;
|
||||
|
||||
return this.graphModel.getVariant(typename, name);
|
||||
}
|
||||
|
||||
onVariantUpdated(variant) {
|
||||
const nodeScope = this.getNodeScope();
|
||||
if(!nodeScope) return;
|
||||
|
||||
//get all nodes with the type the variant applies to
|
||||
const nodes = nodeScope.getNodesWithTypeRecursive(variant.typename);
|
||||
|
||||
//and filter for the ones using the updated variant
|
||||
const nodesWithVariant = nodes.filter(node => {
|
||||
|
||||
//if a variant has been set during runtime, it'll override the value from the model
|
||||
if(node.variant) return node.variant.name === variant.name;
|
||||
|
||||
//otherwise check the model (which always matches the editor)
|
||||
return node.model && node.model.variant === variant.name;
|
||||
});
|
||||
|
||||
//and re-apply the variant
|
||||
for(const node of nodesWithVariant) {
|
||||
node.setVariant(variant);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Variants;
|
||||
Reference in New Issue
Block a user