Skip to content

Commit

Permalink
add activation types into custom widgets: surveyjs#714
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Telnov (DevExpress) authored and Andrew Telnov (DevExpress) committed Oct 12, 2017
1 parent f629fe1 commit e8eb06e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/questionCustomWidgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,61 @@ export class QuestionCustomWidget {
if (this.widgetJson.isFit) return this.widgetJson.isFit(question);
return false;
}
public activatedByChanged(activatedBy: string) {
if(this.widgetJson.activatedByChanged) {
this.widgetJson.activatedByChanged(activatedBy);
}
}
public get isDefaultRender() : boolean { return this.widgetJson.isDefaultRender; }
}

export class CustomWidgetCollection {
public static Instance: CustomWidgetCollection = new CustomWidgetCollection();
private widgetsValues: Array<QuestionCustomWidget> = [];
private widgetsActivatedBy = {};

public onCustomWidgetAdded: Event<(customWidget: QuestionCustomWidget) => any, any> = new Event<(customWidget: QuestionCustomWidget) => any, any>();

public get widgets(): Array<QuestionCustomWidget> { return this.widgetsValues; }
public addCustomWidget(widgetJson: any) {
public addCustomWidget(widgetJson: any, activatedBy: string = "property") {
var name = widgetJson.name;
if (!name) {
name = "widget_" + this.widgets.length + 1;
}
var customWidget = new QuestionCustomWidget(name, widgetJson);
this.widgetsValues.push(customWidget);
this.widgetsActivatedBy[name] = activatedBy;
customWidget.activatedByChanged(activatedBy);
this.onCustomWidgetAdded.fire(customWidget, null);
}
/**
* Returns the way the custom wiget is activated. It can be activated by a property ("property"), question type ("type") or by new/custom question type ("customtype").
* @param widgetName the custom widget name
* @see setActivatedBy
*/
public getActivatedBy(widgetName: string) {
var res = this.widgetsActivatedBy[widgetName];
return res ? res : "property";
}
/**
* Sets the way the custom wiget is activated. The activation types are: property ("property"), question type ("type") or new/custom question type ("customtype"). A custom wiget may support all or only some of this activation types.
* @param widgetName
* @param activatedBy there are three possible variants: "property", "type" and "customtype"
*/
public setActivatedBy(widgetName: string, activatedBy: string) {
if(!widgetName || !activatedBy) return;
var widget = this.getCustomWidgetByName(widgetName);
if(!widget) return;
this.widgetsActivatedBy[widgetName] = activatedBy;
widget.activatedByChanged(activatedBy);
}
public clear() { this.widgetsValues = []; }

public getCustomWidgetByName(name: string): QuestionCustomWidget {
for(var i = 0; i < this.widgets.length; i ++) {
if(this.widgets[i].name == name) return this.widgets[i];
}
return null;
}
public getCustomWidget(question: IQuestion): QuestionCustomWidget {
for (var i = 0; i < this.widgetsValues.length; i ++) {
if (this.widgetsValues[i].isFit(question)) return this.widgetsValues[i];
Expand Down
19 changes: 19 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,25 @@ QUnit.test("assign customWidgets to questions", function (assert) {
assert.equal((<Question>survey.getQuestionByName("question5")).customWidget.name, "second", "has the second custom widget");
CustomWidgetCollection.Instance.clear();
});
QUnit.test("customWidgets activation types changed", function (assert) {
CustomWidgetCollection.Instance.clear();
var lastActivatedBy = "";
var customWidgetJSON = {
name: "widget1",
isFit: (question) => { return question.name == "question2"; },
activatedByChanged: (activatedBy) => {
lastActivatedBy = activatedBy;
}
}
CustomWidgetCollection.Instance.addCustomWidget(customWidgetJSON);
assert.equal(lastActivatedBy, "property", "activatedBy set to 'property' by default");
CustomWidgetCollection.Instance.setActivatedBy("widget1", "type");
assert.equal(lastActivatedBy, "type", "activatedBy set to 'type'");
CustomWidgetCollection.Instance.setActivatedBy("widget1", "customtype");
assert.equal(lastActivatedBy, "customtype", "activatedBy set to 'customtype'");
CustomWidgetCollection.Instance.clear();
});

QUnit.test("assign customWidgets to matrix dynamic cell question", function (assert) {
CustomWidgetCollection.Instance.clear();
CustomWidgetCollection.Instance.addCustomWidget({ name: "first", isFit: (question) => { return question["renderAs"] === 'testwidget'; } });
Expand Down

0 comments on commit e8eb06e

Please sign in to comment.