Skip to content

Commit 97d2dcd

Browse files
authored
Refactor filters from crud model to get/set model with objects representing each level instead of targets. (microsoft#24)
Refactor filters from crud model to get/set model with objects representing each level instead of targets. * Update Service class to implement IService * Add more tests for Page and Visual for SDK to MockApp * Change getPages and getVisuals to return concrete classes instead of node interfaces * Change IReport, IPage, IVisual to use Node suffix to make it clear they are not interface for class, but for traversing tree * Cleanup on tests in attempt to fix error. * Attempt to fix tests by using jasmine.objectContaining when comparing object equality in catch blocks. * Update load documentation to use filters array
1 parent aa88300 commit 97d2dcd

13 files changed

+2094
-3686
lines changed

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"editor.tabSize": 4,
4+
"editor.insertSpaces": true
5+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"dependencies": {
6868
"http-post-message": "^0.2.0",
69-
"powerbi-models": "^0.5.0",
69+
"powerbi-models": "^0.7.2",
7070
"powerbi-router": "^0.1.2",
7171
"window-post-message-proxy": "^0.2.1"
7272
},

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const config = {
2-
version: '2.0.0-beta.7',
2+
version: '2.0.0-beta.8',
33
type: 'js'
44
};
55

src/embed.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export abstract class Embed {
9797
* navContentPaneEnabled: false
9898
* },
9999
* pageName: "DefaultPage",
100-
* filte: "DefaultReportFilter"
100+
* filters: [
101+
* {
102+
* ... DefaultReportFilter ...
103+
* }
104+
* ]
101105
* })
102106
* .catch(error => { ... });
103107
* ```
@@ -109,7 +113,10 @@ export abstract class Embed {
109113
}
110114

111115
return this.service.hpm.post<void>('/report/load', config, { uid: this.config.uniqueId }, this.iframe.contentWindow)
112-
.catch(response => {
116+
.then(response => {
117+
return response.body;
118+
},
119+
response => {
113120
throw response.body;
114121
});
115122
}

src/ifilterable.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as models from 'powerbi-models';
2+
3+
export interface IFilterable {
4+
getFilters(): Promise<(models.IBasicFilter | models.IAdvancedFilter)[]>;
5+
setFilters(filters: (models.IBasicFilter | models.IAdvancedFilter)[]): Promise<void>;
6+
removeFilters(): Promise<void>;
7+
}

src/page.ts

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { IFilterable } from './ifilterable';
2+
import { IReportNode } from './report';
3+
import { Visual } from './visual';
4+
import * as models from 'powerbi-models';
5+
6+
export interface IPageNode {
7+
report: IReportNode;
8+
name: string;
9+
}
10+
11+
export class Page implements IPageNode, IFilterable {
12+
report: IReportNode;
13+
name: string;
14+
// This can be undefined in cases where page is created manually
15+
displayName: string;
16+
17+
constructor(report: IReportNode, name: string, displayName?: string) {
18+
this.report = report;
19+
this.name = name;
20+
this.displayName = displayName;
21+
}
22+
23+
/**
24+
* Gets all page level filters within report
25+
*
26+
* ```javascript
27+
* page.getFilters()
28+
* .then(pages => { ... });
29+
* ```
30+
*/
31+
getFilters() {
32+
return this.report.service.hpm.get<models.IFilter[]>(`/report/pages/${this.name}/filters`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow)
33+
.then(response => response.body,
34+
response => {
35+
throw response.body;
36+
});
37+
}
38+
39+
/**
40+
* Gets all the visuals on the page.
41+
*
42+
* ```javascript
43+
* page.getVisuals()
44+
* .then(visuals => { ... });
45+
* ```
46+
*/
47+
getVisuals(): Promise<Visual[]> {
48+
return this.report.service.hpm.get<models.IVisual[]>(`/report/pages/${this.name}/visuals`, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow)
49+
.then(response => {
50+
return response.body
51+
.map(visual => {
52+
return new Visual(this, visual.name);
53+
});
54+
}, response => {
55+
throw response.body;
56+
});
57+
}
58+
59+
/**
60+
* Remove all filters on this page within the report
61+
*
62+
* ```javascript
63+
* page.removeFilters();
64+
* ```
65+
*/
66+
removeFilters() {
67+
return this.setFilters([]);
68+
}
69+
70+
/**
71+
* Make the current page the active page of the report.
72+
*
73+
* ```javascripot
74+
* page.setActive();
75+
* ```
76+
*/
77+
setActive() {
78+
const page: models.IPage = {
79+
name: this.name,
80+
displayName: null
81+
};
82+
83+
return this.report.service.hpm.put<models.IError[]>('/report/pages/active', page, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow)
84+
.catch(response => {
85+
throw response.body;
86+
});
87+
}
88+
89+
/**
90+
* Sets all filters on the current page.
91+
*
92+
* ```javascript
93+
* page.setFilters(filters);
94+
* .catch(errors => { ... });
95+
* ```
96+
*/
97+
setFilters(filters: (models.IBasicFilter | models.IAdvancedFilter)[]) {
98+
return this.report.service.hpm.put<models.IError[]>(`/report/pages/${this.name}/filters`, filters, { uid: this.report.config.uniqueId }, this.report.iframe.contentWindow)
99+
.catch(response => {
100+
throw response.body;
101+
});
102+
}
103+
104+
/**
105+
* Creates new Visual object given a name of the visual.
106+
*
107+
* Normally you would get Visual objects by calling `page.getVisuals()` but in the case
108+
* that the visual name is known and you want to perform an action on a visaul such as setting a filters
109+
* without having to retrieve it first you can create it directly.
110+
*
111+
* Note: Since you are creating the visual manually there is no guarantee that the visual actually exists in the report and the subsequence requests could fail.
112+
*
113+
* ```javascript
114+
* const visual = report.page('ReportSection1').visual('BarChart1');
115+
* visual.setFilters(filters);
116+
* ```
117+
*/
118+
visual(name: string): Visual {
119+
return new Visual(this, name);
120+
}
121+
}

0 commit comments

Comments
 (0)