Skip to content

Commit

Permalink
add showInToolbox parameter into registerElement function fix #8383 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Jun 9, 2024
1 parent d8e0459 commit 3842aba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
36 changes: 22 additions & 14 deletions src/questionfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class QuestionFactory {
var itemName = surveyLocalization.getString("multipletext_itemname");
return [itemName + "1", itemName + "2"];
}
public registerQuestion(questionType: string, questionCreator: (name: string) => Question): void {
ElementFactory.Instance.registerElement(questionType, questionCreator);
public registerQuestion(questionType: string, questionCreator: (name: string) => Question, showInToolbox: boolean = true): void {
ElementFactory.Instance.registerElement(questionType, questionCreator, showInToolbox);
}
public registerCustomQuestion(questionType: string) : void {
ElementFactory.Instance.registerCustomQuestion(questionType);
Expand All @@ -48,18 +48,18 @@ export class QuestionFactory {

export class ElementFactory {
public static Instance: ElementFactory = new ElementFactory();
private creatorHash: HashTable<(name: string) => IElement> = {};
private creatorHash: HashTable<{showInToolbox: boolean, creator: (name: string) => IElement}> = {};

public registerElement(elementType: string, elementCreator: (name: string) => IElement): void {
this.creatorHash[elementType] = elementCreator;
public registerElement(elementType: string, elementCreator: (name: string) => IElement, showInToolbox: boolean = true): void {
this.creatorHash[elementType] = { showInToolbox: showInToolbox, creator: elementCreator };
}
public registerCustomQuestion = (questionType: string) : void => {
public registerCustomQuestion = (questionType: string, showInToolbox: boolean = true) : void => {
const creator = (name: string): Question => {
const el = Serializer.createClass(questionType);
if(!!el) el.name = name;
return el;
};
this.registerElement(questionType, creator);
this.registerElement(questionType, creator, showInToolbox);
}
public clear(): void {
this.creatorHash = {};
Expand All @@ -70,18 +70,26 @@ export class ElementFactory {
Serializer.removeClass(elementType);
}
}
public getAllToolboxTypes(): Array<string> {
return this.getAllTypesCore(true);
}
public getAllTypes(): Array<string> {
var result = new Array<string>();
for (var key in this.creatorHash) {
result.push(key);
}
return result.sort();
return this.getAllTypesCore(false);
}
public createElement(elementType: string, name: string): IElement {
var creator = this.creatorHash[elementType];
if (!!creator) return creator(name);
var item = this.creatorHash[elementType];
if (!!item && !!item.creator) return item.creator(name);
const compJSON = ComponentCollection.Instance.getCustomQuestionByName(elementType);
if(!!compJSON) return ComponentCollection.Instance.createQuestion(name, compJSON);
return null;
}
private getAllTypesCore(showInToolboxOnly: boolean): Array<string> {
var result = new Array<string>();
for (var key in this.creatorHash) {
if(!showInToolboxOnly || this.creatorHash[key].showInToolbox) {
result.push(key);
}
}
return result.sort();
}
}
15 changes: 13 additions & 2 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SurveyElement } from "../src/survey-element";
import { SurveyModel } from "../src/survey";
import { PageModel } from "../src/page";
import { PanelModel } from "../src/panel";
import { QuestionFactory } from "../src/questionfactory";
import { ElementFactory, QuestionFactory } from "../src/questionfactory";
import { Question } from "../src/question";
import { QuestionHtmlModel } from "../src/question_html";
import { QuestionImageModel } from "../src/question_image";
Expand All @@ -29,7 +29,7 @@ import {
MultipleTextItemModel,
} from "../src/question_multipletext";
import { QuestionMatrixModel } from "../src/question_matrix";
import { ISurveyData, LayoutElementContainer } from "../src/base-interfaces";
import { IElement, ISurveyData, LayoutElementContainer } from "../src/base-interfaces";
import { ItemValue } from "../src/itemvalue";
import { QuestionDropdownModel } from "../src/question_dropdown";
import { QuestionCheckboxModel } from "../src/question_checkbox";
Expand Down Expand Up @@ -5131,6 +5131,17 @@ QUnit.test("Create custom widget from addQuestion", function (assert) {
Serializer.removeClass(cType);
QuestionFactory.Instance.unregisterElement(cType);
});
QUnit.test("ElementFactory.getAllToolboxTypes()", function (assert) {
let defaultToolboxNames = ElementFactory.Instance.getAllToolboxTypes();
let defaultNames = ElementFactory.Instance.getAllTypes();
assert.deepEqual(defaultToolboxNames, defaultNames, "They are the same by default");
const type = "toolbox-test-type";
ElementFactory.Instance.registerElement(type, (name: string): IElement => { return new PanelModel(name); }, false);
defaultToolboxNames = ElementFactory.Instance.getAllToolboxTypes();
defaultNames = ElementFactory.Instance.getAllTypes();
assert.equal(defaultToolboxNames.length + 1, defaultNames.length, "We do use the new type for toolbox");
ElementFactory.Instance.unregisterElement(type);
});
QUnit.test("readOnlyCallback, bug #1818", function (assert) {
CustomWidgetCollection.Instance.clear();
var readOnlyCounter = 0;
Expand Down

0 comments on commit 3842aba

Please sign in to comment.