Skip to content

Commit

Permalink
Merged PR 16158: Javascript SDK - Apply theme API
Browse files Browse the repository at this point in the history
apply theme API
  • Loading branch information
laurent-mic committed Jan 23, 2019
1 parent 2a3a49b commit 6493333
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 14 deletions.
20 changes: 19 additions & 1 deletion dist/powerbi-client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! powerbi-client v2.6.7 | (c) 2016 Microsoft Corporation MIT */
/*! powerbi-client v2.6.8 | (c) 2016 Microsoft Corporation MIT */
declare module "util" {
/**
* Raises a custom event with event data on the specified HTML element.
Expand Down Expand Up @@ -110,6 +110,7 @@ declare module "embed" {
dashboardId?: string;
height?: number;
width?: number;
theme?: models.IReportTheme;
}
export interface IVisualEmbedConfiguration extends IEmbedConfiguration {
visualName: string;
Expand Down Expand Up @@ -933,6 +934,23 @@ declare module "report" {
* ```
*/
refresh(): Promise<void>;
/**
* Apply a theme to the report
*
* ```javascript
* report.applyTheme(theme);
* ```
*/
applyTheme(theme: models.IReportTheme): Promise<void>;
/**
* Reset and apply the default theme of the report
*
* ```javascript
* report.resetTheme();
* ```
*/
resetTheme(): Promise<void>;
private applyThemeInternal(theme);
private viewModeToString(viewMode);
}
}
Expand Down
40 changes: 35 additions & 5 deletions dist/powerbi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/powerbi.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerbi-client",
"version": "2.6.7",
"version": "2.6.8",
"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.",
"main": "dist/powerbi.js",
"typings": "dist/powerbi-client.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const config = {
version: '2.6.7',
version: '2.6.8',
type: 'js'
};

Expand Down
7 changes: 4 additions & 3 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ export class Create extends embed.Embed {
const config = <embed.IEmbedConfiguration>this.config;

this.createConfig = {
datasetId: config.datasetId || this.getId(),
accessToken: config.accessToken,
tokenType: config.tokenType,
datasetId: config.datasetId || this.getId(),
groupId: config.groupId,
settings: settings,
groupId: config.groupId
tokenType: config.tokenType,
theme: config.theme
}
}

Expand Down
1 change: 1 addition & 0 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface IEmbedConfiguration extends IEmbedConfigurationBase {
dashboardId?: string;
height?: number;
width?: number;
theme?: models.IReportTheme;
}

export interface IVisualEmbedConfiguration extends IEmbedConfiguration {
Expand Down
32 changes: 32 additions & 0 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,38 @@ export class Report extends embed.Embed implements IReportNode, IFilterable {
});
}

/**
* Apply a theme to the report
*
* ```javascript
* report.applyTheme(theme);
* ```
*/
applyTheme(theme: models.IReportTheme): Promise<void> {
return this.applyThemeInternal(theme);
}

/**
* Reset and apply the default theme of the report
*
* ```javascript
* report.resetTheme();
* ```
*/
resetTheme(): Promise<void> {
return this.applyThemeInternal(<models.IReportTheme>{});
}

private applyThemeInternal(theme: models.IReportTheme): Promise<void> {
return this.service.hpm.put<models.IError[]>('/report/theme', theme, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}

private viewModeToString(viewMode: models.ViewMode): string {
let mode: string;
switch (viewMode) {
Expand Down
115 changes: 115 additions & 0 deletions test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,40 @@ describe('service', function () {
// Assert
expect(report.createConfig.datasetId).toEqual(testDatasetId);
});

it('theme should be in create config if exists is embedConfig', function () {
// Arrange

const testDatasetId = "ABC123";
const accessToken = 'ABC123';
const theme = {themeJson: {name: "Theme ABC 123"}};
const embedUrl = `https://app.powerbi.com/reportEmbed?datasetId=${testDatasetId}`;
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}"</div>`)
.appendTo('#powerbi-fixture');

// Act
const report = powerbi.createReport($reportContainer[0], { embedUrl: embedUrl, accessToken: accessToken, theme: theme });

// Assert
expect(report.createConfig.theme).toEqual(theme);
});

it('theme should be undefined in create config if not exists is embedConfig', function () {
// Arrange

const testDatasetId = "ABC123";
const accessToken = 'ABC123';
const theme = {themeJson: {name: "Theme ABC 123"}};
const embedUrl = `https://app.powerbi.com/reportEmbed?datasetId=${testDatasetId}`;
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}"</div>`)
.appendTo('#powerbi-fixture');

// Act
const report = powerbi.createReport($reportContainer[0], { embedUrl: embedUrl, accessToken: accessToken });

// Assert
expect(report.createConfig.theme).toBeUndefined();
});
});

describe('reports', function () {
Expand Down Expand Up @@ -725,6 +759,40 @@ describe('service', function () {
// Assert
expect((<embed.IEmbedConfiguration>report.config).id).toEqual(testReportId);
});

it('theme should be in report config if exists is embedConfig', function () {
// Arrange
const testReportId = "ABC123";
const embedUrl = `https://app.powerbi.com/reportEmbed?reportId=${testReportId}`;
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id></div>`)
.appendTo('#powerbi-fixture');

const theme = {themeJson: {name: "Theme ABC 123"}};
const configuration: embed.IEmbedConfiguration = { theme: theme };

// Act
const report = powerbi.embed($reportContainer[0], configuration);

// Assert
expect((<embed.IEmbedConfiguration>report.config).theme).toEqual(theme);
});

it('theme should be undefined in report config if not exists is embedConfig', function () {
// Arrange
const testReportId = "ABC123";
const embedUrl = `https://app.powerbi.com/reportEmbed?reportId=${testReportId}`;
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id></div>`)
.appendTo('#powerbi-fixture');

const theme = {themeJson: {name: "Theme ABC 123"}};
const configuration: embed.IEmbedConfiguration = {};

// Act
const report = powerbi.embed($reportContainer[0], configuration);

// Assert
expect((<embed.IEmbedConfiguration>report.config).theme).toBeUndefined();
});
});

xdescribe('tiles', function () {
Expand Down Expand Up @@ -3795,6 +3863,53 @@ describe('SDK-to-HPM', function () {
});
});
});

describe('theme', function () {
it('report.applyTheme(theme) sends PUT /report/theme with theme in body', function () {
// Arrange
const testData = {
theme: {
themeJson: {
name: "Theme ABC 123"
}
},
response: {
body: null
}
};

spyHpm.post.and.returnValue(Promise.resolve(testData.response));
report.applyTheme(testData.theme)
.then(() => {
spyHpm.post.calls.reset();

// Act
report.reload();

// Assert
expect(spyHpm.post).toHaveBeenCalledWith('/report/load', jasmine.objectContaining(testData.theme), { uid: uniqueId, sdkSessionId: sdkSessionId }, iframe.contentWindow);
});
});

it('report.resetTheme() sends PUT /report/theme with empty object as theme in body', function () {
// Arrange
const response = {
body: null
};

spyHpm.post.and.returnValue(Promise.resolve(response));
report.resetTheme()
.then(() => {
spyHpm.post.calls.reset();

// Act
report.reload();

// Assert
expect(spyHpm.post).toHaveBeenCalledWith('/report/load', jasmine.objectContaining({}), { uid: uniqueId, sdkSessionId: sdkSessionId }, iframe.contentWindow);
});
});
});
});

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

0 comments on commit 6493333

Please sign in to comment.