botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
173 lines • 7.56 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForEach = void 0;
/**
* @module botbuilder-dialogs-adaptive
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const actionScope_1 = require("./actionScope");
const adaptive_expressions_1 = require("adaptive-expressions");
const INDEX = 'dialog.foreach.index';
const VALUE = 'dialog.foreach.value';
/**
* Executes a set of actions once for each item in an in-memory list or collection.
*/
class ForEach extends actionScope_1.ActionScope {
/**
* Initializes a new instance of the [Foreach](xref:botbuilder-dialogs-adaptive.Foreach) class.
*
* @param itemsProperty Optional. Property path expression to the collection of items.
* @param actions Optional. The actions to execute.
*/
constructor(itemsProperty, actions) {
super();
/**
* Property path expression to the item index.
*/
this.index = new adaptive_expressions_1.StringExpression(INDEX);
/**
* Property path expression to the item value.
*/
this.value = new adaptive_expressions_1.StringExpression(VALUE);
if (itemsProperty) {
this.itemsProperty = new adaptive_expressions_1.StringExpression(itemsProperty);
}
if (actions) {
this.actions = actions;
}
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'itemsProperty':
return new adaptive_expressions_1.StringExpressionConverter();
case 'index':
return new adaptive_expressions_1.StringExpressionConverter();
case 'value':
return new adaptive_expressions_1.StringExpressionConverter();
case 'disabled':
return new adaptive_expressions_1.BoolExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* Gets the child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies so they can be added to the containers [Dialog](xref:botbuilder-dialogs.Dialog) set.
*
* @returns The child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies.
*/
getDependencies() {
return this.actions;
}
/**
* Starts a new [Dialog](xref:botbuilder-dialogs.Dialog) and pushes it onto the dialog stack.
*
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _options Optional. Initial information to pass to the dialog.
* @returns A `Promise` representing the asynchronous operation.
*/
beginDialog(dc, _options) {
return __awaiter(this, void 0, void 0, function* () {
if (this.disabled && this.disabled.getValue(dc.state)) {
return yield dc.endDialog();
}
this.currentIndex = -1;
return yield this.nextItem(dc);
});
}
/**
* @protected
* Called when returning control to this [Dialog](xref:botbuilder-dialogs.Dialog) with an [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult)
* with the property `ActionCommand` set to `BreakLoop`.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _actionScopeResult The [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult).
* @returns A `Promise` representing the asynchronous operation.
*/
onBreakLoop(dc, _actionScopeResult) {
return __awaiter(this, void 0, void 0, function* () {
return yield dc.endDialog();
});
}
/**
* @protected
* Called when returning control to this [Dialog](xref:botbuilder-dialogs.Dialog) with an [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult)
* with the property `ActionCommand` set to `ContinueLoop`.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _actionScopeResult The [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult).
* @returns A `Promise` representing the asynchronous operation.
*/
onContinueLoop(dc, _actionScopeResult) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.nextItem(dc);
});
}
/**
* @protected
* Called when the [Dialog](xref:botbuilder-dialogs.Dialog) continues to the next action.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _result Optional. Value returned from the dialog that was called. The type
* of the value returned is dependent on the child dialog.
* @returns A `Promise` representing the asynchronous operation.
*/
onEndOfActions(dc, _result) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.nextItem(dc);
});
}
/**
* @protected
* Calls the next item in the stack.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @returns A `Promise` representing the asynchronous operation.
*/
nextItem(dc) {
return __awaiter(this, void 0, void 0, function* () {
const itemsProperty = this.itemsProperty.getValue(dc.state);
const items = this.convertToList(dc.state.getValue(itemsProperty, []));
if (++this.currentIndex < items.length) {
dc.state.setValue(this.value.getValue(dc.state), items[this.currentIndex].value);
dc.state.setValue(this.index.getValue(dc.state), items[this.currentIndex].index);
return yield this.beginAction(dc, 0);
}
else {
return yield dc.endDialog();
}
});
}
/**
* @protected
* Builds the compute Id for the [Dialog](xref:botbuilder-dialogs.Dialog).
* @returns A `string` representing the compute Id.
*/
onComputeId() {
return `ForEach[${this.itemsProperty.toString()}]`;
}
convertToList(value) {
const result = [];
if (Array.isArray(value)) {
value.forEach((item, index) => result.push({ index: index, value: item }));
}
else if (typeof value === 'object') {
Object.entries(value).forEach(([key, value]) => result.push({ index: key, value: value }));
}
return result;
}
}
exports.ForEach = ForEach;
ForEach.$kind = 'Microsoft.Foreach';
//# sourceMappingURL=forEach.js.map