1
1
import { IHttpPostMessageResponse } from 'http-post-message' ;
2
+ import {
3
+ DisplayOption ,
4
+ FiltersOperations ,
5
+ ICustomPageSize ,
6
+ IFilter ,
7
+ IPage ,
8
+ IUpdateFiltersRequest ,
9
+ IVisual ,
10
+ LayoutType ,
11
+ PageLevelFilters ,
12
+ SectionVisibility
13
+ } from 'powerbi-models' ;
2
14
import { IFilterable } from './ifilterable' ;
3
15
import { IReportNode } from './report' ;
4
16
import { VisualDescriptor } from './visualDescriptor' ;
5
- import * as models from 'powerbi-models' ;
6
- import * as utils from './util' ;
7
- import * as errors from './errors' ;
17
+ import { isRDLEmbed } from './util' ;
18
+ import { APINotSupportedForRDLError } from './errors' ;
8
19
9
20
/**
10
21
* A Page node within a report hierarchy
@@ -58,21 +69,23 @@ export class Page implements IPageNode, IFilterable {
58
69
* 0 - Always Visible
59
70
* 1 - Hidden in View Mode
60
71
*
61
- * @type {models. SectionVisibility }
72
+ * @type {SectionVisibility }
62
73
*/
63
- visibility : models . SectionVisibility ;
74
+ visibility : SectionVisibility ;
64
75
65
76
/**
66
77
* Page size as saved in the report.
67
- * @type {models.ICustomPageSize }
78
+ *
79
+ * @type {ICustomPageSize }
68
80
*/
69
- defaultSize : models . ICustomPageSize ;
81
+ defaultSize : ICustomPageSize ;
70
82
71
83
/**
72
84
* Page display options as saved in the report.
73
- * @type {models.ICustomPageSize }
85
+ *
86
+ * @type {ICustomPageSize }
74
87
*/
75
- defaultDisplayOption : models . DisplayOption ;
88
+ defaultDisplayOption : DisplayOption ;
76
89
77
90
/**
78
91
* Creates an instance of a Power BI report page.
@@ -81,10 +94,10 @@ export class Page implements IPageNode, IFilterable {
81
94
* @param {string } name
82
95
* @param {string } [displayName]
83
96
* @param {boolean } [isActivePage]
84
- * @param {models. SectionVisibility } [visibility]
97
+ * @param {SectionVisibility } [visibility]
85
98
* @hidden
86
99
*/
87
- constructor ( report : IReportNode , name : string , displayName ?: string , isActivePage ?: boolean , visibility ?: models . SectionVisibility , defaultSize ?: models . ICustomPageSize , defaultDisplayOption ?: models . DisplayOption ) {
100
+ constructor ( report : IReportNode , name : string , displayName ?: string , isActivePage ?: boolean , visibility ?: SectionVisibility , defaultSize ?: ICustomPageSize , defaultDisplayOption ?: DisplayOption ) {
88
101
this . report = report ;
89
102
this . name = name ;
90
103
this . displayName = displayName ;
@@ -102,31 +115,36 @@ export class Page implements IPageNode, IFilterable {
102
115
* .then(filters => { ... });
103
116
* ```
104
117
*
105
- * @returns {(Promise<models. IFilter[]>) }
118
+ * @returns {(Promise<IFilter[]>) }
106
119
*/
107
- async getFilters ( ) : Promise < models . IFilter [ ] > {
120
+ async getFilters ( ) : Promise < IFilter [ ] > {
108
121
try {
109
- const response = await this . report . service . hpm . get < models . IFilter [ ] > ( `/report/pages/${ this . name } /filters` , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
122
+ const response = await this . report . service . hpm . get < IFilter [ ] > ( `/report/pages/${ this . name } /filters` , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
110
123
return response . body ;
111
124
} catch ( response ) {
112
125
throw response . body ;
113
126
}
114
127
}
115
128
116
129
/**
117
- * Delete the page from the report
130
+ * Update the filters for the current page according to the operation: Add, replace all, replace by target or remove.
118
131
*
119
132
* ```javascript
120
- * // Delete the page from the report
121
- * page.delete( );
133
+ * page.updateFilters(FiltersOperations.Add, filters)
134
+ * .catch(errors => { ... } );
122
135
* ```
123
136
*
124
- * @returns {Promise<void> }
137
+ * @param {(IFilter[]) } filters
138
+ * @returns {Promise<IHttpPostMessageResponse<void>> }
125
139
*/
126
- async delete ( ) : Promise < void > {
140
+ async updateFilters ( operation : FiltersOperations , filters ?: IFilter [ ] ) : Promise < IHttpPostMessageResponse < void > > {
141
+ const updateFiltersRequest : IUpdateFiltersRequest = {
142
+ filtersOperation : operation ,
143
+ filters : filters as PageLevelFilters [ ]
144
+ } ;
145
+
127
146
try {
128
- const response = await this . report . service . hpm . delete < void > ( `/report/pages/${ this . name } ` , { } , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
129
- return response . body ;
147
+ return await this . report . service . hpm . post < void > ( `/report/pages/${ this . name } /filters` , updateFiltersRequest , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
130
148
} catch ( response ) {
131
149
throw response . body ;
132
150
}
@@ -142,46 +160,65 @@ export class Page implements IPageNode, IFilterable {
142
160
* @returns {Promise<IHttpPostMessageResponse<void>> }
143
161
*/
144
162
async removeFilters ( ) : Promise < IHttpPostMessageResponse < void > > {
145
- return await this . setFilters ( [ ] ) ;
163
+ return await this . updateFilters ( FiltersOperations . RemoveAll ) ;
146
164
}
147
165
148
166
/**
149
- * Makes the current page the active page of the report .
167
+ * Sets all filters on the current page.
150
168
*
151
169
* ```javascript
152
- * page.setActive();
170
+ * page.setFilters(filters)
171
+ * .catch(errors => { ... });
153
172
* ```
154
173
*
174
+ * @param {(IFilter[]) } filters
155
175
* @returns {Promise<IHttpPostMessageResponse<void>> }
156
176
*/
157
- async setActive ( ) : Promise < IHttpPostMessageResponse < void > > {
158
- const page : models . IPage = {
159
- name : this . name ,
160
- displayName : null ,
161
- isActive : true
162
- } ;
177
+ async setFilters ( filters : IFilter [ ] ) : Promise < IHttpPostMessageResponse < void > > {
178
+ try {
179
+ return await this . report . service . hpm . put < void > ( `/report/pages/${ this . name } /filters` , filters , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
180
+ } catch ( response ) {
181
+ throw response . body ;
182
+ }
183
+ }
163
184
185
+ /**
186
+ * Delete the page from the report
187
+ *
188
+ * ```javascript
189
+ * // Delete the page from the report
190
+ * page.delete();
191
+ * ```
192
+ *
193
+ * @returns {Promise<void> }
194
+ */
195
+ async delete ( ) : Promise < void > {
164
196
try {
165
- return await this . report . service . hpm . put < void > ( '/report/pages/active' , page , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
197
+ const response = await this . report . service . hpm . delete < void > ( `/report/pages/${ this . name } ` , { } , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
198
+ return response . body ;
166
199
} catch ( response ) {
167
200
throw response . body ;
168
201
}
169
202
}
170
203
171
204
/**
172
- * Sets all filters on the current page.
205
+ * Makes the current page the active page of the report .
173
206
*
174
207
* ```javascript
175
- * page.setFilters(filters);
176
- * .catch(errors => { ... });
208
+ * page.setActive();
177
209
* ```
178
210
*
179
- * @param {(models.IFilter[]) } filters
180
211
* @returns {Promise<IHttpPostMessageResponse<void>> }
181
212
*/
182
- async setFilters ( filters : models . IFilter [ ] ) : Promise < IHttpPostMessageResponse < void > > {
213
+ async setActive ( ) : Promise < IHttpPostMessageResponse < void > > {
214
+ const page : IPage = {
215
+ name : this . name ,
216
+ displayName : null ,
217
+ isActive : true
218
+ } ;
219
+
183
220
try {
184
- return await this . report . service . hpm . put < void > ( ` /report/pages/${ this . name } /filters` , filters , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
221
+ return await this . report . service . hpm . put < void > ( ' /report/pages/active' , page , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
185
222
} catch ( response ) {
186
223
throw response . body ;
187
224
}
@@ -197,9 +234,9 @@ export class Page implements IPageNode, IFilterable {
197
234
* @returns {Promise<IHttpPostMessageResponse<void>> }
198
235
*/
199
236
async setDisplayName ( displayName : string ) : Promise < IHttpPostMessageResponse < void > > {
200
- const page : models . IPage = {
237
+ const page : IPage = {
201
238
name : this . name ,
202
- displayName,
239
+ displayName : displayName ,
203
240
} ;
204
241
205
242
try {
@@ -220,19 +257,17 @@ export class Page implements IPageNode, IFilterable {
220
257
* @returns {Promise<VisualDescriptor[]> }
221
258
*/
222
259
async getVisuals ( ) : Promise < VisualDescriptor [ ] > {
223
- if ( utils . isRDLEmbed ( this . report . config . embedUrl ) ) {
224
- return Promise . reject ( errors . APINotSupportedForRDLError ) ;
260
+ if ( isRDLEmbed ( this . report . config . embedUrl ) ) {
261
+ return Promise . reject ( APINotSupportedForRDLError ) ;
225
262
}
226
263
227
264
try {
228
- const response = await this . report . service . hpm . get < models . IVisual [ ] > ( `/report/pages/${ this . name } /visuals` , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
265
+ const response = await this . report . service . hpm . get < IVisual [ ] > ( `/report/pages/${ this . name } /visuals` , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
229
266
return response . body
230
- . map ( visual => {
231
- return new VisualDescriptor ( this , visual . name , visual . title , visual . type , visual . layout ) ;
232
- } ) ;
233
- } catch ( response ) {
234
- throw response . body ;
235
- }
267
+ . map ( visual => new VisualDescriptor ( this , visual . name , visual . title , visual . type , visual . layout ) ) ;
268
+ } catch ( response ) {
269
+ throw response . body ;
270
+ }
236
271
}
237
272
238
273
/**
@@ -246,16 +281,16 @@ export class Page implements IPageNode, IFilterable {
246
281
* @returns {(Promise<boolean>) }
247
282
*/
248
283
async hasLayout ( layoutType ) : Promise < boolean > {
249
- if ( utils . isRDLEmbed ( this . report . config . embedUrl ) ) {
250
- return Promise . reject ( errors . APINotSupportedForRDLError ) ;
284
+ if ( isRDLEmbed ( this . report . config . embedUrl ) ) {
285
+ return Promise . reject ( APINotSupportedForRDLError ) ;
251
286
}
252
287
253
- let layoutTypeEnum = models . LayoutType [ layoutType ] ;
288
+ const layoutTypeEnum = LayoutType [ layoutType ] ;
254
289
try {
255
290
const response = await this . report . service . hpm . get < boolean > ( `/report/pages/${ this . name } /layoutTypes/${ layoutTypeEnum } ` , { uid : this . report . config . uniqueId } , this . report . iframe . contentWindow ) ;
256
291
return response . body ;
257
292
} catch ( response ) {
258
293
throw response . body ;
259
294
}
260
295
}
261
- }
296
+ }
0 commit comments