@onirix/i18n-module
Version:
Onirix helper for internationalization experiences.
2 lines (1 loc) • 13.4 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)}var e={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"},n={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"},i="v1",r="studio",o=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}();let s;const E=new Uint8Array(16);function a(){if(!s&&(s="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!s))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return s(E)}const A=[];for(let t=0;t<256;++t)A.push((t+256).toString(16).slice(1));var c={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function T(t,e,n){if(c.randomUUID&&!e&&!t)return c.randomUUID();const i=(t=t||{}).random||(t.rng||a)();if(i[6]=15&i[6]|64,i[8]=63&i[8]|128,e){n=n||0;for(let t=0;t<16;++t)e[n+t]=i[t];return e}return function(t,e=0){return A[t[e+0]]+A[t[e+1]]+A[t[e+2]]+A[t[e+3]]+"-"+A[t[e+4]]+A[t[e+5]]+"-"+A[t[e+6]]+A[t[e+7]]+"-"+A[t[e+8]]+A[t[e+9]]+"-"+A[t[e+10]]+A[t[e+11]]+A[t[e+12]]+A[t[e+13]]+A[t[e+14]]+A[t[e+15]]}(i)}var u=function(){function s(t){this.iframe=t,this.targetWindow=t?t.contentWindow:window,this.id=T(),this.eventManager=new o,this.onMessageReceived=this.onMessageReceived.bind(this),this.pendingActions=new Map}var E=s.prototype;return E.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)}},E.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)}},E.sendMessage=function(e){e=t({oxEmbedSdk:i,source:this.id,destination:r},e),this.targetWindow.postMessage(e,this.origin)},E.sendHandshake=function(){this.targetWindow.postMessage({oxEmbedSdk:i,source:this.id,destination:r,type:"HANDSHAKE"},this.origin)},E.sendDisconnect=function(){this.sendMessage({type:"DISCONNECT"})},E.triggerAction=function(t,e){var n=this,i=T(),r=new Error,o=new Promise(function(t,e){n.pendingActions.set(i,{resolve:t,reject:function(t){var n,i=null!=(n=null==t?void 0:t.message)?n:t;r.message=i,e(r)}})});return this.sendMessage({type:"TRIGGER_ACTION",thread:i,action:t,args:e}),o},E.onMessageReceived=function(t){var e;if(t.origin===this.origin){var n=t.data;if(n.oxEmbedSdk===i&&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)}}},E.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)},E.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)},E.subscribe=function(t,n){if(!(t in e))throw new Error('event "'+t+'" does not exists');return this.eventManager.addEventListener(t,n)},E.unsubscribe=function(t){this.eventManager.removeEventListener(t)},E.enable=function(t,e,i){return void 0===e&&(e=0),void 0===i&&(i=1),this.triggerAction(n.ENABLE_ELEMENT,{oid:t,transition:e,time:i})},E.disable=function(t,e,i){return void 0===e&&(e=0),void 0===i&&(i=1),this.triggerAction(n.DISABLE_ELEMENT,{oid:t,transition:e,time:i})},E.toggle=function(t,e,i){return void 0===e&&(e=0),void 0===i&&(i=1),this.triggerAction(n.TOGGLE_ELEMENT,{oid:t,transition:e,time:i})},E.enableAll=function(){return this.triggerAction(n.ENABLE_ALL)},E.disableAll=function(){return this.triggerAction(n.DISABLE_ALL)},E.play=function(t){return this.triggerAction(n.PLAY,{oid:t})},E.pause=function(t){return this.triggerAction(n.PAUSE,{oid:t})},E.stop=function(t){return this.triggerAction(n.STOP,{oid:t})},E.playPause=function(t){return this.triggerAction(n.PLAY_PAUSE,{oid:t})},E.translate=function(t,e,i,r,o,s){return this.triggerAction(n.TRANSLATE_ELEMENT,{oid:t,x:e,y:i,z:r,time:o,loop:s})},E.translateToPosition=function(t,e,i,r,o,s){return this.triggerAction(n.SET_ELEMENT_POSITION,{oid:t,target:{x:e,y:i,z:r},time:o,lookAt:s})},E.translateToElement=function(t,e,i,r,o){return this.triggerAction(n.SET_ELEMENT_POSITION,{oid:t,target:e,time:i,lookAt:r,loop:o})},E.rotate=function(t,e,i,r,o,s){return this.triggerAction(n.ROTATE_ELEMENT,{oid:t,x:e,y:i,z:r,time:o,loop:s})},E.rotateTo=function(t,e,i,r,o){return this.triggerAction(n.SET_ELEMENT_ROTATION,{oid:t,x:e,y:i,z:r,time:o})},E.rotateToQuaternion=function(t,e,i,r,o,s){return this.triggerAction(n.SET_ELEMENT_ROTATION,{oid:t,x:e,y:i,z:r,w:o,time:s})},E.scale=function(t,e,i,r,o,s){return this.triggerAction(n.SCALE_ELEMENT,{oid:t,x:e,y:i,z:r,time:o,loop:s})},E.playAnimation=function(t,e,i,r,o){return void 0===r&&(r=!1),void 0===o&&(o=void 0),this.triggerAction(n.PLAY_ANIMATION,{oid:t,animation:e,time:o,loop:i,autoStop:r})},E.stopAnimation=function(t,e){return this.triggerAction(n.STOP_ANIMATION,{oid:t,animation:e})},E.setLabelText=function(t,e){return this.triggerAction(n.SET_LABEL_TEXT,{oid:t,text:e})},E.resetScenes=function(){return this.triggerAction(n.RESET_SCENES,{})},E.setCameraRigTransform=function(t){return this.triggerAction(n.SET_CAMERA_RIG_TRANSFORM,{matrix:t.flat()})},E.setCameraRigPosition=function(t,e,i){return this.triggerAction(n.SET_CAMERA_RIG_POSITION,{x:t,y:e,z:i})},E.setCameraRigRotation=function(t,e,i){return this.triggerAction(n.SET_CAMERA_RIG_ROTATION,{x:t,y:e,z:i})},E.setCameraRigQuaternion=function(t,e,i,r){return this.triggerAction(n.SET_CAMERA_RIG_QUATERNION,{x:t,y:e,z:i,w:r})},E.disableCameraControls=function(){return this.triggerAction(n.DISABLE_CAMERA_CONTROLS,{})},E.loadElement=function(e,i){return this.triggerAction(n.LOAD_ELEMENT,t({},i,{oid:e}))},E.createElement=function(e,i){return this.triggerAction(n.CREATE_ELEMENT,t({},i,{newOid:e}))},E.setVariant=function(t,e){return this.triggerAction(n.SET_VARIANT,{oid:t,variant:e})},E.captureScreenFrame=function(){return this.triggerAction(n.CAPTURE_SCREEN_FRAME,{})},E.startScreenCapture=function(){return this.triggerAction(n.START_SCREEN_CAPTURE,{})},E.stopScreenCapture=function(){return this.triggerAction(n.STOP_SCREEN_CAPTURE,{})},E.getAssetImage=function(t,e){return void 0===e&&(e=!0),this.triggerAction(n.GET_ASSET_IMAGE,{assetOid:t,thumb:e})},E.loadSurfacePlaceholder=function(t){return this.triggerAction(n.LOAD_SURFACE_PLACEHOLDER,{placeholderUrl:t})},E.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(n.SET_TRANSFORM_CONTROLS_ALLOWLIST,{allowlist:t})},E.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(n.SET_TRANSFORM_CONTROLS_BLOCKLIST,{blocklist:t})},s}();u.Events=e,u.Actions=n;class _{constructor(t,e,n={template:this.DEFAULT_TEMPLATE,i18nLabelsTexts:null,i18nUITexts:null}){this.DEFAULT_TEMPLATE="ox-i18n",this.embedSDK=null,this.templateName=null,this.errors=[],this.language=null,this.i18nLabelsTexts=null,this.embedSDK=t,this.language=e||this.logMissingLanguageWarning(),this.templateName=n.template||this.DEFAULT_TEMPLATE,this.i18nLabelsTexts=n.i18nLabelsTexts,this.setUII18n(this.language,n.i18nUITexts),this.subscribeToSceneLoadEnd(),document.body.setAttribute("data-ox-i18-locale",this.language)}logMissingLanguageWarning(){return console.warn("The language has not been provided, the first reported will be used."),null}setUII18n(t,e){null!=e&&e.forEach(e=>{const n=e.oxi18n;let i=e.i18n.find(e=>e[t]);i||(i=e.i18n[0]);const r=Object.values(i)[0];document.querySelectorAll(`[data-ox-i18n="${n}"]`).forEach(t=>{t.textContent=r})})}subscribeToSceneLoadEnd(){var t=this;this.embedSDK.subscribe(u.Events.SCENE_LOAD_END,async function(e){t.shouldUseDatasheets()?await t.handleDatasheetScenario(e.elements):await t.setLabelsTextWithoutDatasheets(),t.finish()})}shouldUseDatasheets(){return this.templateName&&!this.i18nLabelsTexts}async handleDatasheetScenario(t){const e=this.getElementsWithDatasheets(t,this.templateName);e.length>0&&(this.language=this.ensureLanguageExists(e),await this.setLabelsTextWithDatasheets(e))}ensureLanguageExists(t){return this.isLanguage(this.language,t)?this.language:this.getDefaultLanguage(t,this.language)}getElementsWithDatasheets(t,e){const n=[];return t.forEach(t=>{var i;null==(i=t.datasheets)||i.forEach(i=>{i.template.name==e&&null!=t.label&&n.push({oid:t.oid,name:t.name,datasheet:i,template:i.template.template})})}),n}isLanguage(t,e){let n=!1;return null!=t&&e[0].template.forEach(e=>{Object.keys(e)[0]==t&&(n=!0)}),n}getDefaultLanguage(t,e){const n=Object.keys(t[0].datasheet.content[0])[0];return null!=e&&console.warn(`The ${e} language does not exist in the selected template. By default the ${n} language will be used.`),n}async setLabelsTextWithDatasheets(t){for(const e of t){let t=!1;for(const n of e.datasheet.content){const i=Object.keys(n)[0],r=Object.values(n)[0];i==this.language&&r&&""!=r&&(t=!0,await this.embedSDK.setLabelText(e.oid,r))}t||await this.notExistLanguage(e.oid)}}async setLabelsTextWithoutDatasheets(){for(const t of this.i18nLabelsTexts){const e=t.i18n.find(t=>null!=t[this.language]);if(e){const n=Object.values(e)[0];await this.embedSDK.setLabelText(t.labelOid,n)}else await this.notExistLanguage(t.labelOid)}}async notExistLanguage(t){await this.embedSDK.setLabelText(t,""),console.warn(`No exist an entry on datasheet of label with oid ${t} for language ${this.language}`),this.errors.push({labelOid:t,language:this.language})}finish(){this.onFinish&&this.onFinish(this.errors)}}export{_ as default};