Skip to content

Commit 01d9e76

Browse files
committed
More refactoring before 2.0.x (GA)
- Add embedUrl to config for later retrieval and easier debugging. - Restore powerbi.get which only returns instance of component. - Separate component type error in two separate conditions between component type being specified vs not matching a known type. - Update README.md to account for changes.
1 parent d6ecab0 commit 01d9e76

12 files changed

Lines changed: 170 additions & 55 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
```
2424
var powerbiService = new Powerbi({ autoEmbedOnContentLoaded: true });
2525
```
26-
- `powerbi.get(element: HTMLElement)` is removed. Use `powerbi.embed(element: HTMLElement, config: IEmbedOptions = {})`.
26+
- `powerbi.get(element: HTMLElement)` now only returns the instance of powerbi component associated with the element and does not implicitly emebed. Use `powerbi.embed(element: HTMLElement, config: IEmbedOptions = {})`.
2727
- Reasons:
2828
- powerbi.embed performed the same function and is more semantic.
29+
- Now that overwrite: true is the default behavior for .embed having a separate method (get) for only retrieving compnents is good separation of intents.
2930
- Embed urls must be provided by the server and will not be constructed by the components. This implies that the attributes `powerbi-report` will no longer be used.
3031
- Reasons:
3132

‎Readme.md‎

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,121 @@
1-
# Power BI for HTML / JavaScript
1+
# powerbi-client
2+
3+
## Installation
4+
5+
Install via Nuget:
26

3-
## Install via Nuget
47
`Install-Package Microsoft.PowerBI.JavaScript -Pre`
58

6-
## Install from NPM
7-
`npm install powerbi-client`
9+
Install from NPM:
10+
11+
`npm install -save powerbi-client`
12+
13+
Install from Bower:
14+
15+
`bower install powerbi-client --save`
816

9-
## Install from Bower
10-
`bower install powerbi-client`
17+
Installing beta versions:
18+
19+
`npm install --save powerbi-client@beta`
1120

1221
## Setup Power BI for embedding
13-
Add the Power BI script include before your apps closing `</body>` tag
22+
23+
Add the Power BI script before your apps closing `</body>` tag
1424

1525
`<script src="/bower_components/powerbi-client/dist/powerbi.js"></script>`
1626

17-
Add your access token to your app so it can be accessed.
18-
*This can be set statically or dynamically via JavaScript.*
27+
This exposes two globals `Powerbi` which is the service class and `powerbi` which is an instance of the service.
1928

29+
30+
# Embedding
31+
32+
## PaaS
33+
### When using PaaS model the tokens issued are for a specific report or tile and the token should be associated with the embed url on the same element to ensure each has a unique token allows embeding multiple reports using the same service instance.
34+
35+
```
36+
<div
37+
powerbi-type="report"
38+
powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=5dac7a4a-4452-46b3-99f6-a25915e0fe55"
39+
powerbi-access-token="eyJ0eXAiO...Qron7qYpY9MI"
40+
></div>
41+
```
42+
43+
## SaaS
44+
### When using SaaS model the tokens issued are for a specific user who can view many types of visuals. This means you can add this as a global token reused for all embeded visuals as shown below:
2045
```
2146
<script>
22-
window.powerbi = window.powerbi || {};
2347
window.powerbi.accessToken = '{{AccessToken}}';
2448
</script>
49+
...
50+
51+
<div
52+
powerbi-type="report"
53+
powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=5dac7a4a-4452-46b3-99f6-a25915e0fe55"
54+
></div>
2555
```
26-
# Embedding
27-
Power BI will automatically search your apps DOM for Power BI embed components on page load.
28-
If your app is dyanamically adding new embed components after page load you will need to manually initialize them via JavaScript.
2956

30-
## Setting the size of embedded components
31-
The report will automatically be embedded based on the size of it's container.
32-
To override the default size of the embeds simply add a CSS class attribute or inline styles for width & height.
57+
# Service Configuration
3358

34-
## Embed a Report into your app
35-
Add the following markup to embed a Power BI Report
59+
As of 2.x Power BI will *NOT* automatically search your apps DOM for Power BI embed components on page load.
60+
If you need this behavior you need to configure the service by calling:
3661

37-
Embed via full qualified Report `EmbedUrl`.
62+
```
63+
powerbi.enableAutoEmbed();
64+
```
65+
See: CHANGELOG.md for more details
3866

39-
`<div powerbi-embed="{{EmbedUrl}}" powerbi-report></div>`
67+
If your app is dyanamically adding new embed components after page load you will need to manually initialize them via JavaScript.
4068

41-
Or, embed with your `ReportId`
69+
# Embed configuration and other options
4270

43-
`<div powerbi-embed powerbi-report="{{ReportId}}"></div>`
71+
All configuration and options will be provided as attributes prefixed with `powerbi-options-` on the containing html element.
4472

45-
### Launch an embedded report in fullscreen mode
73+
1. **Filter Pane**
74+
75+
FilterPane is enabled by default but can be disabled by adding the attribute:
76+
```
77+
<div ... powerbi-options-filter-pane-enabled="false"`></div>
78+
```
79+
80+
## Setting the size of embedded components
81+
The report will automatically be embedded based on the size of it's container.
82+
To override the default size of the embeds simply add a CSS class attribute or inline styles for width & height.
83+
84+
## Launch an embedded report in fullscreen mode
85+
(Assumes element with id `myReport` already contained embedded report)
4686
```
4787
<script>
48-
var elem = document.getElementById('#myReport');
49-
var report = window.powerbi.get(elem);
50-
88+
var element = document.getElementById('#myReport');
89+
var report = powerbi.get(element);
90+
5191
report.fullscreen();
5292
</script>
5393
```
5494

5595
## Dynamically embedding components
5696
### Find all new Power BI components and initialize them.
57-
You can optional pass a DOM element to scope the intialization
97+
`powerbi.init();`
5898

59-
`window.powerbi.init();`
99+
By default this will look for elements within the `document.body`; however you can pass optional DOM element to scope the intialization to smaller subset.
100+
101+
```
102+
var pageContainer = document.getElementsById('#pageContainer');
103+
powerbi.init(pageContainer);
104+
```
60105

61106
### Embed a single component
62107
Setup the a new DOM element with the same structure as above.
63108
Call the `embed` function passing in the single element.
64109

65-
`window.powerbi.embed(element);`
110+
`powerbi.embed(element);`
111+
112+
In the case above, the embed url, access-token, type, options, etc will be determined from the elements' attributes.
113+
You can optionally pass a second argument to embed specifying these values as an object:
114+
115+
`powerbi.embed(element, { embedUrl: '...', accessToken: '...' }`;
66116

67117
### Get a reference to a existing Power BI component
68-
Returns an instance of the Power BI component associated with an element
118+
Retrieve the existing component associated with the element.
119+
120+
`var myPowerBIComponent = powerbi.get(element);`
69121

70-
`var myPowerBIComponent = window.powerbi.get(element);`

‎dist/powerbi.d.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ declare abstract class Embed {
124124
}
125125

126126
export class Report extends Embed {
127-
static attribute: string;
128127
constructor(element: HTMLElement, options: IEmbedOptions);
129128
getEmbedUrl(): string;
130129
}
131130

132131
export class Tile extends Embed {
133-
static attribute: string;
134132
constructor(element: HTMLElement, options: IEmbedOptions);
135133
getEmbedUrl(): string;
136134
}

‎dist/powerbi.js‎

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

‎dist/powerbi.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/powerbi.min.js‎

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

‎src/core.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,14 @@ export default class PowerBi {
9292
this.remove(powerBiElement.powerBiEmbed);
9393
}
9494

95-
const Component = Utils.find(component => config.type === component.type || element.getAttribute(Embed.typeAttribute) === component.type, PowerBi.components);
95+
const componentType = config.type || element.getAttribute(Embed.typeAttribute);
96+
if (!componentType) {
97+
throw new Error(`Attempted to embed using config ${JSON.stringify(config)} on element ${element.outerHTML}, but could not determine what type of component to embed. You must specify a type in the configuration or as an attribute such as '${Embed.typeAttribute}="${Report.name.toLowerCase()}"'.`);
98+
}
9699

100+
const Component = Utils.find(component => componentType === component.name.toLowerCase(), PowerBi.components);
97101
if (!Component) {
98-
throw new Error(`Attempted to embed using config ${JSON.stringify(config)} on element ${element.outerHTML}, but could not determine what type of component to embed. You must specify a type in the configuration or as an attribute such as '${Embed.typeAttribute}="${Report.name}"'.`);
102+
throw new Error(`Attempted to embed component of type: ${componentType} but did not find any matching component. Please verify the type you specified is intended.`);
99103
}
100104

101105
// TODO: Consider removing in favor of passing reference to `this` in constructor
@@ -119,6 +123,19 @@ export default class PowerBi {
119123
window.addEventListener('DOMContentLoaded', (event: Event) => this.init(document.body), false);
120124
}
121125

126+
/**
127+
* Returns instance of component associated with element.
128+
*/
129+
get(element: HTMLElement) {
130+
const powerBiElement = <IPowerBiElement>element;
131+
132+
if (!powerBiElement.powerBiEmbed) {
133+
throw new Error(`You attempted to get an instance of powerbi component associated with element: ${element.outerHTML} but there was no associated instance.`);
134+
}
135+
136+
return powerBiElement.powerBiEmbed;
137+
}
138+
122139
/**
123140
* Remove component from the list of embedded components.
124141
*/

‎src/embed.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ abstract class Embed {
6363
// TODO: Change when Object.assign is available.
6464
this.options = Utils.assign({}, Embed.defaultOptions, options);
6565
this.options.accessToken = this.getAccessToken();
66+
this.options.embedUrl = this.getEmbedUrl();
6667

67-
const embedUrl = this.getEmbedUrl();
68-
const iframeHtml = `<iframe style="width:100%;height:100%;" src="${embedUrl}" scrolling="no" allowfullscreen="true"></iframe>`;
68+
const iframeHtml = `<iframe style="width:100%;height:100%;" src="${this.options.embedUrl}" scrolling="no" allowfullscreen="true"></iframe>`;
6969

7070
this.element.innerHTML = iframeHtml;
7171
this.iframe = <HTMLIFrameElement>this.element.childNodes[0];

‎src/powerbi.d.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ declare abstract class Embed {
124124
}
125125

126126
export class Report extends Embed {
127-
static attribute: string;
128127
constructor(element: HTMLElement, options: IEmbedOptions);
129128
getEmbedUrl(): string;
130129
}
131130

132131
export class Tile extends Embed {
133-
static attribute: string;
134132
constructor(element: HTMLElement, options: IEmbedOptions);
135133
getEmbedUrl(): string;
136134
}

‎src/report.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { default as Embed, IEmbedOptions } from './embed';
22

33
export default class Report extends Embed {
4-
static type = 'report';
5-
64
constructor(element: HTMLElement, options: IEmbedOptions) {
75
/** Force loadAction on options to match the type of component. This is required to bootstrap iframe. */
86
options.loadAction = 'loadReport';

0 commit comments

Comments
 (0)