1
+ $ ( function ( ) {
2
+
3
+ var $staticReport = $ ( '#reportstatic' ) ;
4
+ var $reportsList = $ ( '#reportslist' ) ;
5
+ var $dynamicReport = $ ( '#reportdynamic' ) ;
6
+ var $customPageNavReport = $ ( '#reportcustompagenav' ) ;
7
+ var customPageNavReport ;
8
+ var customPageNavReportPages ;
9
+ var $reportPagesList = $ ( '#reportpagesbuttons' ) ;
10
+ var $resetButton = $ ( '#resetButton' ) ;
11
+ var $prevButton = $ ( '#prevbutton' ) ;
12
+ var $nextButton = $ ( '#nextbutton' ) ;
13
+ var $cycleButton = $ ( '#cyclebutton' ) ;
14
+ var cycleIntervalId ;
15
+ var apiBaseUrl = 'http://powerbipaasapi.azurewebsites.net/api/reports' ;
16
+
17
+ var localReportOverride = {
18
+ embedUrl : 'https://portal.analysis.windows-int.net/appTokenReportEmbed' ,
19
+ id : 'c4d31ef0-7b34-4d80-9bcb-5974d1405572' ,
20
+ accessToken : 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2ZXIiOiIwLjEuMCIsImF1ZCI6Imh0dHBzOi8vYW5hbHlzaXMud2luZG93cy5uZXQvcG93ZXJiaS9hcGkiLCJpc3MiOiJQb3dlckJJU0RLIiwidHlwZSI6ImVtYmVkIiwid2NuIjoiV2FsbGFjZSIsIndpZCI6IjUyMWNkYTJhLTRlZDItNDg5Ni1hYzA0LWM5YzM4MWRjMjUyYSIsInJpZCI6ImM0ZDMxZWYwLTdiMzQtNGQ4MC05YmNiLTU5NzRkMTQwNTU3MiIsIm5iZiI6MTQ2ODAxNTg5NSwiZXhwIjoxNDY4MDE5NDk1fQ.exh-qIpdEoa5lLxJleyGFli8ZvdNNyiDjVyHl9XBAiA'
21
+ } ;
22
+
23
+ /**
24
+ * Basic Embed
25
+ */
26
+ var staticReportId = '5dac7a4a-4452-46b3-99f6-a25915e0fe55' ;
27
+ var staticReportUrl = apiBaseUrl + '/' + staticReportId ;
28
+
29
+ fetch ( staticReportUrl )
30
+ . then ( function ( response ) {
31
+ return response . json ( ) ;
32
+ } )
33
+ . then ( function ( report ) {
34
+ var reportConfig = $ . extend ( {
35
+ type : 'report' ,
36
+ settings : {
37
+ filterPaneEnabled : false ,
38
+ navContentPaneEnabled : false
39
+ }
40
+ } , report , localReportOverride ) ;
41
+ var staticReport = powerbi . embed ( $staticReport . get ( 0 ) , reportConfig ) ;
42
+
43
+ staticReport . on ( 'loaded' , function ( event ) {
44
+ console . log ( 'static report loaded' ) ;
45
+ } ) ;
46
+ staticReport . on ( 'error' , function ( event ) {
47
+ console . log ( 'static report error' ) ;
48
+ } ) ;
49
+
50
+ var customPageNavConfig = $ . extend ( { } , reportConfig , {
51
+ settings : {
52
+ filterPaneEnabled : false ,
53
+ navContentPaneEnabled : true
54
+ }
55
+ } ) ;
56
+
57
+ customPageNavReport = powerbi . embed ( $customPageNavReport . get ( 0 ) , customPageNavConfig ) ;
58
+ customPageNavReport . on ( 'loaded' , function ( event ) {
59
+ console . log ( 'custom page nav report loaded' ) ;
60
+ customPageNavReport . getPages ( )
61
+ . then ( pages => {
62
+ console . log ( 'pages: ' , pages ) ;
63
+ if ( pages . length > 0 ) {
64
+ customPageNavReportPages = pages ;
65
+ const firstPage = customPageNavReportPages [ 0 ] ;
66
+ firstPage . isActive = true ;
67
+
68
+ pages
69
+ . map ( function ( page ) {
70
+ return generateReportPage ( page ) ;
71
+ } )
72
+ . forEach ( function ( element ) {
73
+ $reportPagesList . append ( element ) ;
74
+ } ) ;
75
+ }
76
+ } ) ;
77
+ } ) ;
78
+ customPageNavReport . on ( 'error' , function ( event ) {
79
+ console . log ( 'customPageNavReport error' , event ) ;
80
+ } ) ;
81
+
82
+ customPageNavReport . on ( 'pageChanged' , function ( event ) {
83
+ console . log ( 'pageChanged event received' , event ) ;
84
+ updateActivePage ( event . detail . newPage ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ function updateActivePage ( newPage ) {
89
+ // Remove active class
90
+ var reportButtons = $reportPagesList . children ( 'button' ) ;
91
+
92
+ reportButtons
93
+ . each ( function ( index , element ) {
94
+ var $element = $ ( element ) ;
95
+ var buttonPage = $element . data ( 'page' ) ;
96
+ if ( buttonPage . isActive ) {
97
+ buttonPage . isActive = false ;
98
+ $element . removeClass ( 'active' ) ;
99
+ }
100
+ } ) ;
101
+
102
+ // Set active class
103
+ reportButtons
104
+ . each ( function ( index , element ) {
105
+ var $element = $ ( element ) ;
106
+ var buttonPage = $element . data ( 'page' ) ;
107
+ if ( buttonPage . name === newPage . name ) {
108
+ buttonPage . isActive = true ;
109
+ $element . addClass ( 'active' ) ;
110
+ }
111
+ } ) ;
112
+ }
113
+
114
+ function changePage ( forwards ) {
115
+ // Remove active class
116
+ var reportButtons = $reportPagesList . children ( 'button' ) ;
117
+ var $activeButtonIndex = - 1 ;
118
+
119
+ reportButtons
120
+ . each ( function ( index , element ) {
121
+ var $element = $ ( element ) ;
122
+ var buttonPage = $element . data ( 'page' ) ;
123
+ if ( buttonPage . isActive ) {
124
+ $activeButtonIndex = index ;
125
+ }
126
+ } ) ;
127
+
128
+ if ( forwards ) {
129
+ $activeButtonIndex += 1 ;
130
+ }
131
+ else {
132
+ $activeButtonIndex -= 1 ;
133
+ }
134
+
135
+ if ( $activeButtonIndex > reportButtons . length - 1 ) {
136
+ $activeButtonIndex = 0 ;
137
+ }
138
+ if ( $activeButtonIndex < 0 ) {
139
+ $activeButtonIndex = reportButtons . length - 1 ;
140
+ }
141
+
142
+ reportButtons
143
+ . each ( function ( index , element ) {
144
+ if ( $activeButtonIndex === index ) {
145
+ var $element = $ ( element ) ;
146
+ var buttonPage = $element . data ( 'page' ) ;
147
+
148
+ customPageNavReport . setPage ( buttonPage . name ) ;
149
+ }
150
+ } ) ;
151
+ }
152
+
153
+ /**
154
+ * Dynamic Embed
155
+ */
156
+ function generateReportListItem ( report ) {
157
+ var button = $ ( '<button>' )
158
+ . attr ( {
159
+ type : 'button'
160
+ } )
161
+ . addClass ( 'btn btn-success' )
162
+ . data ( 'report' , report )
163
+ . text ( 'Embed!' ) ;
164
+
165
+ var reportName = $ ( '<span />' )
166
+ . addClass ( 'report-name' )
167
+ . text ( report . name )
168
+
169
+ var element = $ ( '<li>' )
170
+ . append ( reportName )
171
+ . append ( button ) ;
172
+
173
+ return element ;
174
+ }
175
+
176
+ function generateReportPage ( page ) {
177
+ var $page = $ ( '<button>' )
178
+ . attr ( {
179
+ type : 'button'
180
+ } )
181
+ . addClass ( 'btn btn-success' )
182
+ . data ( 'page' , page )
183
+ . text ( page . displayName ) ;
184
+
185
+ if ( page . isActive ) {
186
+ $page . addClass ( 'active' ) ;
187
+ }
188
+
189
+ return $page ;
190
+ }
191
+
192
+ var allReportsUrl = apiBaseUrl ;
193
+ fetch ( allReportsUrl )
194
+ . then ( function ( response ) {
195
+ return response . json ( ) ;
196
+ } )
197
+ . then ( function ( reports ) {
198
+ reports
199
+ . map ( generateReportListItem )
200
+ . forEach ( function ( element ) {
201
+ $reportsList . append ( element ) ;
202
+ } ) ;
203
+ } ) ;
204
+
205
+ // When report button is clicked embed the report
206
+ $reportsList . on ( 'click' , 'button' , function ( event ) {
207
+ var button = event . target ;
208
+ var report = $ ( button ) . data ( 'report' ) ;
209
+ var url = apiBaseUrl + '/' + report . id ;
210
+
211
+ fetch ( url )
212
+ . then ( function ( response ) {
213
+ return response . json ( ) ;
214
+ } )
215
+ . then ( function ( reportWithToken ) {
216
+ var reportConfig = $ . extend ( {
217
+ type : 'report' ,
218
+ settings : {
219
+ filterPaneEnabled : false ,
220
+ navContentPaneEnabled : false
221
+ }
222
+ } , reportWithToken , localReportOverride ) ;
223
+
224
+ powerbi . embed ( $dynamicReport . get ( 0 ) , reportConfig ) ;
225
+ } ) ;
226
+ } ) ;
227
+
228
+ $prevButton . on ( 'click' , function ( event ) {
229
+ changePage ( false ) ;
230
+ } ) ;
231
+
232
+ $nextButton . on ( 'click' , function ( event ) {
233
+ changePage ( true ) ;
234
+ } ) ;
235
+
236
+ $cycleButton . on ( 'click' , function ( event ) {
237
+ $cycleButton . toggleClass ( 'active' ) ;
238
+ $cycleButton . data ( 'cycle' , ! $cycleButton . data ( 'cycle' ) ) ;
239
+
240
+ if ( $cycleButton . data ( 'cycle' ) ) {
241
+ cycleIntervalId = setInterval ( function ( ) {
242
+ console . log ( 'cycle page: ' ) ;
243
+ changePage ( true ) ;
244
+ } , 1000 ) ;
245
+ }
246
+ else {
247
+ clearInterval ( cycleIntervalId ) ;
248
+ }
249
+ } ) ;
250
+
251
+ $resetButton . on ( 'click' , function ( event ) {
252
+ powerbi . reset ( $dynamicReport . get ( 0 ) ) ;
253
+ } ) ;
254
+
255
+
256
+ /**
257
+ * Custom Page Navigation
258
+ */
259
+ $reportPagesList . on ( 'click' , 'button' , function ( event ) {
260
+ var button = event . target ;
261
+ var report = $ ( button ) . data ( 'report' ) ;
262
+ var page = $ ( button ) . data ( 'page' ) ;
263
+
264
+ console . log ( 'Attempting to set page to: ' , page . name ) ;
265
+ customPageNavReport . setPage ( page . name )
266
+ . then ( function ( response ) {
267
+ console . log ( 'Page changed request accepted' ) ;
268
+ } ) ;
269
+ } ) ;
270
+ } ) ;
0 commit comments