forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.ts
86 lines (74 loc) · 2.4 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
import * as service from './service';
import * as models from 'powerbi-models';
import * as embed from './embed';
import * as utils from './util';
import { Defaults } from './defaults';
/**
* 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, baseConfig: embed.IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean) {
let config = <embed.IEmbedConfiguration>baseConfig;
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 {
let config = <embed.IEmbedConfiguration>this.config;
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: embed.IEmbedConfigurationBase): models.IError[] {
let embedConfig = <embed.IEmbedConfiguration>config;
return models.validateTileLoad(embedConfig);
}
/**
* Handle config changes.
*
* @returns {void}
*/
configChanged(isBootstrap: boolean): void {
if (isBootstrap) {
return;
}
// Populate tile id into config object.
(<embed.IEmbedConfiguration>this.config).id = this.getId();
}
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.
*
* @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;
}
}