Skip to content

Allow set survey background image using JSON and Creator #5955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/survey-angular-ui/src/survey-content.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-template #template>
<div #surveyContainer *ngIf="!!model" [class]="model.getRootCss()">
<form onsubmit="return false;">
<div #surveyContainer *ngIf="!!model" [class]="model.getRootCss()" [style.backgroundImage]="model.renderBackgroundImage">
<form onsubmit="return false;" [style.backgroundColor]="model.renderBackgroundOpacity">
<div class="sv_custom_header" [hidden]="model.hasLogo"></div>
<div [class]="model.css.container">
<div *ngIf="model.renderedHasHeader" [class]="model.css.header" [survey]="model" sv-ng-survey-header></div>
Expand Down
4 changes: 2 additions & 2 deletions src/knockout/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
</script>

<script type="text/html" id="survey-content-template">
<div data-bind="css: rootCss">
<form onsubmit="return false;">
<div data-bind="css: rootCss, style: { backgroundImage: renderBackgroundImage }">
<form onsubmit="return false;" data-bind="style: { backgroundColor: renderBackgroundOpacity }">
<div class="sv_custom_header" data-bind="visible: !hasLogo"></div>
<div data-bind="css: containerCss">
<!-- ko template: { name: koTitleTemplate, afterRender: koAfterRenderHeader } -->
Expand Down
10 changes: 8 additions & 2 deletions src/react/reactSurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ export class Survey extends SurveyElementBase<any, any>
}
const rootCss = this.survey.getRootCss();
const cssClasses = this.rootNodeClassName ? this.rootNodeClassName + " " + rootCss : rootCss;
const rootStyle = {
backgroundImage: this.survey.renderBackgroundImage
};
const formStyle = {
backgroundColor: this.survey.renderBackgroundOpacity
};

return (
<div id={this.rootNodeId} ref={this.rootRef} className={cssClasses}>
<form onSubmit={onSubmit}>
<div id={this.rootNodeId} ref={this.rootRef} className={cssClasses} style={rootStyle}>
<form onSubmit={onSubmit} style={formStyle}>
{customHeader}
<div className={this.css.container}>
{header}
Expand Down
28 changes: 28 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ export class SurveyModel extends SurveyElementCore
this.createHtmlLocString("completedBeforeHtml", "completingSurveyBefore", htmlCallBack);
this.createHtmlLocString("loadingHtml", "loadingSurvey", htmlCallBack);
this.createLocalizableString("logo", this, false);
this.createLocalizableString("backgroundImage", this, false);
this.createLocalizableString("startSurveyText", this, false, true);
this.createLocalizableString("pagePrevText", this, false, true);
this.createLocalizableString("pageNextText", this, false, true);
Expand Down Expand Up @@ -1873,6 +1874,31 @@ export class SurveyModel extends SurveyElementCore
}
return "";
}
/**
* Gets or sets a survey backgroundImage.
*/
public get backgroundImage(): string {
return this.getLocalizableStringText("backgroundImage");
}
public set backgroundImage(value: string) {
this.setLocalizableStringText("backgroundImage", value);
}
get locBackgroundImage(): LocalizableString {
return this.getLocalizableString("backgroundImage");
}
get renderBackgroundImage(): string {
return ["url(", this.getLocalizableString("backgroundImage").renderedHtml, ")"].join("");
}
public get backgroundOpacity(): number {
return this.getPropertyValue("backgroundOpacity");
}
public set backgroundOpacity(val: number) {
this.setPropertyValue("backgroundOpacity", val);
}
public get renderBackgroundOpacity(): string {
const alpha = 1 - this.backgroundOpacity;
return ["rgba(255, 255, 255, ", alpha, ")"].join("");
}
/**
* HTML content displayed on the [complete page](https://surveyjs.io/form-library/documentation/design-survey/create-a-multi-page-survey#complete-page).
*
Expand Down Expand Up @@ -7088,5 +7114,7 @@ Serializer.addClass("survey", [
choices: ["auto", "static", "responsive"],
},
"width",
{ name: "backgroundImage", serializationProperty: "locBackgroundImage", visible: false },
{ name: "backgroundOpacity:number", minValue: 0, maxValue: 1, default: 1, visible: false },
{ name: "showBrandInfo:boolean", default: false, visible: false }
]);
8 changes: 6 additions & 2 deletions src/vue/survey.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<template>
<div :class="survey.getRootCss()">
<form onsubmit="return false;">
<div :class="survey.getRootCss()"
:style="{ backgroundImage: vueSurvey.renderBackgroundImage }"
>
<form onsubmit="return false;"
:style="{ backgroundColor: vueSurvey.renderBackgroundOpacity }"
>
<div v-if="!vueSurvey.hasLogo" class="sv_custom_header"></div>
<div :class="css.container">
<survey-header :survey="vueSurvey" />
Expand Down
12 changes: 12 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16746,3 +16746,15 @@ QUnit.test("Use variables as default values in expression", function (assert) {
assert.equal(q2.value, "BBB", "q2.value");
assert.equal(q3.value, "CCC", "q3.value");
});

QUnit.test("backgroundImage backgroundOpacity", assert => {
const imageUrl = "https://image.shutterstock.com/image-photo/agave-cactus-abstract-natural-pattern-600w-1056037874.jpg";
const survey = new SurveyModel({
"backgroundImage": imageUrl,
"backgroundOpacity": 0.6,
});
assert.equal(survey.backgroundImage, imageUrl, "backgroundImage");
assert.equal(survey.renderBackgroundImage, ["url(", imageUrl, ")"].join(""), "renderBackgroundImage");
assert.equal(survey.backgroundOpacity, 0.6, "backgroundOpacity");
assert.equal(survey.renderBackgroundOpacity, "rgba(255, 255, 255, 0.4)", "renderBackgroundOpacity");
});
4 changes: 3 additions & 1 deletion visualRegressionTests/constants.ts

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions visualRegressionTests/tests/defaultV2/survey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Selector, ClientFunction } from "testcafe";
import { setData } from "../../../testCafe/helper";
import { url, frameworks, initSurvey, url_test, takeElementScreenshot, wrapVisualTest, explicitErrorHandler } from "../../helper";
import { backgroundImage } from "../../constants";

const title = "Survey Screenshot";

Expand Down Expand Up @@ -221,6 +222,28 @@ frameworks.forEach(framework => {
await takeElementScreenshot("survey-title-with-logo.png", Selector(".sd-title"), t, comparer); // without title and progress
});

});
test("Check survey with backgroundImage", async (t) => {
await wrapVisualTest(t, async (t, comparer) => {
await t.resizeWindow(1280, 900);
await initSurvey(framework, {
"backgroundImage": backgroundImage,
"backgroundOpacity": 0.7,
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "question1"
}
]
}
]
});
await takeElementScreenshot("survey-with-backgroundImage.png", Selector(".sd-root-modern"), t, comparer);
});

});
test("Check survey navigation bar", async (t) => {
await wrapVisualTest(t, async (t, comparer) => {
Expand Down