@onirix/i18n-module
Version:
Onirix helper for internationalization experiences.
2 lines (1 loc) • 16.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).OnirixI18nModule=e()}(this,function(){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 u=[];for(let t=0;t<256;++t)u.push((t+256).toString(16).slice(1));var c={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function A(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 u[t[e+0]]+u[t[e+1]]+u[t[e+2]]+u[t[e+3]]+"-"+u[t[e+4]]+u[t[e+5]]+"-"+u[t[e+6]]+u[t[e+7]]+"-"+u[t[e+8]]+u[t[e+9]]+"-"+u[t[e+10]]+u[t[e+11]]+u[t[e+12]]+u[t[e+13]]+u[t[e+14]]+u[t[e+15]]}(i)}var T=function(){function s(t){this.iframe=t,this.targetWindow=t?t.contentWindow:window,this.id=A(),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=A(),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}();T.Events=e,T.Actions=n;var l="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function h(t,e,n){if(!t.s){if(n instanceof _){if(!n.s)return void(n.o=h.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(h.bind(null,t,e),h.bind(null,t,2));t.s=e,t.v=n;var i=t.o;i&&i(t)}}var _=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){var i=new t,r=this.s;if(r){var o=1&r?e:n;if(o){try{h(i,1,o(this.v))}catch(t){h(i,2,t)}return i}return this}return this.o=function(t){try{var r=t.v;1&t.s?h(i,1,e?e(r):r):n?h(i,1,n(r)):h(i,2,r)}catch(t){h(i,2,t)}},i},t}();function g(t){return t instanceof _&&1&t.s}var d=/*#__PURE__*/function(){function t(t,e,n){void 0===n&&(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)}var e=t.prototype;return e.logMissingLanguageWarning=function(){return console.warn("The language has not been provided, the first reported will be used."),null},e.setUII18n=function(t,e){null!=e&&e.forEach(function(e){var n=e.oxi18n,i=e.i18n.find(function(e){return e[t]});i||(i=e.i18n[0]);var r=Object.values(i)[0];document.querySelectorAll('[data-ox-i18n="'+n+'"]').forEach(function(t){t.textContent=r})})},e.subscribeToSceneLoadEnd=function(){var t=this;this.embedSDK.subscribe(T.Events.SCENE_LOAD_END,function(e){try{var n=function(){t.finish()},i=t.shouldUseDatasheets()?Promise.resolve(t.handleDatasheetScenario(e.elements)).then(function(){}):Promise.resolve(t.setLabelsTextWithoutDatasheets()).then(function(){});return Promise.resolve(i&&i.then?i.then(n):n())}catch(t){return Promise.reject(t)}})},e.shouldUseDatasheets=function(){return this.templateName&&!this.i18nLabelsTexts},e.handleDatasheetScenario=function(t){try{var e=this,n=e.getElementsWithDatasheets(t,e.templateName),i=function(){if(n.length>0)return e.language=e.ensureLanguageExists(n),Promise.resolve(e.setLabelsTextWithDatasheets(n)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},e.ensureLanguageExists=function(t){return this.isLanguage(this.language,t)?this.language:this.getDefaultLanguage(t,this.language)},e.getElementsWithDatasheets=function(t,e){var n=[];return t.forEach(function(t){var i;null==(i=t.datasheets)||i.forEach(function(i){i.template.name==e&&null!=t.label&&n.push({oid:t.oid,name:t.name,datasheet:i,template:i.template.template})})}),n},e.isLanguage=function(t,e){var n=!1;return null!=t&&e[0].template.forEach(function(e){Object.keys(e)[0]==t&&(n=!0)}),n},e.getDefaultLanguage=function(t,e){var 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},e.setLabelsTextWithDatasheets=function(t){try{var e=this;return Promise.resolve(f(t,function(t){function n(){var n=function(){if(!i)return Promise.resolve(e.notExistLanguage(t.oid)).then(function(){})}();if(n&&n.then)return n.then(function(){})}var i=!1,r=f(t.datasheet.content,function(n){var r=Object.keys(n)[0],o=Object.values(n)[0],s=function(){if(r==e.language&&o&&""!=o)return i=!0,Promise.resolve(e.embedSDK.setLabelText(t.oid,o)).then(function(){})}();if(s&&s.then)return s.then(function(){})});return r&&r.then?r.then(n):n()}))}catch(t){return Promise.reject(t)}},e.setLabelsTextWithoutDatasheets=function(){try{var t=this;return Promise.resolve(f(t.i18nLabelsTexts,function(e){var n=e.i18n.find(function(e){return null!=e[t.language]}),i=function(){if(n){var i=Object.values(n)[0];return Promise.resolve(t.embedSDK.setLabelText(e.labelOid,i)).then(function(){})}return Promise.resolve(t.notExistLanguage(e.labelOid)).then(function(){})}();if(i&&i.then)return i.then(function(){})}))}catch(t){return Promise.reject(t)}},e.notExistLanguage=function(t){try{var e=this;return Promise.resolve(e.embedSDK.setLabelText(t,"")).then(function(){console.warn("No exist an entry on datasheet of label with oid "+t+" for language "+e.language),e.errors.push({labelOid:t,language:e.language})})}catch(t){return Promise.reject(t)}},e.finish=function(){this.onFinish&&this.onFinish(this.errors)},t}();function f(t,e,n){if("function"==typeof t[l]){var i,r,o,s=t[l]();if(function t(E){try{for(;!((i=s.next()).done||n&&n());)if((E=e(i.value))&&E.then){if(!g(E))return void E.then(t,o||(o=h.bind(null,r=new _,2)));E=E.v}r?h(r,1,E):r=E}catch(t){h(r||(r=new _),2,t)}}(),s.return){var E=function(t){try{i.done||s.return()}catch(t){}return t};if(r&&r.then)return r.then(E,function(t){throw E(t)});E()}return r}if(!("length"in t))throw new TypeError("Object is not iterable");for(var a=[],u=0;u<t.length;u++)a.push(t[u]);return function(t,e,n){var i,r,o=-1;return function s(E){try{for(;++o<t.length&&(!n||!n());)if((E=e(o))&&E.then){if(!g(E))return void E.then(s,r||(r=h.bind(null,i=new _,2)));E=E.v}i?h(i,1,E):i=E}catch(t){h(i||(i=new _),2,t)}}(),i}(a,function(t){return e(a[t])},n)}return d});