Skip to content

Commit

Permalink
autogenerate names for pages/panels/questions: surveyjs/survey-creato…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 20, 2017
1 parent 6c3573b commit 7def064
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
38 changes: 28 additions & 10 deletions src/panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JsonObject} from "./jsonobject";
import {Base, ISurveyImpl, IPage, IConditionRunner, ISurvey, ISurveyData, IElement, IQuestion, HashTable, SurveyElement, SurveyPageId} from "./base";
import {Base, ISurveyImpl, IPage, IPanel, IConditionRunner, ISurvey, ISurveyData, IElement, IQuestion, HashTable, SurveyElement, SurveyPageId} from "./base";
import {QuestionBase} from "./questionbase";
import {ConditionRunner} from "./conditions";
import {QuestionFactory} from "./questionfactory";
Expand Down Expand Up @@ -207,19 +207,37 @@ export class PanelModelBase extends SurveyElement implements IConditionRunner, I
* @param list
* @param visibleOnly set it to true to get visible questions only
*/
public addQuestionsToList(list: Array<IQuestion>, visibleOnly: boolean = false) {
public addQuestionsToList(list: Array<IQuestion>, visibleOnly: boolean = false, includingDesignTime: boolean = false) {
this.addElementsToList(list, visibleOnly, includingDesignTime, false);
}
/**
* Fill list array with the panels.
* @param list
*/
public addPanelsIntoList(list: Array<IPanel>, visibleOnly: boolean = false, includingDesignTime: boolean = false) {
this.addElementsToList(list, visibleOnly, includingDesignTime, true);
}
private addElementsToList(list: Array<IElement>, visibleOnly: boolean, includingDesignTime: boolean, isPanel: boolean) {
if (visibleOnly && !this.visible) return;
for (var i = 0; i < this.elements.length; i++) {
var el = this.elements[i];
this.addElementsToListCore(list, this.elements, visibleOnly, includingDesignTime, isPanel);
}
private addElementsToListCore(list: Array<IElement>, elements: Array<IElement>, visibleOnly: boolean, includingDesignTime: boolean, isPanel: boolean) {
for (var i = 0; i < elements.length; i++) {
var el = elements[i];
if (visibleOnly && !el.visible) continue;
if(isPanel && el.isPanel || !isPanel && !el.isPanel) {
list.push(el);
}
if(el.isPanel) {
(<PanelModel>el).addQuestionsToList(list, visibleOnly);
(<PanelModel>el).addElementsToListCore(list, (<PanelModel>el).elements, visibleOnly, includingDesignTime, isPanel);
}
else {
list.push(<IQuestion>el);
if(includingDesignTime) {
this.addElementsToListCore(list, (<SurveyElement><any>el).getElementsInDesign(false), visibleOnly, includingDesignTime, isPanel);
}
}
}
}
}
get rows(): Array<QuestionRowModel> {
if(!this.rowValues) {
this.rowValues = this.buildRows();
Expand Down Expand Up @@ -410,7 +428,7 @@ export class PanelModelBase extends SurveyElement implements IConditionRunner, I
* @param questionType the possible values are: "text", "checkbox", "dropdown", "matrix", "html", "matrixdynamic", "matrixdropdown" and so on.
* @param name a question name
*/
public addNewQuestion(questionType: string, name: string): QuestionBase {
public addNewQuestion(questionType: string, name: string = null): QuestionBase {
var question = QuestionFactory.Instance.createQuestion(questionType, name);
this.addQuestion(question);
return question;
Expand All @@ -419,7 +437,7 @@ export class PanelModelBase extends SurveyElement implements IConditionRunner, I
* Creates a new panel and adds it into the end of the elements list.
* @param name a panel name
*/
public addNewPanel(name: string): PanelModel {
public addNewPanel(name: string = null): PanelModel {
var panel = this.createNewPanel(name);
this.addPanel(panel);
return panel;
Expand Down Expand Up @@ -484,7 +502,7 @@ export class PanelModelBase extends SurveyElement implements IConditionRunner, I
* A container element, similar to the Page objects. However, unlike the Page, Panel can't be a root.
* It may contain questions and other panels.
*/
export class PanelModel extends PanelModelBase implements IElement {
export class PanelModel extends PanelModelBase implements IPanel {
private renderWidthValue: string;
private rightIndentValue: number;
/**
Expand Down
4 changes: 4 additions & 0 deletions src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export class QuestionPanelDynamicModel extends Question implements IQuestionPane
this.locPanelPrevTextValue = new LocalizableString(this);
this.locPanelNextTextValue = new LocalizableString(this);
}
public setSurveyImpl(value: ISurveyImpl) {
super.setSurveyImpl(value);
this.template.setSurveyImpl(this.surveyImpl);
}
private templateOnRowsChanged() {
if(this.isLoadingFromJson) return;
this.rebuildPanels();
Expand Down
17 changes: 14 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,10 +1204,20 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
* Returns the list of all questions in the survey
* @param visibleOnly set it true, if you want to get only visible questions
*/
public getAllQuestions(visibleOnly: boolean = false): Array<IQuestion> {
public getAllQuestions(visibleOnly: boolean = false, includingDesignTime: boolean = false): Array<IQuestion> {
var result = new Array<IQuestion>();
for (var i: number = 0; i < this.pages.length; i++) {
this.pages[i].addQuestionsToList(result, visibleOnly);
this.pages[i].addQuestionsToList(result, visibleOnly, includingDesignTime);
}
return result;
}
/**
* Returns the list of all panels in the survey
*/
public getAllPanels(visibleOnly: boolean = false, includingDesignTime: boolean = false): Array<IPanel> {
var result = new Array<IPanel>();
for (var i: number = 0; i < this.pages.length; i++) {
this.pages[i].addPanelsIntoList(result, visibleOnly, includingDesignTime);
}
return result;
}
Expand Down Expand Up @@ -1625,16 +1635,17 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
this.onPanelVisibleChanged.fire(this, { 'panel': panel, 'visible': newValue });
}
questionAdded(question: IQuestion, index: number, parentPanel: any, rootPanel: any) {
if(!question.name) question.name = this.generateNewName(this.getAllQuestions(false, true), "question");
this.updateVisibleIndexes();
this.addQuestionToProcessedTextValues(question);
if(!question.name) question.name = this.generateNewName(this.getAllQuestions(), "question");
this.onQuestionAdded.fire(this, { 'question': question, 'name': question.name, 'index': index, 'parentPanel': parentPanel, 'rootPanel': rootPanel });
}
questionRemoved(question: IQuestion) {
this.updateVisibleIndexes();
this.onQuestionRemoved.fire(this, { 'question': question, 'name': question.name });
}
panelAdded(panel: IElement, index: number, parentPanel: any, rootPanel: any) {
if(!panel.name) panel.name = this.generateNewName(this.getAllPanels(false, true), "panel");
this.updateVisibleIndexes();
this.onPanelAdded.fire(this, { 'panel': panel, 'name': panel.name, 'index': index, 'parentPanel': parentPanel, 'rootPanel': rootPanel });
}
Expand Down
13 changes: 13 additions & 0 deletions tests/surveypaneldynamictests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ QUnit.test("assign customWidgets to questions in dynamic panel", function (asser
CustomWidgetCollection.Instance.clear();
});

QUnit.test("Auto generate names", function (assert) {
var survey = new SurveyModel();
var page1 = survey.addNewPage();
var panel = <QuestionPanelDynamicModel>page1.addNewQuestion("paneldynamic");
var q2 = panel.template.addNewQuestion("text");
var p1 = panel.template.addNewPanel();
var q3 = page1.addNewQuestion("text");

assert.equal(p1.name, "panel1", "the first name for panel is panel1");
assert.equal(q2.name, "question2", "the second name for question is question2");
assert.equal(q3.name, "question3", "the third name for question is question3");
});

/* Think about this-
QUnit.test("PanelDynamic survey.getPageByQuestion/Element", function (assert) {
var survey = new SurveyModel();
Expand Down
22 changes: 17 additions & 5 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1654,18 +1654,30 @@ QUnit.test("Dublicate errors", function (assert) {
assert.equal(q.value, 26, "the value is 26");
});


QUnit.test("Auto generate names for question/panel/page", function (assert) {
var survey = new SurveyModel();
survey.addNewPage();
var page1 = survey.addNewPage();
assert.equal(survey.pages[0].name, "page1", "the first name is page1");
survey.addNewPage();
var page2 = survey.addNewPage();
assert.equal(survey.pages[1].name, "page2", "the second name is page2");
survey.pages[0].name = "newpage"
survey.addNewPage();
var page3 = survey.addNewPage();
assert.equal(survey.pages[2].name, "page1", "the third name is page1 again");
});

page1.addNewQuestion("text");
page1.addNewQuestion("text");
page3.addNewQuestion("text");
assert.equal(survey.getAllQuestions()[0].name, "question1", "the first name is question1");
assert.equal(survey.getAllQuestions()[1].name, "question2", "the second name is question2");
assert.equal(survey.getAllQuestions()[2].name, "question3", "the third name is question3");

var panel1 = page1.addNewPanel();
var panel2 = panel1.addNewPanel();
var panel3 = page2.addNewPanel();
assert.equal(panel1.name, "panel1", "the first name is panel1");
assert.equal(panel2.name, "panel2", "the second name is panel2");
assert.equal(panel3.name, "panel3", "the third name is panel3");
});

function twoPageSimplestSurvey() {
var survey = new SurveyModel();
Expand Down

0 comments on commit 7def064

Please sign in to comment.