@discoveryjs/discovery
Version:
Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
137 lines (136 loc) • 3.96 kB
JavaScript
import { ObjectMarkerManager } from "../core/object-marker.js";
import modelCommonJoraMethods from "./model-common-jora-methods.js";
import modelCommonJoraAssertions from "./model-common-jora-assertions.js";
import jora from "jora";
export function createLegacyExtensionApi(host, options) {
const objectMarkers = new ObjectMarkerManager(host.logger);
const linkResolvers = [];
const annotations = [];
const contextApi = {
setWorkTitle: options?.setPrepareWorkTitle || (() => Promise.resolve()),
rejectData(message, renderContent) {
throw Object.assign(new Error(message), { renderContent });
},
defineObjectMarker,
lookupObjectMarker,
lookupObjectMarkerAll,
addValueAnnotation,
addQueryHelpers(methods) {
host.logger.warn("addQueryHelpers() is deprecated, use addQueryMethods() instead");
return addQueryMethods(methods);
},
addQueryMethods,
addQueryAssertions,
query(query, ...args) {
return host.queryFn.call({ queryFnFromString: joraSetup }, query)(...args);
}
};
let queryCustomMethods = {
...modelCommonJoraMethods,
query: host.query.bind(host),
pageLink: (pageRef, pageId, pageParams, pageAnchor) => host.encodePageHash(pageId, pageRef, pageParams, pageAnchor),
marker: lookupObjectMarker,
markerAll: lookupObjectMarkerAll,
callAction,
actionHandler: (actionName, ...args) => host.action.has(actionName) ? () => callAction(actionName, ...args) : void 0
};
let queryCustomAssertions = {
...modelCommonJoraAssertions
};
let joraSetup = jora.setup({
methods: queryCustomMethods,
assertions: queryCustomAssertions
});
return {
contextApi,
after(host2) {
Object.assign(host2, {
objectMarkers,
linkResolvers,
annotations,
queryFnFromString: joraSetup
});
}
};
function addQueryMethods(methods) {
joraSetup = jora.setup({
methods: queryCustomMethods = {
...queryCustomMethods,
...methods
}
});
}
function addQueryAssertions(assertions) {
joraSetup = jora.setup({
assertions: queryCustomAssertions = {
...queryCustomAssertions,
...assertions
}
});
}
function defineObjectMarker(name, options2) {
const { page, mark, lookup } = objectMarkers.define(name, options2) || {};
if (!lookup) {
return () => {
};
}
if (page !== null) {
if (!host.page?.isDefined(page)) {
host.logger.error(`Page reference "${page}" doesn't exist`);
return () => {
};
}
linkResolvers.push((value) => {
const marker = lookup(value);
return marker && {
type: page,
text: marker.title,
href: marker.href,
entity: marker.object
};
});
}
addValueAnnotation((value, context) => {
const marker = lookup(value, true);
if (marker !== null && marker.object !== context.host) {
return {
place: "before",
style: "badge",
text: name,
href: marker.href
};
}
});
return mark;
}
function lookupObjectMarker(value, type) {
return objectMarkers.lookup(value, type);
}
function lookupObjectMarkerAll(value) {
return objectMarkers.lookupAll(value);
}
function addValueAnnotation(query, options2 = false) {
if (typeof options2 === "boolean") {
options2 = {
debug: options2
};
}
annotations.push({
query,
...options2
});
}
function callAction(actionName, ...args) {
const lastArg = args[args.length - 1];
let callback = null;
if (typeof lastArg === "function") {
callback = lastArg;
args.pop();
}
const ret = host.action.call(actionName, ...args);
if (ret && callback && typeof ret.then === "function") {
return ret.then(callback);
}
return callback ? callback(ret) : ret;
}
}