<apex:page controller="SearchContactController" showHeader="false" id="page">
|
<apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
|
|
<head>
|
</head>
|
<style>
|
/* 1. 定义表单样式 To Do Later*/
|
|
table {
|
border-collapse: collapse;
|
border-spacing: 0;
|
width: 98%;
|
margin-left: 5px;
|
}
|
</style>
|
|
<body id="body" class="lookupTab">
|
<script type="text/javascript">
|
var staticResources = JSON.parse('{!staticResource}');
|
var contactAWSIds = JSON.parse('{!contactAWSIds}');
|
var contactsInfo = JSON.parse('{!contactsInfo}');
|
var searchContactName = '';
|
queryLeadFromAWS();
|
function searchAWSContact(){
|
console.log('Search process!');
|
//1. reset table;
|
resetTable();
|
//2. get contact name value
|
searchContactName = document.getElementById('page:form:lksrch').value;
|
queryLeadFromAWS();
|
|
}
|
function resetTable(){
|
let queryResult = document.getElementById('QueryResult');
|
let table = document.getElementById('table');
|
if(table){
|
queryResult.removeChild(table);
|
}
|
}
|
function preparePayloadForSearchContact(){
|
let searchPayload = new Object();
|
searchPayload.dataIds = contactAWSIds;
|
searchPayload.contactName = searchContactName;
|
return JSON.stringify(searchPayload);
|
}
|
function queryLeadFromAWS() {
|
//1. Prepare the payload for contact search
|
let requestSearchPayload = preparePayloadForSearchContact();
|
console.log('request payload body:'+requestSearchPayload);
|
//2. Invoke AWS Service
|
fetch(staticResources.searchUrl, {
|
method: 'POST',
|
body: requestSearchPayload,
|
headers: {
|
'Content-Type': 'application/json',
|
'pi-token': staticResources.token
|
}
|
}).then((data) => {
|
return data.json();
|
}).then((result) => {
|
if(result.object&&result.object.length>0){
|
initContactTable(result);
|
}
|
})
|
}
|
|
function redirectToParentPage(obj) {
|
var winMain = window.opener;
|
if (null == winMain) {
|
winMain = window.parent.opener;
|
}
|
let value = obj.currentTarget.innerText;
|
let contactInfo = new Object();
|
contactInfo.Name = obj.currentTarget.innerText;
|
contactInfo.ContactId = obj.currentTarget.id;
|
var selectedContactNode = winMain.document.getElementById('{!JSENCODE($CurrentPage.parameters.contactId)}');
|
selectedContactNode.value = JSON.stringify(contactInfo);
|
closeWindow();
|
}
|
|
function closeWindow() {
|
var winMain = window.opener;
|
if (null == winMain) {
|
winMain = window.parent.opener;
|
}
|
winMain.closePopupWindow();
|
window.close();
|
}
|
function refreshTable(cols,contactInfoList){
|
let myTableDiv = document.getElementById("QueryResult");
|
let table = document.createElement('TABLE');
|
table.border = '1';
|
table.id = 'table'
|
let tableBody = document.createElement('TBODY');
|
table.appendChild(tableBody);
|
let headerTR = document.createElement('TR');
|
tableBody.appendChild(headerTR);
|
for (let i = 0; i < cols.length; i++) {
|
let td = document.createElement('TH');
|
td.width = '75';
|
td.appendChild(document.createTextNode(cols[i]));
|
headerTR.appendChild(td);
|
}
|
//3. Init the AWS data
|
for (let i = 0; i < contactInfoList.length; i++) {
|
let tr = document.createElement('TR');
|
tableBody.appendChild(tr);
|
let contactInfoTemp = contactInfoList[i]
|
for (let j = 0; j < cols.length; j++) {
|
let td = document.createElement('TD');
|
td.width = '75';
|
if(j == 0){
|
td.id = contactsInfo[contactInfoTemp.AWSDataId].Id;
|
}
|
td.appendChild(document.createTextNode(contactInfoTemp[cols[j]]));
|
if (cols[j] == 'Name') {
|
td.addEventListener("click", function (obj) {
|
redirectToParentPage(obj);
|
});
|
}
|
tr.appendChild(td);
|
}
|
}
|
myTableDiv.appendChild(table);
|
}
|
function initContactTable(data) {
|
let cols = ['Name', 'Email', 'Phone'];
|
let contactInfoList = [];
|
let awsDataIds = [];
|
for(var i=0;i<data.object.length;i++){
|
if(data.object[i].dataId){
|
let contactInfo = new Object();
|
contactInfo.Name = data.object[i].lastName;
|
contactInfo.Email = data.object[i].email;
|
contactInfo.Phone = data.object[i].phone;
|
contactInfo.AWSDataId = data.object[i].dataId;
|
awsDataIds.push(contactInfo.AWSDataId);
|
contactInfo.sfRecordId = '';
|
contactInfoList.push(contactInfo);
|
}
|
}
|
let AWSIdToSFIdMapValue = {};
|
console.log(awsDataIds);
|
//Invoke SF BackEnd
|
Visualforce.remoting.Manager.invokeAction(
|
'{!$RemoteAction.SearchContactController.searchContacts}',
|
JSON.stringify(awsDataIds),
|
function (result, event) {
|
if(event.status){
|
if(result.status = 'success'){
|
contactsInfo = JSON.parse(result.message.replace(/("\;)/g,"\""));
|
refreshTable(cols,contactInfoList);
|
}else{
|
console.log('No result');
|
}
|
}
|
},
|
{ escape: true }
|
);
|
}
|
</script>
|
<apex:form id="form">
|
<!-- Search Filter-->
|
<div class="lookup">
|
<div class="bPageTitle">
|
<div class="ptBody secondaryPalette">
|
<div class="content">
|
<img src="/img/s.gif" alt="" class="pageTitleIcon" title="" />
|
<h1>Lookup</h1>
|
</div>
|
</div>
|
</div>
|
<div class="pBody">
|
<label class="assistiveText" for="lksrch">Search</label>
|
<apex:inputText id="lksrch" html-placeholder="{!PIPL_Search_Contact_Label}" value="{!searchKeyWord}" />
|
<input value=" Go! " type="Button" onclick="searchAWSContact()" styleClass="btn" />
|
<div class="bDescription">You can use "*" as a wildcard next to other characters to improve your search results.
|
</div>
|
</div>
|
</div>
|
<div id="QueryResult">
|
</div>
|
</apex:form>
|
</body>
|
</apex:page>
|