Last active
April 5, 2022 05:46
-
-
Save nsdevaraj/0b9e372094f0fad5d756cfb19c8f915e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
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
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
5/ Update function