@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
119 lines • 5.43 kB
JavaScript
const licenseWatermarkId = "cvLicenseWatermark";
export class LicenseHandler {
constructor(_viewer, _apiClient) {
this._viewer = _viewer;
this._apiClient = _apiClient;
this._viewer.add_onResize(() => {
this._updatePosition();
if (this._expireLicenseMessageElement != null)
this._updateHeightExpireLicenseMessage();
});
}
async checkLicense() {
if (this._license == null)
this._license = await this._apiClient.getLicenseAsync();
if (this._license.isEnterprise && !this._license.isTrial) {
if (this._getLeftDays() <= 30) {
this._expireLicenseMessageElement = this._createExpireLicenseMessage();
let form = document.getElementById('editorWrapper').parentElement;
form.insertBefore(this._expireLicenseMessageElement, form.childNodes[0]);
this._updateHeightExpireLicenseMessage();
}
return;
}
if (this._isFullLicense())
return;
this._watermarkElement = this._createElement();
this._viewer.element.parentElement.appendChild(this._watermarkElement);
this._updatePosition();
}
_updatePosition() {
if (this._watermarkElement == null)
return;
const offsetWidth = this._viewer.element.clientWidth;
const offsetHeight = this._viewer.element.clientHeight;
const watermarkWidth = this._watermarkElement.clientWidth;
const watermarkHeight = this._watermarkElement.clientHeight;
const left = offsetWidth - watermarkWidth - LicenseHandler._margin;
const top = offsetHeight - watermarkHeight - LicenseHandler._margin;
this._watermarkElement.style.left = `${left}px`;
this._watermarkElement.style.top = `${top}px`;
}
_isFullLicense() {
return !this._license.isTrial && !this._license.isDeveloper;
}
_createExpireLicenseMessage() {
const div = document.createElement("div");
div.style.background = "rgba(244, 67, 54, 0.1)";
const text = document.createElement("p");
text.style.color = "#F44336";
text.style.fontSize = "14px";
text.style.margin = "0";
text.style.lineHeight = "20px";
text.style.padding = "8px 16px";
text.textContent = `The license expires soon. It won't affect your current design, but please contact the website administrator or customer service department and let them know that you see this message.`;
div.appendChild(text);
return div;
}
_updateHeightExpireLicenseMessage() {
let expireMessage = this._expireLicenseMessageElement;
if (expireMessage == null)
return;
let expireMessageHeight = expireMessage.clientHeight;
document.getElementById("editorWrapper").style.height = `calc(100% - ${expireMessageHeight}px)`;
if (expireMessageHeight > 36) // text expire message license is inline
expireMessage.style.textAlign = "left";
else
expireMessage.style.textAlign = "center";
}
_createElement() {
const div = document.createElement("div");
div.id = licenseWatermarkId;
div.style.width = "auto";
div.style.height = "auto";
div.style.whiteSpace = "nowrap";
div.style.position = "absolute";
div.style.borderRadius = "4px";
div.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
div.style.zIndex = "1";
div.style.pointerEvents = "none";
const textDiv = document.createElement("div");
textDiv.style.padding = "8px";
textDiv.style.display = "flex";
textDiv.style.flexDirection = "column";
const trialNote = document.createElement("span");
trialNote.style.fontFamily = "Roboto";
trialNote.style.fontStyle = "normal";
trialNote.style.fontWeight = "500";
trialNote.style.fontSize = "14px";
trialNote.style.lineHeight = "15px";
trialNote.style.color = "#333333";
const expirationDate = document.createElement("span");
expirationDate.style.fontFamily = "Roboto";
expirationDate.style.fontStyle = "normal";
expirationDate.style.fontSize = "12px";
expirationDate.style.lineHeight = "18px";
expirationDate.style.color = "#333333";
trialNote.textContent = `This is a ${this._getLicenseType()} edition`;
const date = this._license.expirationDate.toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
expirationDate.textContent = `Expires on ${date} (${this._getLeftDays()} days left)`;
textDiv.appendChild(trialNote);
textDiv.appendChild(expirationDate);
div.appendChild(textDiv);
return div;
}
_getLicenseType() {
if (this._license.isTrial)
return "Trial";
if (this._license.isDeveloper)
return "Developer";
return "";
}
_getLeftDays() {
const diffTime = Math.max(0, this._license.expirationDate.getTime() - Date.now());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
}
// License element size in pixel
LicenseHandler._margin = 12;
//# sourceMappingURL=License.js.map