Forked from thebrettbarlow/LightningComponentInVisualforce.resource
Created
September 23, 2021 01:59
-
-
Save lukaszhanusik/8364d81e7e73756954e889f1ba289be4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by brettbarlow on 2/24/18. | |
*/ | |
// LightningComponentInVisualforce | |
window._lightningComponentInVisualforce = (function() { | |
return { | |
LightningComponent : function(type, attributes, locator) { | |
this.type = type; | |
this.attributes = attributes; | |
this.locator = locator; | |
}, | |
addEventHandlers : function(lightningAppName, lightningComponents, uiTheme) { | |
// Theme Name | Description | |
// ---------------------------------------------------------------------- | |
// Theme1 | Obsolete Salesforce theme | |
// Theme2 | Salesforce Classic 2005 user interface theme | |
// Theme3 | Salesforce Classic 2010 user interface theme | |
// Theme4d | Salesforce Lightning Experience user interface theme | |
// Theme4t | Salesforce mobile app theme | |
// Theme4u | Lightning Console theme | |
// PortalDefault | Salesforce Customer Portal theme | |
// Webstore | Salesforce AppExchange theme | |
var isLightningUiTheme = (uiTheme === 'Theme4d' || uiTheme === 'Theme4t') ? true : false; | |
var isClassicUiTheme = (uiTheme === 'Theme3') ? true : false; | |
// Lightning Event Reference: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_events.htm | |
// addEventHandler(String event, Function handler, String phase, String includeFacets) | |
var lightningEventHandler = function(component) { | |
// force:navigateToSObject | |
component.addEventHandler('force:navigateToSObject', function(event) { | |
if(isLightningUiTheme) { | |
sforce.one.navigateToSObject(event.getParam('recordId')); | |
} else if(isClassicUiTheme) { | |
window.parent.location = '/' + event.getParam('recordId'); | |
} else { | |
console.error('Unsupported $User.UITheme: ' + uiTheme); | |
} | |
}) | |
// force:navigateToURL | |
component.addEventHandler('force:navigateToURL', function(event) { | |
if(isLightningUiTheme) { | |
sforce.one.navigateToURL(event.getParam('url')); | |
} else if(isClassicUiTheme) { | |
window.parent.location = event.getParam('url'); | |
} else { | |
console.error('Unsupported $User.UITheme: ' + uiTheme); | |
} | |
}) | |
// force:refreshView | |
component.addEventHandler('force:refreshView', function(event) { | |
if(isLightningUiTheme) { | |
sforce.one.refreshView(); | |
} else if(isClassicUiTheme) { | |
window.parent.location.reload(); | |
} else { | |
console.error('Unsupported $User.UITheme: ' + uiTheme); | |
} | |
}) | |
// force:showToast | |
component.addEventHandler('force:showToast', function(event) { | |
// Inspired by this blog post: http://joebuschmann.com/salesforce-lightning-navigation-events-in-a-visualforce-page/ | |
if(isLightningUiTheme && SfdcApp && SfdcApp.projectOneNavigator && SfdcApp.projectOneNavigator.fireContainerEvent) { | |
// Not officially supported by Salesforce | |
SfdcApp.projectOneNavigator.fireContainerEvent('force:showToast', event.getParams()); | |
} else if(isClassicUiTheme) { | |
// TODO: Roll my own toast with SLDS | |
window.alert(event.getParam('message')); | |
} else { | |
console.error('Unsupported $User.UITheme: ' + uiTheme); | |
} | |
}) | |
}; | |
// Lightning Out Markup Docs: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/lightning_out_markup.htm | |
// $Lightning.use(String appName, Function callback, String lightningEndPointURI, String authToken) | |
// $Lightning.createComponent(String type, Object attributes, String locator, Function callback) | |
// We can call $Lightning.use() more than once, but we must reference the same dependency app in every call | |
// Because of this, I call $Lightning.use() once and then and call $Lightning.createComponent forEach component passed into the function | |
$Lightning.use(lightningAppName, function() { | |
lightningComponents.forEach(function(lightningComponent) { | |
$Lightning.createComponent(lightningComponent.type, lightningComponent.attributes, lightningComponent.locator, lightningEventHandler); | |
}); | |
}); | |
}, | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment