Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function(u){var m="embeddedServiceHelpButton";var x=[".salesforce.com",".force.com",".sfdc.net"];var ad="esw_storage_iframe";var V="4.0";var F=100;var y="__snapinsLoginCallback";var t="__snapinsLogoutCallback";var Z="snapins_invite";function j(){var ae=false;this.settings={appendHelpButton:true,displayHelpButton:true,isExternalPage:true,devMode:false,targetElement:document.body,elementForOnlineDisplay:undefined,elementForOfflineDisplay:undefined,defaultMinimizedText:"",disabledMinimizedText:"",loadingText:"Loading",enabledFeatures:[],entryFeature:"FieldService",storageDomain:document.domain,language:undefined,linkAction:{feature:undefined,name:undefined,valid:false},linkActionParameters:{}};this.featureScripts={};this.eventHandlers={};this.messageHandlers={};this.storageKeys=["ESW_BODY_SCROLL_POSITION","ESW_IS_MINIMIZED","ESW_MINIMIZED_TEXT"];this.defaultSettings={};this.eswFrame=undefined;this.availableFeatures=["script","session"];this.pendingMessages={};this.iframeScriptsToLoad=[];this.domInitInProgress=false;this.componentInitInProgress=false;this.isIframeReady=false;this.requireLogin=false;this.loginPendingSerializedData=undefined;Object.defineProperty(this,"isButtonDisabled",{get:function(){return ae;},set:function(af){ae=af;this.onButtonStatusChange();}.bind(this),configurable:true});this.setupMessageListener();this.getLinkActionData();}j.prototype.log=function p(){if(this.settings.devMode&&console&&console.log){console.log("esw: "+[].slice.apply(arguments).join(", "));}};j.prototype.error=function E(ae){if(ae){this.log("Server Error: "+ae);}else{this.log("Server responed with an unknown error.");}this.fireEvent("error");};j.prototype.warning=function n(ae){if(ae){this.log("Server Warning: "+ae);}else{this.log("Server sent an anonymous warning.");}};j.prototype.getCookie=function i(ag){var ah=document.cookie;var af;var ae;if(ah){af=ah.indexOf(ag+"=");if(af!==-1){af+=(ag+"=").length;ae=ah.indexOf(";",af);if(ae===-1){ae=ah.length;}return ah.substring(af,ae);}}return undefined;};j.prototype.setCookie=function v(ag,ai,ae){var ah=ag+"="+ai+";";var af;if(ae){af=new Date();af.setFullYear(af.getFullYear()+10);ah+="expires="+af.toUTCString()+";";}ah+="path=/;";document.cookie=ah;};j.prototype.mergeSettings=function B(ae){Object.keys(ae).forEach(function(af){if(this.settings[af]===undefined){this.settings[af]=ae[af];}}.bind(this));};j.prototype.loadFeatureScript=function H(af,ai){var ag=af.toLowerCase();var ae=document.createElement("script");var ah=this.settings.gslbBaseURL?this.settings.gslbBaseURL:this.settings.baseCoreURL;ae.type="text/javascript";ae.src=ah+"/embeddedservice/"+this.settings.releaseVersion+"/client/"+ag+".esw"+(this.settings.devMode?"":".min")+".js";ae.onload=function(){this.featureScripts[af](this);this.availableFeatures.push(ag);if(ai){ai();}this.processPendingMessages(ag);}.bind(this);document.body.appendChild(ae);};j.prototype.fireEvent=function U(ah,ae){var af=this.eventHandlers[ah];var ag=[].slice.apply(arguments).slice(2);var ai=[];if(af){af.forEach(function(aj){ai.push(aj.apply(undefined,ag));});}if(ae){return ae(ai);}return true;};j.prototype.isValidEntityId=function Q(ae){return typeof ae==="string"&&(ae.length===18||ae.length===15);};j.prototype.getKeyPrefix=function f(ae){if(this.isValidEntityId(ae)){return ae.substr(0,3);}return undefined;};j.prototype.isOrganizationId=function d(ae){return this.getKeyPrefix(ae)==="00D";};j.prototype.getESWFrame=function L(){if(!this.eswFrame){this.eswFrame=document.getElementById(ad).contentWindow;}return this.eswFrame;};j.prototype.isFrameStorageEnabled=function S(){return this.eswFrame!==undefined;};j.prototype.processPendingMessages=function s(ae){if(this.pendingMessages[ae]){this.pendingMessages[ae].forEach(function(af){this.handleMessage(af.payload);}.bind(this));this.pendingMessages[ae]=undefined;}};j.prototype.loadCSS=function z(){var ae=document.createElement("link");var af=this.settings.gslbBaseURL?this.settings.gslbBaseURL:this.settings.baseCoreURL;ae.href=af+"/embeddedservice/"+this.settings.releaseVersion+"/esw"+(this.settings.devMode?"":".min")+".css";ae.type="text/css";ae.rel="stylesheet";document.getElementsByTagName("head")[0].appendChild(ae);};j.prototype.appendHelpButton=function M(ae){var af=document.createElement("div");af.className=m;af.innerHTML='<div class="helpButton">'+'<a class="helpButtonEnabled uiButton" role="button" href="javascript:void(0)">'+'<span class="embeddedServiceIcon" aria-hidden="true" data-icon="&#59648;"></span>'+'<div class="helpButtonLabel" id="helpButtonSpan"><span class="message">'+this.settings.defaultMinimizedText+"</span></div>"+"</a>"+"</div>";if(!ae){af.style.display="none";}this.settings.targetElement.appendChild(af);if("ontouchstart" in document.documentElement){[].slice.apply(document.querySelectorAll(".embeddedServiceHelpButton .uiButton")).forEach(function(ag){ag.classList.add("no-hover");});}this.onButtonStatusChange(this.isButtonDisabled);};j.prototype.appendIFrame=function R(){var ae=document.createElement("iframe");ae.id=ad;ae.src=this.settings.iframeURL;ae.style.display="none";ae.onload=function(){this.isIframeReady=true;this.iframeScriptsToLoad.forEach(function(af){this.loadStorageScript(af);}.bind(this));this.iframeScriptsToLoad=[];}.bind(this);this.settings.targetElement.appendChild(ae);window.addEventListener("beforeunload",function(){if("ActiveXObject" in window){ae.src="about:blank";}},false);};j.prototype.createLightningComponent=function O(af){var ae={};if(document.getElementById(Z)){embedded_svc.inviteAPI.inviteButton.setOnlineState(false);}this.hideHelpButton();this.fireEvent("beforeCreate");Object.keys(this.settings).forEach(function(ag){ae[ag]=this.settings[ag];}.bind(this));Object.keys(this.defaultSettings).forEach(function(ag){if(ae[ag]===undefined){ae[ag]=this.defaultSettings[ag];}}.bind(this));$Lightning.createComponent("embeddedService:sidebar",{configurationData:ae,serializedSessionData:af},this.settings.targetElement,function(){this.componentInitInProgress=false;this.setHelpButtonText(ae.defaultMinimizedText);}.bind(this));};j.prototype.loadLightningApp=function C(af){var ae;var ag;if(this.settings.isExternalPage&&typeof this.settings.communityEndpointURL!=="string"){throw new Error("communityEndpointURL String property not set");}ae=document.getElementsByClassName("helpButton")[0];if(ae){ag=ae.getBoundingClientRect().width;if(ag>0){ae.style.width=ag+"px";}}this.setHelpButtonText(this.settings.loadingText,false);$Lightning.use("embeddedService:sidebarApp",function(){this.createLightningComponent(af);}.bind(this),this.settings.communityEndpointURL,window.SFIDWidget&&window.SFIDWidget.openid_response&&window.SFIDWidget.openid_response.access_token,this.settings.language&&this.settings.language.trim()!==""?{guestUserLang:this.settings.language}:undefined);};j.prototype.initLightningOut=function P(af){var ae;if(!window.$Lightning||!this.settings.isExternalPage){ae=document.createElement("script");ae.type="text/javascript";ae.onload=function(){this.loadLightningApp(af);}.bind(this);ae.src=this.settings.baseCoreURL+"/lightning/lightning.out.js";document.getElementsByTagName("head")[0].appendChild(ae);}else{this.loadLightningApp(af);}};j.prototype.setHelpButtonText=function G(ah,ai){var ae=ai===undefined?true:ai;var ag=document.getElementById("helpButtonSpan");var af;if(ag){af=ag.querySelector(".message");af.innerHTML=ah;ag.previousSibling.style.display=ae?"inline-block":"none";}};j.prototype.prepareDOM=function Y(){if(this.domInitInProgress){return;}this.domInitInProgress=true;this.appendIFrame();};j.prototype.addSessionHandlers=function N(){this.addMessageHandler("session.onLoad",function(){this.postMessage("session.get",this.storageKeys);}.bind(this));this.addMessageHandler("session.sessionData",function(ae){this.resumeInitWithSessionData(ae);}.bind(this));};j.prototype.addMetaTag=function(ae,ag){var af=document.createElement("meta");af.name=ae;af.content=ag;document.head.appendChild(af);};j.prototype.init=function aa(ag,ai,af,ae,aj,ah){this.settings.baseCoreURL=ag;this.settings.communityEndpointURL=ai;this.settings.gslbBaseURL=af?af:ag;this.settings.orgId=ae;this.settings.releaseVersion=V;this.settings.eswConfigDevName=aj;this.mergeSettings(ah||{});if(typeof this.settings.baseCoreURL!=="string"){throw new Error("Base core URL value must be a string.");}if(!this.isOrganizationId(this.settings.orgId)){throw new Error("Invalid OrganizationId Parameter Value: "+this.settings.orgId);}if(this.fireEvent("validateInit",function(ak){return ak.indexOf(false)!==-1;},this.settings)){return;}if(this.settings.displayHelpButton&&this.settings.appendHelpButton){this.loadCSS();}if(!this.settings.targetElement){throw new Error("No targetElement specified");}this.settings.iframeURL=this.settings.gslbBaseURL+"/embeddedservice/"+this.settings.releaseVersion+(this.settings.devMode?"/eswDev.html":"/esw.html")+"?parent="+document.location.href;this.addSessionHandlers();this.loadFeatures();if(document.readyState==="complete"){setTimeout(this.prepareDOM.bind(this),1);}else{if(document.addEventListener){document.addEventListener("DOMContentLoaded",this.prepareDOM.bind(this),false);window.addEventListener("load",this.prepareDOM.bind(this),false);}else{if(window.attachEvent){window.attachEvent("onload",this.prepareDOM.bind(this));}else{this.log("No available event model. Exiting.");}}}};j.prototype.onHelpButtonClick=function b(){if(!this.componentInitInProgress&&!document.getElementsByClassName("embeddedServiceSidebar").length){this.componentInitInProgress=true;try{this.initLightningOut(this.loginPendingSerializedData);}catch(ae){this.componentInitInProgress=false;throw ae;}}};j.prototype.resumeInitWithSessionData=function X(ag){var af=this.fireEvent("sessionDataRetrieved",function(aj){return aj.indexOf(true)!==-1;},ag);var ae=false;var ai=false;var ah;if(this.settings.linkAction.valid){ae=true;}else{if(af){this.log("Existing session found. Continuing with data: "+ag);ae=true;ai=true;}}if(ae){this.componentInitInProgress=true;if(this.requireLogin){this.loginPendingSerializedData=ai?ag:undefined;ah=setInterval(function(){if(window.SFIDWidget){clearInterval(ah);if(window.SFIDWidget.openid_response){window[y]();}else{window.SFIDWidget.login();}}},F);}else{this.initLightningOut(ai?ag:undefined);}}if(this.settings.appendHelpButton){this.appendHelpButton(this.settings.displayHelpButton&&!af);}};j.prototype.postMessage=function l(af,ae){this.getESWFrame().postMessage({domain:this.settings.storageDomain,data:ae,method:af},this.settings.iframeURL);};j.prototype.setSessionData=function I(ag,ae){var af;if(typeof ag==="object"){af=ag;}else{af={};af[ag]=ae;}if(this.isFrameStorageEnabled()){this.postMessage("session.set",af);}else{Object.keys(af).forEach(function(ah){window.sessionStorage.setItem(ah,af[ah]);});}};j.prototype.deleteSessionData=function T(ae){var af;if(Array.isArray(ae)){af=ae;}else{af=[ae];}if(this.isFrameStorageEnabled()){this.postMessage("session.delete",af);}else{af.forEach(function(ag){window.sessionStorage.removeItem(ag);});}};j.prototype.defineFeature=function q(ae,af){this.featureScripts[ae]=af;};j.prototype.registerStorageKeys=function ac(ae){if(typeof ae==="string"){this.storageKeys.push(name);}else{ae.forEach(function(af){this.storageKeys.push(af);}.bind(this));}};j.prototype.addMessageHandler=function w(ae,af){this.messageHandlers[ae]=af;};j.prototype.loadStorageScript=function k(ae){if(this.isIframeReady){this.postMessage("script.load",ae);}else{this.iframeScriptsToLoad.push(ae);}};j.prototype.loadFeatures=function g(){this.settings.enabledFeatures.forEach(function(ae){if(this.availableFeatures.indexOf(ae.toLowerCase())===-1){this.loadFeatureScript(ae);}}.bind(this));};j.prototype.addEventHandler=function D(af,ae){if(!this.eventHandlers[af]){this.eventHandlers[af]=[];}this.eventHandlers[af].push(ae);};j.prototype.setupMessageListener=function J(){window.addEventListener("message",function(ah){var aj=ah.data;var ae=ah.origin.split(":")[1].replace("//","");var ag;var ai;var af;if(aj&&aj.method&&embedded_svc.isMessageFromSalesforceDomain(ae)){if(aj.method==="session.onLoad"&&this.settings.iframeURL.indexOf(ae)===-1){ai=this.settings.iframeURL.split("/")[2];af=ah.origin.split("/")[2];this.settings.iframeURL=this.settings.iframeURL.replace(ai,af);}ag=aj.method.split(".")[0].toLowerCase();if(this.availableFeatures.indexOf(ag)===-1){if(!this.pendingMessages[ag]){this.pendingMessages[ag]=[];}this.pendingMessages[ag].push({direction:"incoming",payload:aj});}else{this.handleMessage(aj);}}}.bind(this),false);};j.prototype.handleMessage=function a(ae){if(this.messageHandlers[ae.method]){this.messageHandlers[ae.method](ae.data);}else{this.log("Unregistered method "+ae.method+" received.");}};j.prototype.isMessageFromSalesforceDomain=function e(af){var ae=function(ah,ag){return ah.indexOf(ag,ah.length-ag.length)!==-1;};return x.some(function(ag){return ae(af,ag);});};j.prototype.addDefaultSetting=function W(af,ae){this.defaultSettings[af]=ae;};j.prototype.onButtonStatusChange=function K(){var ae=document.querySelector(".embeddedServiceHelpButton a");var af;if(ae){af=ae.querySelector(".message");if(af){if(this.isButtonDisabled){ae.onclick=function(){};ae.classList.remove("helpButtonEnabled");ae.classList.add("helpButtonDisabled");af.innerHTML=this.settings.disabledMinimizedText;}else{ae.onclick=this.onHelpButtonClick.bind(this);ae.classList.remove("helpButtonDisabled");ae.classList.add("helpButtonEnabled");af.innerHTML=this.settings.defaultMinimizedText;}}}};j.prototype.hideHelpButton=function ab(){var ae=document.querySelector("."+m);if(ae){ae.style.display="none";}};j.prototype.showHelpButton=function h(){var ae=document.querySelector("."+m);if(ae){ae.style.display="";}};j.prototype.setDefaultButtonText=function A(af,ae,ag){if(this.settings.entryFeature===af){this.settings.defaultMinimizedText=this.settings.defaultMinimizedText||ae;this.settings.disabledMinimizedText=this.settings.disabledMinimizedText||ag;}};j.prototype.registerLinkAction=function c(af,ae){var ag=this.settings.linkAction;if(ag.feature&&ag.name&&ag.feature.toLowerCase()===af.toLowerCase()&&ag.name.toLowerCase()===ae.toLowerCase()){ag.valid=true;ag.feature=af;this.settings.entryFeature=af;}};j.prototype.getLinkActionData=function r(){window.location.search.replace(/([a-zA-Z0-9._]+)=([^&\s]+)/g,function(af,ag,ai){var ah=ag.toLowerCase();var aj;var ae;if(ah.indexOf("snapins.")===0){ae=ah.replace("snapins.","");if(ae==="action"){aj=ai.split(".");if(aj.length===2){this.settings.linkAction.feature=aj[0];this.settings.linkAction.name=aj[1];}}else{this.settings.linkActionParameters[ae.toLowerCase()]=ai;}}}.bind(this));};j.prototype.requireAuthentication=function o(){var ae=document.createElement("script");var ag=document.createElement("style");var af=document.querySelector(this.settings.loginTargetQuerySelector);this.requireLogin=true;if(!this.settings.loginClientId||!this.settings.loginRedirectURL||!this.settings.loginTargetQuerySelector){throw new Error("Authentication with Field Service requires these valid settings params: loginClientId, loginRedirectURL, loginTargetQuerySelector.");}if(af){this.loginButtonPressed=false;af.addEventListener("click",function(){this.loginButtonPressed=true;}.bind(this));}else{throw new Error("loginTargetQuerySelector is not a valid DOM element.");}this.addMetaTag("salesforce-community",this.settings.communityEndpointURL);this.addMetaTag("salesforce-client-id",this.settings.loginClientId);this.addMetaTag("salesforce-redirect-uri",this.settings.loginRedirectURL);this.addMetaTag("salesforce-mode","popup");this.addMetaTag("salesforce-target",this.settings.loginTargetQuerySelector);this.addMetaTag("salesforce-login-handler",y);this.addMetaTag("salesforce-logout-handler",t);window[y]=function(){var ai=document.querySelector(this.settings.loginTargetQuerySelector);var ah=document.createElement("button");if(this.loginButtonPressed||this.componentInitInProgress){ai.innerHTML="";this.initLightningOut(this.loginPendingSerializedData);}ah.className="fieldServiceStart";ah.innerHTML=this.settings.fieldServiceStartLabel;ah.addEventListener("click",this.onHelpButtonClick.bind(this));ai.appendChild(ah);}.bind(this);window[t]=function(){window.location.reload();};document.head.appendChild(ag);ag.sheet.insertRule(".sfid-logout { display: none; }",0);ae.type="text/javascript";ae.src=this.settings.communityEndpointURL+"/servlet/servlet.loginwidgetcontroller?type=javascript_widget";document.head.appendChild(ae);};window.embedded_svc=new j();Object.getOwnPropertyNames(u).forEach(function(ae){var af=u[ae];if(af==="object"){window.embedded_svc[ae]={};Object.keys(af).forEach(function(ag){window.embedded_svc[ae][ag]=af[ag];});}else{window.embedded_svc[ae]=af;}});})(window.embedded_svc||{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.