Skip to content

Commit

Permalink
createTargetComponentNames attribute type
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp authored and jaltekruse committed Feb 24, 2023
1 parent 933f290 commit 34e5d68
Show file tree
Hide file tree
Showing 14 changed files with 755 additions and 363 deletions.
407 changes: 391 additions & 16 deletions cypress/e2e/DoenetML/tagSpecific/callaction.cy.js

Large diffs are not rendered by default.

55 changes: 38 additions & 17 deletions src/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,6 @@ export default class Core {
return [];
}

if (parent.isShadow) {
console.warn(`Cannot add children to parent ${parentName} as it is a shadow component.`);
return [];
}

ancestors = [
{
componentName: parentName,
Expand Down Expand Up @@ -1253,7 +1248,7 @@ export default class Core {
});

}

if (componentClass.keepChildrenSerialized) {
let childrenAddressed = new Set([]);

Expand Down Expand Up @@ -2610,12 +2605,10 @@ export default class Core {
hasEssential: true,
};

let attributeFromPrimitive = !attributeSpecification.createComponentOfType;

if (attributeSpecification.public) {
stateVarDef.public = true;
stateVarDef.shadowingInstructions = {};
if (attributeFromPrimitive) {
if (attributeSpecification.createPrimitiveOfType) {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createPrimitiveOfType;
if (stateVarDef.shadowingInstructions.createComponentOfType === "string") {
stateVarDef.shadowingInstructions.createComponentOfType = "text";
Expand All @@ -2624,14 +2617,16 @@ export default class Core {
} else if (stateVarDef.shadowingInstructions.createComponentOfType === "numberArray") {
stateVarDef.shadowingInstructions.createComponentOfType = "numberList";
}
} else if (attributeSpecification.createTargetComponentNames) {
throw Error("Cannot make a public state variable from an attribute with createTargetComponentNames");
} else {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createComponentOfType;
}
}

let stateVariableForAttributeValue;

if (!attributeFromPrimitive) {
if (attributeSpecification.createComponentOfType) {

let attributeClass = this.componentInfoObjects.allComponentClasses[attributeSpecification.createComponentOfType];
if (!attributeClass) {
Expand All @@ -2656,11 +2651,16 @@ export default class Core {
variableName: attributeSpecification.fallBackToParentStateVariable
}
}
if (attributeFromPrimitive) {
if (attributeSpecification.createPrimitiveOfType) {
dependencies.attributePrimitive = {
dependencyType: "attributePrimitive",
attributeName: attrName
}
} else if (attributeSpecification.createTargetComponentNames) {
dependencies.attributeTargetComponentNames = {
dependencyType: "attributeTargetComponentNames",
attributeName: attrName
}
} else {
dependencies.attributeComponent = {
dependencyType: "attributeComponent",
Expand All @@ -2682,6 +2682,10 @@ export default class Core {
&& dependencyValues.attributePrimitive !== null
) {
attributeValue = dependencyValues.attributePrimitive;
} else if (dependencyValues.attributeTargetComponentNames !== undefined
&& dependencyValues.attributeTargetComponentNames !== null
) {
attributeValue = dependencyValues.attributeTargetComponentNames;
} else {

// parentValue would be undefined if fallBackToParentStateVariable wasn't specified
Expand Down Expand Up @@ -2718,6 +2722,10 @@ export default class Core {
// can't invert if have primitive
return { success: false }
}
if (dependencyValues.attributeTargetComponentNames !== undefined && dependencyValues.attributeTargetComponentNames !== null) {
// can't invert if have target component names
return { success: false }
}

let haveParentValue = dependencyValues.parentValue !== undefined
&& dependencyValues.parentValue !== null;
Expand Down Expand Up @@ -2801,12 +2809,10 @@ export default class Core {
hasEssential: true,
};

let attributeFromPrimitive = !attributeSpecification.createComponentOfType;

if (attributeSpecification.public) {
stateVarDef.public = true;
stateVarDef.shadowingInstructions = {};
if (attributeFromPrimitive) {
if (attributeSpecification.createPrimitiveOfType) {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createPrimitiveOfType;
if (stateVarDef.shadowingInstructions.createComponentOfType === "string") {
stateVarDef.shadowingInstructions.createComponentOfType = "text";
Expand All @@ -2815,6 +2821,8 @@ export default class Core {
} else if (stateVarDef.shadowingInstructions.createComponentOfType === "numberArray") {
stateVarDef.shadowingInstructions.createComponentOfType = "numberList";
}
} else if (attributeSpecification.createTargetComponentNames) {
throw Error("Cannot make a public state variable from an attribute with createTargetComponentNames");
} else {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createComponentOfType;
}
Expand Down Expand Up @@ -2989,7 +2997,7 @@ export default class Core {
if (attributeSpecification.public) {
stateVarDef.public = true;
stateVarDef.shadowingInstructions = {};
if (attributeFromPrimitive) {
if (attributeSpecification.createPrimitiveOfType) {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createPrimitiveOfType;
if (stateVarDef.shadowingInstructions.createComponentOfType === "string") {
stateVarDef.shadowingInstructions.createComponentOfType = "text";
Expand All @@ -2998,6 +3006,8 @@ export default class Core {
} else if (stateVarDef.shadowingInstructions.createComponentOfType === "numberArray") {
stateVarDef.shadowingInstructions.createComponentOfType = "numberList";
}
} else if (attributeSpecification.createTargetComponentNames) {
throw Error("Cannot make a public state variable from an attribute with createTargetComponentNames");
} else {
stateVarDef.shadowingInstructions.createComponentOfType = attributeSpecification.createComponentOfType;
}
Expand All @@ -3006,7 +3016,7 @@ export default class Core {

let stateVariableForAttributeValue;

if (!attributeFromPrimitive) {
if (attributeSpecification.createComponentOfType) {

let attributeClass = this.componentInfoObjects.allComponentClasses[attributeSpecification.createComponentOfType];
if (!attributeClass) {
Expand All @@ -3024,11 +3034,16 @@ export default class Core {

let thisDependencies = {}

if (attributeFromPrimitive) {
if (attributeSpecification.createPrimitiveOfType) {
thisDependencies.attributePrimitive = {
dependencyType: "attributePrimitive",
attributeName: attrName
}
} else if (attributeSpecification.createTargetComponentNames) {
thisDependencies.attributeTargetComponentNames = {
dependencyType: "attributeTargetComponentNames",
attributeName: attrName
}
} else {
thisDependencies.attributeComponent = {
dependencyType: "attributeComponent",
Expand All @@ -3053,6 +3068,8 @@ export default class Core {
attributeValue = dependencyValues.attributeComponent.stateValues[stateVariableForAttributeValue];
} else if (dependencyValues.attributePrimitive !== undefined && dependencyValues.attributePrimitive !== null) {
attributeValue = dependencyValues.attributePrimitive;
} else if (dependencyValues.attributeTargetComponentNames !== undefined && dependencyValues.attributeTargetComponentNames !== null) {
attributeValue = dependencyValues.attributeTargetComponentNames;
} else {

// parentValue would be undefined if fallBackToParentStateVariable wasn't specified
Expand Down Expand Up @@ -3097,6 +3114,10 @@ export default class Core {
// can't invert if have primitive
return { success: false }
}
if (dependencyValues.attributeTargetComponentNames !== undefined && dependencyValues.attributeTargetComponentNames !== null) {
// can't invert if have target component names
return { success: false }
}

let haveParentValue = dependencyValues.parentValue !== undefined
&& dependencyValues.parentValue !== null;
Expand Down
101 changes: 53 additions & 48 deletions src/Core/Dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -6358,7 +6358,7 @@ class PrimaryShadowDependency extends Dependency {
if (!primaryShadowDependencies.includes(this)) {
primaryShadowDependencies.push(this);
}

if (!component.primaryShadow) {
return {
success: true,
Expand Down Expand Up @@ -6897,6 +6897,58 @@ class CountAmongSiblingsDependency extends Dependency {
dependencyTypeArray.push(CountAmongSiblingsDependency);


class AttributeTargetComponentNamesDependency extends StateVariableDependency {
static dependencyType = "attributeTargetComponentNames";

setUpParameters() {

this.attributeName = this.definition.attributeName;

if (this.definition.parentName) {
this.componentName = this.definition.parentName;
this.specifiedComponentName = this.componentName;
} else {
this.componentName = this.upstreamComponentName;
}

}

async getValue() {

let value = null;
let changes = {};

if (this.componentIdentitiesChanged) {
changes.componentIdentitiesChanged = true;
this.componentIdentitiesChanged = false;
}

if (this.downstreamComponentNames.length === 1) {
let parent = this.dependencyHandler.components[this.componentName];

if (parent) {
value = parent.attributes[this.attributeName];
if (value) {
value = value.targetComponentNames;
} else {
value = null;
}
}

}

// if (!this.doNotProxy && value !== null && typeof value === 'object') {
// value = new Proxy(value, readOnlyProxyHandler)
// }

return { value, changes }
}

}

dependencyTypeArray.push(AttributeTargetComponentNamesDependency);


class TargetComponentDependency extends Dependency {
static dependencyType = "targetComponent";

Expand Down Expand Up @@ -6993,53 +7045,6 @@ class TargetComponentDependency extends Dependency {

dependencyTypeArray.push(TargetComponentDependency);



class ExpandTargetNameDependency extends Dependency {
static dependencyType = "expandTargetName";

setUpParameters() {

this.parentName = this.upstreamComponentName;

this.target = this.definition.target;

}

async getValue() {

let parent = this.dependencyHandler._components[this.parentName];
let parentCreatesNewNamespace = parent.attributes.newNamespace?.primitive;

let namespaceStack = this.parentName.split('/').map(x => ({ namespace: x }))

if (!parentCreatesNewNamespace) {
namespaceStack = namespaceStack.slice(0, namespaceStack.length - 1)
}

let targetComponentName;

try {
targetComponentName = convertComponentTarget({
target: this.target,
namespaceStack,
})
} catch (e) {
targetComponentName = null;
}

return {
value: targetComponentName,
changes: {}
}
}


}

dependencyTypeArray.push(ExpandTargetNameDependency);


class ValueDependency extends Dependency {
static dependencyType = "value";

Expand Down
43 changes: 15 additions & 28 deletions src/Core/components/CallAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export default class CallAction extends InlineComponent {
}

attributes.triggerWith = {
createPrimitiveOfType: "string"
createTargetComponentNames: true,
}

attributes.triggerWhenObjectsClicked = {
createPrimitiveOfType: "string"
createTargetComponentNames: true,
}

attributes.numbers = {
Expand Down Expand Up @@ -162,11 +162,11 @@ export default class CallAction extends InlineComponent {
stateVariableDefinitions.triggerWith = {
returnDependencies: () => ({
triggerWith: {
dependencyType: "attributePrimitive",
dependencyType: "attributeTargetComponentNames",
attributeName: "triggerWith"
},
triggerWhenObjectsClicked: {
dependencyType: "attributePrimitive",
dependencyType: "attributeTargetComponentNames",
attributeName: "triggerWhenObjectsClicked"
},
triggerWhen: {
Expand All @@ -185,13 +185,13 @@ export default class CallAction extends InlineComponent {

let triggerWith = [];
if (dependencyValues.triggerWith !== null) {
for (let target of dependencyValues.triggerWith.split(/\s+/).filter(s => s)) {
triggerWith.push({ target })
for (let nameObj of dependencyValues.triggerWith) {
triggerWith.push({ target: nameObj.absoluteName });
}
}
if (dependencyValues.triggerWhenObjectsClicked !== null) {
for (let target of dependencyValues.triggerWhenObjectsClicked.split(/\s+/).filter(s => s)) {
triggerWith.push({ target, triggeringAction: "click" })
for (let nameObj of dependencyValues.triggerWhenObjectsClicked) {
triggerWith.push({ target: nameObj.absoluteName, triggeringAction: "click" })
}
}

Expand All @@ -208,32 +208,19 @@ export default class CallAction extends InlineComponent {
chainActionOnActionOfStateVariableTargets: {
triggeredAction: "callAction"
},
stateVariablesDeterminingDependencies: ["triggerWith"],
returnDependencies({ stateValues }) {
let dependencies = {
triggerWith: {
dependencyType: "stateVariable",
variableName: "triggerWith"
}
};
if (stateValues.triggerWith) {
for (let [ind, targetObj] of stateValues.triggerWith.entries()) {

dependencies[`triggerWithTargetComponentName${ind}`] = {
dependencyType: "expandTargetName",
target: targetObj.target
}
}
returnDependencies: () => ({
triggerWith: {
dependencyType: "stateVariable",
variableName: "triggerWith"
}
return dependencies;
},
}),
definition({ dependencyValues }) {
let triggerWithTargetIds = [];

if (dependencyValues.triggerWith) {
for (let [ind, targetObj] of dependencyValues.triggerWith.entries()) {
for (let targetObj of dependencyValues.triggerWith) {

let id = dependencyValues[`triggerWithTargetComponentName${ind}`];
let id = targetObj.target;

if (targetObj.triggeringAction) {
id += "|" + targetObj.triggeringAction;
Expand Down
Loading

0 comments on commit 34e5d68

Please sign in to comment.