UNPKG

botbuilder-dialogs-adaptive

Version:

Rule system for the Microsoft BotBuilder dialog system.

92 lines 4.23 kB
"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.SetProperties = void 0; const botbuilder_1 = require("botbuilder"); const adaptive_expressions_1 = require("adaptive-expressions"); const jsonExtensions_1 = require("../jsonExtensions"); const botbuilder_dialogs_1 = require("botbuilder-dialogs"); class PropertyAssignmentsConverter { convert(items) { return items.map(({ property, value }) => ({ property: property instanceof adaptive_expressions_1.StringExpression ? property : new adaptive_expressions_1.StringExpression(property), value: value instanceof adaptive_expressions_1.ValueExpression ? value : new adaptive_expressions_1.ValueExpression(value), })); } } /** * Sets properties with the result of evaluating a value expression. */ class SetProperties extends botbuilder_dialogs_1.Dialog { /** * Initializes a new instance of the [SetProperties](xref:botbuilder-dialogs-adaptive.SetProperties) class. * * @param assignments Optional. [PropertyAssignment](xref:botbuilder-dialogs-adaptive.PropertyAssignment), additional property settings as property/value pairs. */ constructor(assignments) { super(); /** * Additional property settings as property/value pairs. */ this.assignments = []; if (assignments) { this.assignments = assignments; } } /** * @param property The key of the conditional selector configuration. * @returns The converter for the selector configuration. */ getConverter(property) { switch (property) { case 'assignments': return new PropertyAssignmentsConverter(); case 'disabled': return new adaptive_expressions_1.BoolExpressionConverter(); default: return super.getConverter(property); } } /** * 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(); } for (let i = 0; i < this.assignments.length; i++) { const assignment = this.assignments[i]; const value = (0, jsonExtensions_1.evaluateExpression)(dc.state, assignment.value); const property = assignment.property.getValue(dc.state); dc.state.setValue(property, value); } // save all state scopes to their respective botState locations. yield dc.state.saveAllChanges(); 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 `SetProperties[${botbuilder_1.StringUtils.ellipsis(this.assignments.map((item) => item.property.toString()).join(','), 50)}]`; } } exports.SetProperties = SetProperties; SetProperties.$kind = 'Microsoft.SetProperties'; //# sourceMappingURL=setProperties.js.map