Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nested foreach/foreachpage #2306

Merged
merged 4 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"$schema": "../../../../schemas/sdk.schema",
"$kind": "Microsoft.Test.Script",
"dialog": {
"$kind": "Microsoft.AdaptiveDialog",
"id": "root",
"triggers": [
{
"$kind": "Microsoft.OnBeginDialog",
"actions": [
{
"$kind": "Microsoft.SetProperty",
"property": "dialog.todo",
"value": "=[]"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=1"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=2"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=3"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=4"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=5"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"changeType": "push",
"value": "=6"
},
{
"$kind": "Microsoft.ForeachPage",
"itemsProperty": "dialog.todo",
"pageSize": 4,
"pageIndex": "dialog.foreachpage.outerIndex",
"page": "dialog.foreachpage.outerValue",
"actions": [
{
"$kind": "Microsoft.ForeachPage",
"itemsProperty": "dialog.foreachpage.outerValue",
"pageSize": 2,
"actions": [
{
"$kind": "Microsoft.SendActivity",
"activity": "This page have ${count(dialog.foreach.page)} items"
},
{
"$kind": "Microsoft.Foreach",
"itemsProperty": "dialog.foreach.page",
"actions": [
{
"$kind": "Microsoft.SendActivity",
"activity": "index is: ${dialog.foreach.index} and value is: ${dialog.foreach.value}"
}
]
}
]
}
]
},
{
"$kind": "Microsoft.SendActivity",
"activity": "done"
}
]
}
],
"autoEndDialog": true,
"defaultResultProperty": "dialog.result"
},
"script": [
{
"$kind": "Microsoft.Test.UserSays",
"text": "hi"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "This page have 2 items"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 0 and value is: 1"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 1 and value is: 2"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "This page have 2 items"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 0 and value is: 3"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 1 and value is: 4"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "This page have 2 items"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 0 and value is: 5"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 1 and value is: 6"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "done"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"$schema": "../../../../schemas/sdk.schema",
"$kind": "Microsoft.Test.Script",
"dialog": {
"$kind": "Microsoft.AdaptiveDialog",
"id": "root",
"triggers": [
{
"$kind": "Microsoft.OnBeginDialog",
"actions": [
{
"$kind": "Microsoft.SetProperty",
"property": "dialog.todo",
"value": "=[]"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.a",
"value": "a1"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.a",
"value": "a2"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.a",
"value": "a3"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.b",
"value": "b1"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.b",
"value": "b2"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.b",
"value": "b3"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"value": "=dialog.a"
},
{
"$kind": "Microsoft.EditArray",
"itemsProperty": "dialog.todo",
"value": "=dialog.b"
},
{
"$kind": "Microsoft.Foreach",
"itemsProperty": "dialog.todo",
"actions": [
{
"$kind": "Microsoft.Foreach",
"itemsProperty": "dialog.foreach.value",
"index": "dialog.foreach.inner.index",
"value": "dialog.foreach.inner.value",
"actions": [
{
"$kind": "Microsoft.SendActivity",
"activity": "index is: ${dialog.foreach.inner.index} and value is: ${dialog.foreach.inner.value}"
}
]
}
]
}
]
}
],
"autoEndDialog": true,
"defaultResultProperty": "dialog.result"
},
"script": [
{
"$kind": "Microsoft.Test.UserSays",
"text": "hi"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 0 and value is: a1"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 1 and value is: a2"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 2 and value is: a3"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 0 and value is: b1"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 1 and value is: b2"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "index is: 2 and value is: b3"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ describe('ActionTests', function() {
await testRunner.runTestScript('Action_EndDialog');
});

it('Foreach_Nested', async () => {
await testRunner.runTestScript('Action_Foreach_Nested');
});

it('Foreach', async () => {
await testRunner.runTestScript('Action_Foreach');
});
Expand All @@ -88,6 +92,10 @@ describe('ActionTests', function() {
await testRunner.runTestScript('Action_ForeachPage_Empty');
});

it('ForeachPage_Nested', async () => {
await testRunner.runTestScript('Action_ForeachPage_Nested');
});

it('ForeachPage_Partial', async () => {
await testRunner.runTestScript('Action_ForeachPage_Partial');
});
Expand Down
18 changes: 14 additions & 4 deletions libraries/botbuilder-dialogs-adaptive/src/actions/forEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export class ForEach<O extends object = {}> extends ActionScope<O> {
*/
public itemsProperty: StringExpression;

/**
* Property path expression to the item index.
*/
public index: StringExpression = new StringExpression(INDEX);

/**
* Property path expression to the item value.
*/
public value: StringExpression = new StringExpression(VALUE);

/**
* An optional expression which if is true will disable this action.
*/
Expand All @@ -39,7 +49,7 @@ export class ForEach<O extends object = {}> extends ActionScope<O> {
if (this.disabled && this.disabled.getValue(dc.state)) {
return await dc.endDialog();
}
dc.state.setValue(INDEX, -1);
dc.state.setValue(this.index.getValue(dc.state), -1);
return await this.nextItem(dc);
}

Expand All @@ -58,11 +68,11 @@ export class ForEach<O extends object = {}> extends ActionScope<O> {
protected async nextItem(dc: DialogContext): Promise<DialogTurnResult> {
const itemsProperty = this.itemsProperty.getValue(dc.state);
const items: any[] = dc.state.getValue(itemsProperty, []);
let index = dc.state.getValue(INDEX);
let index = dc.state.getValue(this.index.getValue(dc.state));

if (++index < items.length) {
dc.state.setValue(VALUE, items[index]);
dc.state.setValue(INDEX, index);
dc.state.setValue(this.value.getValue(dc.state), items[index]);
dc.state.setValue(this.index.getValue(dc.state), index);
return await this.beginAction(dc, 0);
} else {
return await dc.endDialog();
Expand Down
18 changes: 15 additions & 3 deletions libraries/botbuilder-dialogs-adaptive/src/actions/forEachPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export class ForEachPage<O extends object = {}> extends ActionScope<O> {
*/
public itemsProperty: StringExpression;

/**
* Expression used to compute the list that should be enumerated.
*/
public page: StringExpression = new StringExpression(FOREACHPAGE);

/**
* Expression used to compute the list that should be enumerated.
*/
public pageIndex: StringExpression = new StringExpression(FOREACHPAGEINDEX);

/**
* Page size, default to 10.
*/
Expand All @@ -52,6 +62,8 @@ export class ForEachPage<O extends object = {}> extends ActionScope<O> {
if (this.disabled && this.disabled.getValue(dc.state)) {
return await dc.endDialog();
}

dc.state.setValue(this.pageIndex.getValue(dc.state), 0);
return await this.nextPage(dc);
}

Expand All @@ -72,7 +84,7 @@ export class ForEachPage<O extends object = {}> extends ActionScope<O> {
}

private async nextPage(dc: DialogContext): Promise<DialogTurnResult> {
let pageIndex = dc.state.getValue(FOREACHPAGEINDEX, 0);
let pageIndex = dc.state.getValue(this.pageIndex.getValue(dc.state), 0);
const pageSize = this.pageSize.getValue(dc.state);
const itemOffset = pageSize * pageIndex;

Expand All @@ -81,8 +93,8 @@ export class ForEachPage<O extends object = {}> extends ActionScope<O> {
if (items.length > 0) {
const page = this.getPage(items, itemOffset, pageSize);
if (page && page.length > 0) {
dc.state.setValue(FOREACHPAGE, page);
dc.state.setValue(FOREACHPAGEINDEX, ++pageIndex);
dc.state.setValue(this.page.getValue(dc.state), page);
dc.state.setValue(this.pageIndex.getValue(dc.state), ++pageIndex);
return await this.beginAction(dc, 0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ export class AdaptiveDialogComponentRegistration implements ComponentRegistratio
}));
this.registerBuilder('Microsoft.Foreach', new AdaptiveTypeBuilder(ForEach, this._resourceExplorer, {
'itemsProperty': new StringExpressionConverter(),
'index': new StringExpressionConverter(),
'value': new StringExpressionConverter(),
'disabled': new BoolExpressionConverter()
}));
this.registerBuilder('Microsoft.ForeachPage', new AdaptiveTypeBuilder(ForEachPage, this._resourceExplorer, {
'itemsProperty': new StringExpressionConverter(),
'page': new StringExpressionConverter(),
'pageIndex': new StringExpressionConverter(),
'pageSize': new IntExpressionConverter(),
'disabled': new BoolExpressionConverter()
}));
Expand Down