forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.ts
487 lines (450 loc) · 13.2 KB
/
page.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { IHttpPostMessageResponse } from 'http-post-message';
import {
CommonErrorCodes,
DisplayOption,
FiltersOperations,
ICustomPageSize,
IFilter,
IPage,
IUpdateFiltersRequest,
IVisual,
LayoutType,
PageLevelFilters,
PageSizeType,
SectionVisibility,
VisualContainerDisplayMode,
IPageBackground,
IPageWallpaper,
ISmartNarratives,
} from 'powerbi-models';
import { IFilterable } from './ifilterable';
import { IReportNode, Report } from './report';
import { VisualDescriptor } from './visualDescriptor';
import { isRDLEmbed } from './util';
import { APINotSupportedForRDLError } from './errors';
/**
* A Page node within a report hierarchy
*
* @export
* @interface IPageNode
*/
export interface IPageNode {
report: IReportNode;
name: string;
}
/**
* A Power BI report page
*
* @export
* @class Page
* @implements {IPageNode}
* @implements {IFilterable}
*/
export class Page implements IPageNode, IFilterable {
/**
* The parent Power BI report that this page is a member of
*
* @type {IReportNode}
*/
report: IReportNode;
/**
* The report page name
*
* @type {string}
*/
name: string;
/**
* The user defined display name of the report page, which is undefined if the page is created manually
*
* @type {string}
*/
displayName: string;
/**
* Is this page is the active page
*
* @type {boolean}
*/
isActive: boolean;
/**
* The visibility of the page.
* 0 - Always Visible
* 1 - Hidden in View Mode
*
* @type {SectionVisibility}
*/
visibility: SectionVisibility;
/**
* Page size as saved in the report.
*
* @type {ICustomPageSize}
*/
defaultSize: ICustomPageSize;
/**
* Mobile view page size (if defined) as saved in the report.
*
* @type {ICustomPageSize}
*/
mobileSize: ICustomPageSize;
/**
* Page display options as saved in the report.
*
* @type {ICustomPageSize}
*/
defaultDisplayOption: DisplayOption;
/**
* Page background color.
*
* @type {IPageBackground}
*/
background: IPageBackground;
/**
* Page wallpaper color.
*
* @type {IPageWallpaper}
*/
wallpaper: IPageWallpaper;
/**
* Creates an instance of a Power BI report page.
*
* @param {IReportNode} report
* @param {string} name
* @param {string} [displayName]
* @param {boolean} [isActivePage]
* @param {SectionVisibility} [visibility]
* @hidden
*/
constructor(report: IReportNode, name: string, displayName?: string, isActivePage?: boolean, visibility?: SectionVisibility, defaultSize?: ICustomPageSize, defaultDisplayOption?: DisplayOption, mobileSize?: ICustomPageSize, background?: IPageBackground, wallpaper?: IPageWallpaper) {
this.report = report;
this.name = name;
this.displayName = displayName;
this.isActive = isActivePage;
this.visibility = visibility;
this.defaultSize = defaultSize;
this.mobileSize = mobileSize;
this.defaultDisplayOption = defaultDisplayOption;
this.background = background;
this.wallpaper = wallpaper;
}
/**
* Get insights for report page
*
* ```javascript
* page.getSmartNarrativeInsights();
* ```
*
* @returns {Promise<ISmartNarratives>}
*/
async getSmartNarrativeInsights(): Promise<ISmartNarratives > {
if (isRDLEmbed(this.report.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
try {
const response = await this.report.service.hpm.get<ISmartNarratives>(`/report/pages/${this.name}/smartNarrativeInsights`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
/**
* Gets all page level filters within the report.
*
* ```javascript
* page.getFilters()
* .then(filters => { ... });
* ```
*
* @returns {(Promise<IFilter[]>)}
*/
async getFilters(): Promise<IFilter[]> {
try {
const response = await this.report.service.hpm.get<IFilter[]>(`/report/pages/${this.name}/filters`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
/**
* Update the filters for the current page according to the operation: Add, replace all, replace by target or remove.
*
* ```javascript
* page.updateFilters(FiltersOperations.Add, filters)
* .catch(errors => { ... });
* ```
*
* @param {(IFilter[])} filters
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async updateFilters(operation: FiltersOperations, filters?: IFilter[]): Promise<IHttpPostMessageResponse<void>> {
const updateFiltersRequest: IUpdateFiltersRequest = {
filtersOperation: operation,
filters: filters as PageLevelFilters[]
};
try {
return await this.report.service.hpm.post<void>(`/report/pages/${this.name}/filters`, updateFiltersRequest, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Removes all filters from this page of the report.
*
* ```javascript
* page.removeFilters();
* ```
*
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async removeFilters(): Promise<IHttpPostMessageResponse<void>> {
return await this.updateFilters(FiltersOperations.RemoveAll);
}
/**
* Sets all filters on the current page.
*
* ```javascript
* page.setFilters(filters)
* .catch(errors => { ... });
* ```
*
* @param {(IFilter[])} filters
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async setFilters(filters: IFilter[]): Promise<IHttpPostMessageResponse<void>> {
try {
return await this.report.service.hpm.put<void>(`/report/pages/${this.name}/filters`, filters, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Delete the page from the report
*
* ```javascript
* // Delete the page from the report
* page.delete();
* ```
*
* @returns {Promise<void>}
*/
async delete(): Promise<void> {
try {
const response = await this.report.service.hpm.delete<void>(`/report/pages/${this.name}`, {}, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
/**
* Makes the current page the active page of the report.
*
* ```javascript
* page.setActive();
* ```
*
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async setActive(): Promise<IHttpPostMessageResponse<void>> {
const page: IPage = {
name: this.name,
displayName: null,
isActive: true
};
try {
return await this.report.service.hpm.put<void>('/report/pages/active', page, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Set displayName to the current page.
*
* ```javascript
* page.setName(displayName);
* ```
*
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async setDisplayName(displayName: string): Promise<IHttpPostMessageResponse<void>> {
const page: IPage = {
name: this.name,
displayName: displayName,
};
try {
return await this.report.service.hpm.put<void>(`/report/pages/${this.name}/name`, page, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Gets all the visuals on the page.
*
* ```javascript
* page.getVisuals()
* .then(visuals => { ... });
* ```
*
* @returns {Promise<VisualDescriptor[]>}
*/
async getVisuals(): Promise<VisualDescriptor[]> {
if (isRDLEmbed(this.report.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
try {
const response = await this.report.service.hpm.get<IVisual[]>(`/report/pages/${this.name}/visuals`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body
.map((visual) => new VisualDescriptor(this, visual.name, visual.title, visual.type, visual.layout));
} catch (response) {
throw response.body;
}
}
/**
* Gets a visual by name on the page.
*
* ```javascript
* page.getVisualByName(visualName: string)
* .then(visual => {
* ...
* });
* ```
*
* @param {string} visualName
* @returns {Promise<VisualDescriptor>}
*/
async getVisualByName(visualName: string): Promise<VisualDescriptor> {
if (isRDLEmbed(this.report.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
try {
const response = await this.report.service.hpm.get<IVisual[]>(`/report/pages/${this.name}/visuals`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
const visual = response.body.find((v: IVisual) => v.name === visualName);
if (!visual) {
return Promise.reject(CommonErrorCodes.NotFound);
}
return new VisualDescriptor(this, visual.name, visual.title, visual.type, visual.layout);
} catch (response) {
throw response.body;
}
}
/**
* Updates the display state of a visual in a page.
*
* ```javascript
* page.setVisualDisplayState(visualName, displayState)
* .catch(error => { ... });
* ```
*
* @param {string} visualName
* @param {VisualContainerDisplayMode} displayState
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async setVisualDisplayState(visualName: string, displayState: VisualContainerDisplayMode): Promise<IHttpPostMessageResponse<void>> {
const pageName = this.name;
const report = this.report as Report;
return report.setVisualDisplayState(pageName, visualName, displayState);
}
/**
* Updates the position of a visual in a page.
*
* ```javascript
* page.moveVisual(visualName, x, y, z)
* .catch(error => { ... });
* ```
*
* @param {string} visualName
* @param {number} x
* @param {number} y
* @param {number} z
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async moveVisual(visualName: string, x: number, y: number, z?: number): Promise<IHttpPostMessageResponse<void>> {
const pageName = this.name;
const report = this.report as Report;
return report.moveVisual(pageName, visualName, x, y, z);
}
/**
* Resize a visual in a page.
*
* ```javascript
* page.resizeVisual(visualName, width, height)
* .catch(error => { ... });
* ```
*
* @param {string} visualName
* @param {number} width
* @param {number} height
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async resizeVisual(visualName: string, width: number, height: number): Promise<IHttpPostMessageResponse<void>> {
const pageName = this.name;
const report = this.report as Report;
return report.resizeVisual(pageName, visualName, width, height);
}
/**
* Updates the size of active page.
*
* ```javascript
* page.resizePage(pageSizeType, width, height)
* .catch(error => { ... });
* ```
*
* @param {PageSizeType} pageSizeType
* @param {number} width
* @param {number} height
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async resizePage(pageSizeType: PageSizeType, width?: number, height?: number): Promise<IHttpPostMessageResponse<void>> {
if (!this.isActive) {
return Promise.reject('Cannot resize the page. Only the active page can be resized');
}
const report = this.report as Report;
return report.resizeActivePage(pageSizeType, width, height);
}
/**
* Gets the list of slicer visuals on the page.
*
* ```javascript
* page.getSlicers()
* .then(slicers => {
* ...
* });
* ```
*
* @returns {Promise<IVisual[]>}
*/
async getSlicers(): Promise<IVisual[]> {
if (isRDLEmbed(this.report.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
try {
const response = await this.report.service.hpm.get<IVisual[]>(`/report/pages/${this.name}/visuals`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body
.filter((visual: IVisual) => visual.type === 'slicer')
.map((visual: IVisual) => new VisualDescriptor(this, visual.name, visual.title, visual.type, visual.layout));
} catch (response) {
throw response.body;
}
}
/**
* Checks if page has layout.
*
* ```javascript
* page.hasLayout(layoutType)
* .then(hasLayout: boolean => { ... });
* ```
*
* @returns {(Promise<boolean>)}
*/
async hasLayout(layoutType: LayoutType): Promise<boolean> {
if (isRDLEmbed(this.report.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
const layoutTypeEnum = LayoutType[layoutType];
try {
const response = await this.report.service.hpm.get<boolean>(`/report/pages/${this.name}/layoutTypes/${layoutTypeEnum}`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
}