Skip to content
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
82 changes: 82 additions & 0 deletions src/dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as service from './service';
import * as embed from './embed';
import * as models from 'powerbi-models';
import * as wpmp from 'window-post-message-proxy';
import * as hpm from 'http-post-message';
import * as utils from './util';

/**
* A Dashboard node within a dashboard hierarchy
*
* @export
* @interface IDashboardNode
*/
export interface IDashboardNode {
iframe: HTMLIFrameElement;
service: service.IService;
config: embed.IInternalEmbedConfiguration
}

/**
* A Power BI Dashboard embed component
*
* @export
* @class Dashboard
* @extends {embed.Embed}
* @implements {IDashboardNode}
* @implements {IFilterable}
*/
export class Dashboard extends embed.Embed implements IDashboardNode {
static allowedEvents = ["tileClicked", "error"];
static dashboardIdAttribute = 'powerbi-dashboard-id';
static typeAttribute = 'powerbi-type';
static type = "Dashboard";

/**
* Creates an instance of a Power BI Dashboard.
*
* @param {service.Service} service
* @param {HTMLElement} element
*/
constructor(service: service.Service, element: HTMLElement, config: embed.IEmbedConfiguration) {
super(service, element, config);
Array.prototype.push.apply(this.allowedEvents, Dashboard.allowedEvents);
}

/**
* This adds backwards compatibility for older config which used the dashboardId query param to specify dashboard id.
* E.g. https://powerbi-df.analysis-df.windows.net/dashboardEmbedHost?dashboardId=e9363c62-edb6-4eac-92d3-2199c5ca2a9e
*
* By extracting the id we can ensure id is always explicitly provided as part of the load configuration.
*
* @static
* @param {string} url
* @returns {string}
*/
static findIdFromEmbedUrl(url: string): string {
const dashboardIdRegEx = /dashboardId="?([^&]+)"?/
const dashboardIdMatch = url.match(dashboardIdRegEx);

let dashboardId;
if (dashboardIdMatch) {
dashboardId = dashboardIdMatch[1];
}

return dashboardId;
}

/**
* Get dashboard id from first available location: options, attribute, embed url.
*
* @returns {string}
*/
getId(): string {
const dashboardId = this.config.id || this.element.getAttribute(Dashboard.dashboardIdAttribute) || Dashboard.findIdFromEmbedUrl(this.config.embedUrl);

if (typeof dashboardId !== 'string' || dashboardId.length === 0) {
throw new Error(`Dashboard id is required, but it was not found. You must provide an id either as part of embed configuration or as attribute '${Dashboard.dashboardIdAttribute}'.`);
}

return dashboardId;
}
}
23 changes: 14 additions & 9 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,20 @@ export abstract class Embed {
if (errors) {
throw errors;
}

return this.service.hpm.post<void>('/report/load', config, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
utils.assign(this.config, config);
return response.body;
},
response => {
throw response.body;
});

let loadPath = '/report/load';
if(this.config && this.config.type === 'dashboard') {
loadPath = '/dashboard/load';
}

return this.service.hpm.post<void>(loadPath, config, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
utils.assign(this.config, config);
return response.body;
},
response => {
throw response.body;
});
}

/**
Expand Down
22 changes: 17 additions & 5 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as embed from './embed';
import { Report } from './report';
import { Dashboard } from './dashboard';
import { Tile } from './tile';
import { Page } from './page';
import * as utils from './util';
Expand Down Expand Up @@ -67,9 +68,10 @@ export class Service implements IService {
/**
* A list of components that this service can embed
*/
private static components: (typeof Report | typeof Tile)[] = [
private static components: (typeof Report | typeof Tile | typeof Dashboard)[] = [
Tile,
Report
Report,
Dashboard
];

/**
Expand All @@ -90,7 +92,7 @@ export class Service implements IService {
/**The Configuration object for the service*/
private config: IServiceConfiguration;

/** A list of Report and Tile components that have been embedded using this service instance. */
/** A list of Dashboard, Report and Tile components that have been embedded using this service instance. */
private embeds: embed.Embed[];
/** TODO: Look for way to make hpm private without sacraficing ease of maitenance. This should be private but in embed needs to call methods. */
hpm: hpm.HttpPostMessage;
Expand Down Expand Up @@ -144,6 +146,16 @@ export class Service implements IService {

this.handleEvent(event);
});
this.router.post(`/dashboards/:uniqueId/events/:eventName`, (req, res) => {
const event: IEvent<any> = {
type: 'dashboard',
id: req.params.uniqueId,
name: req.params.eventName,
value: req.body
};

this.handleEvent(event);
});

this.embeds = [];

Expand Down Expand Up @@ -257,7 +269,7 @@ export class Service implements IService {
* @param {HTMLElement} element
* @returns {(Report | Tile)}
*/
get(element: HTMLElement): Report | Tile {
get(element: HTMLElement): Report | Tile | Dashboard {
const powerBiElement = <IPowerBiElement>element;

if (!powerBiElement.powerBiEmbed) {
Expand All @@ -273,7 +285,7 @@ export class Service implements IService {
* @param {string} uniqueId
* @returns {(Report | Tile)}
*/
find(uniqueId: string): Report | Tile {
find(uniqueId: string): Report | Tile | Dashboard {
return utils.find(x => x.config.uniqueId === uniqueId, this.embeds);
}

Expand Down
Loading