botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
136 lines • 5.41 kB
JavaScript
"use strict";
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.IfCondition = 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 converters_1 = require("../converters");
const botbuilder_1 = require("botbuilder");
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
/**
* Conditional branch.
*/
class IfCondition extends botbuilder_dialogs_1.Dialog {
/**
* Initializes a new instance of the [IfCondition](xref:botbuilder-dialogs-adaptive.IfCondition) class.
*
* @param condition Optional. Conditional expression to evaluate.
* @param elseActions Optional. The actions to run if [condition](#condition) returns false.
*/
constructor(condition, elseActions) {
super();
/**
* The actions to run if [condition](#condition) returns true.
*/
this.actions = [];
/**
* The actions to run if [condition](#condition) returns false.
*/
this.elseActions = [];
if (condition) {
this.condition = new adaptive_expressions_1.BoolExpression(condition);
}
if (elseActions) {
this.elseActions = elseActions;
}
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'condition':
return new adaptive_expressions_1.BoolExpressionConverter();
case 'actions':
case 'elseActions':
return converters_1.DialogListConverter;
case 'disabled':
return new adaptive_expressions_1.BoolExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* @protected
* Gets the true scope.
* @returns An [ActionScope](xref:botbuilder-dialogs-adaptive.ActionScope).
*/
get trueScope() {
if (!this._trueScope) {
this._trueScope = new actionScope_1.ActionScope(this.actions);
this._trueScope.id = `True${this.id}`;
}
return this._trueScope;
}
/**
* @protected
* Gets the false scope.
* @returns An [ActionScope](xref:botbuilder-dialogs-adaptive.ActionScope).
*/
get falseScope() {
if (!this._falseScope) {
this._falseScope = new actionScope_1.ActionScope(this.elseActions);
this._falseScope.id = `False${this.id}`;
}
return this._falseScope;
}
/**
* 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 [].concat(this.trueScope, this.falseScope);
}
/**
* 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();
}
const conditionResult = this.condition.getValue(dc.state);
if (conditionResult) {
return yield dc.replaceDialog(this.trueScope.id);
}
else if (!conditionResult && this.falseScope.actions && this.falseScope.actions.length > 0) {
return yield dc.replaceDialog(this.falseScope.id);
}
return yield dc.endDialog();
});
}
/**
* @protected
* Builds the compute Id for the [Dialog](xref:botbuilder-dialogs.Dialog).
* @returns A `string` representing the compute Id.
*/
onComputeId() {
const label = this.condition ? this.condition.toString() : '';
const idList = this.actions.map((action) => action.id);
return `If[${label}|${botbuilder_1.StringUtils.ellipsis(idList.join(','), 50)}]`;
}
}
exports.IfCondition = IfCondition;
IfCondition.$kind = 'Microsoft.IfCondition';
//# sourceMappingURL=ifCondition.js.map