Skip to content

Commit

Permalink
Create CreatorPresetEditor class #5215
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed May 21, 2024
1 parent c980561 commit 70643f6
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 187 deletions.
1 change: 1 addition & 0 deletions packages/survey-creator-core/src/entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export * from "../plugins/undo-redo";
export * from "../plugins/undo-redo/undo-redo-manager";
export * from "../pages-controller";
export * from "../presets/presets";
export * from "../presets/presets-editor";

require("../components/property-panel/property-panel-item.scss");
require("../components/property-panel/property-panel.scss");
Expand Down
97 changes: 97 additions & 0 deletions packages/survey-creator-core/src/presets/presets-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { SurveyCreatorModel } from "../creator-base";
import { CreatorPreset, ICreatorPresetData } from "./presets";
import { SurveyModel } from "survey-core";
import { CreatorPresetEditableBase } from "./presets-base";

export class CreatorPresetEditor {
private presetValue: CreatorPreset;
private creatorValue: SurveyCreatorModel;
private modelValue: SurveyModel;
constructor(json?: ICreatorPresetData, creator?: SurveyCreatorModel) {
this.presetValue = new CreatorPreset(json);
this.creatorValue = creator || new SurveyCreatorModel({});
this.modelValue = this.createModel();
}
public get preset(): CreatorPreset { return this.presetValue; }
public get creator(): SurveyCreatorModel { return this.creatorValue; }
public get model(): SurveyModel { return this.modelValue; }
protected createModel(): SurveyModel {
const editablePresets = this.createEditablePresets();
const model = new SurveyModel(this.getEditModelJson(editablePresets));
model.editablePresets = editablePresets;
model.showCompleteButton = false;
model.keepIncorrectValues = true;
model.addNavigationItem({
id: "preset_save",
title: "Save", //TODO
action: () => {
this.applyFromSurveyModel();
}
});
editablePresets.forEach(item => item.setupQuestions(model, this.creator));
const json = this.preset.getJson() || {};
editablePresets.forEach(item => item.setupQuestionsValue(model, json[item.path], this.creator));
model.onCurrentPageChanged.add((sender, options) => {
editablePresets.forEach(item => item.setupOnCurrentPage(model, this.creator));
});
model.onValueChanged.add((sender, options) => {
editablePresets.forEach(item => item.updateOnValueChanged(model, this.creator, options.name));
});
model.onMatrixDetailPanelVisibleChanged.add((sender, options) => {
editablePresets.forEach(item => item.updateOnMatrixDetailPanelVisibleChanged(model, this.creator, options));
});
return model;
}
public applyFromSurveyModel(): boolean {
if(!this.validateEditableModel(this.model)) return false;
this.preset.setJson(this.getJsonFromSurveyModel());
this.model.setValue("json_result", JSON.stringify(this.preset.getJson(), null, 2));
this.preset.apply(this.creator);
return true;
}
public getJsonFromSurveyModel(): any {
const res: any = {};
this.model.editablePresets.forEach(preset => {
const val = preset.getJsonValue(this.model);
if(!!val) {
res[preset.path] = val;
}
});
return res;
}
private validateEditableModel(model: SurveyModel): boolean {
if(!model.validate(true, true)) return false;
const editablePresets = model.editablePresets;
for(let i = 0; i < editablePresets.length; i ++) {
if(!editablePresets[i].validate(model)) return false;
}
return true;
}
private createEditablePresets(): Array<CreatorPresetEditableBase> {
const res = [];
this.preset.children.forEach(preset => {
const editable = preset.createEditable();
if (editable) {
res.push(editable);
}
});
return res;
}
private getEditModelJson(editablePresets: Array<CreatorPresetEditableBase>): any {
const modelJson = { pages: [], showTOC: true, showQuestionNumbers: false };
editablePresets.forEach(preset => {
const pages = preset.createPages();
if (Array.isArray(pages)) {
pages.forEach(page => modelJson.pages.push(page));
}
});
modelJson.pages.push({
name: "page_result",
title: "Preset JSON result",
elements: [
{ type: "comment", rows: 40, name: "json_result", titleLocation: "hidden", placeholder: "Please click on the 'Save' button" }
]
});
return modelJson;
}
}
3 changes: 2 additions & 1 deletion packages/survey-creator-core/src/presets/presets-toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export class CreatorPresetToolboxDefinition extends CreatorPresetBase {
private applyDefinition(creator: SurveyCreatorModel, defintion: Array<ICreatorPresetToolboxItem>): void {
if (!Array.isArray(defintion)) return;
const tb = creator.toolbox;
defintion.forEach(item => {
const def: Array<ICreatorPresetToolboxItem> = JSON.parse(JSON.stringify(defintion));
def.forEach(item => {
if (typeof item === "object" && !!item.name) {
const action = tb.getItemByName(item.name);
if (action) {
Expand Down
84 changes: 0 additions & 84 deletions packages/survey-creator-core/src/presets/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { CreatorPresetTabs } from "./presets-tabs";
import { CreatorPresetPropertyGrid } from "./presets-properties";
import { IToolboxCategoryDefinition } from "../toolbox";
import { ISurveyPropertyGridDefinition } from "../question-editor/definition";
import { SurveyModel } from "survey-core";
import { SurveyCreatorModel } from "../creator-base";

export interface ICreatorPresetData {
propertyGrid?: {
Expand Down Expand Up @@ -35,86 +33,4 @@ export class CreatorPreset extends CreatorPresetBase {
return [new CreatorPresetTabs(), new CreatorPresetToolbox(),
new CreatorPresetPropertyGrid()];
}
public createEditModel(creator?: SurveyCreatorModel): SurveyModel {
const editablePresets = this.createEditablePresets();
const model = new SurveyModel(this.getEditModelJson(editablePresets));
model.editablePresets = editablePresets;
model.showCompleteButton = false;
model.keepIncorrectValues = true;
const editingCreator = !!creator ? creator : new SurveyCreatorModel({});
model.addNavigationItem({
id: "preset_save",
title: "Save", //TODO
action: () => {
this.applyFromSurveyModel(model, creator);
}
});
editablePresets.forEach(item => item.setupQuestions(model, editingCreator));
const json = this.json ? this.json : {};
editablePresets.forEach(item => item.setupQuestionsValue(model, json[item.path], editingCreator));
model.onCurrentPageChanged.add((sender, options) => {
editablePresets.forEach(item => item.setupOnCurrentPage(model, editingCreator));
});
model.onValueChanged.add((sender, options) => {
editablePresets.forEach(item => item.updateOnValueChanged(model, editingCreator, options.name));
});
model.onMatrixDetailPanelVisibleChanged.add((sender, options) => {
editablePresets.forEach(item => item.updateOnMatrixDetailPanelVisibleChanged(model, editingCreator, options));
});
return model;
}
public applyFromSurveyModel(model: SurveyModel, creator?: SurveyCreatorModel): boolean {
if(!this.validatEditableModel(model)) return false;
this.setJson(this.getJsonFromSurveyModel(model));
model.setValue("json_result", JSON.stringify(this.getJson(), null, 2));
if (creator) {
this.apply(creator);
}
return true;
}
private validatEditableModel(model: SurveyModel): boolean {
if(!model.validate(true, true)) return false;
const editablePresets = model.editablePresets;
for(let i = 0; i < editablePresets.length; i ++) {
if(!editablePresets[i].validate(model)) return false;
}
return true;
}
public getJsonFromSurveyModel(model: SurveyModel): any {
const res: any = {};
model.editablePresets.forEach(preset => {
const val = preset.getJsonValue(model);
if(!!val) {
res[preset.path] = val;
}
});
return res;
}
private createEditablePresets(): Array<CreatorPresetEditableBase> {
const res = [];
this.children.forEach(preset => {
const editable = preset.createEditable();
if (editable) {
res.push(editable);
}
});
return res;
}
private getEditModelJson(editablePresets: Array<CreatorPresetEditableBase>): any {
const modelJson = { pages: [], showTOC: true, showQuestionNumbers: false };
editablePresets.forEach(preset => {
const pages = preset.createPages();
if (Array.isArray(pages)) {
pages.forEach(page => modelJson.pages.push(page));
}
});
modelJson.pages.push({
name: "page_result",
title: "Preset JSON result",
elements: [
{ type: "comment", rows: 40, name: "json_result", titleLocation: "hidden", placeholder: "Please click on the 'Save' button" }
]
});
return modelJson;
}
}
Loading

0 comments on commit 70643f6

Please sign in to comment.