Skip to content

Commit

Permalink
Implement: Allow to set null to creator.selectedElement #672
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Feb 20, 2020
1 parent 5a5b6e6 commit d3f2b92
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
53 changes: 18 additions & 35 deletions src/editor.ts
Expand Up @@ -845,7 +845,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
return options.text;
};
this.selectPage = (page: Survey.PageModel) => {
this.surveyObjects.selectObject(page);
this.selectedElement = page;
};

this.elementPropertyGridValue = new SurveyElementPropertyGrid(this);
Expand Down Expand Up @@ -1086,7 +1086,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
visible: this.koIsShowDesigner,
enabled: false,
action: () => {
this.surveyObjects.selectObject(this.survey);
this.selectedElement = this.survey;
this.showQuestionEditor(this.survey);
},
title: this.getLocString("ed.settings")
Expand Down Expand Up @@ -1810,7 +1810,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
var page = <Survey.Page>this.survey.pages[indexTo];
this.surveyObjects.survey = null; // TODO may be we don't need this hack
this.surveyObjects.survey = this.survey;
this.surveyObjects.selectObject(page);
this.selectedElement = page;
this.setModified({
type: "PAGE_MOVED",
page: page,
Expand Down Expand Up @@ -1879,7 +1879,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
this.dirtyPageUpdate(); //TODO why this is need ? (ko problem)
} else if (property.name === "page") {
this.selectPage(value);
this.surveyObjects.selectObject(obj);
this.selectedElement = obj;
}
}

Expand Down Expand Up @@ -1926,25 +1926,6 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
if (!this.showPropertyGrid) return;
this.elementPropertyGridValue.selectedObject = newObj;
}
// TODO undoredo
// private doUndoRedo(item: UndoRedoItem) {
// this.initSurvey(item.surveyJSON);
// if (item.selectedObjName) {
// var selObj = this.findObjByName(item.selectedObjName);
// if (selObj) {
// this.surveyObjects.selectObject(selObj);
// }
// }
// this.setState("modified");
// this.isAutoSave && this.doSave();
// }
private findObjByName(name: string): Survey.Base {
var page = this.survey.getPageByName(name);
if (page) return page;
var question = <Survey.Question>this.survey.getQuestionByName(name);
if (question) return question;
return null;
}
private canSwitchViewType(newType: string): boolean {
if (newType && this.koViewType() == newType) return false;
if (this.koViewType() == "designer") {
Expand Down Expand Up @@ -2066,7 +2047,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
var options = { newSelectedElement: obj };
this.onSelectedElementChanging.fire(this, options);
if (obj != options.newSelectedElement) {
this.surveyObjects.selectObject(options.newSelectedElement);
this.selectedElement = options.newSelectedElement;
return;
}
var canDeleteObject = false;
Expand All @@ -2086,7 +2067,9 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
SurveyHelper.scrollIntoViewIfNeeded(el);
}
} else {
this.disableSurveySelectedElementChanging = true;
this.survey.selectedElement = null;
this.disableSurveySelectedElementChanging = false;
}
this.koCanDeleteObject(canDeleteObject);
//Select2 work-around
Expand Down Expand Up @@ -2192,6 +2175,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
}
return !!classInfo && classInfo.name === "question";
}
private disableSurveySelectedElementChanging: boolean = false;
private initSurvey(json: any) {
var self = this;
this.surveyValue(
Expand Down Expand Up @@ -2472,14 +2456,15 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
this.onDesignerSurveyCreated.fire(this, { survey: this.surveyValue() });
this.survey.render(this.surveyjs);
this.surveyObjects.survey = this.survey;
this.surveyObjects.selectObject(this.surveyValue());
this.selectedElement = this.surveyValue();
this.surveyValue().onSelectedElementChanged.add(
(sender: Survey.Survey, options) => {
self.surveyObjects.selectObject(sender["selectedElement"]);
if (self.disableSurveySelectedElementChanging) return;
self.selectedElement = sender["selectedElement"];
}
);
this.surveyValue().onEditButtonClick.add((sender: Survey.Survey) => {
self.showQuestionEditor(self.koSelectedObject().value);
self.showQuestionEditor(self.selectedElement);
});
this.surveyValue().onElementDoubleClick.add(
(sender: Survey.Survey, options) => {
Expand Down Expand Up @@ -2679,17 +2664,14 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
}
}
private getSelectedObjAsQuestion(): Survey.Question {
var obj = this.koSelectedObject().value;
var obj = this.selectedElement;
if (!obj) return null;
return SurveyHelper.getObjectType(obj) == ObjType.Question
? <Survey.Question>obj
: null;
}
public deleteCurrentObject() {
this.deleteObject(this.koSelectedObject().value);
}
private editCurrentObject() {
this.showQuestionEditor(this.koSelectedObject().value);
this.deleteObject(this.selectedElement);
}
private convertCurrentObject(obj: Survey.Question, className: string) {
var newQuestion = QuestionConverter.convertObject(obj, className);
Expand Down Expand Up @@ -2767,7 +2749,8 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {

//TODO why this is need ? (ko problem)
private dirtyPageUpdate = () => {
var selectedObject = this.koSelectedObject().value;
var selectedObject = this.selectedElement;
if (!selectedObject) return;
if (SurveyHelper.getObjectType(selectedObject) !== ObjType.Page) {
if (
SurveyHelper.getObjectType(selectedObject) === ObjType.Question &&
Expand All @@ -2777,7 +2760,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
}
return;
}
this.surveyObjects.selectObject(selectedObject);
this.selectedElement = selectedObject;
};

/**
Expand Down Expand Up @@ -2840,7 +2823,7 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
} else {
this.survey.currentPage.removeElement(obj);
this.survey.selectedElement = null;
this.surveyObjects.selectObject(this.survey.currentPage);
this.selectedElement = this.survey.currentPage;
}
this.setModified({
type: "OBJECT_DELETED",
Expand Down
4 changes: 4 additions & 0 deletions src/surveyObjects.ts
Expand Up @@ -54,6 +54,10 @@ export class SurveyObjects {
this.koSelected(item);
}
public selectObject(obj: Survey.Base) {
if (obj === null) {
this.koSelected(null);
return;
}
var objs = this.koObjects();
for (var i = 0; i < objs.length; i++) {
if (objs[i].value == obj) {
Expand Down
36 changes: 34 additions & 2 deletions tests/surveyCreatorTests.ts
Expand Up @@ -760,7 +760,7 @@ QUnit.test(

QUnit.test("Do not allow to select page object", function(assert) {
var creator = new SurveyCreator();
creator.text = JSON.stringify(getSurveyJson());
creator.JSON = getSurveyJson();
creator.selectedElement = creator.survey.getQuestionByName("question1");
assert.equal(creator.selectedElement.name, "question1");
creator.onSelectedElementChanging.add(function(c, options) {
Expand All @@ -772,7 +772,39 @@ QUnit.test("Do not allow to select page object", function(assert) {
}
});
creator.selectedElement = creator.survey.pages[0];
assert.equal(creator.selectedElement.getType(), "survey");
assert.ok(creator.selectedElement, "There is the selected object");
assert.equal(
creator.selectedElement.getType(),
"survey",
"The selected object is survey"
);
});

QUnit.test("Do not allow to select page/survey objects", function(assert) {
var creator = new SurveyCreator();
creator.onSelectedElementChanging.add(function(c, options) {
var el = options.newSelectedElement;
if (el != null && (el.getType() == "page" || el.getType() == "survey")) {
options.newSelectedElement =
creator.survey.getAllQuestions().length > 0
? creator.survey.getAllQuestions()[0]
: null;
}
});
creator.selectedElement = null;
assert.notOk(
creator.selectedElement,
"There is no selected element, do not select survey"
);
creator.JSON = getSurveyJson();
creator.selectedElement = creator.survey.getQuestionByName("question1");
assert.equal(creator.selectedElement.name, "question1");
creator.selectedElement = creator.survey.pages[0];
assert.equal(
creator.selectedElement.getType(),
"text",
"Do not select page or survey"
);
});

QUnit.test("Change elemenent page", function(assert) {
Expand Down

0 comments on commit d3f2b92

Please sign in to comment.