@sumsub/websdk
Version:
SumSub WebSdk
2 lines (1 loc) • 11.8 kB
JavaScript
var t=function(){return t=Object.assign||function(t){for(var i,e=1,o=arguments.length;e<o;e++)for(var n in i=arguments[e])Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n]);return t},t.apply(this,arguments)},i=function(){function i(t,i,e,o){var n=this;this.iframe=null,this.iframeId=null,this.sessionId="",this.$container=null,this.initialized=!1,this.scrollPosition=-1,this.scrollElement=null,this.onMessage=function(t){n.onSdkMessage(t)},this.baseUrl=t||this.getCurrentBaseUrl("https://in.sumsub.com",i.accessToken),this.config=i,this.config.packageVersion=this.getVersion(),this.config.origin=location.origin,this.callbacks=e,this.options=o}return i.prototype.getVersion=function(){return"2.8.0"},i.prototype.getCurrentBaseUrl=function(t,i){var e=["_act-sbx-jwt-","_act-jwt-"].find(function(t){return i.startsWith(t)});if(e)for(var o=0,n=i.replace(e,"").replace("-v2","").split(".").map(function(t){try{return atob(t)}catch(i){return t}}).map(function(t){try{return JSON.parse(t)}catch(i){return t}});o<n.length;o++){var s=n[o];if(null==s?void 0:s.url)return s.url}return t},i.prototype.getIframeId=function(){try{return btoa(this.config.accessToken)}catch(t){return this.config.accessToken}},i.prototype.launch=function(t){this.options.addViewportTag&&this.addViewportTag(),this.iframe=this.createIframe(t),this.sessionId="",this.iframeId="id_"+this.getIframeId(),this.iframe&&this.config?(this.registerEventListener(),this.iframe.src=this.getIframeSrc()):console.error("Idensic was not initialized since either provided element was not found in the DOM or invalid config")},i.prototype.addViewportTag=function(){var t=document.getElementsByName("viewport");if(!t||0===t.length){var i=document.createElement("meta");i.setAttribute("name","viewport"),i.setAttribute("content","width=device-width,user-scalable=yes"),document.head.appendChild(i)}},i.prototype.createIframe=function(t){var i="string"==typeof t?document.querySelector(t):t;if(!i)return console.error("Provide a valid selector for the iframe container"),null;this.$container=i;var e=document.createElement("iframe");for(e.width="100%",e.scrolling="no",e.allow="camera; microphone; geolocation; clipboard-write; hid",e.setAttribute("frameborder","0");i.firstChild;)i.removeChild(i.firstChild);return i.appendChild(e),e},i.prototype.getIframeSrc=function(){var t="?_="+this.iframeId,i=this.baseUrl+"/websdk/websdk.html";return this.config.theme&&(t+="&theme="+this.config.theme),this.config.customizationName&&(t+="&customizationName="+encodeURIComponent(this.config.customizationName)),this.config.translationName&&(t+="&translationName="+encodeURIComponent(this.config.translationName)),i+t},i.prototype.registerEventListener=function(){window.addEventListener("message",this.onMessage)},i.prototype.onSdkMessage=function(i){var e;if(this.baseUrl===i.origin){var o=i.data;if(o.method&&~o.method.indexOf("idCheck")&&(!this.sessionId||this.sessionId===o.sessionId)&&(null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)){if("idCheck.onReady"==o.method&&o.frameId===this.iframeId){this.sessionId=o.sessionId;var n={options:{adaptIframeHeight:this.options.adaptIframeHeight,enableScrollIntoView:this.options.enableScrollIntoView}};this.iframe.contentWindow.postMessage(t(t({method:"idCheck.init"},this.config),n),"*")}if("idCheck.onInitialized"==o.method&&(this.initialized=!0),"idCheck.onResize"==o.method&&this.options.adaptIframeHeight&&(this.iframe.style.height=o.height+"px"),"idCheck.onSetBorderRadius"!=o.method)if("idCheck.scrollTo"==o.method&&this.options.adaptIframeHeight)this.scrollTo(o.top);else if("idCheck.getScrollPosition"!=o.method)if("idCheck.restoreScrollPosition"!=o.method){var s=o.method;delete o.method,delete o.frameId,delete o.sessionId,"idCheck.onError"===s&&"invalid-token"===o.code?this.callExpirationHandler():"idCheck.onError"===s&&"function"==typeof this.callbacks.onError?this.callbacks.onError(o):"function"==typeof this.callbacks.onMessage&&this.callbacks.onMessage(s,o)}else this.restoreScrollPosition();else this.getScrollPosition();else this.iframe&&o.radius&&"string"==typeof o.radius&&(this.iframe.style.borderRadius=o.radius,this.iframe.style.overflow="hidden")}}},i.prototype.callExpirationHandler=function(){var t=this,i=this.callbacks.expirationHandler;i.legacy?i.handler(function(i){return t.updateAccessToken(i)}):i.handler().then(function(i){return t.updateAccessToken(i)},function(i){null!=i||(i="Failed to update access token"),i.message&&(i=i.message),"string"!=typeof i&&(i=String(i)),t.updateAccessToken(null,i)})},i.prototype.getScrolledElement=function(){for(var t,i=null===(t=this.iframe)||void 0===t?void 0:t.parentElement;0===(null==i?void 0:i.scrollTop)&&"BODY"!==(null==i?void 0:i.tagName);)i=null==i?void 0:i.parentElement;return i},i.prototype.getScrollPosition=function(){var t,i,e=this.getScrolledElement();this.scrollPosition=(null==e?void 0:e.scrollTop)||-1,this.scrollElement=this.scrollPosition>0&&e?e:null,null===(i=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===i||i.postMessage({method:"idCheck.scrollPositionStored"},"*")},i.prototype.restoreScrollPosition=function(){this.scrollElement&&this.scrollPosition>=0&&(this.scrollElement.scrollTop=this.scrollPosition),this.scrollPosition=-1,this.scrollElement=null},i.prototype.scrollTo=function(t){var i;if(this.options.enableScrollIntoView){var e=this.getScrolledElement();if(0===(null==e?void 0:e.scrollTop)&&"BODY"===(null==e?void 0:e.tagName)){var o=(null===(i=this.iframe)||void 0===i?void 0:i.getBoundingClientRect().top)||0;window.scrollTo({top:o+t,behavior:"smooth"})}else null==e||e.scrollTo({top:t,behavior:"smooth"})}},i.prototype.updateAccessToken=function(t,i){var e,o;null===(o=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===o||o.postMessage({method:"idCheck.updateAccessToken",accessToken:t,error:i},"*")},i.prototype.destroy=function(){for(window.removeEventListener("message",this.onMessage);this.$container&&this.$container.firstChild;)this.$container.removeChild(this.$container.firstChild);this.$container=null},i.prototype.navigateBack=function(){var t,i;null===(i=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===i||i.postMessage({method:"idCheck.callNavigationBack"},"*")},i.prototype.singleStepNext=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSingleStepNext",step:t},"*")},i.prototype.setLanguage=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetLanguage",language:t},"*")},i.prototype.setTheme=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetTheme",theme:t},"*")},i}(),e=function(){function t(t,i){if(this.config=null,this.eventHandlers={},this.anyEventHandler=null,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},"string"!=typeof t)throw new Error("Access token must be a string");if("function"!=typeof i)throw new Error("updateAccessToken callback is required");this.accessToken=t,this.updateAccessToken=i}return t.prototype.onTestEnv=function(){return this},t.prototype.withBaseUrl=function(t){return this.baseUrl=t,this},t.prototype.withConf=function(t){return this.config=t,this},t.prototype.withOptions=function(t){var i;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(i=t.enableScrollIntoView)||void 0===i||i,this},t.prototype.on=function(t,i){return this.eventHandlers[t]=i,this},t.prototype.onMessage=function(t){return this.anyEventHandler=t,this},t.prototype.onNavigationUiControlsStateChanged=function(t){return this.eventHandlers["idCheck.onNavigationUiControlsStateChanged"]=t,this},t.prototype.build=function(){var t,e,o,n,s,r,a,l,c,d,h,u,f,p,g,m=this;return new i(this.baseUrl,{theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(e=this.config)||void 0===e?void 0:e.customizationName,translationName:null===(o=this.config)||void 0===o?void 0:o.translationName,accessToken:this.accessToken,lang:null===(n=this.config)||void 0===n?void 0:n.lang,email:null===(s=this.config)||void 0===s?void 0:s.email,phone:null===(r=this.config)||void 0===r?void 0:r.phone,country:null===(a=this.config)||void 0===a?void 0:a.country,uiConf:null===(l=this.config)||void 0===l?void 0:l.uiConf,i18n:null===(c=this.config)||void 0===c?void 0:c.i18n,documentsByCountries:null===(d=this.config)||void 0===d?void 0:d.documentsByCountries,documentDefinitions:null===(h=this.config)||void 0===h?void 0:h.documentDefinitions,autoSelectDocumentDefinitions:null===(u=this.config)||void 0===u?void 0:u.autoSelectDocumentDefinitions,controlledNavigationBack:null===(f=this.config)||void 0===f?void 0:f.controlledNavigationBack,singleStep:null===(p=this.config)||void 0===p?void 0:p.singleStep,preferredCameras:null===(g=this.config)||void 0===g?void 0:g.preferredCameras},{expirationHandler:{legacy:!1,handler:this.updateAccessToken},onMessage:function(t,i){var e,o=m.eventHandlers[t];o?o(i):null===(e=m.anyEventHandler)||void 0===e||e.call(m,t,i)}},this.options)},t}(),o=function(){function t(t,i){this.debugEnabled=!1,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},this.config=null,this.accessToken=null,this.expirationHandler=null,this.baseUrl=t,this.flowName=i}return t.prototype.withAccessToken=function(t,i){if(this.accessToken=t,!i||"function"!=typeof i)throw new Error('Invalid parameter, "expirationHandler" must be a function');return this.expirationHandler=i,this},t.prototype.debug=function(t){return this.debugEnabled=t,this},t.prototype.withOptions=function(t){var i;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(i=t.enableScrollIntoView)||void 0===i||i,this},t.prototype.withConf=function(t){return this.config=t,this},t.prototype.build=function(){var t,e,o,n,s,r,a,l,c,d,h,u,f,p,g;if(!this.accessToken||!this.expirationHandler)throw new Error("Configure access token end the expiration handler before");return new i(this.baseUrl,{theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(e=this.config)||void 0===e?void 0:e.customizationName,translationName:null===(o=this.config)||void 0===o?void 0:o.translationName,accessToken:this.accessToken,flowName:this.flowName,lang:null===(n=this.config)||void 0===n?void 0:n.lang,email:null===(s=this.config)||void 0===s?void 0:s.email,phone:null===(r=this.config)||void 0===r?void 0:r.phone,country:null===(a=this.config)||void 0===a?void 0:a.country,uiConf:null===(l=this.config)||void 0===l?void 0:l.uiConf,i18n:null===(c=this.config)||void 0===c?void 0:c.i18n,documentsByCountries:null===(d=this.config)||void 0===d?void 0:d.documentsByCountries,documentDefinitions:null===(h=this.config)||void 0===h?void 0:h.documentDefinitions,autoSelectDocumentDefinitions:null===(u=this.config)||void 0===u?void 0:u.autoSelectDocumentDefinitions,singleStep:null===(f=this.config)||void 0===f?void 0:f.singleStep},{expirationHandler:{legacy:!0,handler:this.expirationHandler},onMessage:null===(p=this.config)||void 0===p?void 0:p.onMessage,onError:null===(g=this.config)||void 0===g?void 0:g.onError},{adaptIframeHeight:this.options.adaptIframeHeight,addViewportTag:this.options.addViewportTag,enableScrollIntoView:this.options.enableScrollIntoView,debug:this.debugEnabled})},t}(),n={Builder:function(t,i){return new o(t,i)},init:function(t,i){return new e(t,i)}};export{n as default};