@finapi/web-form
Version:
Library for integrating the finAPI Web Form
99 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unload = exports.load = void 0;
var spinner_1 = require("./spinner");
var apiServer_util_1 = require("./utils/apiServer.util");
var dom_util_1 = require("./utils/dom.util");
var webComponentServer_util_1 = require("./utils/webComponentServer.util");
var webComponentUrl_util_1 = require("./utils/webComponentUrl.util");
/**
* Injects Web Component into the target HTMLElement. When this function is
* called for the first time, it will also create a new script tag that will
* fetch the Web Component source js file. Before the new Web Component, any
* previous instance will be first automatically unloaded.
*
* @param {HTMLElement} target - a target element into which the Web Form will
* be appended as a child
* @param {WebFormProps} properties - Web Form properties
* @param {WebFormHandlers} handlers - Web Form event handlers
* @param {Document} ownerDocument - a document into which body a script tag
* that loads Web Component source is injected. If undefined, the
* globally-scoped document is selected.
*/
function load(target, properties, handlers, ownerDocument) {
var _a;
spinner_1.showSpinner(target);
var targetDocument = ownerDocument !== null && ownerDocument !== void 0 ? ownerDocument : document;
var webComponentServer = webComponentServer_util_1.getWebComponentServer({
sourceUrl: properties.sourceUrl,
targetUrl: properties.targetUrl,
targetEnvironment: properties.targetEnvironment,
});
var apiServer = apiServer_util_1.getApiServer({
targetUrl: properties.targetUrl,
targetEnvironment: properties.targetEnvironment,
});
var webComponentUrl = webComponentUrl_util_1.getWebComponentUrl({
webComponentServer: webComponentServer,
});
var isEntryPointLoaded = targetDocument.querySelector("#web-form-source") != null;
if (!isEntryPointLoaded) {
var entryPoint = targetDocument.createElement("script");
entryPoint.src = webComponentUrl;
entryPoint.id = "web-form-source";
entryPoint.async = false;
entryPoint.onerror = handlers === null || handlers === void 0 ? void 0 : handlers.onLoadError;
targetDocument.body.appendChild(entryPoint);
}
unload(targetDocument);
var webForm = dom_util_1.createWebFormElement(targetDocument);
var propertyPort = webForm.properties || webForm;
propertyPort.platform = (_a = properties.platform) !== null && _a !== void 0 ? _a : "EMBEDDED";
propertyPort.customerSupportUrl = properties.customerSupportUrl;
propertyPort.backToAppUrl = properties.backToAppUrl;
propertyPort.token = properties.token;
propertyPort.layoutConfig = properties.layoutConfig;
propertyPort.targetUrl = apiServer;
propertyPort.noMargins = properties.noMargins;
propertyPort.language = properties.language;
propertyPort.redirectUrl = properties.redirectUrl;
propertyPort.errorRedirectUrl = properties.errorRedirectUrl;
propertyPort.abortRedirectUrl = properties.abortRedirectUrl;
propertyPort.isInvalidUrl = properties.isInvalidUrl;
propertyPort.infoParam = properties.infoParam;
webForm.addEventListener("loaded", function () { return spinner_1.removeSpinner(targetDocument); });
if (handlers) {
if (handlers.onComplete) {
webForm.addEventListener("complete", handlers.onComplete);
}
if (handlers.onFail) {
webForm.addEventListener("fail", handlers.onFail);
}
if (handlers.onAbort) {
webForm.addEventListener("abort", handlers.onAbort);
}
if (handlers.onLoaded) {
webForm.addEventListener("loaded", handlers.onLoaded);
}
if (handlers.onBlindRedirectCallback) {
webForm.addEventListener("blindRedirectCallback", handlers.onBlindRedirectCallback);
}
}
target.appendChild(webForm);
}
exports.load = load;
/**
* Removes Web Component from the document.
*
* @param {Document} ownerDocument - a document in which the web component was
* injected
*/
function unload(ownerDocument) {
var targetDocument = ownerDocument || document;
var webForm = dom_util_1.getWebFormElement(targetDocument);
if (webForm) {
dom_util_1.removeWebFormElement(webForm);
}
}
exports.unload = unload;
//# sourceMappingURL=loader.js.map