Last active
November 25, 2022 08:58
-
-
Save afawcett/6a38c589e3ae18ad2d16d4ee98e00b17 to your computer and use it in GitHub Desktop.
Salesforce London World Tour 2016: Lightning Out: Components on Any Platform
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
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" | |
controller="AddressFinderController" access="global"> | |
<aura:attribute name="accounts" type="Account[]"/> | |
<aura:attribute name="contacts" type="Contact[]"/> | |
<aura:registerEvent name="AddressInfo" type="c:AddressInfo"/> | |
<div class="slds-form--stacked"> | |
<div class="slds-form-element"> | |
<label class="slds-form-element__label" for="inputSample2">Account Search</label> | |
<div class="slds-form-element__control"> | |
<input id="inputSample2" class="slds-input" type="text" placeholder="Enter partial account name" onkeyup="{!c.onSearchKeyChange}" /> | |
</div> | |
</div> | |
<fieldset class="slds-form-element"> | |
<legend class="form-element__legend slds-form-element__label">Accounts</legend> | |
<div class="slds-form-element__control"> | |
<aura:iteration items="{!v.accounts}" var="account"> | |
<label class="slds-radio"> | |
<input type="radio" id="{!account.Id}" name="account" onchange="{!c.onAccountSelected}"/> | |
<span class="slds-radio--faux"></span> | |
<span class="slds-form-element__label">{!account.Name}</span> | |
</label> | |
</aura:iteration> | |
</div> | |
</fieldset> | |
<fieldset class="slds-form-element"> | |
<legend class="form-element__legend slds-form-element__label">Contacts</legend> | |
<div class="slds-form-element__control"> | |
<aura:iteration items="{!v.contacts}" var="contact"> | |
<label class="slds-radio"> | |
<input type="radio" id="{!contact.Id}" name="contact" onchange="{!c.onContactSelected}"/> | |
<span class="slds-radio--faux"></span> | |
<span class="slds-form-element__label">{!contact.Name}</span> | |
</label> | |
</aura:iteration> | |
</div> | |
</fieldset> | |
</div> | |
</aura:component> |
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
<aura:application extends="ltng:outApp" access="global" > | |
<ltng:require styles="https://lo-demo-london2016-summer-dev-ed.lightning.force.com//resource/slds103/assets/styles/salesforce-lightning-design-system-ltng.css" /> | |
<aura:dependency resource="c:AddressFinder"/> | |
</aura:application> |
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
public class AddressFinderController { | |
@AuraEnabled | |
public static List<Account> findByName(String searchKey) { | |
String name = '%' + searchKey + '%'; | |
return [SELECT id, name FROM Account WHERE name LIKE :name LIMIT 50]; | |
} | |
@AuraEnabled | |
public static List<Contact> findContactsByAccountId(Id accountId) { | |
return [SELECT id, name FROM Contact WHERE AccountId = :accountId LIMIT 50]; | |
} | |
@AuraEnabled | |
public static AddressInfo getAddressInfo(Id contactId) { | |
Contact contact = [select Id, Name, AccountId from Contact where Id = :contactId]; | |
Account account = [select Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry from Account where Id = :contact.AccountId]; | |
AddressInfo address = new AddressInfo(); | |
address.AddressLine1 = contact.Name; | |
address.AddressLine2 = account.BillingStreet; | |
address.AddressLine3 = account.BillingCity; | |
address.AddressLine4 = account.BillingPostalCode; | |
address.AddressLine5 = account.BillingCountry; | |
return address; | |
} | |
} |
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
({ | |
onSearchKeyChange : function(cmp, event, helper) { | |
helper.searchKeyChange(cmp, event); | |
}, | |
onAccountSelected : function(cmp, event, helper) { | |
helper.accountSelected(cmp, event); | |
}, | |
onContactSelected : function(cmp, event, helper) { | |
helper.contactSelected(cmp, event); | |
} | |
}) |
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
({ | |
searchKeyChange: function (cmp, event) { | |
var action = cmp.get("c.findByName"); | |
action.setParams({ "searchKey": event.target.value }); | |
action.setCallback(this, function(a) { | |
cmp.set("v.accounts", a.getReturnValue()); | |
}); | |
$A.enqueueAction(action); | |
}, | |
accountSelected : function(cmp, event) { | |
var action = cmp.get("c.findContactsByAccountId"); | |
action.setParams({ "accountId": event.target.id }); | |
action.setCallback(this, function(a) { | |
cmp.set("v.contacts", a.getReturnValue()); | |
}); | |
$A.enqueueAction(action); | |
}, | |
contactSelected : function(cmp, event) { | |
var contactId = event.target.id; | |
var action = cmp.get("c.getAddressInfo"); | |
action.setParams({ "contactId": contactId }); | |
action.setCallback(this, function(a) { | |
console.log(a.getReturnValue()); | |
var compEvent = $A.get("e.c:AddressInfo"); | |
compEvent.setParams({"address" : a.getReturnValue() } ); | |
compEvent.fire(); | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |
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
global class AddressInfo { | |
@AuraEnabled | |
global String AddressLine1; | |
@AuraEnabled | |
global String AddressLine2; | |
@AuraEnabled | |
global String AddressLine3; | |
@AuraEnabled | |
global String AddressLine4; | |
@AuraEnabled | |
global String AddressLine5; | |
} |
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
<aura:event type="APPLICATION" access="global"> | |
<aura:attribute name="address" type="c.AddressInfo"/> | |
</aura:event> |
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
/** | |
* Creates a menu entry in the Google Docs UI when the document is opened. | |
* | |
* @param {object} e The event parameter for a simple onOpen trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. | |
*/ | |
function onOpen(e) { | |
DocumentApp.getUi().createAddonMenu() | |
.addItem('Start', 'showSidebar') | |
.addItem('Log Out', 'logout') | |
.addToUi(); | |
} | |
/** | |
* Runs when the add-on is installed. | |
* | |
* @param {object} e The event parameter for a simple onInstall trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. (In practice, onInstall triggers always | |
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or | |
* AuthMode.NONE.) | |
*/ | |
function onInstall(e) { | |
onOpen(e); | |
} | |
/** | |
* Opens a sidebar in the document containing the add-on's user interface. | |
*/ | |
function showSidebar() { | |
var driveService = getDriveService(); | |
if (!driveService.hasAccess()) { | |
var authorizationUrl = driveService.getAuthorizationUrl(); | |
var template = HtmlService.createTemplate( | |
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' + | |
'Reopen the sidebar when the authorization is complete.'); | |
template.authorizationUrl = authorizationUrl; | |
var page = template.evaluate(); | |
DocumentApp.getUi().showSidebar(page); | |
} else { | |
var ui = HtmlService.createHtmlOutputFromFile('Sidebar') | |
.setTitle('Lightning Out Demo') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
DocumentApp.getUi().showSidebar(ui); | |
} | |
} | |
function getDriveService() { | |
// Create a new service with the given name. The name will be used when | |
// persisting the authorized token, so ensure it is unique within the | |
// scope of the property store. | |
return OAuth2.createService('salesforce') | |
// Set the endpoint URLs, which are the same for all Google services. | |
.setAuthorizationBaseUrl('https://login.salesforce.com/services/oauth2/authorize') | |
.setTokenUrl('https://login.salesforce.com/services/oauth2/token') | |
// Set the client ID and secret, from the Google Developers Console. | |
.setClientId('yourclientid') | |
.setClientSecret('yourclientsecret') | |
// Set the name of the callback function in the script referenced | |
// above that should be invoked to complete the OAuth flow. | |
.setCallbackFunction('authCallback') | |
// Set the property store where authorized tokens should be persisted. | |
.setPropertyStore(PropertiesService.getUserProperties()) | |
} | |
/** | |
* Runs when the add-on is installed. | |
* | |
* @param {object} request HTTP request | |
*/ | |
function authCallback(request) { | |
var driveService = getDriveService(); | |
var isAuthorized = driveService.handleCallback(request); | |
if (isAuthorized) { | |
return HtmlService.createHtmlOutput('Success! You can close this tab.'); | |
} else { | |
return HtmlService.createHtmlOutput('Denied. You can close this tab'); | |
} | |
} | |
/** | |
* Logoff | |
*/ | |
function logout(){ | |
OAuth2.createService('salesforce') | |
.setPropertyStore(PropertiesService.getUserProperties()) | |
.reset(); | |
} | |
/** | |
* Adds the given address to the document | |
**/ | |
function addAddresss(address) { | |
var doc = DocumentApp.getActiveDocument(); | |
var body = doc.getBody(); | |
body.appendParagraph(address.AddressLine1); | |
if(address.AddressLine2!=null) { | |
body.appendParagraph(address.AddressLine2); | |
} | |
if(address.AddressLine3!=null) { | |
body.appendParagraph(address.AddressLine3); | |
} | |
if(address.AddressLine4!=null) { | |
body.appendParagraph(address.AddressLine4); | |
} | |
if(address.AddressLine5!=null) { | |
body.appendParagraph(address.AddressLine5); | |
} | |
} | |
/** | |
* Returns the Salesforce oAuth token to the client | |
*/ | |
function getSalesforce() { | |
var salesforce = { | |
token: getDriveService().getAccessToken() | |
}; | |
return salesforce; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment