@4site/engrid-scripts 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/.prettierrc.js +226 -0
  2. package/LICENSE +24 -0
  3. package/dist/a11y.d.ts +6 -0
  4. package/dist/a11y.js +46 -0
  5. package/dist/add-name-to-message.d.ts +5 -0
  6. package/dist/add-name-to-message.js +49 -0
  7. package/dist/advocacy.d.ts +7 -0
  8. package/dist/advocacy.js +34 -0
  9. package/dist/amount-label.d.ts +6 -0
  10. package/dist/amount-label.js +29 -0
  11. package/dist/app.d.ts +19 -0
  12. package/dist/app.js +340 -0
  13. package/dist/apple-pay.d.ts +13 -0
  14. package/dist/apple-pay.js +169 -0
  15. package/dist/auto-country-select.d.ts +10 -0
  16. package/dist/auto-country-select.js +63 -0
  17. package/dist/auto-year.d.ts +7 -0
  18. package/dist/auto-year.js +35 -0
  19. package/dist/autocomplete.d.ts +5 -0
  20. package/dist/autocomplete.js +38 -0
  21. package/dist/autosubmit.d.ts +5 -0
  22. package/dist/autosubmit.js +14 -0
  23. package/dist/branding-html.d.ts +14 -0
  24. package/dist/branding-html.js +70 -0
  25. package/dist/capitalize-fields.d.ts +7 -0
  26. package/dist/capitalize-fields.js +22 -0
  27. package/dist/click-to-expand.d.ts +4 -0
  28. package/dist/click-to-expand.js +39 -0
  29. package/dist/cookie.d.ts +53 -0
  30. package/dist/cookie.js +76 -0
  31. package/dist/country-disable.d.ts +4 -0
  32. package/dist/country-disable.js +22 -0
  33. package/dist/country-redirect.d.ts +7 -0
  34. package/dist/country-redirect.js +61 -0
  35. package/dist/custom-currency.d.ts +9 -0
  36. package/dist/custom-currency.js +112 -0
  37. package/dist/data-attributes.d.ts +5 -0
  38. package/dist/data-attributes.js +122 -0
  39. package/dist/data-hide.d.ts +15 -0
  40. package/dist/data-hide.js +66 -0
  41. package/dist/data-layer.d.ts +23 -0
  42. package/dist/data-layer.js +312 -0
  43. package/dist/data-replace.d.ts +26 -0
  44. package/dist/data-replace.js +76 -0
  45. package/dist/debug-hidden-fields.d.ts +4 -0
  46. package/dist/debug-hidden-fields.js +42 -0
  47. package/dist/debug-panel.d.ts +27 -0
  48. package/dist/debug-panel.js +508 -0
  49. package/dist/deprecated.d.ts +5 -0
  50. package/dist/deprecated.js +40 -0
  51. package/dist/digital-wallets.d.ts +8 -0
  52. package/dist/digital-wallets.js +143 -0
  53. package/dist/ecard-to-target.d.ts +11 -0
  54. package/dist/ecard-to-target.js +73 -0
  55. package/dist/ecard.d.ts +8 -0
  56. package/dist/ecard.js +61 -0
  57. package/dist/embedded-ecard.d.ts +18 -0
  58. package/dist/embedded-ecard.js +267 -0
  59. package/dist/en-validators.d.ts +10 -0
  60. package/dist/en-validators.js +89 -0
  61. package/dist/engrid.d.ts +49 -0
  62. package/dist/engrid.js +547 -0
  63. package/dist/event-tickets.d.ts +3 -0
  64. package/dist/event-tickets.js +24 -0
  65. package/dist/events/country.d.ts +12 -0
  66. package/dist/events/country.js +42 -0
  67. package/dist/events/donation-amount.d.ts +16 -0
  68. package/dist/events/donation-amount.js +119 -0
  69. package/dist/events/donation-frequency.d.ts +17 -0
  70. package/dist/events/donation-frequency.js +115 -0
  71. package/dist/events/en-form.d.ts +20 -0
  72. package/dist/events/en-form.js +52 -0
  73. package/dist/events/index.d.ts +6 -0
  74. package/dist/events/index.js +6 -0
  75. package/dist/events/processing-fees.d.ts +20 -0
  76. package/dist/events/processing-fees.js +92 -0
  77. package/dist/events/remember-me-events.d.ts +18 -0
  78. package/dist/events/remember-me-events.js +36 -0
  79. package/dist/exit-intent-lightbox.d.ts +14 -0
  80. package/dist/exit-intent-lightbox.js +145 -0
  81. package/dist/expand-region-name.d.ts +7 -0
  82. package/dist/expand-region-name.js +44 -0
  83. package/dist/fast-form-fill.d.ts +8 -0
  84. package/dist/fast-form-fill.js +75 -0
  85. package/dist/freshaddress.d.ts +18 -0
  86. package/dist/freshaddress.js +204 -0
  87. package/dist/give-by-select.d.ts +9 -0
  88. package/dist/give-by-select.js +82 -0
  89. package/dist/iframe.d.ts +16 -0
  90. package/dist/iframe.js +217 -0
  91. package/dist/index.d.ts +82 -0
  92. package/dist/index.js +84 -0
  93. package/dist/input-has-value-and-focus.d.ts +9 -0
  94. package/dist/input-has-value-and-focus.js +50 -0
  95. package/dist/input-placeholders.d.ts +7 -0
  96. package/dist/input-placeholders.js +82 -0
  97. package/dist/interfaces/ecard-to-target-options.d.ts +9 -0
  98. package/dist/interfaces/ecard-to-target-options.js +8 -0
  99. package/dist/interfaces/embedded-ecard-options.d.ts +8 -0
  100. package/dist/interfaces/embedded-ecard-options.js +7 -0
  101. package/dist/interfaces/exit-intent-options.d.ts +13 -0
  102. package/dist/interfaces/exit-intent-options.js +13 -0
  103. package/dist/interfaces/options.d.ts +147 -0
  104. package/dist/interfaces/options.js +50 -0
  105. package/dist/interfaces/translate-options.d.ts +7 -0
  106. package/dist/interfaces/translate-options.js +42 -0
  107. package/dist/interfaces/upsell-options.d.ts +24 -0
  108. package/dist/interfaces/upsell-options.js +33 -0
  109. package/dist/live-currency.d.ts +15 -0
  110. package/dist/live-currency.js +140 -0
  111. package/dist/live-frequency.d.ts +13 -0
  112. package/dist/live-frequency.js +109 -0
  113. package/dist/live-variables.d.ts +21 -0
  114. package/dist/live-variables.js +147 -0
  115. package/dist/loader.d.ts +10 -0
  116. package/dist/loader.js +175 -0
  117. package/dist/logger.d.ts +16 -0
  118. package/dist/logger.js +84 -0
  119. package/dist/media-attribution.d.ts +4 -0
  120. package/dist/media-attribution.js +66 -0
  121. package/dist/menu.d.ts +0 -0
  122. package/dist/menu.js +1 -0
  123. package/dist/min-max-amount.d.ts +13 -0
  124. package/dist/min-max-amount.js +71 -0
  125. package/dist/mobile-cta.d.ts +9 -0
  126. package/dist/mobile-cta.js +71 -0
  127. package/dist/modal.d.ts +19 -0
  128. package/dist/modal.js +129 -0
  129. package/dist/neverbounce.d.ts +23 -0
  130. package/dist/neverbounce.js +244 -0
  131. package/dist/other-amount.d.ts +6 -0
  132. package/dist/other-amount.js +70 -0
  133. package/dist/page-background.d.ts +7 -0
  134. package/dist/page-background.js +55 -0
  135. package/dist/plaid.d.ts +6 -0
  136. package/dist/plaid.js +49 -0
  137. package/dist/postal-code-validator.d.ts +20 -0
  138. package/dist/postal-code-validator.js +118 -0
  139. package/dist/premium-gift.d.ts +10 -0
  140. package/dist/premium-gift.js +115 -0
  141. package/dist/progress-bar.d.ts +3 -0
  142. package/dist/progress-bar.js +40 -0
  143. package/dist/remember-me.d.ts +61 -0
  144. package/dist/remember-me.js +358 -0
  145. package/dist/required-if-visible.d.ts +8 -0
  146. package/dist/required-if-visible.js +60 -0
  147. package/dist/set-attr.d.ts +4 -0
  148. package/dist/set-attr.js +31 -0
  149. package/dist/set-recurr-freq.d.ts +7 -0
  150. package/dist/set-recurr-freq.js +70 -0
  151. package/dist/show-hide-radio-checkboxes.d.ts +13 -0
  152. package/dist/show-hide-radio-checkboxes.js +161 -0
  153. package/dist/show-if-amount.d.ts +15 -0
  154. package/dist/show-if-amount.js +117 -0
  155. package/dist/show-if-present.d.ts +7 -0
  156. package/dist/show-if-present.js +79 -0
  157. package/dist/skip-link.d.ts +4 -0
  158. package/dist/skip-link.js +36 -0
  159. package/dist/src-defer.d.ts +6 -0
  160. package/dist/src-defer.js +70 -0
  161. package/dist/supporter-hub.d.ts +9 -0
  162. package/dist/supporter-hub.js +75 -0
  163. package/dist/swap-amounts.d.ts +45 -0
  164. package/dist/swap-amounts.js +81 -0
  165. package/dist/thank-you-page-conditional-content.d.ts +8 -0
  166. package/dist/thank-you-page-conditional-content.js +46 -0
  167. package/dist/ticker.d.ts +11 -0
  168. package/dist/ticker.js +66 -0
  169. package/dist/tidycontact.d.ts +46 -0
  170. package/dist/tidycontact.js +1202 -0
  171. package/dist/translate-fields.d.ts +12 -0
  172. package/dist/translate-fields.js +548 -0
  173. package/dist/universal-opt-in.d.ts +7 -0
  174. package/dist/universal-opt-in.js +91 -0
  175. package/dist/upsell-lightbox.d.ts +31 -0
  176. package/dist/upsell-lightbox.js +363 -0
  177. package/dist/url-params-to-body-attrs.d.ts +5 -0
  178. package/dist/url-params-to-body-attrs.js +14 -0
  179. package/dist/url-to-form.d.ts +7 -0
  180. package/dist/url-to-form.js +32 -0
  181. package/dist/us-only-form.d.ts +4 -0
  182. package/dist/us-only-form.js +30 -0
  183. package/dist/version.d.ts +1 -0
  184. package/dist/version.js +1 -0
  185. package/dist/vgs.d.ts +16 -0
  186. package/dist/vgs.js +250 -0
  187. package/dist/welcome-back.d.ts +10 -0
  188. package/dist/welcome-back.js +112 -0
  189. package/package.json +32 -0
@@ -0,0 +1,1202 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { EnForm, ENGrid, EngridLogger } from ".";
11
+ export class TidyContact {
12
+ constructor() {
13
+ var _a, _b, _c, _d, _e;
14
+ this.logger = new EngridLogger("TidyContact", "#FFFFFF", "#4d9068", "📧");
15
+ this.endpoint = "https://api.tidycontact.io";
16
+ this.wasCalled = false; // True if the API endpoint was called
17
+ this.httpStatus = 0;
18
+ this.timeout = 5; // Seconds to API Timeout
19
+ this.isDirty = false; // True if the address was changed by the user
20
+ this._form = EnForm.getInstance();
21
+ this.countries_list = [
22
+ ["Afghanistan", "af", "93", "070 123 4567"],
23
+ ["Albania", "al", "355", "067 212 3456"],
24
+ ["Algeria", "dz", "213", "0551 23 45 67"],
25
+ ["American Samoa", "as", "1", "(684) 733-1234"],
26
+ ["Andorra", "ad", "376", "312 345"],
27
+ ["Angola", "ao", "244", "923 123 456"],
28
+ ["Anguilla", "ai", "1", "(264) 235-1234"],
29
+ ["Antigua and Barbuda", "ag", "1", "(268) 464-1234"],
30
+ ["Argentina", "ar", "54", "011 15-2345-6789"],
31
+ ["Armenia", "am", "374", "077 123456"],
32
+ ["Aruba", "aw", "297", "560 1234"],
33
+ ["Australia", "au", "61", "0412 345 678"],
34
+ ["Austria", "at", "43", "0664 123456"],
35
+ ["Azerbaijan", "az", "994", "040 123 45 67"],
36
+ ["Bahamas", "bs", "1", "(242) 359-1234"],
37
+ ["Bahrain", "bh", "973", "3600 1234"],
38
+ ["Bangladesh", "bd", "880", "01812-345678"],
39
+ ["Barbados", "bb", "1", "(246) 250-1234"],
40
+ ["Belarus", "by", "375", "8 029 491-19-11"],
41
+ ["Belgium", "be", "32", "0470 12 34 56"],
42
+ ["Belize", "bz", "501", "622-1234"],
43
+ ["Benin", "bj", "229", "90 01 12 34"],
44
+ ["Bermuda", "bm", "1", "(441) 370-1234"],
45
+ ["Bhutan", "bt", "975", "17 12 34 56"],
46
+ ["Bolivia", "bo", "591", "71234567"],
47
+ ["Bosnia and Herzegovina", "ba", "387", "061 123 456"],
48
+ ["Botswana", "bw", "267", "71 123 456"],
49
+ ["Brazil", "br", "55", "(11) 96123-4567"],
50
+ ["British Indian Ocean Territory", "io", "246", "380 1234"],
51
+ ["British Virgin Islands", "vg", "1", "(284) 300-1234"],
52
+ ["Brunei", "bn", "673", "712 3456"],
53
+ ["Bulgaria", "bg", "359", "048 123 456"],
54
+ ["Burkina Faso", "bf", "226", "70 12 34 56"],
55
+ ["Burundi", "bi", "257", "79 56 12 34"],
56
+ ["Cambodia", "kh", "855", "091 234 567"],
57
+ ["Cameroon", "cm", "237", "6 71 23 45 67"],
58
+ ["Canada", "ca", "1", "(506) 234-5678"],
59
+ ["Cape Verde", "cv", "238", "991 12 34"],
60
+ ["Caribbean Netherlands", "bq", "599", "318 1234"],
61
+ ["Cayman Islands", "ky", "1", "(345) 323-1234"],
62
+ ["Central African Republic", "cf", "236", "70 01 23 45"],
63
+ ["Chad", "td", "235", "63 01 23 45"],
64
+ ["Chile", "cl", "56", "(2) 2123 4567"],
65
+ ["China", "cn", "86", "131 2345 6789"],
66
+ ["Christmas Island", "cx", "61", "0412 345 678"],
67
+ ["Cocos Islands", "cc", "61", "0412 345 678"],
68
+ ["Colombia", "co", "57", "321 1234567"],
69
+ ["Comoros", "km", "269", "321 23 45"],
70
+ ["Congo", "cd", "243", "0991 234 567"],
71
+ ["Congo", "cg", "242", "06 123 4567"],
72
+ ["Cook Islands", "ck", "682", "71 234"],
73
+ ["Costa Rica", "cr", "506", "8312 3456"],
74
+ ["Côte d’Ivoire", "ci", "225", "01 23 45 6789"],
75
+ ["Croatia", "hr", "385", "092 123 4567"],
76
+ ["Cuba", "cu", "53", "05 1234567"],
77
+ ["Curaçao", "cw", "599", "9 518 1234"],
78
+ ["Cyprus", "cy", "357", "96 123456"],
79
+ ["Czech Republic", "cz", "420", "601 123 456"],
80
+ ["Denmark", "dk", "45", "32 12 34 56"],
81
+ ["Djibouti", "dj", "253", "77 83 10 01"],
82
+ ["Dominica", "dm", "1", "(767) 225-1234"],
83
+ ["Dominican Republic", "do", "1", "(809) 234-5678"],
84
+ ["Ecuador", "ec", "593", "099 123 4567"],
85
+ ["Egypt", "eg", "20", "0100 123 4567"],
86
+ ["El Salvador", "sv", "503", "7012 3456"],
87
+ ["Equatorial Guinea", "gq", "240", "222 123 456"],
88
+ ["Eritrea", "er", "291", "07 123 456"],
89
+ ["Estonia", "ee", "372", "5123 4567"],
90
+ ["Eswatini", "sz", "268", "7612 3456"],
91
+ ["Ethiopia", "et", "251", "091 123 4567"],
92
+ ["Falkland Islands", "fk", "500", "51234"],
93
+ ["Faroe Islands", "fo", "298", "211234"],
94
+ ["Fiji", "fj", "679", "701 2345"],
95
+ ["Finland", "fi", "358", "041 2345678"],
96
+ ["France", "fr", "33", "06 12 34 56 78"],
97
+ ["French Guiana", "gf", "594", "0694 20 12 34"],
98
+ ["French Polynesia", "pf", "689", "87 12 34 56"],
99
+ ["Gabon", "ga", "241", "06 03 12 34"],
100
+ ["Gambia", "gm", "220", "301 2345"],
101
+ ["Georgia", "ge", "995", "555 12 34 56"],
102
+ ["Germany", "de", "49", "01512 3456789"],
103
+ ["Ghana", "gh", "233", "023 123 4567"],
104
+ ["Gibraltar", "gi", "350", "57123456"],
105
+ ["Greece", "gr", "30", "691 234 5678"],
106
+ ["Greenland", "gl", "299", "22 12 34"],
107
+ ["Grenada", "gd", "1", "(473) 403-1234"],
108
+ ["Guadeloupe", "gp", "590", "0690 00 12 34"],
109
+ ["Guam", "gu", "1", "(671) 300-1234"],
110
+ ["Guatemala", "gt", "502", "5123 4567"],
111
+ ["Guernsey", "gg", "44", "07781 123456"],
112
+ ["Guinea", "gn", "224", "601 12 34 56"],
113
+ ["Guinea-Bissau", "gw", "245", "955 012 345"],
114
+ ["Guyana", "gy", "592", "609 1234"],
115
+ ["Haiti", "ht", "509", "34 10 1234"],
116
+ ["Honduras", "hn", "504", "9123-4567"],
117
+ ["Hong Kong", "hk", "852", "5123 4567"],
118
+ ["Hungary", "hu", "36", "06 20 123 4567"],
119
+ ["Iceland", "is", "354", "611 1234"],
120
+ ["India", "in", "91", "081234 56789"],
121
+ ["Indonesia", "id", "62", "0812-345-678"],
122
+ ["Iran", "ir", "98", "0912 345 6789"],
123
+ ["Iraq", "iq", "964", "0791 234 5678"],
124
+ ["Ireland", "ie", "353", "085 012 3456"],
125
+ ["Isle of Man", "im", "44", "07924 123456"],
126
+ ["Israel", "il", "972", "050-234-5678"],
127
+ ["Italy", "it", "39", "312 345 6789"],
128
+ ["Jamaica", "jm", "1", "(876) 210-1234"],
129
+ ["Japan", "jp", "81", "090-1234-5678"],
130
+ ["Jersey", "je", "44", "07797 712345"],
131
+ ["Jordan", "jo", "962", "07 9012 3456"],
132
+ ["Kazakhstan", "kz", "7", "8 (771) 000 9998"],
133
+ ["Kenya", "ke", "254", "0712 123456"],
134
+ ["Kiribati", "ki", "686", "72001234"],
135
+ ["Kosovo", "xk", "383", "043 201 234"],
136
+ ["Kuwait", "kw", "965", "500 12345"],
137
+ ["Kyrgyzstan", "kg", "996", "0700 123 456"],
138
+ ["Laos", "la", "856", "020 23 123 456"],
139
+ ["Latvia", "lv", "371", "21 234 567"],
140
+ ["Lebanon", "lb", "961", "71 123 456"],
141
+ ["Lesotho", "ls", "266", "5012 3456"],
142
+ ["Liberia", "lr", "231", "077 012 3456"],
143
+ ["Libya", "ly", "218", "091-2345678"],
144
+ ["Liechtenstein", "li", "423", "660 234 567"],
145
+ ["Lithuania", "lt", "370", "(8-612) 34567"],
146
+ ["Luxembourg", "lu", "352", "628 123 456"],
147
+ ["Macau", "mo", "853", "6612 3456"],
148
+ ["North Macedonia", "mk", "389", "072 345 678"],
149
+ ["Madagascar", "mg", "261", "032 12 345 67"],
150
+ ["Malawi", "mw", "265", "0991 23 45 67"],
151
+ ["Malaysia", "my", "60", "012-345 6789"],
152
+ ["Maldives", "mv", "960", "771-2345"],
153
+ ["Mali", "ml", "223", "65 01 23 45"],
154
+ ["Malta", "mt", "356", "9696 1234"],
155
+ ["Marshall Islands", "mh", "692", "235-1234"],
156
+ ["Martinique", "mq", "596", "0696 20 12 34"],
157
+ ["Mauritania", "mr", "222", "22 12 34 56"],
158
+ ["Mauritius", "mu", "230", "5251 2345"],
159
+ ["Mayotte", "yt", "262", "0639 01 23 45"],
160
+ ["Mexico", "mx", "52", "222 123 4567"],
161
+ ["Micronesia", "fm", "691", "350 1234"],
162
+ ["Moldova", "md", "373", "0621 12 345"],
163
+ ["Monaco", "mc", "377", "06 12 34 56 78"],
164
+ ["Mongolia", "mn", "976", "8812 3456"],
165
+ ["Montenegro", "me", "382", "067 622 901"],
166
+ ["Montserrat", "ms", "1", "(664) 492-3456"],
167
+ ["Morocco", "ma", "212", "0650-123456"],
168
+ ["Mozambique", "mz", "258", "82 123 4567"],
169
+ ["Myanmar", "mm", "95", "09 212 3456"],
170
+ ["Namibia", "na", "264", "081 123 4567"],
171
+ ["Nauru", "nr", "674", "555 1234"],
172
+ ["Nepal", "np", "977", "984-1234567"],
173
+ ["Netherlands", "nl", "31", "06 12345678"],
174
+ ["New Caledonia", "nc", "687", "75.12.34"],
175
+ ["New Zealand", "nz", "64", "021 123 4567"],
176
+ ["Nicaragua", "ni", "505", "8123 4567"],
177
+ ["Niger", "ne", "227", "93 12 34 56"],
178
+ ["Nigeria", "ng", "234", "0802 123 4567"],
179
+ ["Niue", "nu", "683", "888 4012"],
180
+ ["Norfolk Island", "nf", "672", "3 81234"],
181
+ ["North Korea", "kp", "850", "0192 123 4567"],
182
+ ["Northern Mariana Islands", "mp", "1", "(670) 234-5678"],
183
+ ["Norway", "no", "47", "406 12 345"],
184
+ ["Oman", "om", "968", "9212 3456"],
185
+ ["Pakistan", "pk", "92", "0301 2345678"],
186
+ ["Palau", "pw", "680", "620 1234"],
187
+ ["Palestine", "ps", "970", "0599 123 456"],
188
+ ["Panama", "pa", "507", "6123-4567"],
189
+ ["Papua New Guinea", "pg", "675", "7012 3456"],
190
+ ["Paraguay", "py", "595", "0961 456789"],
191
+ ["Peru", "pe", "51", "912 345 678"],
192
+ ["Philippines", "ph", "63", "0905 123 4567"],
193
+ ["Poland", "pl", "48", "512 345 678"],
194
+ ["Portugal", "pt", "351", "912 345 678"],
195
+ ["Puerto Rico", "pr", "1", "(787) 234-5678"],
196
+ ["Qatar", "qa", "974", "3312 3456"],
197
+ ["Réunion", "re", "262", "0692 12 34 56"],
198
+ ["Romania", "ro", "40", "0712 034 567"],
199
+ ["Russia", "ru", "7", "8 (912) 345-67-89"],
200
+ ["Rwanda", "rw", "250", "0720 123 456"],
201
+ ["Saint Barthélemy", "bl", "590", "0690 00 12 34"],
202
+ ["Saint Helena", "sh", "290", "51234"],
203
+ ["Saint Kitts and Nevis", "kn", "1", "(869) 765-2917"],
204
+ ["Saint Lucia", "lc", "1", "(758) 284-5678"],
205
+ ["Saint Martin", "mf", "590", "0690 00 12 34"],
206
+ ["Saint Pierre and Miquelon", "pm", "508", "055 12 34"],
207
+ ["Saint Vincent and the Grenadines", "vc", "1", "(784) 430-1234"],
208
+ ["Samoa", "ws", "685", "72 12345"],
209
+ ["San Marino", "sm", "378", "66 66 12 12"],
210
+ ["São Tomé and Príncipe", "st", "239", "981 2345"],
211
+ ["Saudi Arabia", "sa", "966", "051 234 5678"],
212
+ ["Senegal", "sn", "221", "70 123 45 67"],
213
+ ["Serbia", "rs", "381", "060 1234567"],
214
+ ["Seychelles", "sc", "248", "2 510 123"],
215
+ ["Sierra Leone", "sl", "232", "(025) 123456"],
216
+ ["Singapore", "sg", "65", "8123 4567"],
217
+ ["Sint Maarten", "sx", "1", "(721) 520-5678"],
218
+ ["Slovakia", "sk", "421", "0912 123 456"],
219
+ ["Slovenia", "si", "386", "031 234 567"],
220
+ ["Solomon Islands", "sb", "677", "74 21234"],
221
+ ["Somalia", "so", "252", "7 1123456"],
222
+ ["South Africa", "za", "27", "071 123 4567"],
223
+ ["South Korea", "kr", "82", "010-2000-0000"],
224
+ ["South Sudan", "ss", "211", "0977 123 456"],
225
+ ["Spain", "es", "34", "612 34 56 78"],
226
+ ["Sri Lanka", "lk", "94", "071 234 5678"],
227
+ ["Sudan", "sd", "249", "091 123 1234"],
228
+ ["Suriname", "sr", "597", "741-2345"],
229
+ ["Svalbard and Jan Mayen", "sj", "47", "412 34 567"],
230
+ ["Sweden", "se", "46", "070-123 45 67"],
231
+ ["Switzerland", "ch", "41", "078 123 45 67"],
232
+ ["Syria", "sy", "963", "0944 567 890"],
233
+ ["Taiwan", "tw", "886", "0912 345 678"],
234
+ ["Tajikistan", "tj", "992", "917 12 3456"],
235
+ ["Tanzania", "tz", "255", "0621 234 567"],
236
+ ["Thailand", "th", "66", "081 234 5678"],
237
+ ["Timor-Leste", "tl", "670", "7721 2345"],
238
+ ["Togo", "tg", "228", "90 11 23 45"],
239
+ ["Tokelau", "tk", "690", "7290"],
240
+ ["Tonga", "to", "676", "771 5123"],
241
+ ["Trinidad and Tobago", "tt", "1", "(868) 291-1234"],
242
+ ["Tunisia", "tn", "216", "20 123 456"],
243
+ ["Turkey", "tr", "90", "0501 234 56 78"],
244
+ ["Turkmenistan", "tm", "993", "8 66 123456"],
245
+ ["Turks and Caicos Islands", "tc", "1", "(649) 231-1234"],
246
+ ["Tuvalu", "tv", "688", "90 1234"],
247
+ ["U.S. Virgin Islands", "vi", "1", "(340) 642-1234"],
248
+ ["Uganda", "ug", "256", "0712 345678"],
249
+ ["Ukraine", "ua", "380", "050 123 4567"],
250
+ ["United Arab Emirates", "ae", "971", "050 123 4567"],
251
+ ["United Kingdom", "gb", "44", "07400 123456"],
252
+ ["United States", "us", "1", "(201) 555-0123"],
253
+ ["Uruguay", "uy", "598", "094 231 234"],
254
+ ["Uzbekistan", "uz", "998", "8 91 234 56 78"],
255
+ ["Vanuatu", "vu", "678", "591 2345"],
256
+ ["Vatican City", "va", "39", "312 345 6789"],
257
+ ["Venezuela", "ve", "58", "0412-1234567"],
258
+ ["Vietnam", "vn", "84", "091 234 56 78"],
259
+ ["Wallis and Futuna", "wf", "681", "82 12 34"],
260
+ ["Western Sahara", "eh", "212", "0650-123456"],
261
+ ["Yemen", "ye", "967", "0712 345 678"],
262
+ ["Zambia", "zm", "260", "095 5123456"],
263
+ ["Zimbabwe", "zw", "263", "071 234 5678"],
264
+ ["Åland Islands", "ax", "358", "041 2345678"],
265
+ ];
266
+ this.countries_dropdown = null;
267
+ this.country_ip = null;
268
+ this.options = ENGrid.getOption("TidyContact");
269
+ if (this.options === false || !((_a = this.options) === null || _a === void 0 ? void 0 : _a.cid))
270
+ return;
271
+ this.loadOptions();
272
+ if (!this.hasAddressFields() && !this.phoneEnabled()) {
273
+ this.logger.log("No address fields found");
274
+ return;
275
+ }
276
+ this.createFields();
277
+ this.addEventListeners();
278
+ if (ENGrid.checkNested(window.EngagingNetworks, "require", "_defined", "enjs", "checkSubmissionFailed") &&
279
+ !window.EngagingNetworks.require._defined.enjs.checkSubmissionFailed() &&
280
+ ENGrid.getFieldValue((_c = (_b = this.options) === null || _b === void 0 ? void 0 : _b.address_fields) === null || _c === void 0 ? void 0 : _c.address1) !=
281
+ "") {
282
+ this.logger.log("Address Field is not empty");
283
+ this.isDirty = true;
284
+ }
285
+ if (this.phoneEnabled()) {
286
+ this.createPhoneFields();
287
+ this.createPhoneMarginVariable();
288
+ this.logger.log("Phone Standardization is enabled");
289
+ if (this.countryDropDownEnabled()) {
290
+ this.renderFlagsDropDown();
291
+ }
292
+ const phoneField = ENGrid.getField((_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.address_fields) === null || _e === void 0 ? void 0 : _e.phone);
293
+ if (phoneField) {
294
+ phoneField.addEventListener("keyup", (e) => {
295
+ this.handlePhoneInputKeydown(e);
296
+ });
297
+ this.setDefaultPhoneCountry();
298
+ }
299
+ }
300
+ }
301
+ loadOptions() {
302
+ var _a, _b, _c, _d;
303
+ if (this.options) {
304
+ if (!this.options.address_fields) {
305
+ this.options.address_fields = {
306
+ address1: "supporter.address1",
307
+ address2: "supporter.address2",
308
+ address3: "supporter.address3",
309
+ city: "supporter.city",
310
+ region: "supporter.region",
311
+ postalCode: "supporter.postcode",
312
+ country: "supporter.country",
313
+ phone: "supporter.phoneNumber2", // Phone field
314
+ };
315
+ }
316
+ this.options.address_enable = (_a = this.options.address_enable) !== null && _a !== void 0 ? _a : true;
317
+ if (this.options.phone_enable) {
318
+ this.options.phone_flags = (_b = this.options.phone_flags) !== null && _b !== void 0 ? _b : true;
319
+ this.options.phone_country_from_ip =
320
+ (_c = this.options.phone_country_from_ip) !== null && _c !== void 0 ? _c : true;
321
+ this.options.phone_preferred_countries =
322
+ (_d = this.options.phone_preferred_countries) !== null && _d !== void 0 ? _d : [];
323
+ }
324
+ }
325
+ }
326
+ createFields() {
327
+ var _a, _b, _c, _d, _e, _f;
328
+ if (!this.options || !this.hasAddressFields())
329
+ return;
330
+ // Creating Latitude and Longitude fields
331
+ const latitudeField = ENGrid.getField("supporter.geo.latitude");
332
+ const longitudeField = ENGrid.getField("supporter.geo.longitude");
333
+ if (!latitudeField) {
334
+ ENGrid.createHiddenInput("supporter.geo.latitude", "");
335
+ this.logger.log("Creating Hidden Field: supporter.geo.latitude");
336
+ }
337
+ if (!longitudeField) {
338
+ ENGrid.createHiddenInput("supporter.geo.longitude", "");
339
+ this.logger.log("Creating Hidden Field: supporter.geo.longitude");
340
+ }
341
+ if (this.options.record_field) {
342
+ const recordField = ENGrid.getField(this.options.record_field);
343
+ if (!recordField) {
344
+ ENGrid.createHiddenInput(this.options.record_field, "");
345
+ this.logger.log("Creating Hidden Field: " + this.options.record_field);
346
+ }
347
+ }
348
+ if (this.options.date_field) {
349
+ const dateField = ENGrid.getField(this.options.date_field);
350
+ if (!dateField) {
351
+ ENGrid.createHiddenInput(this.options.date_field, "");
352
+ this.logger.log("Creating Hidden Field: " + this.options.date_field);
353
+ }
354
+ }
355
+ if (this.options.status_field) {
356
+ const statusField = ENGrid.getField(this.options.status_field);
357
+ if (!statusField) {
358
+ ENGrid.createHiddenInput(this.options.status_field, "");
359
+ this.logger.log("Creating Hidden Field: " + this.options.status_field);
360
+ }
361
+ }
362
+ // If there's no Address 2 or Address 3 field, create them
363
+ if (!ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.address2)) {
364
+ ENGrid.createHiddenInput((_b = this.options.address_fields) === null || _b === void 0 ? void 0 : _b.address2, "");
365
+ this.logger.log("Creating Hidden Field: " + ((_c = this.options.address_fields) === null || _c === void 0 ? void 0 : _c.address2));
366
+ }
367
+ if (!ENGrid.getField((_d = this.options.address_fields) === null || _d === void 0 ? void 0 : _d.address3)) {
368
+ ENGrid.createHiddenInput((_e = this.options.address_fields) === null || _e === void 0 ? void 0 : _e.address3, "");
369
+ this.logger.log("Creating Hidden Field: " + ((_f = this.options.address_fields) === null || _f === void 0 ? void 0 : _f.address3));
370
+ }
371
+ }
372
+ createPhoneFields() {
373
+ if (!this.options)
374
+ return;
375
+ ENGrid.createHiddenInput("tc.phone.country", "");
376
+ this.logger.log("Creating hidden field: tc.phone.country");
377
+ if (this.options.phone_record_field) {
378
+ const recordField = ENGrid.getField(this.options.phone_record_field);
379
+ if (!recordField) {
380
+ ENGrid.createHiddenInput(this.options.phone_record_field, "");
381
+ this.logger.log("Creating hidden field: " + this.options.phone_record_field);
382
+ }
383
+ }
384
+ if (this.options.phone_date_field) {
385
+ const dateField = ENGrid.getField(this.options.phone_date_field);
386
+ if (!dateField) {
387
+ ENGrid.createHiddenInput(this.options.phone_date_field, "");
388
+ this.logger.log("Creating hidden field: " + this.options.phone_date_field);
389
+ }
390
+ }
391
+ if (this.options.phone_status_field) {
392
+ const statusField = ENGrid.getField(this.options.phone_status_field);
393
+ if (!statusField) {
394
+ ENGrid.createHiddenInput(this.options.phone_status_field, "");
395
+ this.logger.log("Creating hidden field: " + this.options.phone_status_field);
396
+ }
397
+ }
398
+ }
399
+ createPhoneMarginVariable() {
400
+ var _a;
401
+ if (!this.options)
402
+ return;
403
+ const phone = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
404
+ if (phone) {
405
+ const phoneStyle = window.getComputedStyle(phone);
406
+ const marginTop = phoneStyle.marginTop;
407
+ const marginBottom = phoneStyle.marginBottom;
408
+ document.documentElement.style.setProperty("--tc-phone-margin-top", marginTop);
409
+ document.documentElement.style.setProperty("--tc-phone-margin-bottom", marginBottom);
410
+ }
411
+ }
412
+ addEventListeners() {
413
+ if (!this.options)
414
+ return;
415
+ // Add event listeners to fields
416
+ if (this.options.address_fields) {
417
+ for (const [key, value] of Object.entries(this.options.address_fields)) {
418
+ const field = ENGrid.getField(value);
419
+ if (!field)
420
+ continue;
421
+ field.addEventListener("change", () => {
422
+ this.logger.log("Changed " + field.name, true);
423
+ this.isDirty = true;
424
+ });
425
+ }
426
+ }
427
+ // Add event listener to submit
428
+ this._form.onSubmit.subscribe(this.callAPI.bind(this));
429
+ // Attach the API call event to the Give By Select to anticipate the use of Digital Wallets
430
+ const transactionGiveBySelect = document.getElementsByName("transaction.giveBySelect");
431
+ if (transactionGiveBySelect) {
432
+ transactionGiveBySelect.forEach((giveBySelect) => {
433
+ giveBySelect.addEventListener("change", () => {
434
+ if (["stripedigitalwallet", "paypaltouch"].includes(giveBySelect.value.toLowerCase())) {
435
+ this.logger.log("Clicked Digital Wallet Button");
436
+ window.setTimeout(() => {
437
+ this.callAPI();
438
+ }, 500);
439
+ }
440
+ });
441
+ });
442
+ }
443
+ }
444
+ checkSum(str) {
445
+ return __awaiter(this, void 0, void 0, function* () {
446
+ // encode as UTF-8
447
+ const msgBuffer = new TextEncoder().encode(str);
448
+ // hash the message
449
+ const hashBuffer = yield crypto.subtle.digest("SHA-256", msgBuffer);
450
+ // convert ArrayBuffer to Array
451
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
452
+ // convert bytes to hex string
453
+ const hashHex = hashArray
454
+ .map((b) => ("00" + b.toString(16)).slice(-2))
455
+ .join("");
456
+ return hashHex;
457
+ });
458
+ }
459
+ todaysDate() {
460
+ return new Date()
461
+ .toLocaleString("en-ZA", {
462
+ year: "numeric",
463
+ month: "2-digit",
464
+ day: "2-digit",
465
+ })
466
+ .replace(/\/+/g, ""); // Format date as YYYYMMDD
467
+ }
468
+ countryAllowed(country) {
469
+ var _a;
470
+ if (!this.options)
471
+ return false;
472
+ // If the country list is empty, allow all countries
473
+ if (!this.options.countries || this.options.countries.length === 0) {
474
+ return true;
475
+ }
476
+ return !!((_a = this.options.countries) === null || _a === void 0 ? void 0 : _a.includes(country.toLowerCase()));
477
+ }
478
+ fetchTimeOut(url, params) {
479
+ const abort = new AbortController();
480
+ const signal = abort.signal;
481
+ params = Object.assign(Object.assign({}, params), { signal });
482
+ const promise = fetch(url, params);
483
+ if (signal)
484
+ signal.addEventListener("abort", () => abort.abort());
485
+ const timeout = setTimeout(() => abort.abort(), this.timeout * 1000);
486
+ return promise.finally(() => clearTimeout(timeout));
487
+ }
488
+ writeError(error) {
489
+ if (!this.options)
490
+ return;
491
+ const recordField = ENGrid.getField(this.options.record_field);
492
+ const dateField = ENGrid.getField(this.options.date_field);
493
+ const statusField = ENGrid.getField(this.options.status_field);
494
+ if (recordField) {
495
+ let errorType = "";
496
+ switch (this.httpStatus) {
497
+ case 400:
498
+ errorType = "Bad Request";
499
+ break;
500
+ case 401:
501
+ errorType = "Unauthorized";
502
+ break;
503
+ case 403:
504
+ errorType = "Forbidden";
505
+ break;
506
+ case 404:
507
+ errorType = "Not Found";
508
+ break;
509
+ case 408:
510
+ errorType = "API Request Timeout";
511
+ break;
512
+ case 500:
513
+ errorType = "Internal Server Error";
514
+ break;
515
+ case 503:
516
+ errorType = "Service Unavailable";
517
+ break;
518
+ default:
519
+ errorType = "Unknown Error";
520
+ break;
521
+ }
522
+ const errorData = {
523
+ status: this.httpStatus,
524
+ error: typeof error === "string" ? error : errorType.toUpperCase(),
525
+ };
526
+ recordField.value = JSON.stringify(errorData);
527
+ }
528
+ if (dateField) {
529
+ dateField.value = this.todaysDate();
530
+ }
531
+ if (statusField) {
532
+ statusField.value = "ERROR-API";
533
+ }
534
+ }
535
+ setFields(data) {
536
+ var _a, _b, _c, _d, _e;
537
+ if (!this.options || !this.options.address_enable)
538
+ return {};
539
+ let response = {};
540
+ const country = this.getCountry();
541
+ const postalCodeValue = ENGrid.getFieldValue((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.postalCode);
542
+ const zipDivider = (_b = this.options.us_zip_divider) !== null && _b !== void 0 ? _b : "+";
543
+ // Check if there's no address2 field
544
+ const address2Field = ENGrid.getField((_c = this.options.address_fields) === null || _c === void 0 ? void 0 : _c.address2);
545
+ if ("address2" in data && !address2Field) {
546
+ const address = ENGrid.getFieldValue((_d = this.options.address_fields) === null || _d === void 0 ? void 0 : _d.address1);
547
+ if (address == data.address1 + " " + data.address2) {
548
+ delete data.address1;
549
+ delete data.address2;
550
+ }
551
+ else {
552
+ data.address1 = data.address1 + " " + data.address2;
553
+ delete data.address2;
554
+ }
555
+ }
556
+ if ("postalCode" in data &&
557
+ postalCodeValue.replace("+", zipDivider) ===
558
+ data.postalCode.replace("+", zipDivider)) {
559
+ // Postal code is the same
560
+ delete data.postalCode;
561
+ }
562
+ // Set the fields
563
+ for (const key in data) {
564
+ const fieldKey = this.options.address_fields &&
565
+ Object.keys(this.options.address_fields).includes(key)
566
+ ? this.options.address_fields[key]
567
+ : key;
568
+ const field = ENGrid.getField(fieldKey);
569
+ if (field) {
570
+ let value = data[key];
571
+ if (key === "postalCode" &&
572
+ ["US", "USA", "United States"].includes(country)) {
573
+ value = (_e = value.replace("+", zipDivider)) !== null && _e !== void 0 ? _e : ""; // Replace the "+" with the zip divider
574
+ }
575
+ response[key] = { from: field.value, to: value };
576
+ this.logger.log(`Set ${field.name} to ${value} (${field.value})`);
577
+ ENGrid.setFieldValue(fieldKey, value, false);
578
+ }
579
+ else {
580
+ this.logger.log(`Field ${key} not found`);
581
+ }
582
+ }
583
+ return response;
584
+ }
585
+ hasAddressFields() {
586
+ var _a, _b, _c, _d, _e, _f;
587
+ if (!this.options || !this.options.address_enable)
588
+ return false;
589
+ const address1 = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.address1);
590
+ const address2 = ENGrid.getField((_b = this.options.address_fields) === null || _b === void 0 ? void 0 : _b.address2);
591
+ const city = ENGrid.getField((_c = this.options.address_fields) === null || _c === void 0 ? void 0 : _c.city);
592
+ const region = ENGrid.getField((_d = this.options.address_fields) === null || _d === void 0 ? void 0 : _d.region);
593
+ const postalCode = ENGrid.getField((_e = this.options.address_fields) === null || _e === void 0 ? void 0 : _e.postalCode);
594
+ const country = ENGrid.getField((_f = this.options.address_fields) === null || _f === void 0 ? void 0 : _f.country);
595
+ return !!(address1 || address2 || city || region || postalCode || country);
596
+ }
597
+ canUseAPI() {
598
+ var _a, _b, _c, _d;
599
+ if (!this.options || !this.hasAddressFields())
600
+ return false;
601
+ const country = !!this.getCountry();
602
+ const address1 = !!ENGrid.getFieldValue((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.address1);
603
+ const city = !!ENGrid.getFieldValue((_b = this.options.address_fields) === null || _b === void 0 ? void 0 : _b.city);
604
+ const region = !!ENGrid.getFieldValue((_c = this.options.address_fields) === null || _c === void 0 ? void 0 : _c.region);
605
+ const postalCode = !!ENGrid.getFieldValue((_d = this.options.address_fields) === null || _d === void 0 ? void 0 : _d.postalCode);
606
+ if (country && address1) {
607
+ return (city && region) || postalCode;
608
+ }
609
+ this.logger.log("API cannot be used");
610
+ return false;
611
+ }
612
+ canUsePhoneAPI() {
613
+ var _a;
614
+ if (!this.options)
615
+ return false;
616
+ if (this.phoneEnabled()) {
617
+ const phone = !!ENGrid.getFieldValue((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
618
+ const countryPhone = !!ENGrid.getFieldValue("tc.phone.country");
619
+ return phone && countryPhone;
620
+ }
621
+ this.logger.log("Phone API is not enabled");
622
+ return false;
623
+ }
624
+ getCountry() {
625
+ var _a, _b;
626
+ if (!this.options)
627
+ return "";
628
+ const countryFallback = (_a = this.options.country_fallback) !== null && _a !== void 0 ? _a : "";
629
+ const country = ENGrid.getFieldValue((_b = this.options.address_fields) === null || _b === void 0 ? void 0 : _b.country);
630
+ return country || countryFallback.toUpperCase();
631
+ }
632
+ getCountryByCode(code) {
633
+ var _a;
634
+ const countryItem = (_a = this.countries_list.find((country) => country.includes(code))) !== null && _a !== void 0 ? _a : "";
635
+ if (countryItem) {
636
+ return {
637
+ name: countryItem[0],
638
+ code: countryItem[1],
639
+ dialCode: countryItem[2],
640
+ placeholder: countryItem[3],
641
+ };
642
+ }
643
+ return null;
644
+ }
645
+ phoneEnabled() {
646
+ return !!(this.options && this.options.phone_enable);
647
+ }
648
+ countryDropDownEnabled() {
649
+ return !!(this.options && this.options.phone_flags);
650
+ }
651
+ getCountryFromIP() {
652
+ return __awaiter(this, void 0, void 0, function* () {
653
+ return fetch(`https://${window.location.hostname}/cdn-cgi/trace`)
654
+ .then((res) => res.text())
655
+ .then((t) => {
656
+ let data = t.replace(/[\r\n]+/g, '","').replace(/\=+/g, '":"');
657
+ data = '{"' + data.slice(0, data.lastIndexOf('","')) + '"}';
658
+ const jsondata = JSON.parse(data);
659
+ this.country_ip = jsondata.loc;
660
+ return this.country_ip;
661
+ });
662
+ });
663
+ }
664
+ renderFlagsDropDown() {
665
+ var _a;
666
+ if (!this.options)
667
+ return;
668
+ const phoneInput = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
669
+ if (!phoneInput)
670
+ return;
671
+ this.countries_dropdown = document.createElement("div");
672
+ this.countries_dropdown.classList.add("tc-flags-container");
673
+ const selectedFlag = document.createElement("div");
674
+ selectedFlag.classList.add("tc-selected-flag");
675
+ selectedFlag.setAttribute("role", "combobox");
676
+ selectedFlag.setAttribute("aria-haspopup", "listbox");
677
+ selectedFlag.setAttribute("aria-expanded", "false");
678
+ selectedFlag.setAttribute("aria-owns", "tc-flags-list");
679
+ selectedFlag.setAttribute("aria-label", "Select Country");
680
+ selectedFlag.setAttribute("tabindex", "0");
681
+ const seletedFlagInner = document.createElement("div");
682
+ seletedFlagInner.classList.add("tc-flag");
683
+ // seletedFlagInner.innerHTML = this.getFlagImage("us", "United States");
684
+ const flagArrow = document.createElement("div");
685
+ flagArrow.classList.add("tc-flag-arrow");
686
+ // flagArrow.innerHTML = "▼";
687
+ selectedFlag.appendChild(seletedFlagInner);
688
+ selectedFlag.appendChild(flagArrow);
689
+ selectedFlag.addEventListener("click", (e) => {
690
+ e.preventDefault();
691
+ e.stopPropagation();
692
+ if (selectedFlag.classList.contains("tc-open")) {
693
+ this.closeCountryDropDown();
694
+ }
695
+ else {
696
+ this.openCountryDropDown();
697
+ }
698
+ });
699
+ const countryList = document.createElement("ul");
700
+ countryList.classList.add("tc-country-list");
701
+ countryList.classList.add("tc-hide");
702
+ countryList.setAttribute("id", "tc-country-list");
703
+ countryList.setAttribute("role", "listbox");
704
+ countryList.setAttribute("aria-label", "List of Countries");
705
+ countryList.setAttribute("aria-hidden", "true");
706
+ if (this.options.phone_preferred_countries.length > 0) {
707
+ const preferredCountries = [];
708
+ this.options.phone_preferred_countries.forEach((country) => {
709
+ const countryItem = this.getCountryByCode(country);
710
+ if (countryItem) {
711
+ preferredCountries.push(countryItem);
712
+ }
713
+ });
714
+ this.appendCountryItems(countryList, preferredCountries, "tc-country-list-item", true);
715
+ const divider = document.createElement("li");
716
+ divider.classList.add("tc-divider");
717
+ divider.setAttribute("role", "separator");
718
+ divider.setAttribute("aria-disabled", "true");
719
+ countryList.appendChild(divider);
720
+ this.logger.log("Rendering preferred countries", JSON.stringify(preferredCountries));
721
+ }
722
+ const countryListItems = [];
723
+ this.countries_list.forEach((country) => {
724
+ countryListItems.push({
725
+ name: country[0],
726
+ code: country[1],
727
+ dialCode: country[2],
728
+ placeholder: country[3],
729
+ });
730
+ });
731
+ this.appendCountryItems(countryList, countryListItems, "tc-country-list-item");
732
+ countryList.addEventListener("click", (e) => {
733
+ e.preventDefault();
734
+ e.stopPropagation();
735
+ const target = e.target.closest("li");
736
+ if (target.classList.contains("tc-country-list-item")) {
737
+ const countryItem = this.getCountryByCode(target.getAttribute("data-country-code"));
738
+ if (countryItem) {
739
+ this.setPhoneCountry(countryItem);
740
+ }
741
+ }
742
+ });
743
+ countryList.addEventListener("mouseover", (e) => {
744
+ e.preventDefault();
745
+ e.stopPropagation();
746
+ const target = e.target.closest("li.tc-country-list-item");
747
+ if (target) {
748
+ this.highlightCountry(target.getAttribute("data-country-code"));
749
+ }
750
+ });
751
+ this.countries_dropdown.appendChild(selectedFlag);
752
+ this.countries_dropdown.appendChild(countryList);
753
+ phoneInput.parentNode.insertBefore(this.countries_dropdown, phoneInput);
754
+ phoneInput.parentNode.classList.add("tc-has-country-flags");
755
+ this.countries_dropdown.addEventListener("keydown", (e) => {
756
+ var _a, _b;
757
+ const isDropdownHidden = (_b = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-country-list")) === null || _b === void 0 ? void 0 : _b.classList.contains("tc-hide");
758
+ if (isDropdownHidden &&
759
+ ["ArrowUp", "Up", "ArrowDown", "Down", " ", "Enter"].indexOf(e.key) !==
760
+ -1) {
761
+ // prevent form from being submitted if "ENTER" was pressed
762
+ e.preventDefault();
763
+ // prevent event from being handled again by document
764
+ e.stopPropagation();
765
+ this.openCountryDropDown();
766
+ }
767
+ // allow navigation from dropdown to input on TAB
768
+ if (e.key === "Tab")
769
+ this.closeCountryDropDown();
770
+ });
771
+ document.addEventListener("keydown", (e) => {
772
+ var _a, _b;
773
+ const isDropdownHidden = (_b = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-country-list")) === null || _b === void 0 ? void 0 : _b.classList.contains("tc-hide");
774
+ if (!isDropdownHidden) {
775
+ // prevent down key from scrolling the whole page,
776
+ // and enter key from submitting a form etc
777
+ e.preventDefault();
778
+ // up and down to navigate
779
+ if (e.key === "ArrowUp" ||
780
+ e.key === "Up" ||
781
+ e.key === "ArrowDown" ||
782
+ e.key === "Down")
783
+ this.handleUpDownKey(e.key);
784
+ // enter to select
785
+ else if (e.key === "Enter")
786
+ this.handleEnterKey();
787
+ // esc to close
788
+ else if (e.key === "Escape")
789
+ this.closeCountryDropDown();
790
+ }
791
+ });
792
+ document.addEventListener("click", (e) => {
793
+ var _a, _b;
794
+ const isDropdownHidden = (_b = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-country-list")) === null || _b === void 0 ? void 0 : _b.classList.contains("tc-hide");
795
+ if (!isDropdownHidden &&
796
+ !e.target.closest(".tc-country-list")) {
797
+ this.closeCountryDropDown();
798
+ }
799
+ });
800
+ }
801
+ handleUpDownKey(key) {
802
+ var _a;
803
+ const highlightedCountry = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-highlight");
804
+ if (highlightedCountry) {
805
+ let next = key === "ArrowUp" || key === "Up"
806
+ ? highlightedCountry.previousElementSibling
807
+ : highlightedCountry.nextElementSibling;
808
+ if (next) {
809
+ if (next.classList.contains("tc-divider")) {
810
+ next =
811
+ key === "ArrowUp" || key === "Up"
812
+ ? next.previousElementSibling
813
+ : next.nextElementSibling;
814
+ }
815
+ this.highlightCountry(next === null || next === void 0 ? void 0 : next.getAttribute("data-country-code"));
816
+ }
817
+ }
818
+ }
819
+ handleEnterKey() {
820
+ var _a;
821
+ const highlightedCountry = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-highlight");
822
+ if (highlightedCountry) {
823
+ const countryItem = this.getCountryByCode(highlightedCountry === null || highlightedCountry === void 0 ? void 0 : highlightedCountry.getAttribute("data-country-code"));
824
+ this.setPhoneCountry(countryItem);
825
+ }
826
+ }
827
+ handlePhoneInputKeydown(e) {
828
+ const phoneInput = e.target;
829
+ const phoneNumber = phoneInput.value;
830
+ if (phoneNumber.charAt(0) === "+") {
831
+ if (phoneNumber.length > 2) {
832
+ const countryItem = this.getCountryByCode(phoneNumber.substring(1, 3));
833
+ if (countryItem) {
834
+ this.setPhoneCountry(countryItem);
835
+ }
836
+ else {
837
+ this.setDefaultPhoneCountry();
838
+ }
839
+ }
840
+ }
841
+ }
842
+ openCountryDropDown() {
843
+ if (!this.countries_dropdown)
844
+ return;
845
+ const countryList = this.countries_dropdown.querySelector(".tc-country-list");
846
+ const selectedFlag = this.countries_dropdown.querySelector(".tc-selected-flag");
847
+ if (countryList && selectedFlag) {
848
+ countryList.classList.remove("tc-hide");
849
+ selectedFlag.setAttribute("aria-expanded", "true");
850
+ selectedFlag.classList.add("tc-open");
851
+ }
852
+ }
853
+ closeCountryDropDown() {
854
+ var _a;
855
+ if (!this.options)
856
+ return;
857
+ if (!this.countries_dropdown)
858
+ return;
859
+ const countryList = this.countries_dropdown.querySelector(".tc-country-list");
860
+ const selectedFlag = this.countries_dropdown.querySelector(".tc-selected-flag");
861
+ if (countryList && selectedFlag) {
862
+ countryList.classList.add("tc-hide");
863
+ selectedFlag.setAttribute("aria-expanded", "false");
864
+ selectedFlag.classList.remove("tc-open");
865
+ }
866
+ const phoneInput = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
867
+ phoneInput.focus();
868
+ }
869
+ getFlagImage(code, name) {
870
+ return `<picture>
871
+ <source
872
+ loading="lazy"
873
+ type="image/webp"
874
+ srcset="https://flagcdn.com/h20/${code}.webp,
875
+ https://flagcdn.com/h40/${code}.webp 2x,
876
+ https://flagcdn.com/h60/${code}.webp 3x">
877
+ <source
878
+ loading="lazy"
879
+ type="image/png"
880
+ srcset="https://flagcdn.com/h20/${code}.png,
881
+ https://flagcdn.com/h40/${code}.png 2x,
882
+ https://flagcdn.com/h60/${code}.png 3x">
883
+ <img
884
+ loading="lazy"
885
+ src="https://flagcdn.com/h20/${code}.png"
886
+ height="20"
887
+ alt="${name}">
888
+ </picture>`;
889
+ }
890
+ appendCountryItems(countryContainer, countries, className, preferred = false) {
891
+ let html = "";
892
+ // for each country
893
+ for (let i = 0; i < countries.length; i++) {
894
+ const c = countries[i];
895
+ const idSuffix = !!preferred ? "-preferred" : "" !== null && "" !== void 0 ? "" : "";
896
+ // open the list item
897
+ html += `<li class='tc-country ${className}' tabIndex='-1' id='tc-item-${c.code}${idSuffix}' role='option' data-dial-code='${c.dialCode}' data-country-code='${c.code}' aria-selected='false'>`;
898
+ // add the flag
899
+ html += `<div class='tc-flag-box'><div class='tc-flag tc-${c.code}'>${this.getFlagImage(c.code, c.name)}</div></div>`;
900
+ // and the country name and dial code
901
+ html += `<span class='tc-country-name'>${c.name}</span>`;
902
+ html += `<span class='tc-dial-code'>+${c.dialCode}</span>`;
903
+ // close the list item
904
+ html += "</li>";
905
+ }
906
+ countryContainer.insertAdjacentHTML("beforeend", html);
907
+ }
908
+ setDefaultPhoneCountry() {
909
+ var _a;
910
+ if (!this.options)
911
+ return;
912
+ // First, try to get the country from IP
913
+ if (this.options.phone_country_from_ip) {
914
+ this.getCountryFromIP()
915
+ .then((country) => {
916
+ this.logger.log("Country from IP:", country);
917
+ this.setPhoneCountry(this.getCountryByCode((country !== null && country !== void 0 ? country : "us").toLowerCase()));
918
+ })
919
+ .catch((error) => {
920
+ this.setPhoneCountry(this.getCountryByCode("us"));
921
+ });
922
+ return;
923
+ }
924
+ // Then, get the default country Text
925
+ const countryField = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.country);
926
+ if (countryField) {
927
+ const countryText = countryField.options[countryField.selectedIndex].text;
928
+ // Then, get the country code from the Text
929
+ const countryData = this.getCountryByCode(countryText);
930
+ if (countryData) {
931
+ this.setPhoneCountry(countryData);
932
+ return;
933
+ }
934
+ else if (this.options.phone_preferred_countries.length > 0) {
935
+ // If no country code is found, use the first priority country
936
+ this.setPhoneCountry(this.getCountryByCode(this.options.phone_preferred_countries[0]));
937
+ return;
938
+ }
939
+ }
940
+ // If nothing works, GO USA!
941
+ this.setPhoneCountry(this.getCountryByCode("us"));
942
+ }
943
+ setPhoneCountry(country) {
944
+ var _a, _b, _c, _d, _e, _f;
945
+ if (!this.options || !country)
946
+ return;
947
+ const countryInput = ENGrid.getField("tc.phone.country");
948
+ if (countryInput.value === country.code)
949
+ return;
950
+ const phoneInput = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
951
+ if (this.countryDropDownEnabled()) {
952
+ const selectedFlag = (_b = this.countries_dropdown) === null || _b === void 0 ? void 0 : _b.querySelector(".tc-selected-flag");
953
+ const flagElement = (_c = this.countries_dropdown) === null || _c === void 0 ? void 0 : _c.querySelector(".tc-flag");
954
+ if (selectedFlag && flagElement) {
955
+ flagElement.innerHTML = this.getFlagImage(country.code, country.name);
956
+ selectedFlag.setAttribute("data-country", country.code);
957
+ }
958
+ const currentSelectedCountry = (_d = this.countries_dropdown) === null || _d === void 0 ? void 0 : _d.querySelector(".tc-country-list-item[aria-selected='true']");
959
+ if (currentSelectedCountry) {
960
+ currentSelectedCountry.classList.remove("tc-selected");
961
+ currentSelectedCountry.setAttribute("aria-selected", "false");
962
+ }
963
+ const currentHighlightedCountry = (_e = this.countries_dropdown) === null || _e === void 0 ? void 0 : _e.querySelector(".tc-highlight");
964
+ if (currentHighlightedCountry) {
965
+ currentHighlightedCountry.classList.remove("tc-highlight");
966
+ }
967
+ const countryListItem = (_f = this.countries_dropdown) === null || _f === void 0 ? void 0 : _f.querySelector(`.tc-country-list-item[data-country-code='${country.code}']`);
968
+ if (countryListItem) {
969
+ countryListItem.classList.add("tc-selected");
970
+ countryListItem.setAttribute("aria-selected", "true");
971
+ countryListItem.classList.add("tc-highlight");
972
+ }
973
+ if (selectedFlag === null || selectedFlag === void 0 ? void 0 : selectedFlag.classList.contains("tc-open"))
974
+ this.closeCountryDropDown();
975
+ }
976
+ phoneInput.setAttribute("placeholder", country.placeholder);
977
+ countryInput.value = country.code;
978
+ this.logger.log(`Setting phone country to ${country.code} - ${country.name}`);
979
+ }
980
+ highlightCountry(countryCode) {
981
+ var _a, _b;
982
+ if (!countryCode)
983
+ return;
984
+ const currentHighlightedCountry = (_a = this.countries_dropdown) === null || _a === void 0 ? void 0 : _a.querySelector(".tc-highlight");
985
+ if (currentHighlightedCountry) {
986
+ currentHighlightedCountry.classList.remove("tc-highlight");
987
+ }
988
+ const countryList = (_b = this.countries_dropdown) === null || _b === void 0 ? void 0 : _b.querySelector(".tc-country-list");
989
+ if (countryList) {
990
+ const country = countryList.querySelector(`.tc-country[data-country-code='${countryCode}']`);
991
+ if (country) {
992
+ country.classList.add("tc-highlight");
993
+ country.scrollIntoView({
994
+ behavior: "smooth",
995
+ block: "nearest",
996
+ inline: "nearest",
997
+ });
998
+ }
999
+ }
1000
+ }
1001
+ setPhoneDataFromAPI(data, id) {
1002
+ var _a;
1003
+ return __awaiter(this, void 0, void 0, function* () {
1004
+ if (!this.options)
1005
+ return;
1006
+ const phoneField = ENGrid.getField((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.phone);
1007
+ const recordField = ENGrid.getField(this.options.phone_record_field);
1008
+ const dateField = ENGrid.getField(this.options.phone_date_field);
1009
+ const statusField = ENGrid.getField(this.options.phone_status_field);
1010
+ let record = {};
1011
+ record["formData"] = { [phoneField.name]: phoneField.value };
1012
+ record["formatted"] = data.formatted;
1013
+ record["number_type"] = data.number_type;
1014
+ if (data.valid === true) {
1015
+ if (phoneField.value !== data.formatted.e164) {
1016
+ record["phone"] = {
1017
+ from: phoneField.value,
1018
+ to: data.formatted.e164,
1019
+ };
1020
+ phoneField.value = data.formatted.e164;
1021
+ }
1022
+ yield this.checkSum(JSON.stringify(record)).then((checksum) => {
1023
+ this.logger.log("Phone Checksum", checksum);
1024
+ record["requestId"] = id; // We don't want to add the requestId to the checksum
1025
+ record["checksum"] = checksum;
1026
+ });
1027
+ if (recordField) {
1028
+ record = Object.assign({ date: this.todaysDate(), status: "SUCCESS" }, record);
1029
+ recordField.value = JSON.stringify(record);
1030
+ }
1031
+ if (dateField) {
1032
+ dateField.value = this.todaysDate();
1033
+ }
1034
+ if (statusField) {
1035
+ statusField.value = "SUCCESS";
1036
+ }
1037
+ }
1038
+ else {
1039
+ yield this.checkSum(JSON.stringify(record)).then((checksum) => {
1040
+ this.logger.log("Phone Checksum", checksum);
1041
+ record["requestId"] = id; // We don't want to add the requestId to the checksum
1042
+ record["checksum"] = checksum;
1043
+ });
1044
+ if (recordField) {
1045
+ record = Object.assign({ date: this.todaysDate(), status: "ERROR" }, record);
1046
+ recordField.value = JSON.stringify(record);
1047
+ }
1048
+ if (dateField) {
1049
+ dateField.value = this.todaysDate();
1050
+ }
1051
+ if (statusField) {
1052
+ statusField.value =
1053
+ "error" in data ? `ERROR: ` + data.error : "INVALIDPHONE";
1054
+ }
1055
+ }
1056
+ });
1057
+ }
1058
+ callAPI() {
1059
+ var _a, _b, _c, _d, _e, _f;
1060
+ if (!this.options)
1061
+ return;
1062
+ if (!this.isDirty || this.wasCalled)
1063
+ return;
1064
+ if (!this._form.submit) {
1065
+ this.logger.log("Form Submission Interrupted by Other Component");
1066
+ return;
1067
+ }
1068
+ const recordField = ENGrid.getField(this.options.record_field);
1069
+ const dateField = ENGrid.getField(this.options.date_field);
1070
+ const statusField = ENGrid.getField(this.options.status_field);
1071
+ const latitudeField = ENGrid.getField("supporter.geo.latitude");
1072
+ const longitudeField = ENGrid.getField("supporter.geo.longitude");
1073
+ if (!this.canUseAPI() && !this.canUsePhoneAPI()) {
1074
+ this.logger.log("Not Enough Data to Call API");
1075
+ if (dateField) {
1076
+ dateField.value = this.todaysDate();
1077
+ }
1078
+ if (statusField) {
1079
+ statusField.value = "PARTIALADDRESS";
1080
+ }
1081
+ return true;
1082
+ }
1083
+ // Call the API
1084
+ const address1 = ENGrid.getFieldValue((_a = this.options.address_fields) === null || _a === void 0 ? void 0 : _a.address1);
1085
+ const address2 = ENGrid.getFieldValue((_b = this.options.address_fields) === null || _b === void 0 ? void 0 : _b.address2);
1086
+ const city = ENGrid.getFieldValue((_c = this.options.address_fields) === null || _c === void 0 ? void 0 : _c.city);
1087
+ const region = ENGrid.getFieldValue((_d = this.options.address_fields) === null || _d === void 0 ? void 0 : _d.region);
1088
+ const postalCode = ENGrid.getFieldValue((_e = this.options.address_fields) === null || _e === void 0 ? void 0 : _e.postalCode);
1089
+ const country = this.getCountry();
1090
+ if (!this.countryAllowed(country)) {
1091
+ this.logger.log("Country not allowed: " + country);
1092
+ if (recordField) {
1093
+ let record = {};
1094
+ record = Object.assign({ date: this.todaysDate(), status: "DISALLOWED" }, record);
1095
+ recordField.value = JSON.stringify(record);
1096
+ }
1097
+ if (dateField) {
1098
+ dateField.value = this.todaysDate();
1099
+ }
1100
+ if (statusField) {
1101
+ statusField.value = "DISALLOWED";
1102
+ }
1103
+ return true;
1104
+ }
1105
+ let formData = {
1106
+ url: window.location.href,
1107
+ cid: this.options.cid,
1108
+ };
1109
+ if (this.canUseAPI()) {
1110
+ formData = Object.assign(formData, {
1111
+ address1,
1112
+ address2,
1113
+ city,
1114
+ region,
1115
+ postalCode,
1116
+ country,
1117
+ });
1118
+ }
1119
+ if (this.canUsePhoneAPI()) {
1120
+ formData.phone = ENGrid.getFieldValue((_f = this.options.address_fields) === null || _f === void 0 ? void 0 : _f.phone);
1121
+ formData.phoneCountry = ENGrid.getFieldValue("tc.phone.country");
1122
+ }
1123
+ this.wasCalled = true;
1124
+ this.logger.log("FormData", JSON.parse(JSON.stringify(formData)));
1125
+ const ret = this.fetchTimeOut(this.endpoint, {
1126
+ headers: { "Content-Type": "application/json; charset=utf-8" },
1127
+ method: "POST",
1128
+ body: JSON.stringify(formData),
1129
+ })
1130
+ .then((response) => {
1131
+ this.httpStatus = response.status;
1132
+ return response.json();
1133
+ })
1134
+ .then((data) => __awaiter(this, void 0, void 0, function* () {
1135
+ this.logger.log("callAPI response", JSON.parse(JSON.stringify(data)));
1136
+ if (data.valid === true) {
1137
+ let record = {};
1138
+ if ("changed" in data) {
1139
+ record = this.setFields(data.changed);
1140
+ }
1141
+ record["formData"] = formData;
1142
+ yield this.checkSum(JSON.stringify(record)).then((checksum) => {
1143
+ this.logger.log("Checksum", checksum);
1144
+ record["requestId"] = data.requestId; // We don't want to add the requestId to the checksum
1145
+ record["checksum"] = checksum;
1146
+ });
1147
+ if ("latitude" in data) {
1148
+ latitudeField.value = data.latitude;
1149
+ record["latitude"] = data.latitude;
1150
+ }
1151
+ if ("longitude" in data) {
1152
+ longitudeField.value = data.longitude;
1153
+ record["longitude"] = data.longitude;
1154
+ }
1155
+ if (recordField) {
1156
+ record = Object.assign({ date: this.todaysDate(), status: "SUCCESS" }, record);
1157
+ recordField.value = JSON.stringify(record);
1158
+ }
1159
+ if (dateField) {
1160
+ dateField.value = this.todaysDate();
1161
+ }
1162
+ if (statusField) {
1163
+ statusField.value = "SUCCESS";
1164
+ }
1165
+ }
1166
+ else {
1167
+ let record = {};
1168
+ record["formData"] = formData;
1169
+ yield this.checkSum(JSON.stringify(record)).then((checksum) => {
1170
+ this.logger.log("Checksum", checksum);
1171
+ record["requestId"] = data.requestId; // We don't want to add the requestId to the checksum
1172
+ record["checksum"] = checksum;
1173
+ });
1174
+ if (recordField) {
1175
+ record = Object.assign({ date: this.todaysDate(), status: "ERROR" }, record);
1176
+ recordField.value = JSON.stringify(record);
1177
+ }
1178
+ if (dateField) {
1179
+ dateField.value = this.todaysDate();
1180
+ }
1181
+ if (statusField) {
1182
+ statusField.value =
1183
+ "error" in data ? `ERROR: ` + data.error : "INVALIDADDRESS";
1184
+ }
1185
+ }
1186
+ if (this.phoneEnabled() && "phone" in data) {
1187
+ yield this.setPhoneDataFromAPI(data.phone, data.requestId);
1188
+ }
1189
+ }))
1190
+ .catch((error) => {
1191
+ if (error.toString().includes("AbortError")) {
1192
+ // fetch aborted due to timeout
1193
+ this.logger.log("Fetch aborted");
1194
+ this.httpStatus = 408;
1195
+ }
1196
+ // network error or json parsing error
1197
+ this.writeError(error);
1198
+ });
1199
+ this._form.submitPromise = ret;
1200
+ return ret;
1201
+ }
1202
+ }