1+ import * as service from './service' ;
2+ import * as embed from './embed' ;
3+ import * as models from 'powerbi-models' ;
4+ import * as wpmp from 'window-post-message-proxy' ;
5+ import * as hpm from 'http-post-message' ;
6+ import * as utils from './util' ;
7+
8+ /**
9+ * A Dashboard node within a dashboard hierarchy
10+ *
11+ * @export
12+ * @interface IDashboardNode
13+ */
14+ export interface IDashboardNode {
15+ iframe : HTMLIFrameElement ;
16+ service : service . IService ;
17+ config : embed . IInternalEmbedConfiguration
18+ }
19+
20+ /**
21+ * A Power BI Dashboard embed component
22+ *
23+ * @export
24+ * @class Dashboard
25+ * @extends {embed.Embed }
26+ * @implements {IDashboardNode}
27+ * @implements {IFilterable}
28+ */
29+ export class Dashboard extends embed . Embed implements IDashboardNode {
30+ static allowedEvents = [ "tileClicked" , "error" ] ;
31+ static dashboardIdAttribute = 'powerbi-dashboard-id' ;
32+ static typeAttribute = 'powerbi-type' ;
33+ static type = "Dashboard" ;
34+
35+ /**
36+ * Creates an instance of a Power BI Dashboard.
37+ *
38+ * @param {service.Service } service
39+ * @param {HTMLElement } element
40+ */
41+ constructor ( service : service . Service , element : HTMLElement , config : embed . IEmbedConfiguration ) {
42+ super ( service , element , config ) ;
43+ Array . prototype . push . apply ( this . allowedEvents , Dashboard . allowedEvents ) ;
44+ }
45+
46+ /**
47+ * This adds backwards compatibility for older config which used the dashboardId query param to specify dashboard id.
48+ * E.g. https://powerbi-df.analysis-df.windows.net/dashboardEmbedHost?dashboardId=e9363c62-edb6-4eac-92d3-2199c5ca2a9e
49+ *
50+ * By extracting the id we can ensure id is always explicitly provided as part of the load configuration.
51+ *
52+ * @static
53+ * @param {string } url
54+ * @returns {string }
55+ */
56+ static findIdFromEmbedUrl ( url : string ) : string {
57+ const dashboardIdRegEx = / d a s h b o a r d I d = " ? ( [ ^ & ] + ) " ? /
58+ const dashboardIdMatch = url . match ( dashboardIdRegEx ) ;
59+
60+ let dashboardId ;
61+ if ( dashboardIdMatch ) {
62+ dashboardId = dashboardIdMatch [ 1 ] ;
63+ }
64+
65+ return dashboardId ;
66+ }
67+
68+ /**
69+ * Get dashboard id from first available location: options, attribute, embed url.
70+ *
71+ * @returns {string }
72+ */
73+ getId ( ) : string {
74+ const dashboardId = this . config . id || this . element . getAttribute ( Dashboard . dashboardIdAttribute ) || Dashboard . findIdFromEmbedUrl ( this . config . embedUrl ) ;
75+
76+ if ( typeof dashboardId !== 'string' || dashboardId . length === 0 ) {
77+ 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 } '.` ) ;
78+ }
79+
80+ return dashboardId ;
81+ }
82+ }
0 commit comments