forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.ts
More file actions
134 lines (115 loc) · 4.01 KB
/
tile.ts
File metadata and controls
134 lines (115 loc) · 4.01 KB
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
124
125
126
127
128
129
130
131
132
133
134
import * as service from './service';
import * as models from 'powerbi-models';
import * as embed from './embed';
import * as utils from './util';
/**
* The Power BI tile embed component
*
* @export
* @class Tile
* @extends {Embed}
*/
export class Tile extends embed.Embed {
static type = "Tile";
static allowedEvents = ["tileClicked", "tileLoaded"];
constructor(service: service.Service, element: HTMLElement, config: embed.IEmbedConfiguration) {
config.embedUrl = utils.addParamToUrl(config.embedUrl, 'dashboardId', config.dashboardId);
config.embedUrl = utils.addParamToUrl(config.embedUrl, 'tileId', config.id);
super(service, element, config);
Array.prototype.push.apply(this.allowedEvents, Tile.allowedEvents);
window.addEventListener("message", this.receiveMessage.bind(this), false);
}
/**
* The ID of the tile
*
* @returns {string}
*/
getId(): string {
const tileId = this.config.id || Tile.findIdFromEmbedUrl(this.config.embedUrl);
if (typeof tileId !== 'string' || tileId.length === 0) {
throw new Error(`Tile id is required, but it was not found. You must provide an id either as part of embed configuration.`);
}
return tileId;
}
/**
* Validate load configuration.
*/
validate(config: models.IReportLoadConfiguration): models.IError[] {
// we create load tile configuration from report load configuration
// so we need to validate it
return models.validateReportLoad(config);
}
/**
* Sends load configuration data for tile
*
* @param {models.ILoadConfiguration} config
* @returns {Promise<void>}
*/
load(config: embed.IInternalEmbedConfiguration): Promise<void> {
const errors = this.validate(config);
if (errors) {
throw errors;
}
let height = config.height ? config.height : this.iframe.offsetHeight;
let width = config.width ? config.width : this.iframe.offsetWidth;
let action = config.action ? config.action : 'loadTile';
let tileConfig = {
action: action,
height: height,
width: width,
accessToken: config.accessToken,
tokenType: config.tokenType,
}
this.iframe.contentWindow.postMessage(JSON.stringify(tileConfig), "*")
// In order to use this function the same way we use it in embed
// we need to keep the return type the same as 'load' in embed
return new Promise<void>( () => {
return;
});
}
/**
* Adds the ability to get tileId from url.
* By extracting the ID we can ensure that the ID is always explicitly provided as part of the load configuration.
*
* @static
* @param {string} url
* @returns {string}
*/
static findIdFromEmbedUrl(url: string): string {
const tileIdRegEx = /tileId="?([^&]+)"?/
const tileIdMatch = url.match(tileIdRegEx);
let tileId;
if (tileIdMatch) {
tileId = tileIdMatch[1];
}
return tileId;
}
/**
* Adds the ability to get events from iframe
*
* @param event: MessageEvent
*/
private receiveMessage(event: MessageEvent): void {
if (event.data) {
try {
let messageData = JSON.parse(event.data);
let value = {
navigationUrl: messageData.navigationUrl,
errors: messageData.error,
openReport: messageData.openReport
};
const tileEvent: service.IEvent<any> = {
type: 'tile',
id: this.config.uniqueId,
name: messageData.event,
value: value
};
this.service.handleTileEvents(tileEvent);
}
catch (e) {
console.log("invalid message data");
return;
}
}
}
}