forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.ts
90 lines (80 loc) · 2.87 KB
/
dashboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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);
this.loadPath = "/dashboard/load";
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;
}
/**
* Validate load configuration.
*/
validate(config: models.IDashboardLoadConfiguration): models.IError[] {
return models.validateDashboardLoad(config);
}
}