Skip to content

Commit

Permalink
Refactor HtmlOnCondition and Validators Editors, surveyjs/survey-libr…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Apr 22, 2019
1 parent 2c76e40 commit 3372819
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 245 deletions.
21 changes: 11 additions & 10 deletions src/localization/english.ts
Expand Up @@ -6,7 +6,8 @@ export var enStrings = {
survey: {
edit: "Edit",
externalHelpLink: "Watch and learn how to create surveys",
externalHelpLinkUrl: "https://www.youtube.com/channel/UCH2ru9okIcooNZfpIbyq4qQ?view_as=subscriber",
externalHelpLinkUrl:
"https://www.youtube.com/channel/UCH2ru9okIcooNZfpIbyq4qQ?view_as=subscriber",
dropQuestion: "Please drop a question here from the Toolbox on the left.",
copy: "Copy",
addToToolbox: "Add to toolbox",
Expand Down Expand Up @@ -229,6 +230,14 @@ export var enStrings = {
html: "Html",
expression: "Expression",

minValue: "Minimum value",
maxValue: "Maximum value",
minLength: "Minimum length",
allowDigits: "Allow digits",
minCount: "Minimum count",
maxCount: "Maximum count",
regex: "Regular expression",

tabs: {
general: "General",
fileOptions: "Options",
Expand Down Expand Up @@ -541,15 +550,7 @@ export var enStrings = {
hasNone: "hasNone",
noneText: "noneText",

text: "text",
minValue: "Minimum value",
maxValue: "Maximum value",
minLength: "Minimum length",
maxLength: "Maximum length",
allowDigits: "Allow digits",
minCount: "Minimum count",
maxCount: "Maximum count",
regex: "Regular expression"
text: "text"
}
};
//Uncomment this line on creating a translation file. You should replace "en" and enStrings with your locale ("fr", "de" and so on) and your variable.
Expand Down
92 changes: 18 additions & 74 deletions src/propertyEditors/propertyHtmlConditionEditor.ts
@@ -1,102 +1,46 @@
import * as ko from "knockout";
import * as Survey from "survey-knockout";
import { SurveyPropertyItemsEditor } from "./propertyItemsEditor";
import {
SurveyPropertyOneSelectedEditor,
SurveyPropertyOneSelectedItem
} from "./propertyOneSelectedEditor";

import { SurveyPropertyEditorBase } from "./propertyEditorBase";
import { SurveyQuestionEditor } from "../questionEditors/questionEditor";
import { SurveyPropertyEditorFactory } from "./propertyEditorFactory";
import { editorLocalization } from "../entries";

export class SurveyPropertyHtmlConditionEditor extends SurveyPropertyItemsEditor {
public selectedObjectEditor: any;
public koSelected: any;
export class SurveyPropertyHtmlConditionEditor extends SurveyPropertyOneSelectedEditor {
constructor(property: Survey.JsonObjectProperty) {
super(property);
var self = this;
this.selectedObjectEditor = ko.observable(null);
this.koSelected = ko.observable(null);
this.koSelected.subscribe(function(newValue) {
if (!!self.selectedObjectEditor()) {
self.selectedObjectEditor().apply();
}
var editor =
newValue != null
? new SurveyQuestionEditor(
newValue.htmlConditionItem,
null,
null,
self.options
)
: null;
self.selectedObjectEditor(editor);
});
this.onDeleteClick = function() {
self.koItems.remove(self.koSelected());
};
}
public get editorType(): string {
return "htmlconditions";
}
protected AddItem() {
super.AddItem();
this.koSelected(this.koItems()[this.koItems().length - 1]);
}
protected onValueChanged() {
super.onValueChanged();
if (this.koSelected) {
this.koSelected(this.koItems().length > 0 ? this.koItems()[0] : null);
}
protected getObjClassName() {
return "htmlconditionitem";
}
protected createEditorItem(item: any) {
var jsonObj = new Survey.JsonObject();
var htmlConditionItem = Survey.JsonObject.metaData.createClass(
"htmlconditionitem"
);
jsonObj.toObject(item, htmlConditionItem);
this.setItemProperties(htmlConditionItem);
htmlConditionItem.survey = this.object;
return new SurveyPropertyHtmlConditionItem(htmlConditionItem);
}
protected createNewEditorItem(): any {
return this.createEditorItem(
Survey.JsonObject.metaData.createClass("htmlconditionitem")
);
}

protected createItemFromEditorItem(editorItem: any) {
var item = <SurveyPropertyHtmlConditionItem>editorItem;
return item.htmlConditionItem;
}
private setItemProperties(validator: any) {
if (this.object) {
validator["survey"] = this.object.survey;
}
validator.locOwner = this;
protected createOneSelectedItem(obj: any): SurveyPropertyOneSelectedItem {
return new SurveyPropertyHtmlConditionItem(obj);
}
}

export class SurveyPropertyHtmlConditionItem {
public koText: any;
constructor(public htmlConditionItem: Survey.Base) {
this.koText = ko.observable(this.text);
export class SurveyPropertyHtmlConditionItem extends SurveyPropertyOneSelectedItem {
constructor(public obj: Survey.Base) {
super(obj);
var self = this;
htmlConditionItem.registerFunctionOnPropertyValueChanged(
"expression",
function() {
self.objectChanged();
}
);
obj.registerFunctionOnPropertyValueChanged("expression", function() {
self.objectChanged();
});
}
public get text() {
var expression = this.htmlConditionItem["expression"];
public getText() {
var expression = this.obj["expression"];
return (
editorLocalization.getString("ed.completedHtmlOnConditionItemText") +
" " +
(!!expression ? expression : "?")
);
}
public objectChanged() {
this.koText(this.text);
}
}

SurveyPropertyEditorFactory.registerEditor("htmlconditions", function(
Expand Down
1 change: 0 additions & 1 deletion src/propertyEditors/propertyModalEditor.ts
Expand Up @@ -78,7 +78,6 @@ export class SurveyPropertyModalEditor extends SurveyPropertyEditorBase {
["format"](this.property.name)
);
}
var name = property ? property.name : "";
this.modalName =
"modelEditor" + this.editorType + SurveyPropertyModalEditor.idCounter;
SurveyPropertyModalEditor.idCounter++;
Expand Down
96 changes: 96 additions & 0 deletions src/propertyEditors/propertyOneSelectedEditor.ts
@@ -0,0 +1,96 @@
import * as ko from "knockout";
import * as Survey from "survey-knockout";
import { SurveyPropertyItemsEditor } from "./propertyItemsEditor";
import { SurveyQuestionEditor } from "../questionEditors/questionEditor";

export class SurveyPropertyOneSelectedEditor extends SurveyPropertyItemsEditor {
public selectedObjectEditor: any;
public koSelected: any;
public koAvailableClasses: any;
private currentObjClassName: string;
constructor(property: Survey.JsonObjectProperty) {
super(property);
var self = this;
this.koAvailableClasses = ko.observableArray(this.getAvailableClasses());
this.selectedObjectEditor = ko.observable(null);
this.koSelected = ko.observable(null);
this.koSelected.subscribe(function(newValue) {
if (!!self.selectedObjectEditor()) {
self.selectedObjectEditor().apply();
}
var editor =
newValue != null
? new SurveyQuestionEditor(newValue.obj, null, null, self.options)
: null;
self.selectedObjectEditor(editor);
});
this.onDeleteClick = function() {
self.koItems.remove(self.koSelected());
};
this.onAddClick = function(item: any) {
self.addNewItem(!!item ? item.value : null);
};
}
public get editorTypeTemplate(): string {
return "oneselected";
}
public get editorType(): string {
return "oneselected";
}
protected createOneSelectedItem(obj: any): SurveyPropertyOneSelectedItem {
return new SurveyPropertyOneSelectedItem(obj);
}
protected getObjClassName() {
return this.currentObjClassName;
}
protected getAvailableClasses(): Array<any> {
return [];
}
protected addNewItem(className: string) {
this.currentObjClassName = className;
this.AddItem();
this.koSelected(this.koItems()[this.koItems().length - 1]);
}
protected onValueChanged() {
super.onValueChanged();
if (this.koSelected) {
this.koSelected(this.koItems().length > 0 ? this.koItems()[0] : null);
}
}
protected createEditorItem(item: any) {
var jsonObj = new Survey.JsonObject();
var newItem = Survey.JsonObject.metaData.createClass(item.getType());
jsonObj.toObject(item, newItem);
this.setItemProperties(newItem);
return this.createOneSelectedItem(newItem);
}
protected createNewEditorItem(): any {
return this.createEditorItem(
Survey.JsonObject.metaData.createClass(this.getObjClassName())
);
}
protected createItemFromEditorItem(editorItem: any) {
var item = <SurveyPropertyOneSelectedItem>editorItem;
delete item.obj["survey"];
return item.obj;
}
private setItemProperties(obj: any) {
if (this.object) {
obj["survey"] = this.object.survey;
}
obj.locOwner = this;
}
}

export class SurveyPropertyOneSelectedItem {
public koText: any;
constructor(public obj: Survey.Base) {
this.koText = ko.observable(this.getText());
}
public getText() {
return "";
}
public objectChanged() {
this.koText(this.getText());
}
}

0 comments on commit 3372819

Please sign in to comment.