Advertisement
Guest User

leads.js

a guest
Jul 29th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /*
  4. All modal HTML goes here
  5. */
  6.  
  7. /*
  8. All initialization logic goes here
  9. */
  10. var leadList = []
  11. var components;
  12. //chrome.runtime.sendMessage({'function': 'loadTypekit'})
  13.  
  14.  
  15. // Check if salesforce page is a lead page and push leads (assume if there are query params, leadId is always first)
  16. if (getPageType() == "instant") {
  17. $("#topButtonRow").append("<input value='Sendbloom 1-to-1' id='sendbloomModal' class='btn' name='sendbloom' type='button'>")
  18. leadList.push(components[3].split("?")[0])
  19. // Check if salesforce page is a report page
  20. checkForClickToCall();
  21. } else if (getPageType() == "prospects") {
  22. // If the report is a lead report, then each row contains exactly one lead with many links to that lead
  23. // Get the first lead link of each row
  24. $(".tabularReportTable").find("tr").find("a[href*='00Q']:first").each(function () {
  25. if(components[2].includes("dbx.my.salesforce.com")){
  26. leadList.push(this.pathname.split("/")[1].split("?")[0])
  27. } else {
  28. leadList.push(this.pathname.split("/")[1])
  29. }
  30. })
  31. // The same but for contact reports
  32. $(".tabularReportTable").find("tr").find("a[href*='003']:first").each(function () {
  33. if(components[2].includes("dbx.my.salesforce.com")){
  34. leadList.push(this.pathname.split("/")[1].split("?")[0])
  35. } else {
  36. leadList.push(this.pathname.split("/")[1])
  37. }
  38. })
  39. // Only add the add to sendbloom button if there is at least one lead on the page
  40. if(leadList.length > 0) {
  41. $(".reportActions").append("<input value='Add to Sendbloom Prospects' id='sendbloomReportButton' class='btn' name='sendbloom' type='button'>")
  42. }
  43. }
  44.  
  45. function addLeadsToProspects() {
  46. // delegate to the background page, which will do the actual work
  47. var data = {
  48. 'loading':true,
  49. "prospects": leadList,
  50. 'report-name': $(".noSecondHeader").text()
  51. }
  52.  
  53. var reports_url = sendbloomDomain + "/labs/gmail/";
  54. var loading_reports_url = reports_url + "?loading=true"
  55. loadSendbloomOneToOne(loading_reports_url);
  56.  
  57. // Need to make a POST request to upload a list of prospects
  58. // GET request supports 2047 char max and limits the number of sf ids we can send
  59.  
  60. sendbloomApiCall({
  61. endpoint:'/api/prospects/',
  62. data:data,
  63. method:'POST',
  64. successCallback: function(data) {
  65. var reports_url_data = {
  66. report_name: $(".noSecondHeader").text(),
  67. reports: true,
  68. success: true,
  69. //loading: false,
  70. eligible_count: data.eligibleCount,
  71. ineligible_count: data.ineligibleCount,
  72. };
  73. var reports_url = sendbloomDomain + "/labs/gmail/?" + $.param(reports_url_data);
  74. $('#sb-iframe-container-modal').remove()
  75. loadSendbloomOneToOne(reports_url);
  76. },
  77. errorCallback: function(response) {
  78. var error = JSON.parse(response.responseText);
  79. var reports_url_data = {
  80. report_name: $(".noSecondHeader").text(),
  81. reports: true,
  82. success: false,
  83. //loading: false,
  84. ineligible_count: leadList.length
  85. };
  86. var reports_url = sendbloomDomain + "/labs/gmail/?" + $.param(reports_url_data);
  87.  
  88. $('#sb-iframe-container-modal').remove()
  89. loadSendbloomOneToOne(reports_url);
  90. },
  91. });
  92. }
  93.  
  94. function checkForClickToCall(){
  95. $.ajax({
  96. url: sendbloomDomain + '/settings/calling/verify',
  97. type : "POST",
  98. method: 'POST',
  99. dataType: 'json',
  100. }).done(function(data) {
  101. if(data.flag == "true"){
  102. $("#topButtonRow").append("<input value='Phone' id='phoneButton' class='btn' name='sendbloom' type='button'>");
  103. $("#topButtonRow").append("<input value='Mobile' id='mobileButton' class='btn' name='sendbloom' type='button'>");
  104. $('#phoneButton').bind('click', phoneButton);
  105. $('#mobileButton').bind('click', mobileButton);
  106. }
  107. }).fail(function(error) {
  108. });
  109. }
  110.  
  111. function getPageType() {
  112. components = document.location.href.split("/")
  113. if(components.length == 4
  114. // Adding in dropbox custom url hardcoded. Will generalize this for other users later
  115. // Either no query params and standard url or query params allowed with dbx.my.salesforce
  116. && (components[2].match(/.salesforce\.com/) || components[2].match(/dbx.my.salesforce.com/))
  117. && (components[3].match(/^00Q/) || components[3].match(/^003/))) {
  118. return "instant"
  119. } else if (components.length == 4
  120. && ((!components[3].match(/\?/)
  121. && components[2].match(/.salesforce\.com/)) ||
  122. components[2].match(/dbx.my.salesforce.com/))
  123. && components[3].match(/^00O/)) {
  124. return "prospects"
  125. } else {
  126. return null
  127. }
  128. }
  129.  
  130. /*Method to get prospect salesforce id from the url*/
  131. function getProspectSalesforceId() {
  132. components = document.location.pathname.split("/");
  133. for (var i=0; i<components.length; i++) {
  134. if (components[i].match(/^00Q/) || components[i].match(/^003/)) {
  135. return components[i];
  136. }
  137. }
  138. return null;
  139. }
  140.  
  141.  
  142. var modal_active = false;
  143.  
  144. $('#sendbloomModal').bind('click',function() {
  145. var lead_id = getProspectSalesforceId();
  146. var labs_url = sendbloomDomain + "/labs/gmail/?source=sfdc";
  147. if (lead_id) {
  148. labs_url = labs_url + "&prospect=" + lead_id.toString()
  149. }
  150. loadSendbloomOneToOne(labs_url)
  151. });
  152.  
  153. $('#sendbloomReportButton').bind('click',function() {
  154. addLeadsToProspects();
  155. });
  156.  
  157. function phoneButton(){
  158. var tds = document.getElementsByClassName("labelCol");
  159. var phoneLabel = {};
  160. for(var i = 0; i<tds.length; i++){
  161. if(tds[i].textContent == "Phone"){
  162. phoneLabel = tds[i];
  163. break;
  164. }
  165. }
  166. var phoneNumber = $.parseHTML(phoneLabel.nextSibling.innerHTML)[0].innerHTML;
  167. if(phoneNumber != "&nbsp;"){
  168. callProspect(phoneNumber);
  169. }else{
  170. alert("Prospect has no phone number");
  171. }
  172. }
  173.  
  174. function mobileButton(){
  175. var tds = document.getElementsByClassName("labelCol");
  176. var phoneLabel = {};
  177. for(var i = 0; i<tds.length; i++){
  178. if(tds[i].textContent == "Mobile"){
  179. phoneLabel = tds[i];
  180. break;
  181. }
  182. }
  183. var phoneNumber = $.parseHTML(phoneLabel.nextSibling.innerHTML)[0].innerHTML;
  184. if(phoneNumber != "&nbsp;"){
  185. callProspect(phoneNumber);
  186. }else{
  187. alert("Prospect has no mobile number");
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement