Skip to content

Commit 6493333

Browse files
committed
Merged PR 16158: Javascript SDK - Apply theme API
apply theme API
1 parent 2a3a49b commit 6493333

File tree

9 files changed

+211
-14
lines changed

9 files changed

+211
-14
lines changed

dist/powerbi-client.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! powerbi-client v2.6.7 | (c) 2016 Microsoft Corporation MIT */
1+
/*! powerbi-client v2.6.8 | (c) 2016 Microsoft Corporation MIT */
22
declare module "util" {
33
/**
44
* Raises a custom event with event data on the specified HTML element.
@@ -110,6 +110,7 @@ declare module "embed" {
110110
dashboardId?: string;
111111
height?: number;
112112
width?: number;
113+
theme?: models.IReportTheme;
113114
}
114115
export interface IVisualEmbedConfiguration extends IEmbedConfiguration {
115116
visualName: string;
@@ -933,6 +934,23 @@ declare module "report" {
933934
* ```
934935
*/
935936
refresh(): Promise<void>;
937+
/**
938+
* Apply a theme to the report
939+
*
940+
* ```javascript
941+
* report.applyTheme(theme);
942+
* ```
943+
*/
944+
applyTheme(theme: models.IReportTheme): Promise<void>;
945+
/**
946+
* Reset and apply the default theme of the report
947+
*
948+
* ```javascript
949+
* report.resetTheme();
950+
* ```
951+
*/
952+
resetTheme(): Promise<void>;
953+
private applyThemeInternal(theme);
936954
private viewModeToString(viewMode);
937955
}
938956
}

dist/powerbi.js

Lines changed: 35 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/powerbi.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "powerbi-client",
3-
"version": "2.6.7",
3+
"version": "2.6.8",
44
"description": "JavaScript library for embedding Power BI into your apps. Provides service which makes it easy to embed different types of components and an object model which allows easy interaction with these components such as changing pages, applying filters, and responding to data selection.",
55
"main": "dist/powerbi.js",
66
"typings": "dist/powerbi-client.d.ts",

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const config = {
2-
version: '2.6.7',
2+
version: '2.6.8',
33
type: 'js'
44
};
55

src/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ export class Create extends embed.Embed {
4848
const config = <embed.IEmbedConfiguration>this.config;
4949

5050
this.createConfig = {
51-
datasetId: config.datasetId || this.getId(),
5251
accessToken: config.accessToken,
53-
tokenType: config.tokenType,
52+
datasetId: config.datasetId || this.getId(),
53+
groupId: config.groupId,
5454
settings: settings,
55-
groupId: config.groupId
55+
tokenType: config.tokenType,
56+
theme: config.theme
5657
}
5758
}
5859

src/embed.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface IEmbedConfiguration extends IEmbedConfigurationBase {
5858
dashboardId?: string;
5959
height?: number;
6060
width?: number;
61+
theme?: models.IReportTheme;
6162
}
6263

6364
export interface IVisualEmbedConfiguration extends IEmbedConfiguration {

src/report.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,38 @@ export class Report extends embed.Embed implements IReportNode, IFilterable {
365365
});
366366
}
367367

368+
/**
369+
* Apply a theme to the report
370+
*
371+
* ```javascript
372+
* report.applyTheme(theme);
373+
* ```
374+
*/
375+
applyTheme(theme: models.IReportTheme): Promise<void> {
376+
return this.applyThemeInternal(theme);
377+
}
378+
379+
/**
380+
* Reset and apply the default theme of the report
381+
*
382+
* ```javascript
383+
* report.resetTheme();
384+
* ```
385+
*/
386+
resetTheme(): Promise<void> {
387+
return this.applyThemeInternal(<models.IReportTheme>{});
388+
}
389+
390+
private applyThemeInternal(theme: models.IReportTheme): Promise<void> {
391+
return this.service.hpm.put<models.IError[]>('/report/theme', theme, { uid: this.config.uniqueId }, this.iframe.contentWindow)
392+
.then(response => {
393+
return response.body;
394+
})
395+
.catch(response => {
396+
throw response.body;
397+
});
398+
}
399+
368400
private viewModeToString(viewMode: models.ViewMode): string {
369401
let mode: string;
370402
switch (viewMode) {

test/test.spec.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,40 @@ describe('service', function () {
639639
// Assert
640640
expect(report.createConfig.datasetId).toEqual(testDatasetId);
641641
});
642+
643+
it('theme should be in create config if exists is embedConfig', function () {
644+
// Arrange
645+
646+
const testDatasetId = "ABC123";
647+
const accessToken = 'ABC123';
648+
const theme = {themeJson: {name: "Theme ABC 123"}};
649+
const embedUrl = `https://app.powerbi.com/reportEmbed?datasetId=${testDatasetId}`;
650+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}"</div>`)
651+
.appendTo('#powerbi-fixture');
652+
653+
// Act
654+
const report = powerbi.createReport($reportContainer[0], { embedUrl: embedUrl, accessToken: accessToken, theme: theme });
655+
656+
// Assert
657+
expect(report.createConfig.theme).toEqual(theme);
658+
});
659+
660+
it('theme should be undefined in create config if not exists is embedConfig', function () {
661+
// Arrange
662+
663+
const testDatasetId = "ABC123";
664+
const accessToken = 'ABC123';
665+
const theme = {themeJson: {name: "Theme ABC 123"}};
666+
const embedUrl = `https://app.powerbi.com/reportEmbed?datasetId=${testDatasetId}`;
667+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}"</div>`)
668+
.appendTo('#powerbi-fixture');
669+
670+
// Act
671+
const report = powerbi.createReport($reportContainer[0], { embedUrl: embedUrl, accessToken: accessToken });
672+
673+
// Assert
674+
expect(report.createConfig.theme).toBeUndefined();
675+
});
642676
});
643677

644678
describe('reports', function () {
@@ -725,6 +759,40 @@ describe('service', function () {
725759
// Assert
726760
expect((<embed.IEmbedConfiguration>report.config).id).toEqual(testReportId);
727761
});
762+
763+
it('theme should be in report config if exists is embedConfig', function () {
764+
// Arrange
765+
const testReportId = "ABC123";
766+
const embedUrl = `https://app.powerbi.com/reportEmbed?reportId=${testReportId}`;
767+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id></div>`)
768+
.appendTo('#powerbi-fixture');
769+
770+
const theme = {themeJson: {name: "Theme ABC 123"}};
771+
const configuration: embed.IEmbedConfiguration = { theme: theme };
772+
773+
// Act
774+
const report = powerbi.embed($reportContainer[0], configuration);
775+
776+
// Assert
777+
expect((<embed.IEmbedConfiguration>report.config).theme).toEqual(theme);
778+
});
779+
780+
it('theme should be undefined in report config if not exists is embedConfig', function () {
781+
// Arrange
782+
const testReportId = "ABC123";
783+
const embedUrl = `https://app.powerbi.com/reportEmbed?reportId=${testReportId}`;
784+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id></div>`)
785+
.appendTo('#powerbi-fixture');
786+
787+
const theme = {themeJson: {name: "Theme ABC 123"}};
788+
const configuration: embed.IEmbedConfiguration = {};
789+
790+
// Act
791+
const report = powerbi.embed($reportContainer[0], configuration);
792+
793+
// Assert
794+
expect((<embed.IEmbedConfiguration>report.config).theme).toBeUndefined();
795+
});
728796
});
729797

730798
xdescribe('tiles', function () {
@@ -3795,6 +3863,53 @@ describe('SDK-to-HPM', function () {
37953863
});
37963864
});
37973865
});
3866+
3867+
describe('theme', function () {
3868+
it('report.applyTheme(theme) sends PUT /report/theme with theme in body', function () {
3869+
// Arrange
3870+
const testData = {
3871+
theme: {
3872+
themeJson: {
3873+
name: "Theme ABC 123"
3874+
}
3875+
},
3876+
response: {
3877+
body: null
3878+
}
3879+
};
3880+
3881+
spyHpm.post.and.returnValue(Promise.resolve(testData.response));
3882+
report.applyTheme(testData.theme)
3883+
.then(() => {
3884+
spyHpm.post.calls.reset();
3885+
3886+
// Act
3887+
report.reload();
3888+
3889+
// Assert
3890+
expect(spyHpm.post).toHaveBeenCalledWith('/report/load', jasmine.objectContaining(testData.theme), { uid: uniqueId, sdkSessionId: sdkSessionId }, iframe.contentWindow);
3891+
});
3892+
});
3893+
3894+
it('report.resetTheme() sends PUT /report/theme with empty object as theme in body', function () {
3895+
// Arrange
3896+
const response = {
3897+
body: null
3898+
};
3899+
3900+
spyHpm.post.and.returnValue(Promise.resolve(response));
3901+
report.resetTheme()
3902+
.then(() => {
3903+
spyHpm.post.calls.reset();
3904+
3905+
// Act
3906+
report.reload();
3907+
3908+
// Assert
3909+
expect(spyHpm.post).toHaveBeenCalledWith('/report/load', jasmine.objectContaining({}), { uid: uniqueId, sdkSessionId: sdkSessionId }, iframe.contentWindow);
3910+
});
3911+
});
3912+
});
37983913
});
37993914

38003915
describe('SDK-to-Router (Event subscription)', function () {

0 commit comments

Comments
 (0)