preset-sdk-url-params
Version:
Frontend SDK for embedding Preset data analytics into your own application
2 lines • 8.25 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.presetSdk=t():e.presetSdk=t()}(globalThis,(()=>(()=>{"use strict";var __webpack_modules__={665:(module,__webpack_exports__,__webpack_require__)=>{var enterModule;__webpack_require__.d(__webpack_exports__,{J:()=>Switchboard}),module=__webpack_require__.hmd(module),enterModule="undefined"!=typeof reactHotLoaderGlobal?reactHotLoaderGlobal.enterModule:void 0,enterModule&&enterModule(module);var __signature__="undefined"!=typeof reactHotLoaderGlobal?reactHotLoaderGlobal.default.signature:function(e){return e},Actions;function isGet(e){return e.switchboardAction===Actions.GET}function isReply(e){return e.switchboardAction===Actions.REPLY}function isEmit(e){return e.switchboardAction===Actions.EMIT}function isError(e){return e.switchboardAction===Actions.ERROR}!function(e){e.GET="get",e.REPLY="reply",e.EMIT="emit",e.ERROR="error"}(Actions||(Actions={}));class Switchboard{constructor(e){this.port=void 0,this.name="",this.methods={},this.incrementor=1,this.debugMode=void 0,this.isInitialised=void 0,e&&this.init(e)}init(e){if(this.isInitialised)return void this.logError("already initialized");const{port:t,name:r="switchboard",debug:o=!1}=e;this.port=t,this.name=r,this.debugMode=o,t.addEventListener("message",(async e=>{this.log("message received",e);const t=e.data;if(isGet(t))this.port.postMessage(await this.getMethodResult(t));else if(isEmit(t)){const{method:e,args:r}=t,o=this.methods[e];o&&o(r)}})),this.isInitialised=!0}async getMethodResult(e){let{messageId:t,method:r,args:o}=e;const s=this.methods[r];if(null==s)return{switchboardAction:Actions.ERROR,messageId:t,error:`[${this.name}] Method "${r}" is not defined`};try{const e=await s(o);return{switchboardAction:Actions.REPLY,messageId:t,result:e}}catch(e){return this.logError(e),{switchboardAction:Actions.ERROR,messageId:t,error:`[${this.name}] Method "${r}" threw an error`}}}defineMethod(e,t){this.methods[e]=t}get(e,t){return void 0===t&&(t=void 0),new Promise(((r,o)=>{if(!this.isInitialised)return void o(new Error("Switchboard not initialised"));const s=this.getNewMessageId(),i=e=>{const t=e.data;if(t.messageId===s)if(this.port.removeEventListener("message",i),isReply(t))r(t.result);else{const e=isError(t)?t.error:"Unexpected response message";o(new Error(e))}};this.port.addEventListener("message",i),this.port.start();const a={switchboardAction:Actions.GET,method:e,messageId:s,args:t};this.port.postMessage(a)}))}emit(e,t){if(void 0===t&&(t=void 0),!this.isInitialised)return void this.logError("Switchboard not initialised");const r={switchboardAction:Actions.EMIT,method:e,args:t};this.port.postMessage(r)}start(){this.isInitialised?this.port.start():this.logError("Switchboard not initialised")}log(){if(this.debugMode){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];console.debug(`[${this.name}]`,...t)}}logError(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];console.error(`[${this.name}]`,...t)}getNewMessageId(){return`m_${this.name}_${this.incrementor++}`}__reactstandin__regenerateByEval(key,code){this[key]=eval(code)}}const _default=new Switchboard;var __WEBPACK_DEFAULT_EXPORT__=_default,reactHotLoader,leaveModule;reactHotLoader="undefined"!=typeof reactHotLoaderGlobal?reactHotLoaderGlobal.default:void 0,reactHotLoader&&(reactHotLoader.register(isGet,"isGet","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts"),reactHotLoader.register(isReply,"isReply","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts"),reactHotLoader.register(isEmit,"isEmit","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts"),reactHotLoader.register(isError,"isError","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts"),reactHotLoader.register(Switchboard,"Switchboard","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts"),reactHotLoader.register(_default,"default","/Users/ville/apple/apache-superset/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts")),leaveModule="undefined"!=typeof reactHotLoaderGlobal?reactHotLoaderGlobal.leaveModule:void 0,leaveModule&&leaveModule(module)}},__webpack_module_cache__={};function __webpack_require__(e){var t=__webpack_module_cache__[e];if(void 0!==t)return t.exports;var r=__webpack_module_cache__[e]={id:e,loaded:!1,exports:{}};return __webpack_modules__[e](r,r.exports,__webpack_require__),r.loaded=!0,r.exports}__webpack_require__.d=(e,t)=>{for(var r in t)__webpack_require__.o(t,r)&&!__webpack_require__.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},__webpack_require__.hmd=e=>((e=Object.create(e)).children||(e.children=[]),Object.defineProperty(e,"exports",{enumerable:!0,set:()=>{throw new Error("ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: "+e.id)}}),e),__webpack_require__.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),__webpack_require__.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};return(()=>{__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{_initComms:()=>d,embedDashboard:()=>n});var e=__webpack_require__(665);const t="__embedded_comms__",r={visible:"show_filters",expanded:"expand_filters"},o=5e3,s=1e4,i=3e5;function a(e){const t=JSON.parse(atob(e.split(".")[1]).toString()),r=new Date(/[^0-9\.]/g.test(t.exp)?t.exp:1e3*parseFloat(t.exp));return("Invalid Date"!==r.toString()?Math.max(s,r.getTime()-Date.now()):i)-o}async function n({id:e,supersetDomain:t,mountPoint:o,fetchGuestToken:s,dashboardUiConfig:i,debug:n=!1}){function c(...t){n&&console.debug(`[preset-frontend-sdk][dashboard ${e}]`,...t)}var l;function u(e){return e.map((([e,t])=>`${e}=${t}`)).join("&")}c("embedding"),(l=Element.prototype).hasOwnProperty("replaceChildren")||Object.defineProperty(l,"replaceChildren",{configurable:!0,enumerable:!0,value:function(...e){for(;this.firstChild;)this.removeChild(this.firstChild);e.forEach((e=>{this.appendChild("string"==typeof e?document.createTextNode(e):e)}))}}),t.endsWith("/")&&(t=t.slice(0,-1));const[p,_]=await Promise.all([s(),async function(){return new Promise((s=>{const a=document.createElement("iframe"),l=i?`uiConfig=${function(){let e=0;return i&&(i.hideTitle&&(e+=1),i.hideTab&&(e+=2),i.hideChartControls&&(e+=8)),e}()}`:"",p=i?.filters||{},_=Object.entries(p).map((([e,t])=>[r[e],t])).filter((([e])=>e)),h=_.length>0?u(_):null,b=p.urlParams?u(Object.entries(p.urlParams)):null;a.sandbox.add("allow-same-origin"),a.sandbox.add("allow-scripts"),a.sandbox.add("allow-presentation"),a.sandbox.add("allow-downloads"),a.sandbox.add("allow-top-navigation"),a.sandbox.add("allow-forms"),a.sandbox.add("allow-popups"),a.addEventListener("load",(()=>{const e=d(a.contentWindow,t,n);c("sent message channel to the iframe"),s(e)}));const m=[l,h,b].filter(Boolean).join("&"),f=m?`?${m}`:"";a.src=`${t}/embedded/${e}${f}`,o?.replaceChildren(a),c("placed the iframe")}))}()]);let h;return _.emit("guestToken",{guestToken:p}),c("sent guest token"),c("setTimeout for refreshGuestToken",a(p)),h=setTimeout((async function e(){c("(refreshGuestToken) refreshing guest token");try{const t=await s();c("(refreshGuestToken) got new guest token",t),_.emit("guestToken",{guestToken:t}),c("sent new guest token to ourPort"),c("setting new refreshGuestTokenInterval",a(t)),h=setTimeout(e,a(t)),c("set new refreshGuestTokenInterval")}catch(e){c("Error refreshing guest token",e)}}),a(p)),{getScrollSize:()=>_.get("getScrollSize"),unmount:function(){c("unmounting"),o?.replaceChildren(),clearInterval(h)}}}function d(r,o,s=!1){const i=new MessageChannel,a=i.port1,n=i.port2;return r.postMessage({type:t,handshake:"port transfer"},o,[n]),new e.J({port:a,name:"preset-frontend-sdk",debug:s})}})(),__webpack_exports__})()));
//# sourceMappingURL=index.js.map