forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarksManager.ts
More file actions
179 lines (161 loc) · 5.03 KB
/
bookmarksManager.ts
File metadata and controls
179 lines (161 loc) · 5.03 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import {
BookmarksPlayMode,
IApplyBookmarkByNameRequest,
IApplyBookmarkStateRequest,
ICaptureBookmarkOptions,
ICaptureBookmarkRequest,
IPlayBookmarkRequest,
IReportBookmark
} from 'powerbi-models';
import { IHttpPostMessageResponse } from 'http-post-message';
import { Service } from './service';
import { IEmbedConfigurationBase } from './embed';
import { isRDLEmbed } from './util';
import { APINotSupportedForRDLError } from './errors';
/**
* APIs for managing the report bookmarks.
*
* @export
* @interface IBookmarksManager
*/
export interface IBookmarksManager {
getBookmarks(): Promise<IReportBookmark[]>;
apply(bookmarkName: string): Promise<IHttpPostMessageResponse<void>>;
play(playMode: BookmarksPlayMode): Promise<IHttpPostMessageResponse<void>>;
capture(options?: ICaptureBookmarkOptions): Promise<IReportBookmark>;
applyState(state: string): Promise<IHttpPostMessageResponse<void>>;
}
/**
* Manages report bookmarks.
*
* @export
* @class BookmarksManager
* @implements {IBookmarksManager}
*/
export class BookmarksManager implements IBookmarksManager {
/**
* @hidden
*/
constructor(private service: Service, private config: IEmbedConfigurationBase, private iframe?: HTMLIFrameElement) {
}
/**
* Gets bookmarks that are defined in the report.
*
* ```javascript
* // Gets bookmarks that are defined in the report
* bookmarksManager.getBookmarks()
* .then(bookmarks => {
* ...
* });
* ```
*
* @returns {Promise<IReportBookmark[]>}
*/
async getBookmarks(): Promise<IReportBookmark[]> {
if (isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
try {
const response = await this.service.hpm.get<IReportBookmark[]>(`/report/bookmarks`, { uid: this.config.uniqueId }, this.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
/**
* Apply bookmark by name.
*
* ```javascript
* bookmarksManager.apply(bookmarkName)
* ```
*
* @param {string} bookmarkName The name of the bookmark to be applied
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async apply(bookmarkName: string): Promise<IHttpPostMessageResponse<void>> {
if (isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
const request: IApplyBookmarkByNameRequest = {
name: bookmarkName
};
try {
return await this.service.hpm.post<void>(`/report/bookmarks/applyByName`, request, { uid: this.config.uniqueId }, this.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Play bookmarks: Enter or Exit bookmarks presentation mode.
*
* ```javascript
* // Enter presentation mode.
* bookmarksManager.play(BookmarksPlayMode.Presentation)
* ```
*
* @param {BookmarksPlayMode} playMode Play mode can be either `Presentation` or `Off`
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async play(playMode: BookmarksPlayMode): Promise<IHttpPostMessageResponse<void>> {
if (isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
const playBookmarkRequest: IPlayBookmarkRequest = {
playMode: playMode
};
try {
return await this.service.hpm.post<void>(`/report/bookmarks/play`, playBookmarkRequest, { uid: this.config.uniqueId }, this.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
/**
* Capture bookmark from current state.
*
* ```javascript
* bookmarksManager.capture(options)
* ```
*
* @param {ICaptureBookmarkOptions} [options] Options for bookmark capturing
* @returns {Promise<IReportBookmark>}
*/
async capture(options?: ICaptureBookmarkOptions): Promise<IReportBookmark> {
if (isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
const request: ICaptureBookmarkRequest = {
options: options || {}
};
try {
const response = await this.service.hpm.post<IReportBookmark>(`/report/bookmarks/capture`, request, { uid: this.config.uniqueId }, this.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
/**
* Apply bookmark state.
*
* ```javascript
* bookmarksManager.applyState(bookmarkState)
* ```
*
* @param {string} state A base64 bookmark state to be applied
* @returns {Promise<IHttpPostMessageResponse<void>>}
*/
async applyState(state: string): Promise<IHttpPostMessageResponse<void>> {
if (isRDLEmbed(this.config.embedUrl)) {
return Promise.reject(APINotSupportedForRDLError);
}
const request: IApplyBookmarkStateRequest = {
state: state
};
try {
return await this.service.hpm.post<void>(`/report/bookmarks/applyState`, request, { uid: this.config.uniqueId }, this.iframe.contentWindow);
} catch (response) {
throw response.body;
}
}
}