forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
94 lines (80 loc) · 2.75 KB
/
create.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 * as service from './service';
import * as models from 'powerbi-models';
import * as embed from './embed';
import * as utils from './util';
import { Defaults } from './defaults';
export class Create extends embed.Embed {
constructor(service: service.Service, element: HTMLElement, config: embed.IEmbedConfiguration, phasedRender?: boolean) {
super(service, element, config, /* iframe */ undefined, phasedRender);
}
/**
* Gets the dataset ID from the first available location: createConfig or embed url.
*
* @returns {string}
*/
getId(): string {
const datasetId = (this.createConfig && this.createConfig.datasetId) ? this.createConfig.datasetId : Create.findIdFromEmbedUrl(this.config.embedUrl);
if (typeof datasetId !== 'string' || datasetId.length === 0) {
throw new Error('Dataset id is required, but it was not found. You must provide an id either as part of embed configuration.');
}
return datasetId;
}
/**
* Validate create report configuration.
*/
validate(config: embed.IEmbedConfigurationBase): models.IError[] {
return models.validateCreateReport(config);
}
/**
* Populate config for create
*
* @param {IEmbedConfigurationBase}
* @returns {void}
*/
populateConfig(baseConfig: embed.IEmbedConfigurationBase): void {
super.populateConfig(baseConfig);
// TODO: Change when Object.assign is available.
const settings = utils.assign({}, Defaults.defaultSettings, baseConfig.settings);
this.config = utils.assign({ settings }, baseConfig);
const config = <embed.IEmbedConfiguration>this.config;
this.createConfig = {
accessToken: config.accessToken,
datasetId: config.datasetId || this.getId(),
groupId: config.groupId,
settings: settings,
tokenType: config.tokenType,
theme: config.theme
}
}
/**
* checks if the report is saved.
*
* ```javascript
* report.isSaved()
* ```
*
* @returns {Promise<boolean>}
*/
isSaved(): Promise<boolean> {
return utils.isSavedInternal(this.service.hpm, this.config.uniqueId, this.iframe.contentWindow);
}
/**
* Adds the ability to get datasetId from url.
* (e.g. http://embedded.powerbi.com/appTokenReportEmbed?datasetId=854846ed-2106-4dc2-bc58-eb77533bf2f1).
*
* By extracting the ID we can ensure that the ID is always explicitly provided as part of the create configuration.
*
* @static
* @param {string} url
* @returns {string}
*/
static findIdFromEmbedUrl(url: string): string {
const datasetIdRegEx = /datasetId="?([^&]+)"?/
const datasetIdMatch = url.match(datasetIdRegEx);
let datasetId;
if (datasetIdMatch) {
datasetId = datasetIdMatch[1];
}
return datasetId;
}
}