UNPKG

@discoveryjs/discovery

Version:

Frontend framework for rapid data (JSON) analysis, shareable serverless reports and dashboards

128 lines (127 loc) 3.64 kB
import { ObjectMarkerManager } from "../core/object-marker.js"; import { hasOwn } from "../core/utils/object-utils.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(helpers) { joraSetup = jora.setup({ methods: queryCustomMethods = { ...queryCustomMethods, ...helpers } }); }, query(query, ...args) { return host.queryFn.call({ queryFnFromString: joraSetup }, query)(...args); } }; let queryCustomMethods = { query: host.query.bind(host), overrideProps, 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 joraSetup = jora.setup({ methods: queryCustomMethods }); return { contextApi, after(host2) { Object.assign(host2, { objectMarkers, linkResolvers, annotations, queryFnFromString: joraSetup }); } }; 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 overrideProps(current, props = this.context.props) { if (!props) { return current; } const result = { ...current }; for (const key of Object.keys(result)) { if (hasOwn(props, key)) { result[key] = props[key]; } } return result; } 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; } }