Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Last active April 5, 2022 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsdevaraj/0b9e372094f0fad5d756cfb19c8f915e to your computer and use it in GitHub Desktop.
Save nsdevaraj/0b9e372094f0fad5d756cfb19c8f915e to your computer and use it in GitHub Desktop.
import powerbi from "powerbi-visuals-api";
import IVisualLicenseManager = powerbi.extensibility.IVisualLicenseManager;
import LicenseNotificationType = powerbi.LicenseNotificationType;
import LicenseInfoResult = powerbi.extensibility.visual.LicenseInfoResult;
interface IDisplayedNotification {
isIconDisplayed: boolean;
isBlockerDisplayed: boolean;
isEnvironmentBlockerDisplayed: boolean;
isFeatureBlockedDisplayed: boolean;
}
export default class LicenseAPI {
public licenseManager: IVisualLicenseManager;
public hasServicePlans: boolean | undefined = undefined;
public isLicenseRequestBusy: boolean = false;
private displayedNotification: IDisplayedNotification;
public isNotificationDisplayed(): boolean {
return this.displayedNotification.isIconDisplayed ||
this.displayedNotification.isBlockerDisplayed ||
this.displayedNotification.isFeatureBlockedDisplayed ||
this.displayedNotification.isEnvironmentBlockerDisplayed;
}
public getServicePlans(): void {
this.licenseManager.getAvailableServicePlans().then((result: LicenseInfoResult) => {
console.log(result);
this.hasServicePlans = !!(result.plans && result.plans.length && result.plans.some((plan) => plan.state === powerbi.ServicePlanState.Active || plan.state === powerbi.ServicePlanState.Warning));
console.log(JSON.stringify(result));
if (!this.hasServicePlans) {
console.log("No Service Plan")
}
}).catch((err) => {
this.hasServicePlans = undefined;
console.log(err);
}).finally(() => {
// this.btnServicePlans.style.backgroundColor = this.hasServicePlans === undefined ? 'blue' : (this.hasServicePlans === true ? 'green' : 'red');
});
}
public displayLicenseNotification(type: LicenseNotificationType): void {
if (!this.isLicenseRequestBusy) {
this.isLicenseRequestBusy = true;
this.licenseManager.notifyLicenseRequired(type).then((value) => {
if (value) {
console.log('Notification is displayed', value);
this.updateDisplayedNotification(type, this.displayedNotification.isFeatureBlockedDisplayed);
} else {
// It might happen for several scenarios, for example when the visual try to raise a General notification in edit mode or in an unsupported environment.
console.log('Notification is not displayed', value);
}
}).catch((err) => {
console.log('ERROR', err);
}).finally(() => {
this.isLicenseRequestBusy = false;
});
} else {
console.log('license request is being handled1');
}
}
public displayFeatureBlockedNotification(tooltip: string): void {
if (!this.isLicenseRequestBusy) {
this.isLicenseRequestBusy = true;
this.licenseManager.notifyFeatureBlocked(tooltip).then((value) => {
if (value) {
// console.log('Feature Notification is displayed', value);
this.displayedNotification.isFeatureBlockedDisplayed = true;
} else {
//console.log('Feature Notification is not displayed', value);
}
}).catch((err) => {
console.log('ERROR', err);
}).finally(() => {
this.isLicenseRequestBusy = false;
});
this.isLicenseRequestBusy = false;
}
}
public clearNotification(): void {
this.isLicenseRequestBusy = false;
if (!this.isLicenseRequestBusy) {
this.isLicenseRequestBusy = true;
this.licenseManager.clearLicenseNotification().then((value) => {
if (value) {
console.log('Notification is removed', value);
this.updateDisplayedNotification(undefined /* notification */, false /* isFeatureBlcoked */);
} else {
console.log('Notification is not removed', value);
}
}).catch((err) => {
console.log('ERROR', err);
}).finally(() => {
this.isLicenseRequestBusy = false;
});
} else {
console.log('license request is being handled');
}
}
public updateDisplayedNotification(notification: LicenseNotificationType | undefined, isFeatureBlocked: boolean): void {
this.displayedNotification = {
isIconDisplayed: notification === LicenseNotificationType.General,
isBlockerDisplayed: notification === LicenseNotificationType.VisualIsBlocked,
isEnvironmentBlockerDisplayed: notification === LicenseNotificationType.UnsupportedEnv,
isFeatureBlockedDisplayed: isFeatureBlocked && !(notification === LicenseNotificationType.UnsupportedEnv || notification === LicenseNotificationType.VisualIsBlocked)
}
}
}
export const license = new LicenseAPI();
@nsdevaraj
Copy link
Author

1/ URL Param

?useCVLicenses=1

2/ Package.json and pbiviz.json - 4.4.0

3/ src visual modifications

import * as LicenseAPI from "./licenseAPI";

4/ Constructor

    LicenseAPI.license.licenseManager = options.host.licenseManager;

    this.getServicePlans();

5/ Update function

public update(options: VisualUpdateOptions): void {
    if (LicenseAPI.license.hasServicePlans === false && !LicenseAPI.license.isNotificationDisplayed()) {
        console.log('update - user does not have service plans, but notification is not displayed');
        LicenseAPI.license.displayLicenseNotification(powerbi.LicenseNotificationType.VisualIsBlocked)     
    }
    if (!LicenseAPI.license.hasServicePlans && options.viewMode== powerbi.ViewMode.View) {
        console.log("Block Service Plan")
        LicenseAPI.license.displayLicenseNotification(powerbi.LicenseNotificationType.VisualIsBlocked)     
    }  

@nsdevaraj
Copy link
Author

On PRO feature action trigger:
LicenseAPI.license.displayFeatureBlockedNotification(tooltipString);

Off PRO feature action trigger:
LicenseAPI.license.clearNotification()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment