@proofkit/webviewer
Version:
A utility to fetch data from FileMaker webviewer
85 lines (84 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const uuid = require("uuid");
let webViewerName;
function setWebViewerName(name) {
webViewerName = name;
}
const globalSettings = {
/**
*
* set the name of the WebViewer to use for all fetches
* @param name the Layout Object Name of the FileMaker WebViewer to callback too
*/
setWebViewerName
};
function fmFetch(scriptName, data, callback) {
if (callback) {
return _execScript(scriptName, data, callback);
} else {
return new Promise((resolve) => {
_execScript(scriptName, data, (result) => {
resolve(result);
});
});
}
}
const cbs = {};
if (typeof window !== "undefined") {
window.handleFmWVFetchCallback = function(data, fetchId) {
setTimeout(() => {
const cb = cbs[fetchId];
delete cbs[fetchId];
if (!cb) {
console.error("Callback is missing for fetchId: " + fetchId);
return false;
}
try {
data = JSON.parse(data);
} catch (e) {
}
cb(data);
}, 1);
return true;
};
}
function _execScript(scriptName, data, cb) {
const fetchId = uuid.v4();
cbs[fetchId] = cb;
const param = {
data,
callback: { fetchId, fn: "handleFmWVFetchCallback", webViewerName }
};
callFMScript(scriptName, param);
}
function callFMScript(scriptName, data, option) {
let params = data;
try {
if (typeof data !== "string") params = JSON.stringify(data);
} catch (e) {
}
if (!window.FileMaker) {
throw new Error(
`Could not call script, '${scriptName}'. 'window.FileMaker' was not available at the time this function was called.`
);
}
if (option) {
window.FileMaker.PerformScriptWithOption(scriptName, params, option);
} else {
window.FileMaker.PerformScript(scriptName, params);
}
}
const FMScriptOption = {
CONTINUE: "0",
HALT: "1",
EXIT: "2",
RESUME: "3",
PAUSE: "4",
SUSPEND_AND_RESUME: "5"
};
exports.FMScriptOption = FMScriptOption;
exports.callFMScript = callFMScript;
exports.fmFetch = fmFetch;
exports.globalSettings = globalSettings;
//# sourceMappingURL=main.cjs.map