botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
79 lines • 2.99 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.ChoiceSet = void 0;
const languageGeneratorExtensions_1 = require("../languageGeneratorExtensions");
/**
* Defines ChoiceSet collection.
*/
class ChoiceSet extends Array {
/**
* Initializes a new instance of the [ChoiceSet](xref:botbuilder-dialogs-adaptive.ChoiceSet) class.
*
* @param obj Choice values.
*/
constructor(obj) {
super();
if (typeof obj === 'string') {
this.template = obj;
}
else if (obj instanceof Array) {
obj.forEach((o) => {
if (typeof o === 'string') {
this.push({
value: o,
});
}
else if (typeof o === 'object') {
this.push(o);
}
});
}
}
/**
* Bind data to template.
*
* @param dialogContext DialogContext
* @param data Data to bind to.
* @returns Data binded ChoiceSet.
*/
bind(dialogContext, data) {
return __awaiter(this, void 0, void 0, function* () {
if (this.template == null) {
return this;
}
const languageGenerator = dialogContext.services.get(languageGeneratorExtensions_1.languageGeneratorKey);
if (languageGenerator == null) {
throw new Error('language generator is missing.');
}
const lgResult = yield languageGenerator.generate(dialogContext, this.template, data);
if (lgResult instanceof ChoiceSet) {
return lgResult;
}
else if (typeof lgResult === 'string') {
try {
const jObj = JSON.parse(lgResult);
if (Array.isArray(jObj)) {
return new ChoiceSet(jObj);
}
return jObj;
}
catch (_) {
const choices = lgResult.split('|').map((u) => u.trim());
return new ChoiceSet(choices);
}
}
return new ChoiceSet(lgResult);
});
}
}
exports.ChoiceSet = ChoiceSet;
//# sourceMappingURL=choiceSet.js.map