forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.ts
468 lines (419 loc) · 13.2 KB
/
report.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import * as service from './service';
import * as embed from './embed';
import * as models from 'powerbi-models';
import * as wpmp from 'window-post-message-proxy';
import * as hpm from 'http-post-message';
import * as utils from './util';
import * as errors from './errors';
import { IFilterable } from './ifilterable';
import { IPageNode, Page } from './page';
import { Defaults } from './defaults';
import { IReportLoadConfiguration } from 'powerbi-models';
import { BookmarksManager } from './bookmarksManager';
/**
* A Report node within a report hierarchy
*
* @export
* @interface IReportNode
*/
export interface IReportNode {
iframe: HTMLIFrameElement;
service: service.IService;
config: embed.IEmbedConfiguration
}
/**
* The Power BI Report embed component
*
* @export
* @class Report
* @extends {embed.Embed}
* @implements {IReportNode}
* @implements {IFilterable}
*/
export class Report extends embed.Embed implements IReportNode, IFilterable {
static allowedEvents = ["filtersApplied", "pageChanged", "commandTriggered", "swipeStart", "swipeEnd", "bookmarkApplied"];
static reportIdAttribute = 'powerbi-report-id';
static filterPaneEnabledAttribute = 'powerbi-settings-filter-pane-enabled';
static navContentPaneEnabledAttribute = 'powerbi-settings-nav-content-pane-enabled';
static typeAttribute = 'powerbi-type';
static type = "Report";
public bookmarksManager: BookmarksManager;
/**
* Creates an instance of a Power BI Report.
*
* @param {service.Service} service
* @param {HTMLElement} element
* @param {embed.IEmbedConfiguration} config
*/
constructor(service: service.Service, element: HTMLElement, baseConfig: embed.IEmbedConfigurationBase, phasedRender?: boolean, iframe?: HTMLIFrameElement) {
const config = <embed.IEmbedConfiguration>baseConfig;
const filterPaneEnabled = (config.settings && config.settings.filterPaneEnabled) || !(element.getAttribute(Report.filterPaneEnabledAttribute) === "false");
const navContentPaneEnabled = (config.settings && config.settings.navContentPaneEnabled) || !(element.getAttribute(Report.navContentPaneEnabledAttribute) === "false");
const settings = utils.assign({
filterPaneEnabled,
navContentPaneEnabled
}, config.settings);
const configCopy = utils.assign({ settings }, config);
super(service, element, configCopy, iframe, phasedRender);
this.loadPath = "/report/load";
this.phasedLoadPath = "/report/prepare";
Array.prototype.push.apply(this.allowedEvents, Report.allowedEvents);
this.bookmarksManager = new BookmarksManager(service, config, this.iframe);
}
/**
* Adds backwards compatibility for the previous load configuration, which used the reportId query parameter to specify the report ID
* (e.g. http://embedded.powerbi.com/appTokenReportEmbed?reportId=854846ed-2106-4dc2-bc58-eb77533bf2f1).
*
* 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 reportIdRegEx = /reportId="?([^&]+)"?/
const reportIdMatch = url.match(reportIdRegEx);
let reportId;
if (reportIdMatch) {
reportId = reportIdMatch[1];
}
return reportId;
}
/**
* Render a preloaded report, using phased embedding API
*
* ```javascript
* // Load report
* var report = powerbi.load(element, config);
*
* ...
*
* // Render report
* report.render()
* ```
*
* @returns {Promise<void>}
*/
render(config?: IReportLoadConfiguration): Promise<void> {
return this.service.hpm.post<models.IError[]>(`/report/render`, config, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}
/**
* Gets filters that are applied at the report level.
*
* ```javascript
* // Get filters applied at report level
* report.getFilters()
* .then(filters => {
* ...
* });
* ```
*
* @returns {Promise<models.IFilter[]>}
*/
getFilters(): Promise<models.IFilter[]> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.service.hpm.get<models.IFilter[]>(`/report/filters`, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => response.body,
response => {
throw response.body;
});
}
/**
* Gets the report ID from the first available location: options, attribute, embed url.
*
* @returns {string}
*/
getId(): string {
let config = <embed.IEmbedConfiguration>this.config;
const reportId = config.id || this.element.getAttribute(Report.reportIdAttribute) || Report.findIdFromEmbedUrl(config.embedUrl);
if (typeof reportId !== 'string' || reportId.length === 0) {
throw new Error(`Report id is required, but it was not found. You must provide an id either as part of embed configuration or as attribute '${Report.reportIdAttribute}'.`);
}
return reportId;
}
/**
* Gets the list of pages within the report.
*
* ```javascript
* report.getPages()
* .then(pages => {
* ...
* });
* ```
*
* @returns {Promise<Page[]>}
*/
getPages(): Promise<Page[]> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.service.hpm.get<models.IPage[]>('/report/pages', { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body
.map(page => {
return new Page(this, page.name, page.displayName, page.isActive, page.visibility, page.defaultSize, page.defaultDisplayOption);
});
}, response => {
throw response.body;
});
}
/**
* Creates an instance of a Page.
*
* Normally you would get Page objects by calling `report.getPages()`, but in the case
* that the page name is known and you want to perform an action on a page without having to retrieve it
* you can create it directly.
*
* Note: Because you are creating the page manually there is no guarantee that the page actually exists in the report, and subsequent requests could fail.
*
* ```javascript
* const page = report.page('ReportSection1');
* page.setActive();
* ```
*
* @param {string} name
* @param {string} [displayName]
* @param {boolean} [isActive]
* @returns {Page}
*/
page(name: string, displayName?: string, isActive?: boolean, visibility?: models.SectionVisibility): Page {
return new Page(this, name, displayName, isActive, visibility);
}
/**
* Prints the active page of the report by invoking `window.print()` on the embed iframe component.
*/
print(): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.service.hpm.post<models.IError[]>('/report/print', null, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}
/**
* Removes all filters at the report level.
*
* ```javascript
* report.removeFilters();
* ```
*
* @returns {Promise<void>}
*/
removeFilters(): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.setFilters([]);
}
/**
* Sets the active page of the report.
*
* ```javascript
* report.setPage("page2")
* .catch(error => { ... });
* ```
*
* @param {string} pageName
* @returns {Promise<void>}
*/
setPage(pageName: string): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
const page: models.IPage = {
name: pageName,
displayName: null,
isActive: true
};
return this.service.hpm.put<models.IError[]>('/report/pages/active', page, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.catch(response => {
throw response.body;
});
}
/**
* Sets filters at the report level.
*
* ```javascript
* const filters: [
* ...
* ];
*
* report.setFilters(filters)
* .catch(errors => {
* ...
* });
* ```
*
* @param {(models.IFilter[])} filters
* @returns {Promise<void>}
*/
setFilters(filters: models.IFilter[]): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.service.hpm.put<models.IError[]>(`/report/filters`, filters, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.catch(response => {
throw response.body;
});
}
/**
* Updates visibility settings for the filter pane and the page navigation pane.
*
* ```javascript
* const newSettings = {
* navContentPaneEnabled: true,
* filterPaneEnabled: false
* };
*
* report.updateSettings(newSettings)
* .catch(error => { ... });
* ```
*
* @param {models.ISettings} settings
* @returns {Promise<void>}
*/
updateSettings(settings: models.ISettings): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl) && settings.customLayout != null) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.service.hpm.patch<models.IError[]>('/report/settings', settings, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.catch(response => {
throw response.body;
});
}
/**
* Validate load configuration.
*/
validate(config: embed.IEmbedConfigurationBase): models.IError[] {
return models.validateReportLoad(config);
}
/**
* Populate config for load config
*
* @param {IEmbedConfigurationBase}
* @returns {void}
*/
populateConfig(baseConfig: embed.IEmbedConfigurationBase): void {
let config = <embed.IEmbedConfiguration>baseConfig;
if (config.settings && (config.settings.layoutType === models.LayoutType.MobileLandscape || config.settings.layoutType === models.LayoutType.MobilePortrait))
config.embedUrl = utils.addParamToUrl(config.embedUrl, "isMobile", "true")
super.populateConfig(config);
// TODO: Change when Object.assign is available.
const settings = utils.assign({}, Defaults.defaultSettings, config.settings);
config = utils.assign({ settings }, config);
config.id = this.getId();
this.config = config;
}
/**
* Switch Report view mode.
*
* @returns {Promise<void>}
*/
switchMode(viewMode: models.ViewMode | string): Promise<void> {
let newMode: string;
if (typeof viewMode === "string"){
newMode = viewMode;
}
else {
newMode = this.viewModeToString(viewMode);
}
let url = '/report/switchMode/' + newMode;
return this.service.hpm.post<models.IError[]>(url, null, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}
/**
* Refreshes data sources for the report.
*
* ```javascript
* report.refresh();
* ```
*/
refresh(): Promise<void> {
return this.service.hpm.post<models.IError[]>('/report/refresh', null, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}
/**
* checks if the report is saved.
*
* ```javascript
* report.isSaved()
* ```
*
* @returns {Promise<boolean>}
*/
isSaved(): Promise<boolean> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return utils.isSavedInternal(this.service.hpm, this.config.uniqueId, this.iframe.contentWindow);
}
/**
* Apply a theme to the report
*
* ```javascript
* report.applyTheme(theme);
* ```
*/
applyTheme(theme: models.IReportTheme): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.applyThemeInternal(theme);
}
/**
* Reset and apply the default theme of the report
*
* ```javascript
* report.resetTheme();
* ```
*/
resetTheme(): Promise<void> {
if (utils.isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(errors.APINotSupportedForRDLError);
}
return this.applyThemeInternal(<models.IReportTheme>{});
}
private applyThemeInternal(theme: models.IReportTheme): Promise<void> {
return this.service.hpm.put<models.IError[]>('/report/theme', theme, { uid: this.config.uniqueId }, this.iframe.contentWindow)
.then(response => {
return response.body;
})
.catch(response => {
throw response.body;
});
}
private viewModeToString(viewMode: models.ViewMode): string {
let mode: string;
switch (viewMode) {
case models.ViewMode.Edit:
mode = "edit";
break;
case models.ViewMode.View:
mode = "view";
break;
}
return mode;
}
}