trimble-connect-workspace-api
Version:
Trimble Connect Workspace API
2 lines (1 loc) • 8.07 kB
JavaScript
const e={postMessage:(e,n)=>{const t=JSON.stringify(e);connectWsApiIntegrator.postMessage(t)}},n={postMessage:(e,n)=>{window.chrome.webview.postMessage(e)}},t={postMessage:(e,n)=>{window.webkit.messageHandlers.webwindowinterop.postMessage(e)}},i={postMessage:(e,n)=>{const t=JSON.stringify(e);window.hybridWebViewHost.sendMessage(t)}},r=()=>crypto.randomUUID(),o=()=>{const e=window;return!!(e&&e.chrome&&e.chrome.webview)},s=()=>{const e=window;return!(!e||!("CefSharp"in e))},a=async()=>(window.connectWsApiIntegrator||await CefSharp.BindObjectAsync("connectWsApiIntegrator"),e),c=()=>n,u=async e=>{let n;return n=e===window?s()?await a():o()?c():e:e,n},f="Trimble.dispatcher.v1",d={},p={};let l=1;function w(e){const n=m();return p[n]=e,()=>delete p[n]}function g(e,n,t,i,r=3e5){return new Promise(((o,s)=>{const a=m(),c={scope:f,type:"request",id:a,api:t,args:i};let u;r>0&&(u=setTimeout((()=>{delete d[a],s(new Error("dispatcher.ts | sendRequest(): Operation timed out."))}),r)),d[a]=e=>{delete d[a],u&&clearTimeout(u),e.error?s(e.error):o(e.result)},e.postMessage(c,n)}))}function h(e,n,t,i){const r={scope:f,type:"event",event:t,data:i};u(e).then((e=>{e.postMessage(r,n)})).catch((e=>{console.error("sendEvent() call failed",e)}))}async function y(e){const n="null"===e.origin?"*":e.origin,t=e.data;if(function(e){return b(e)&&"event"===e.type}(t))for(const i in p){if(!p.hasOwnProperty(i))continue;const r=p[i].event;if(r){r(e.source,n,t.event,t.data)}}else if(function(e){return b(e)&&"request"===e.type}(t)){const i=await u(e.source);let r;for(const e in p){if(!p.hasOwnProperty(e))continue;const o=p[e].request;if(o&&!r){const e=o(i,n,t.api,t.args);if(void 0!==e)try{r={scope:f,type:"response",id:t.id,api:t.api,result:await e}}catch(e){r={scope:f,type:"response",id:t.id,api:t.api,error:String(e)}}}}r||(r={scope:f,type:"response",id:t.id,api:t.api,error:"Not supported"}),i.postMessage(r,n)}else if(function(e){return b(e)&&"response"===e.type}(t)){const e=d[t.id];e&&e(t)}}function b(e){return!!e&&e.scope===f}function m(){return l++}const v={},M=[];let S;const k=new Promise((e=>{S=()=>{S=()=>{},e()}}));let O;function P(e,n,r=1e4){async function u(e,n){const t=await g(e,n,".connect_api_client_v1",[],0);if(t&&"object"==typeof t){return R(t,((t,i,o)=>{if(".api_function_v1"===i){const i=o?o+"."+String(t):String(t);return(...t)=>g(e,n,i,t,r)}return i}))}throw new Error("Failed to connect")}function f(e){if(!e)return"*";try{return new URL(e).origin}catch(e){return"*"}}if(n&&(O=w({event:(e,t,i,r)=>n(i,r)})),e===window){if(0!==Object.keys(v).length)return Promise.resolve(v);if(s())return new Promise((async e=>{const n=await a();e(await u(n,"*"))}));if(o()){return u(c(),"*")}if(function(){const e=window;return e&&e.webkit&&e.webkit.messageHandlers&&e.webkit.messageHandlers.webwindowinterop}()){return u(t,"*")}if(function(){const e=window;return e&&e.hybridWebViewHost}()){return u(i,"*")}return Promise.resolve(v)}if((d=e)&&"function"==typeof d.postMessage)return u(e,"*");if((e=>e&&"object"==typeof e.contentWindow)(e)){const n=[];return n.push(new Promise(((n,t)=>{const i=async()=>{e.removeEventListener("load",i),e.contentWindow?n(await u(e.contentWindow,f(e.src))):t(new Error("Cannot access the target content window"))};e.addEventListener("load",i)}))),e.contentWindow&&n.push(u(e.contentWindow,f(e.src))),Promise.race(n)}return Promise.reject(new Error("Target must be a window or an iframe"));var d}function _(e){if("object"!=typeof e)throw new Error("Api must be an object");C(v,e),S()}function j(e){const n=M.find((n=>n.dispatcher===e.dispatcher));if(n)return n.identifier=e.identifier||n.identifier,n.origin=e.origin||n.origin,n;if(e.dispatcher){if(e.identifier){if(M.find((n=>n.identifier===e.identifier)))throw new Error(`[Workspace API] Client with identifier '${e.identifier}' already registered!`)}else e.identifier=r();return M.push(e),e}}function V(e){const n=M.findIndex((n=>n.dispatcher===e.dispatcher));-1!==n&&M.splice(n,1)}const E=(e,n,t,i)=>{if(t.origin){let r;r=t.identifier&&i&&t.identifier===i?Object.assign(Object.assign({},n),{origin:{isSelf:!0}}):n,h(t.dispatcher,t.origin,e,r)}};function H(e,n,t,i){if(t&&t.identifier){const r=M.find((e=>e.identifier===t.identifier));r&&E(e,n,r,i)}else for(const t of M)E(e,n,t,i)}function T(){try{return window.self!==window.top||s()||o()}catch(e){return!0}}function W(e,n){return"function"==typeof e&&"function"==typeof n&&e.name===n.name||("string"==typeof e&&"string"==typeof n||"number"==typeof e&&"number"==typeof n)}function C(e,n){for(const t in n){const i=n[t];if(i)if(t in e){const n=e[t];if("object"==typeof i&&"object"==typeof n)C(n,i);else{if(!W(i,n))throw new Error(`Cannot merge ${t} (${n} and ${i})`);e[t]=i}}else switch(typeof i){case"object":{const n={};C(n,i),e[t]=n;break}default:e[t]=i}}}function R(e,n,t){const i={};for(const r in e){const o=e[r];if((void 0!==t||"onConnect"!==r&&"onRequest"!==r)&&o)if("object"==typeof o){const e=t&&t+"."+String(r)||String(r);i[r]=R(o,n,e)}else i[r]=n(r,e[r],t)}return i}var A,L,q,D;w({request:(e,n,t,i)=>{if(".connect_api_client_v1"===t)return k.then((()=>{let t=M.find((n=>n.dispatcher===e));if(t)t.origin=n;else{if(!e)return;t={dispatcher:e,origin:n,identifier:r()},M.push(t)}var i;(i=v)&&"function"==typeof i.onConnect&&v.onConnect(t);return R(v,((e,n)=>"function"==typeof n?".api_function_v1":n))}));{const r=M.find((t=>t.dispatcher===e&&t.origin===n));if(r){if((o=v)&&"function"==typeof o.onRequest){const e=v.onRequest(r,t,i);if(void 0!==e)return e}const e=function(e,n){const t=n.split(".");let i=e;for(const e of t)i="object"==typeof i&&i&&e in i?i[e]:void 0;return i}(v,t);if("function"==typeof e){const n=e.apply(void 0,i);return void 0===n?Promise.resolve(n):n}return Promise.resolve(e)}return}var o}}),function(e){e[e.LengthMeasure=0]="LengthMeasure",e[e.AreaMeasure=1]="AreaMeasure",e[e.VolumeMeasure=2]="VolumeMeasure",e[e.MassMeasure=3]="MassMeasure",e[e.AngleMeasure=4]="AngleMeasure",e[e.StringValue=5]="StringValue",e[e.IntValue=6]="IntValue",e[e.DoubleValue=7]="DoubleValue",e[e.DateTime=8]="DateTime",e[e.Logical=9]="Logical",e[e.Boolean=10]="Boolean"}(A||(A={})),function(e){e[e.Selected=1]="Selected",e[e.Hidden=4]="Hidden",e[e.SelectedHidden=5]="SelectedHidden",e[e.Visible=6]="Visible",e[e.SelectedVisible=7]="SelectedVisible",e[e.Highlighted=8]="Highlighted"}(L||(L={})),function(e){e.Project="project",e.Viewer="3dviewer"}(q||(q={})),function(e){e.Models="models",e.Layers="layers",e.Attachments="attachments",e.ToDos="todos",e.Views="views",e.ClashSets="clashes",e.Organizer="organizer",e.DataTable="contentbrowser",e.Topics="topics",e.LiveCollaboration="livecollaboration",e.StatusSharing="statussharing",e.Connect2Fab="connect2fab",e.QrMarkers="qrmarkers",e.RealityCapture="realitycapture"}(D||(D={}));const N=["reset","selection","areaSelection","measurement","pointMarkup","cloudMarkup","arrowMarkup","lineMarkup","textMarkup","freelineMarkup","clipPlane","verticalClipPlane","picking"],I=["edge","point","surface"];var U,$,x;function B(e,n,t){return window.removeEventListener("message",G),window.addEventListener("message",G),O&&O(),P(e,n,t)}function z(e){return _(e)}function F(e,n,t,i){h(e,n,t,i)}function G(e){return y(e)}function J(e="prod"){return`https://${{int:"web.int",qa:"web.qa",stage:"web.stage",prod:"web"}[e]}.connect.trimble.com?isEmbedded=true`}!function(e){e.LookAround="look_around",e.Pan="pan",e.Panorama="panorama",e.Rotate="rotate",e.Walk="walk"}(U||(U={})),function(e){e[e.Guid=0]="Guid",e[e.String=1]="String",e[e.SpatialHash=2]="SpatialHash",e[e.DwgHandle=3]="DwgHandle",e[e.None=4]="None"}($||($={})),function(e){e[e.SORT_NONE=0]="SORT_NONE",e[e.SORT_UP=1]="SORT_UP",e[e.SORT_DOWN=-1]="SORT_DOWN"}(x||(x={}));export{U as CameraMode,q as ExtensionType,$ as IdentifierType,A as PropertyType,x as SortDirection,N as TOOLS,I as TOOL_SNAP_TYPES,D as TabPanelId,L as ViewEntityStates,B as connect,G as dispatcherEventListener,z as expose,J as getConnectEmbedUrl,T as isApplicationEmbedded,j as preregister,V as removeClient,F as sendEvent,H as sendEventToAllClients};