Skip to content

Commit

Permalink
Text pre-processing (completedHtml, question/page titles) support nes…
Browse files Browse the repository at this point in the history
…ted properties and arrays: #177
  • Loading branch information
andrewtelnov committed Jan 5, 2017
1 parent 3dd92f4 commit d549a90
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Base, ISurvey, HashTable, IQuestion, IConditionRunner, IPage, SurveyErro
import {ISurveyTriggerOwner, SurveyTrigger} from "./trigger";
import {PageModel} from "./page";
import {TextPreProcessor} from "./textPreProcessor";
import {ProcessValue} from "./conditionProcessValue";
import {dxSurveyService} from "./dxSurveyService";
import {JsonError} from "./jsonobject";
import {surveyLocalization} from "./surveyStrings";
Expand Down Expand Up @@ -69,7 +70,7 @@ export class SurveyModel extends Base implements ISurvey, ISurveyTriggerOwner {
super();
var self = this;
this.textPreProcessor = new TextPreProcessor();
this.textPreProcessor.onHasValue = function (name: string) { return self.processedTextValues[name.toLowerCase()]; };
this.textPreProcessor.onHasValue = function (name: string) { return self.hasProcessedTextValue(name); };
this.textPreProcessor.onProcess = function (name: string) { return self.getProcessedTextValue(name); };
this.pages.push = function (value) {
value.data = self;
Expand Down Expand Up @@ -612,19 +613,25 @@ export class SurveyModel extends Base implements ISurvey, ISurveyTriggerOwner {
private addQuestionToProcessedTextValues(question: IQuestion) {
this.processedTextValues[question.name.toLowerCase()] = "question";
}
private hasProcessedTextValue(name: string): boolean {
var firstName = new ProcessValue().getFirstName(name);
return this.processedTextValues[firstName.toLowerCase()];
}
private getProcessedTextValue(name: string): any {
var name = name.toLowerCase();
var val = this.processedTextValues[name];
var firstName = new ProcessValue().getFirstName(name);
var val = this.processedTextValues[firstName.toLowerCase()];
if (!val) return null;
if (val == "variable") {
return this.getVariable(name.toLowerCase());
}
if (val == "question") {
var question = this.getQuestionByName(name, true);
return question != null ? this.getValue(question.name) : null;
var question = this.getQuestionByName(firstName, true);
if (!question) return null;
name = question.name + name.substr(firstName.length);
return new ProcessValue().getValue(name, this.valuesHash);
}
if (val == "value") {
return this.getValue(name);
}
if (val == "variable") {
return this.getVariable(name);
return new ProcessValue().getValue(name, this.valuesHash);
}
return val(name);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,29 @@ QUnit.test("pre process title", function (assert) {
survey.completedHtml = "<div>Your e-mail: <b>{email}</b>{var1}{val1}</div>";
assert.equal(survey.processedCompletedHtml, "<div>Your e-mail: <b>andrew.telnov@gmail.com</b>[it is var1][it is val1]</div>");
});

QUnit.test("pre process completedHtml nested properties and arrays", function (assert) {
var survey = new SurveyModel();
var page = survey.addNewPage("page1");

var multipleText = new QuestionMultipleTextModel("mt");
multipleText.addItem("t1");
multipleText.addItem("t2");
page.addQuestion(multipleText);

var dynamicMatrix = new QuestionMatrixDynamicModel("matrix");
dynamicMatrix.addColumn("col1");
dynamicMatrix.addColumn("col2");
dynamicMatrix.addColumn("col3");
page.addQuestion(dynamicMatrix);

multipleText.value = { t2: "Year" };
dynamicMatrix.value = [{ col1: 1 }, {col2: 2017}];

survey.completedHtml = "{mt.t2}:{matrix[1].col2}";
assert.equal(survey.processedCompletedHtml, "Year:2017");
});

QUnit.test("question fullTitle", function (assert) {
var survey = twoPageSimplestSurvey();
var question = <Question>survey.pages[0].questions[1];
Expand Down

0 comments on commit d549a90

Please sign in to comment.