@onirix/embed-sdk
Version:
Onirix helper library for iframe event communication.
2 lines (1 loc) • 15.5 kB
JavaScript
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},t.apply(this,arguments)}let e;const n=new Uint8Array(16);function i(){if(!e&&(e="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!e))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return e(n)}const r=[];for(let t=0;t<256;++t)r.push((t+256).toString(16).slice(1));var o={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function E(t,e,n){if(o.randomUUID&&!e&&!t)return o.randomUUID();const E=(t=t||{}).random||(t.rng||i)();if(E[6]=15&E[6]|64,E[8]=63&E[8]|128,e){n=n||0;for(let t=0;t<16;++t)e[n+t]=E[t];return e}return function(t,e=0){return r[t[e+0]]+r[t[e+1]]+r[t[e+2]]+r[t[e+3]]+"-"+r[t[e+4]]+r[t[e+5]]+"-"+r[t[e+6]]+r[t[e+7]]+"-"+r[t[e+8]]+r[t[e+9]]+"-"+r[t[e+10]]+r[t[e+11]]+r[t[e+12]]+r[t[e+13]]+r[t[e+14]]+r[t[e+15]]}(E)}var s={READY:"READY",SCENE_LOAD_START:"SCENE_LOAD_START",SCENE_LOAD_END:"SCENE_LOAD_END",SCENE_LOST:"SCENE_LOST",SCENE_RADIUS_ENTER:"SCENE_RADIUS_ENTER",SCENE_RADIUS_EXIT:"SCENE_RADIUS_EXIT",ELEMENT_CLICK:"ELEMENT_CLICK",OPEN_MARKERS_PANEL:"OPEN_MARKERS_PANEL",CLOSE_MARKERS_PANEL:"CLOSE_MARKERS_PANEL",MAP_READY:"MAP_READY",SCENE_LOCATION_CLICK:"SCENE_LOCATION_CLICK",BACK_TO_MAP:"BACK_TO_MAP",ON_TOUCH_START:"ON_TOUCH_START",ON_TOUCH_END:"ON_TOUCH_END",ON_POSE:"ON_POSE",SCENE_CLICK:"SCENE_CLICK",ON_SCENE_TOUCH_START:"ON_SCENE_TOUCH_START",ON_SCENE_TOUCH_END:"ON_SCENE_TOUCH_END",SESSION_ENDED:"SESSION_ENDED",SCENE_DETECTED:"SCENE_DETECTED",LAZY_LOAD_END:"LAZY_LOAD_END",NAVIGATION_STARTED:"NAVIGATION_STARTED",NAVIGATION_COMPLETED:"NAVIGATION_COMPLETED",NAVIGATION_CANCELLED:"NAVIGATION_CANCELLED"},a={ENABLE_ELEMENT:"ENABLE_ELEMENT",DISABLE_ELEMENT:"DISABLE_ELEMENT",TOGGLE_ELEMENT:"TOGGLE_ELEMENT",ENABLE_ALL:"ENABLE_ALL",DISABLE_ALL:"DISABLE_ALL",PLAY:"PLAY",PAUSE:"PAUSE",STOP:"STOP",TRANSLATE_ELEMENT:"TRANSLATE_ELEMENT",ROTATE_ELEMENT:"ROTATE_ELEMENT",SCALE_ELEMENT:"SCALE_ELEMENT",SET_ELEMENT_POSITION:"SET_ELEMENT_POSITION",SET_ELEMENT_ROTATION:"SET_ELEMENT_ROTATION",SET_ELEMENT_SCALE:"SET_ELEMENT_SCALE",PLAY_ANIMATION:"PLAY_ANIMATION",STOP_ANIMATION:"STOP_ANIMATION",SET_LABEL_TEXT:"SET_LABEL_TEXT",RESET_SCENES:"RESET_SCENES",PLAY_PAUSE:"PLAY_PAUSE",SET_CAMERA_RIG_TRANSFORM:"SET_CAMERA_RIG_TRANSFORM",SET_CAMERA_RIG_POSITION:"SET_CAMERA_RIG_POSITION",SET_CAMERA_RIG_ROTATION:"SET_CAMERA_RIG_ROTATION",SET_CAMERA_RIG_QUATERNION:"SET_CAMERA_RIG_QUATERNION",DISABLE_CAMERA_CONTROLS:"DISABLE_CAMERA_CONTROLS",LOAD_ELEMENT:"LOAD_ELEMENT",CREATE_ELEMENT:"CREATE_ELEMENT",SET_VARIANT:"SET_VARIANT",CAPTURE_SCREEN_FRAME:"CAPTURE_SCREEN_FRAME",START_SCREEN_CAPTURE:"START_SCREEN_CAPTURE",STOP_SCREEN_CAPTURE:"STOP_SCREEN_CAPTURE",GET_ASSET_IMAGE:"GET_ASSET_IMAGE",LOAD_SURFACE_PLACEHOLDER:"LOAD_SURFACE_PLACEHOLDER",SET_TRANSFORM_CONTROLS_ALLOWLIST:"SET_TRANSFORM_CONTROLS_ALLOWLIST",SET_TRANSFORM_CONTROLS_BLOCKLIST:"SET_TRANSFORM_CONTROLS_BLOCKLIST",SET_SCREEN_CAPTURE_OVERLAY:"SET_SCREEN_CAPTURE_OVERLAY",CLEAR_SCREEN_CAPTURE_OVERLAY:"CLEAR_SCREEN_CAPTURE_OVERLAY",GET_METADATA:"GET_METADATA",LOAD_SCENE:"LOAD_SCENE",HIGHTLIGHT_PIECE:"HIGHTLIGHT_PIECE",SET_OPACITY:"SET_OPACITY",INIT_NAVIGATION:"INIT_NAVIGATION",CANCEL_NAVIGATION:"CANCEL_NAVIGATION",HIGHLIGHT_NODES:"HIGHLIGHT_NODES",HIGHLIGHT_ELEMENT_NODE:"HIGHLIGHT_ELEMENT_NODE",CLEAR_HIGHLIGHT_NODES:"CLEAR_HIGHLIGHT_NODES",SET_PROPERTIES:"SET_PROPERTIES"},A={NONE:0,FADE:1,GROW:2,GROW_X:3,GROW_Y:4,GROW_Z:5,BOUNCE:6,TRANSLATION:7},T="v1",_="studio",c=function(){function t(){this.eventListeners={},this.eventListenersCount=0}var e=t.prototype;return e.addEventListener=function(t,e){this.eventListeners[t]||(this.eventListeners[t]=[]);var n=this.eventListenersCount++;return this.eventListeners[t].push({id:n,func:e}),n},e.triggerEvent=function(t,e){this.eventListeners[t]&&this.eventListeners[t].map(function(t){return t.func(e)})},e.removeEventListener=function(t){for(var e=0,n=Object.keys(this.eventListeners);e<n.length;e++)for(var i=n[e],r=0;r<i.length;r++)if(i[r].id===t){i.splice(r,1);break}},t}(),u=function(){function e(t){this.iframe=t,this.targetWindow=t?t.contentWindow:window,this.id=E(),this.eventManager=new c,this.onMessageReceived=this.onMessageReceived.bind(this),this.pendingActions=new Map}var n=e.prototype;return n.connect=function(){try{var t=function(){e.origin.match(/\.onirix\.com$/)||(e.iframe?console.warn("The Onirix Embed SDK is trying to connect to the origin `"+e.origin+"`, but that is not a recognised Onirix origin. The iframe provided to the Onirix Embed SDK constructor is not pointing to an Onirix Experience."):console.warn("The Onirix Embed SDK is trying to connect to the origin `"+e.origin+"`, but that is not a recognised Onirix origin. You may need to provide an iframe in the Onirix Embed SDK constructor."));var t=new Promise(function(t,n){e.onConnect=t});return window.addEventListener("message",e.onMessageReceived),e.sendHandshake(),Promise.resolve(t).then(function(){})},e=this;try{e.origin=e.targetWindow.location.origin}catch(t){e.origin=new URL(e.iframe.src).origin}var n=function(){if("null"===e.origin)return Promise.resolve(new Promise(function(t){return e.iframe.addEventListener("load",t,{once:!0})})).then(function(){e.origin=new URL(e.iframe.src).origin})}();return Promise.resolve(n&&n.then?n.then(t):t())}catch(t){return Promise.reject(t)}},n.disconnect=function(){try{var t=this;return t.sendDisconnect(),window.removeEventListener("message",t.onMessageReceived),t.pendingActions.clear(),Promise.resolve()}catch(t){return Promise.reject(t)}},n.sendMessage=function(e,n){e=t({oxEmbedSdk:T,source:this.id,destination:_},e),this.targetWindow.postMessage(e,this.origin,null!=n?n:[])},n.sendHandshake=function(){this.targetWindow.postMessage({oxEmbedSdk:T,source:this.id,destination:_,type:"HANDSHAKE"},this.origin)},n.sendDisconnect=function(){this.sendMessage({type:"DISCONNECT"})},n.triggerAction=function(t,e,n){var i=this,r=E(),o=new Error,s=new Promise(function(t,e){i.pendingActions.set(r,{resolve:t,reject:function(t){var n,i=null!=(n=null==t?void 0:t.message)?n:t;o.message=i,e(t)}})});return this.sendMessage({type:"TRIGGER_ACTION",thread:r,action:t,args:e},n),s},n.onMessageReceived=function(t){var e;if(t.origin===this.origin){var n=t.data;if(n.oxEmbedSdk===T&&n.destination===this.id)switch(n.type){case"CONNECT":null==(e=this.onConnect)||e.call(this),this.onConnect=void 0;break;case"RAISE_EVENT":this.eventManager.triggerEvent(n.event,n.args);break;case"REJECT_ACTION":this.onActionRejected(n.thread,n.error);break;case"RESOLVE_ACTION":this.onActionResolved(n.thread,n.value)}}},n.onActionResolved=function(t,e){var n=this.pendingActions.get(t);n?(n.resolve(e),this.pendingActions.delete(t)):console.warn("[Embed SDK] Resolving an already resolved action: "+t)},n.onActionRejected=function(t,e){var n=this.pendingActions.get(t);n?(n.reject(new Error(e)),this.pendingActions.delete(t)):console.warn("[Embed SDK] Rejecting an already resolved action: "+t)},n.subscribe=function(t,e){if(!(t in s))throw new Error('event "'+t+'" does not exists');return this.eventManager.addEventListener(t,e)},n.unsubscribe=function(t){this.eventManager.removeEventListener(t)},n.enable=function(e,n,i){void 0===n&&(n=A.NONE),void 0===i&&(i=void 0);var r=this._buildTransitionParamsForEnableDisableOrToggle(n,i);return this.triggerAction(a.ENABLE_ELEMENT,t({oid:e,transition:r.type},r.params))},n.disable=function(e,n,i){void 0===n&&(n=A.NONE),void 0===i&&(i=void 0);var r=this._buildTransitionParamsForEnableDisableOrToggle(n,i);return this.triggerAction(a.DISABLE_ELEMENT,t({oid:e,transition:r.type},r.params))},n.toggle=function(e,n,i){void 0===n&&(n=A.NONE),void 0===i&&(i=void 0);var r=this._buildTransitionParamsForEnableDisableOrToggle(n,i);return this.triggerAction(a.TOGGLE_ELEMENT,t({oid:e,transition:r.type},r.params))},n._buildTransitionParams=function(t,e){var n,i,r;return void 0===e&&(e=void 0),"number"==typeof t&&(t={type:t}),t.type=null!=(n=t.type)?n:A.NONE,t.time=null!=(i=null!=(r=t.time)?r:e)?i:1,t},n._buildTransitionParamsForEnableDisableOrToggle=function(e,n){var i=this._buildTransitionParams(e,n),r=i.type;return delete(i=t({},i)).type,{type:r,params:i}},n.enableAll=function(){return this.triggerAction(a.ENABLE_ALL)},n.disableAll=function(){return this.triggerAction(a.DISABLE_ALL)},n.play=function(t){return this.triggerAction(a.PLAY,{oid:t})},n.pause=function(t){return this.triggerAction(a.PAUSE,{oid:t})},n.stop=function(t){return this.triggerAction(a.STOP,{oid:t})},n.playPause=function(t){return this.triggerAction(a.PLAY_PAUSE,{oid:t})},n.translate=function(t,e,n,i,r,o){return this.triggerAction(a.TRANSLATE_ELEMENT,{oid:t,x:e,y:n,z:i,time:r,loop:o})},n.translateToPosition=function(t,e,n,i,r,o){return this.triggerAction(a.SET_ELEMENT_POSITION,{oid:t,target:{x:e,y:n,z:i},time:r,lookAt:o})},n.translateToElement=function(t,e,n,i,r){return this.triggerAction(a.SET_ELEMENT_POSITION,{oid:t,target:e,time:n,lookAt:i,loop:r})},n.rotate=function(t,e,n,i,r,o){return this.triggerAction(a.ROTATE_ELEMENT,{oid:t,x:e,y:n,z:i,time:r,loop:o})},n.rotateTo=function(t,e,n,i,r){return this.triggerAction(a.SET_ELEMENT_ROTATION,{oid:t,x:e,y:n,z:i,time:r})},n.rotateToQuaternion=function(t,e,n,i,r,o){return this.triggerAction(a.SET_ELEMENT_ROTATION,{oid:t,x:e,y:n,z:i,w:r,time:o})},n.scale=function(t,e,n,i,r,o){return this.triggerAction(a.SCALE_ELEMENT,{oid:t,x:e,y:n,z:i,time:r,loop:o})},n.playAnimation=function(t,e,n,i,r){return void 0===i&&(i=!1),void 0===r&&(r=void 0),this.triggerAction(a.PLAY_ANIMATION,{oid:t,animation:e,time:r,loop:n,autoStop:i})},n.stopAnimation=function(t,e){return this.triggerAction(a.STOP_ANIMATION,{oid:t,animation:e})},n.setLabelText=function(t,e){return this.triggerAction(a.SET_LABEL_TEXT,{oid:t,text:e})},n.resetScenes=function(){return this.triggerAction(a.RESET_SCENES,{})},n.setCameraRigTransform=function(t){return this.triggerAction(a.SET_CAMERA_RIG_TRANSFORM,{matrix:t.flat()})},n.setCameraRigPosition=function(t,e,n){return this.triggerAction(a.SET_CAMERA_RIG_POSITION,{x:t,y:e,z:n})},n.setCameraRigRotation=function(t,e,n){return this.triggerAction(a.SET_CAMERA_RIG_ROTATION,{x:t,y:e,z:n})},n.setCameraRigQuaternion=function(t,e,n,i){return this.triggerAction(a.SET_CAMERA_RIG_QUATERNION,{x:t,y:e,z:n,w:i})},n.disableCameraControls=function(){return this.triggerAction(a.DISABLE_CAMERA_CONTROLS,{})},n.loadElement=function(e,n,i){void 0===i&&(i={});var r=this._buildTransitionParamsForCreateElementOrLoadElement(i,n);return this.triggerAction(a.LOAD_ELEMENT,t({},n,{transition:r,oid:e}))},n.createElement=function(e,n,i){void 0===i&&(i={});var r=this._buildTransitionParamsForCreateElementOrLoadElement(i,n);return this.triggerAction(a.CREATE_ELEMENT,t({},n,{transition:r,newOid:e}))},n._buildTransitionParamsForCreateElementOrLoadElement=function(e,n){var i,r=this._buildTransitionParams(null!=(i=null==n?void 0:n.transition)?i:{});null!=n&&n.sound&&(r.sound=n.sound),null!=n&&n.time&&(r.time=n.time);var o=this._buildTransitionParams(e),E=t({},r,o);return E.type=o.type!==A.NONE?o.type:r.type,E},n.setVariant=function(t,e){return this.triggerAction(a.SET_VARIANT,{oid:t,variant:e})},n.captureScreenFrame=function(){return this.triggerAction(a.CAPTURE_SCREEN_FRAME,{})},n.startScreenCapture=function(){return this.triggerAction(a.START_SCREEN_CAPTURE,{})},n.stopScreenCapture=function(){return this.triggerAction(a.STOP_SCREEN_CAPTURE,{})},n.setScreenCaptureOverlay=function(t,e,n,i,r,o,E,s,A){try{var T,_,c,u,N=function(){return Promise.resolve(createImageBitmap(t,T,_,c,u)).then(function(t){return S.triggerAction(a.SET_SCREEN_CAPTURE_OVERLAY,{source:t,x:o,y:E,width:s,height:A},[t])})},S=this;void 0===o&&void 0===E?(T=0,_=0,o=e,E=n):(T=e,_=n);var g=function(){if(void 0===s&&void 0===A){var e=function(){A=r,void 0!==(s=i)&&void 0!==A||(s=c,A=u)},n=function(){if(t instanceof VideoFrame)c=t.displayWidth-T,u=t.displayHeight-_;else{var e=function(){if(t instanceof Blob)return Promise.resolve(createImageBitmap(t)).then(function(t){c=t.width-T,u=t.height-_});c=t.width-T,u=t.height-_}();if(e&&e.then)return e.then(function(){})}}();return n&&n.then?n.then(e):e()}c=i,u=r}();return Promise.resolve(g&&g.then?g.then(N):N())}catch(t){return Promise.reject(t)}},n.clearScreenCaptureOverlay=function(){return this.triggerAction(a.CLEAR_SCREEN_CAPTURE_OVERLAY,{})},n.getAssetImage=function(t,e){return void 0===e&&(e=!0),this.triggerAction(a.GET_ASSET_IMAGE,{assetOid:t,thumb:e})},n.loadSurfacePlaceholder=function(t){return this.triggerAction(a.LOAD_SURFACE_PLACEHOLDER,{placeholderUrl:t})},n.setTransformControlsAllowlist=function(t){if(!(t&&t[Symbol.iterator]||void 0===t))throw new Error("`allowlist` must be an iterable or `undefined`.");return void 0===t||t instanceof Set||(t=new Set(t)),this.triggerAction(a.SET_TRANSFORM_CONTROLS_ALLOWLIST,{allowlist:t})},n.setTransformControlsBlocklist=function(t){if(!(t&&t[Symbol.iterator]||void 0===t))throw new Error("`blocklist` must be an iterable or `undefined`.");return void 0===t||t instanceof Set||(t=new Set(t)),this.triggerAction(a.SET_TRANSFORM_CONTROLS_BLOCKLIST,{blocklist:t})},n.getMetadata=function(t,e){if("string"!=typeof t)throw new Error("expected OID to be a `string`, but got a `"+typeof t+"` instead");if(!("number"==typeof e||"string"==typeof e||Array.isArray(e)||"object"==typeof e&&e instanceof RegExp))throw new Error("expected metadata query to be an `Array`, `string`, `number` or `RegExp`, but got a `"+typeof e+"` instead");if(Array.isArray(e)&&e.some(function(t){return"number"==typeof e||"string"!=typeof t&&!("object"==typeof t&&t instanceof RegExp)})){var n=e.map(function(t){return typeof t}).filter(function(t){return"string"!==t}),i="["+Array.from(new Set(n)).join(", ")+"]";throw new Error("expected metadata query to be an `Array` of `string`, `number` or `RegExp`, but got types such as `"+i+"`")}return this.triggerAction(a.GET_METADATA,{oid:t,query:e})},n.loadScene=function(t){if("string"!=typeof t)throw new Error("expected the scene OID to be a `string`, but got a `"+typeof t+"`");return this.triggerAction(a.LOAD_SCENE,{sceneOid:t})},n.highlightBIMPieces=function(t,e){return this.triggerAction(a.HIGHTLIGHT_PIECE,{enable:t,params:e})},n.setOpacity=function(t,e,n){if(void 0===n&&(n=0),e<0||e>1)throw new Error("Opacity must be between 0 and 1.");return this.triggerAction(a.SET_OPACITY,{oid:t,opacity:e,time:n})},n.initNavigation=function(t,e,n){return void 0===n&&(n=!1),this.triggerAction(a.INIT_NAVIGATION,{destination:t,origin:e,fullRoute:n})},n.cancelNavigation=function(){return this.triggerAction(a.CANCEL_NAVIGATION)},n.enableHighlightNodes=function(t,e){return this.triggerAction(a.HIGHLIGHT_NODES,{enable:t,params:e})},n.highlightElementNode=function(t,e){return this.triggerAction(a.HIGHLIGHT_ELEMENT_NODE,{oid:t,name:e})},n.clearHighlightNodes=function(){return this.triggerAction(a.CLEAR_HIGHLIGHT_NODES)},n.setProperties=function(t,e){return this.triggerAction(a.SET_PROPERTIES,{oid:t,params:e})},e}();u.Events=s,u.Actions=a,u.Transitions=A,u.Sounds={SUCCESS:"success",SCORE:"score",SWEEP:"sweep",INTERFACE_ZOOM:"interface zoom",SWIPE:"swipe",FINGER_TAP:"finger_tap",SPRINT_JUMP:"sprint_jump",CINEMATIC_HIT:"cinematic_hit",POP:"pop",GLITCH:"glitch",BLOOP:"bloop",IMPACT:"impact"};export{u as default};