Skip to content

Commit 54bebb1

Browse files
author
Matt Mazzola
committed
Add getUnique id function on embed which gets uniqueId from config, then attribute, or generates unique string. This allows developers to find embed instance using service by known value with contextual meaning unrelated to report which is dynamic.
1 parent 493f3b1 commit 54bebb1

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/embed.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare global {
2424
export interface IEmbedConfiguration {
2525
type?: string;
2626
id?: string;
27+
uniqueId?: string;
2728
embedUrl?: string;
2829
accessToken?: string;
2930
settings?: models.ISettings;
@@ -43,6 +44,7 @@ export interface IInternalEventHandler<T> {
4344
export abstract class Embed {
4445
public static accessTokenAttribute = 'powerbi-access-token';
4546
public static embedUrlAttribute = 'powerbi-embed-url';
47+
public static nameAttribute = 'powerbi-name';
4648
public static typeAttribute = 'powerbi-type';
4749
public static type: string;
4850

@@ -72,7 +74,7 @@ export abstract class Embed {
7274
this.config.accessToken = this.getAccessToken(service.accessToken);
7375
this.config.embedUrl = this.getEmbedUrl();
7476
this.config.id = this.getId();
75-
this.config.uniqueId = utils.createRandomString();
77+
this.config.uniqueId = this.getUniqueId();
7678

7779
const iframeHtml = `<iframe style="width:100%;height:100%;" src="${this.config.embedUrl}" scrolling="no" allowfullscreen="true"></iframe>`;
7880

@@ -188,6 +190,14 @@ export abstract class Embed {
188190
return embedUrl;
189191
}
190192

193+
/**
194+
* Get unique id from first available location: options, attribute.
195+
* If neither is provided generate unique string.
196+
*/
197+
private getUniqueId(): string {
198+
return this.config.uniqueId || this.element.getAttribute(Embed.nameAttribute) || utils.createRandomString();
199+
}
200+
191201
/**
192202
* Get report id from first available location: options, attribute.
193203
*/

src/service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ export class Service {
219219
return powerBiElement.powerBiEmbed;
220220
}
221221

222+
/**
223+
* Find embed instance by name / unique id provided.
224+
*/
225+
find(uniqueId: string): Report | Tile {
226+
return utils.find(x => x.config.uniqueId === uniqueId, this.embeds);
227+
}
228+
222229
/**
223230
* Given an html element which has component embedded within it, remove the component from list of embeds, remove association with component, and remove the iframe.
224231
*/

test/test.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,48 @@ describe('service', function () {
171171
expect(attemptToEmbed).toThrowError();
172172
});
173173

174+
it('should get uqiqueId from config first', function () {
175+
// Arrange
176+
const testUniqueId = 'fakeUniqueId';
177+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed`;
178+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id="abc123" powerbi-name="differentUniqueId"></div>`)
179+
.appendTo('#powerbi-fixture');
180+
181+
// Act
182+
const report = powerbi.embed($reportContainer[0], { uniqueId: testUniqueId });
183+
184+
// Assert
185+
expect(report.config.uniqueId).toEqual(testUniqueId);
186+
});
187+
188+
it('should get uqiqueId from name attribute if uniqueId is not specified in config', function () {
189+
// Arrange
190+
const testUniqueId = 'fakeUniqueId';
191+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed`;
192+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id="abc123" powerbi-name="${testUniqueId}"></div>`)
193+
.appendTo('#powerbi-fixture');
194+
195+
// Act
196+
const report = powerbi.embed($reportContainer[0]);
197+
198+
// Assert
199+
expect(report.config.uniqueId).toEqual(testUniqueId);
200+
});
201+
202+
it('should generate uqiqueId if uniqueId is not specified in config or attribute', function () {
203+
// Arrange
204+
const testUniqueId = 'fakeUniqueId';
205+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed`;
206+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report" powerbi-report-id="abc123"></div>`)
207+
.appendTo('#powerbi-fixture');
208+
209+
// Act
210+
const report = powerbi.embed($reportContainer[0]);
211+
212+
// Assert
213+
expect(report.config.uniqueId).toEqual(jasmine.any(String));
214+
});
215+
174216
it('if component is already embedded in element re-use the existing component by calling load with the new information', function () {
175217
// Arrange
176218
const $element = $('<div powerbi-embed-url="https://app.powerbi.com/reportEmbed?reportId=ABC123" powerbi-type="report"></div>')
@@ -388,6 +430,29 @@ describe('service', function () {
388430
// Assert
389431
expect($element.html()).toEqual('');
390432
});
433+
434+
it('removes the powerbi instance from the list of embeds', function () {
435+
// Arrange
436+
const $element = $('<div></div>');
437+
const testEmbedConfig = {
438+
type: 'report',
439+
embedUrl: 'fakeUrl',
440+
id: 'fakeReportId',
441+
accessToken: 'fakeToken',
442+
uniqueId: 'fakeUniqeId'
443+
};
444+
powerbi.embed($element.get(0), testEmbedConfig);
445+
446+
// Act
447+
const report = powerbi.find(testEmbedConfig.uniqueId);
448+
expect(report).toBeDefined();
449+
450+
powerbi.reset($element.get(0));
451+
452+
// Assert
453+
const report2 = powerbi.find(testEmbedConfig.uniqueId);
454+
expect(report2).toBeUndefined();
455+
});
391456
});
392457
});
393458

0 commit comments

Comments
 (0)