/
tile.ts
94 lines (82 loc) · 2.34 KB
/
tile.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
import { IError, validateTileLoad } from 'powerbi-models';
import { Service } from './service';
import { Embed, IEmbedConfigurationBase, ITileEmbedConfiguration } from './embed';
/**
* The Power BI tile embed component
*
* @export
* @class Tile
* @extends {Embed}
*/
export class Tile extends Embed {
/** @hidden */
static type = "Tile";
/** @hidden */
static allowedEvents = ["tileClicked", "tileLoaded"];
/**
* @hidden
*/
constructor(service: Service, element: HTMLElement, baseConfig: IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean) {
const config = baseConfig as ITileEmbedConfiguration;
super(service, element, config, /* iframe */ undefined, phasedRender, isBootstrap);
this.loadPath = "/tile/load";
Array.prototype.push.apply(this.allowedEvents, Tile.allowedEvents);
}
/**
* The ID of the tile
*
* @returns {string}
*/
getId(): string {
const config = this.config as ITileEmbedConfiguration;
const tileId = 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: IEmbedConfigurationBase): IError[] {
const embedConfig = config as ITileEmbedConfiguration;
return validateTileLoad(embedConfig);
}
/**
* Handle config changes.
*
* @returns {void}
*/
configChanged(isBootstrap: boolean): void {
if (isBootstrap) {
return;
}
// Populate tile id into config object.
(this.config as ITileEmbedConfiguration).id = this.getId();
}
/**
* @hidden
* @returns {string}
*/
getDefaultEmbedUrlEndpoint(): string {
return "tileEmbed";
}
/**
* 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.
*
* @hidden
* @static
* @param {string} url
* @returns {string}
*/
static findIdFromEmbedUrl(url: string): string {
const tileIdRegEx = /tileId="?([^&]+)"?/;
const tileIdMatch = url.match(tileIdRegEx);
let tileId: string;
if (tileIdMatch) {
tileId = tileIdMatch[1];
}
return tileId;
}
}