/
index.js
525 lines (415 loc) · 18.5 KB
/
index.js
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// ----------------------------------------------------------------------------
// To stop the page load on click event
$(document).on("click", ".allow-focus", function (element) {
element.stopPropagation();
});
// On page resize, visuals should get rearranged according to the page
$(document).ready(function () {
// Bootstrap the bookmark embed container
powerbi.bootstrap(reportContainer, { type: "report" });
// Embed the report by calling Endpoint
embedCustomLayoutReport();
$("#btn-one-col").click(function () {
onModifyLayoutClicked(0, 1, this);
});
$("#btn-two-col-colspan").click(function () {
onModifyLayoutClicked(1, 2, this);
});
$("#btn-two-col-rowspan").click(function () {
onModifyLayoutClicked(2, 2, this);
});
$("#btn-two-cols").click(function () {
onModifyLayoutClicked(0, 2, this);
});
$("#btn-three-cols").click(function () {
onModifyLayoutClicked(0, 3, this);
});
// Focus on the first visual selection when the dropdown opens
visualsDiv.on("shown.bs.dropdown", function () {
$("input[type=checkbox]")[0].focus();
});
layoutsDiv.on("shown.bs.dropdown", function () {
// Focus on the current selected layout
const activeLayout = $(".active-columns-btn");
activeLayout.focus();
});
// Move the focus back to the button which triggered the dropdown
visualsDiv.on("hidden.bs.dropdown", function () {
chooseVisualsBtn.focus();
});
// Move the focus back to the button which triggered the dropdown
layoutsDiv.on("hidden.bs.dropdown", function () {
chooseLayoutBtn.focus();
});
// Close the layouts dropdown when focus moves from first layout-option to button
layoutButtons.on("keydown", function (e) {
// Shift + Tab
if (e.shiftKey && (e.key === Keys.TAB || e.keyCode === KEYCODE_TAB)) {
if (document.activeElement.id === firstButtonId) {
// Close the layouts dropdown
layoutsDropdown.removeClass("show");
layoutsDiv.removeClass("show");
document.getElementById("choose-layouts-btn").setAttribute("aria-expanded", false);
}
}
});
window.addEventListener("resize", renderVisuals);
});
// Close the visuals dropdown when focus moves from first checkbox to button
$(document).on("keydown", "input:checkbox", function (e) {
// Shift + Tab
if (e.shiftKey && (e.key === Keys.TAB || e.keyCode === KEYCODE_TAB)) {
if (this.id === firstVisualId) {
// Close the visuals dropdown
visualsDropdown.removeClass("show");
visualsDiv.removeClass("show");
document.getElementById("choose-visuals-btn").setAttribute("aria-expanded", false);
}
}
});
// Show tooltip only when ellipsis is active
$(document).on("mouseenter", ".text-truncate", function () {
const element = $(this);
if (this.offsetWidth < this.scrollWidth && !element.prop("title")) {
element.prop("title", element.text());
}
});
// Embed the report and retrieve all report visuals
async function embedCustomLayoutReport() {
// Default columns value is two columns
layoutShowcaseState.columns = COLUMNS.TWO;
layoutShowcaseState.span = SPAN_TYPE.NONE;
// Load custom layout report properties into session
await loadLayoutShowcaseReportIntoSession();
// Get embed application token from globals
let accessToken = reportConfig.accessToken;
// Get embed URL from globals
let embedUrl = reportConfig.embedUrl;
// Get report Id from globals
let embedReportId = reportConfig.reportId;
// Use View permissions
let permissions = models.Permissions.View;
// Embed configuration used to describe the what and how to embed
// This object is used when calling powerbi.embed
// This also includes settings and options such as filters
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details
let config = {
type: "report",
tokenType: models.TokenType.Embed,
accessToken: accessToken,
embedUrl: embedUrl,
id: embedReportId,
permissions: permissions,
settings: {
panes: {
filters: {
visible: false
},
pageNavigation: {
visible: false
}
}
}
};
// Embed Power BI report when Access token and Embed URL are available
layoutShowcaseState.layoutReport = powerbi.load(reportContainer, config);
// For accessibility insights
layoutShowcaseState.layoutReport.setComponentTitle("Playground showcase custom layouts report");
layoutShowcaseState.layoutReport.setComponentTabIndex(0);
// Clear any other loaded handler events
layoutShowcaseState.layoutReport.off("loaded");
// Triggers when a report schema is successfully loaded
layoutShowcaseState.layoutReport.on("loaded", async function () {
const pages = await layoutShowcaseState.layoutReport.getPages();
// Retrieve first page.
let activePage = jQuery.grep(pages, function (page) { return page.isActive; })[0];
// Set layoutPageName to active page name
layoutShowcaseState.layoutPageName = activePage.name;
// Get the visuals of the active page
const visuals = await activePage.getVisuals();
let reportVisuals = visuals.map(function (visual) {
return {
name: visual.name,
title: visual.title,
checked: true
};
});
await createVisualsArray(reportVisuals);
// Implement phase embedding to first load the report, arrange the visuals and then render
layoutShowcaseState.layoutReport.render();
// Phase-embedding
// Hide the loader
$("#overlay").hide();
$("#main-div").children().show();
console.log("Report render successfully");
});
// Clear any other loaded handler events
layoutShowcaseState.layoutReport.off("rendered");
// Triggers when a report is successfully embedded in UI
layoutShowcaseState.layoutReport.on("rendered", function () {
layoutShowcaseState.layoutReport.off("rendered");
console.log("The personalize top insights report rendered successfully");
// Protection against cross-origin failure
try {
if (window.parent.playground && window.parent.playground.logShowcaseDoneRendering) {
window.parent.playground.logShowcaseDoneRendering("PersonalizeTopInsights");
}
} catch { }
});
// Clear any other error handler events
layoutShowcaseState.layoutReport.off("error");
// Handle embed errors
layoutShowcaseState.layoutReport.on("error", function (event) {
let errorMsg = event.detail;
console.error(errorMsg);
});
}
// Create visuals array from the report visuals and update the HTML
async function createVisualsArray(reportVisuals) {
// Remove all visuals without titles (i.e cards)
layoutShowcaseState.layoutVisuals = reportVisuals.filter(function (visual) {
return visual.title !== undefined;
});
// Clear visualDropdown div
visualsDropdown.empty();
// Build checkbox html list and insert the html code to visualDropdown div
layoutShowcaseState.layoutVisuals.forEach(function (element) {
visualsDropdown.append(buildVisualElement(element));
});
// Store the id of the first visual in state
firstVisualId = $("input:checkbox")[0].id;
// Render all visuals
await renderVisuals();
}
// Build visual checkbox HTML element
function buildVisualElement(visual) {
let labelElement = document.createElement("label");
labelElement.setAttribute("class", "checkbox-container checked");
labelElement.setAttribute("for", "visual_" + visual.name);
labelElement.setAttribute("role", "menuitem");
let inputElement = document.createElement("input");
inputElement.setAttribute("type", "checkbox");
inputElement.setAttribute("id", "visual_" + visual.name);
inputElement.setAttribute("value", visual.name);
inputElement.setAttribute("onclick", "onCheckboxClicked(this);");
inputElement.setAttribute("checked", "true");
labelElement.append(inputElement);
let spanElement = document.createElement("span");
spanElement.setAttribute("class", "checkbox-checkmark");
labelElement.append(spanElement);
let secondSpanElement = document.createElement("span");
secondSpanElement.setAttribute("class", "checkbox-title text-truncate");
let checkboxTitleElement = document.createTextNode(visual.title);
secondSpanElement.append(checkboxTitleElement);
labelElement.append(secondSpanElement);
return labelElement;
}
// Returns true if current browser is Firefox
function isBrowserFirefox() {
// Refer https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#Browser_Name
return navigator.userAgent.includes("Firefox");
}
// Render all visuals with current configuration
async function renderVisuals() {
// render only if report and visuals initialized
if (!layoutShowcaseState.layoutReport || !layoutShowcaseState.layoutVisuals) {
return;
}
// Get report-container width and height
let reportContainer = $("#report-container");
let reportWidth = reportContainer.width();
let reportHeight = 0;
// Adjust the report width in Firefox to circumvent the horizontal scrollbar issue
if (isBrowserFirefox()) {
// Adjust custom layout width for scrollbar
reportWidth -= 8;
}
// Filter the visuals list to display only the checked visuals
let checkedVisuals = layoutShowcaseState.layoutVisuals.filter(function (visual) { return visual.checked; });
// Calculating the combined width of the all visuals in a row
let visualsTotalWidth = reportWidth - (LAYOUT_SHOWCASE.MARGIN * (layoutShowcaseState.columns + 1));
// Get all the available width for visuals total width, get the space from right margin of the report
visualsTotalWidth += LAYOUT_SHOWCASE.MARGIN / 2;
// Calculate the width of a single visual, according to the number of columns
// For one and three columns visuals width will be a third of visuals total width
let visualWidth = visualsTotalWidth / layoutShowcaseState.columns;
// Building visualsLayout object
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Custom-Layout
let visualsLayout = {};
// Visuals starting point
let x = LAYOUT_SHOWCASE.MARGIN;
let y = LAYOUT_SHOWCASE.MARGIN;
// Calculate visualHeight with margins
let visualHeight = visualWidth * LAYOUT_SHOWCASE.VISUAL_ASPECT_RATIO;
// Section means a single unit that will be repeating as pattern to form the layout
// These 2 variables are used for the 2 custom layouts with spanning
let rowsPerSection = 2;
let visualsPerSection = 3;
// Calculate the number of rows
let rows = 0;
if (layoutShowcaseState.span === SPAN_TYPE.COLSPAN) {
rows = rowsPerSection * Math.floor(checkedVisuals.length / visualsPerSection);
if (checkedVisuals.length % visualsPerSection) {
rows += 1;
}
reportHeight = Math.max(reportHeight, (rows * visualHeight) + (rows + 1) * LAYOUT_SHOWCASE.MARGIN);
checkedVisuals.forEach(function (element, idx) {
visualsLayout[element.name] = {
x: x,
y: y,
width: (idx % visualsPerSection === visualsPerSection - 1) ? visualWidth * 2 + LAYOUT_SHOWCASE.MARGIN : visualWidth,
height: visualHeight,
displayState: {
// Change the selected visuals display mode to visible
mode: models.VisualContainerDisplayMode.Visible
}
};
// Calculating (x,y) position for the next visual
x += LAYOUT_SHOWCASE.MARGIN + ((idx % visualsPerSection === visualsPerSection - 1) ? visualWidth * 2 : visualWidth);
// Reset x
if (x + visualWidth > reportWidth) {
x = LAYOUT_SHOWCASE.MARGIN;
y += visualHeight + LAYOUT_SHOWCASE.MARGIN;
}
});
} else if (layoutShowcaseState.span === SPAN_TYPE.ROWSPAN) {
rows = rowsPerSection * Math.floor(checkedVisuals.length / visualsPerSection);
if (checkedVisuals.length % visualsPerSection) {
rows += 2;
}
reportHeight = Math.max(reportHeight, (rows * visualHeight) + (rows + 1) * LAYOUT_SHOWCASE.MARGIN);
checkedVisuals.forEach(function (element, idx) {
visualsLayout[element.name] = {
x: x,
y: y,
width: visualWidth,
height: !(idx % visualsPerSection) ? visualHeight * 2 + LAYOUT_SHOWCASE.MARGIN : visualHeight,
displayState: {
// Change the selected visuals display mode to visible
mode: models.VisualContainerDisplayMode.Visible
}
};
// Calculating (x,y) position for the next visual
x += visualWidth + LAYOUT_SHOWCASE.MARGIN;
// Reset x
if (x + visualWidth > reportWidth) {
x = ((idx + 1) % visualsPerSection === 0) ? LAYOUT_SHOWCASE.MARGIN : (2 * LAYOUT_SHOWCASE.MARGIN) + visualWidth;
y += (idx % visualsPerSection === 0) ? visualHeight * 2 : visualHeight + LAYOUT_SHOWCASE.MARGIN;
}
});
} else if (layoutShowcaseState.span === SPAN_TYPE.NONE) {
if (layoutShowcaseState.columns === COLUMNS.One) {
visualHeight /= 2;
}
rows = Math.ceil(checkedVisuals.length / layoutShowcaseState.columns);
reportHeight = Math.max(reportHeight, (rows * visualHeight) + (rows + 1) * LAYOUT_SHOWCASE.MARGIN);
checkedVisuals.forEach(function (element) {
visualsLayout[element.name] = {
x: x,
y: y,
width: visualWidth,
height: visualHeight,
displayState: {
// Change the selected visuals display mode to visible
mode: models.VisualContainerDisplayMode.Visible
}
};
// Calculating (x,y) position for the next visual
x += visualWidth + LAYOUT_SHOWCASE.MARGIN;
// Reset x
if (x + visualWidth > reportWidth) {
x = LAYOUT_SHOWCASE.MARGIN;
y += visualHeight + LAYOUT_SHOWCASE.MARGIN;
}
});
}
// Building visualsLayout object
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Custom-Layout
// Building pagesLayout object
let pagesLayout = {};
pagesLayout[layoutShowcaseState.layoutPageName] = {
defaultLayout: {
displayState: {
// Default display mode for visuals is hidden
mode: models.VisualContainerDisplayMode.Hidden
}
},
visualsLayout: visualsLayout
};
// Building settings object
let settings = {
// Change page background to transparent
background: models.BackgroundType.Transparent,
layoutType: models.LayoutType.Custom,
customLayout: {
pageSize: {
type: models.PageSizeType.Custom,
width: reportWidth,
height: reportHeight
},
displayOption: models.DisplayOption.FitToPage,
pagesLayout: pagesLayout
}
};
// If reportWidth or reportHeight is changed, change display option to actual size to add scroll bar
if (reportWidth !== reportContainer.width() || reportHeight !== reportContainer.height()) {
// Reset the height of the report-container to avoid the scroll-bar
resetContainerHeight(reportHeight);
settings.customLayout.displayOption = models.DisplayOption.ActualSize;
}
// Call updateSettings with the new settings object
await layoutShowcaseState.layoutReport.updateSettings(settings);
}
// Reset the report-container based on the visuals inside it
function resetContainerHeight(newHeight) {
const reportContainer = $("#report-container");
reportContainer.height(newHeight);
}
// Update the visuals list with the change and re-render all visuals
function onCheckboxClicked(checkbox) {
let visual = jQuery.grep(layoutShowcaseState.layoutVisuals, function (visual) { return visual.name === checkbox.value; })[0];
visual.checked = $(checkbox).is(":checked");
renderVisuals();
};
// Update columns number and re-render the visuals
function onModifyLayoutClicked(spanType, column, clickedElement) {
// Selecting the layout option as per the selection
if (spanType === SPAN_TYPE.ROWSPAN) {
layoutShowcaseState.columns = column;
layoutShowcaseState.span = SPAN_TYPE.ROWSPAN;
} else if (spanType === SPAN_TYPE.COLSPAN) {
layoutShowcaseState.columns = column;
layoutShowcaseState.span = SPAN_TYPE.COLSPAN;
} else {
layoutShowcaseState.columns = column;
layoutShowcaseState.span = SPAN_TYPE.NONE;
}
setLayoutButtonActive(clickedElement);
renderVisuals();
}
// Set clicked columns button active
function setLayoutButtonActive(clickedElement) {
// CSS classes
const activeBtnClass = "active-columns-btn";
const layoutButton = "btn-layout";
const buttons = document.getElementsByClassName("btn-util");
// DOM objects
const activeBtnClassElements = $("." + activeBtnClass);
// Add the White background to the previous active layout button
activeBtnClassElements.addClass(layoutButton);
// Remove the selection from the previous active layout button
activeBtnClassElements.removeClass(activeBtnClass);
// Remove the white background to the currently selected button
$(clickedElement).removeClass(layoutButton);
// Add the active class to the current selected layout
$(clickedElement).addClass(activeBtnClass);
// Reset the aria-checked property
for (btn of buttons) {
btn.setAttribute("aria-checked", false);
}
// Apply the aria-checked property to the selected layout button
clickedElement.setAttribute("aria-checked", true);
}