forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.ts
123 lines (108 loc) · 4.04 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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';
import { Defaults } from './defaults';
/**
* A Dashboard node within a dashboard hierarchy
*
* @export
* @interface IDashboardNode
*/
export interface IDashboardNode {
iframe: HTMLIFrameElement;
service: service.IService;
config: embed.IEmbedConfigurationBase
}
/**
* 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.IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean) {
super(service, element, config, /* iframe */ undefined, phasedRender, isBootstrap);
this.loadPath = "/dashboard/load";
this.phasedLoadPath = "/dashboard/prepare";
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 {
let config = <embed.IEmbedConfiguration>this.config;
const dashboardId = config.id || this.element.getAttribute(Dashboard.dashboardIdAttribute) || Dashboard.findIdFromEmbedUrl(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(baseConfig: embed.IEmbedConfigurationBase): models.IError[] {
const config = baseConfig as embed.IEmbedConfiguration;
let error = models.validateDashboardLoad(config);
return error ? error : this.ValidatePageView(config.pageView);
}
/**
* Handle config changes.
*
* @returns {void}
*/
configChanged(isBootstrap: boolean): void {
if (isBootstrap) {
return;
}
// Populate dashboard id into config object.
(<embed.IEmbedConfiguration>this.config).id = this.getId();
}
getDefaultEmbedUrlEndpoint(): string {
return "dashboardEmbed";
}
/**
* Validate that pageView has a legal value: if page view is defined it must have one of the values defined in models.PageView
*/
private ValidatePageView(pageView: models.PageView): models.IError[] {
if (pageView && pageView !== "fitToWidth" && pageView !== "oneColumn" && pageView !== "actualSize") {
return [{message: "pageView must be one of the followings: fitToWidth, oneColumn, actualSize"}];
}
}
}