@4site/engrid-scripts 0.19.22 → 0.20.0

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.
@@ -0,0 +1,14 @@
1
+ export declare class OptInLadder {
2
+ private logger;
3
+ private _form;
4
+ constructor();
5
+ private runAsParent;
6
+ private runAsChildRegular;
7
+ private runAsChildThankYou;
8
+ private inIframe;
9
+ private saveStepToSessionStorage;
10
+ private getFirstPageUrl;
11
+ private saveOptInsToSessionStorage;
12
+ private isEmbeddedThankYouPage;
13
+ private hidePage;
14
+ }
@@ -0,0 +1,199 @@
1
+ // This component is responsible for showing a ladder of checkboxes, one at a time, to the user.
2
+ // If the page is not embedded in an iframe, and there are EN's Opt-In fields on the page, we will store the values to sessionStorage upon Form Submit.
3
+ // If the page is embedded in an iframe and on a Thank You Page, we will look for .optin-ladder elements, compare the values to sessionStorage, and show the next checkbox in the ladder, removing all but the first match.
4
+ // If the page is embedded in an iframe and on a Thank You Page, and the child iFrame is also a Thank You Page, we will look for a sessionStorage that has the current ladder step and the total number of steps.
5
+ // If the current step is less than the total number of steps, we will redirect to the first page. If the current step is equal to the total number of steps, we will show the Thank You Page.
6
+ import { EngridLogger, ENGrid, EnForm } from ".";
7
+ export class OptInLadder {
8
+ constructor() {
9
+ this.logger = new EngridLogger("OptInLadder", "lightgreen", "darkgreen", "✔");
10
+ this._form = EnForm.getInstance();
11
+ if (!this.inIframe()) {
12
+ this.runAsParent();
13
+ }
14
+ else if (ENGrid.getPageNumber() === 1) {
15
+ this.runAsChildRegular();
16
+ }
17
+ else {
18
+ this.runAsChildThankYou();
19
+ }
20
+ }
21
+ runAsParent() {
22
+ // Grab all the checkboxes with the name starting with "supporter.questions"
23
+ const checkboxes = document.querySelectorAll('input[name^="supporter.questions"]');
24
+ if (checkboxes.length === 0) {
25
+ this.logger.log("No checkboxes found");
26
+ return;
27
+ }
28
+ this._form.onSubmit.subscribe(() => {
29
+ // Save the checkbox values to sessionStorage
30
+ this.saveOptInsToSessionStorage();
31
+ });
32
+ this.logger.log("Running as Parent");
33
+ if (ENGrid.getPageNumber() === 1) {
34
+ // Delete supporter.questions from sessionStorage
35
+ sessionStorage.removeItem("engrid.supporter.questions");
36
+ }
37
+ }
38
+ runAsChildRegular() {
39
+ if (!this.isEmbeddedThankYouPage()) {
40
+ this.logger.log("Not Embedded on a Thank You Page");
41
+ return;
42
+ }
43
+ const optInHeaders = document.querySelectorAll(".en__component--copyblock.optin-ladder");
44
+ const optInFormBlocks = document.querySelectorAll(".en__component--formblock.optin-ladder");
45
+ if (optInHeaders.length === 0 && optInFormBlocks.length === 0) {
46
+ this.logger.log("No optin-ladder elements found");
47
+ return;
48
+ }
49
+ // Check if the e-mail field exist and is not empty
50
+ const emailField = ENGrid.getField("supporter.emailAddress");
51
+ if (!emailField || !emailField.value) {
52
+ this.logger.log("Email field is empty");
53
+ // Since this is a OptInLadder page with no e-mail address, hide the page
54
+ this.hidePage();
55
+ return;
56
+ }
57
+ const sessionStorageCheckboxValues = JSON.parse(sessionStorage.getItem("engrid.supporter.questions") || "{}");
58
+ let currentStep = 0;
59
+ let totalSteps = optInHeaders.length;
60
+ let currentHeader = null;
61
+ let currentFormBlock = null;
62
+ for (let i = 0; i < optInHeaders.length; i++) {
63
+ const header = optInHeaders[i];
64
+ // Get the optin number from the .optin-ladder-XXXX class
65
+ const optInNumber = header.className.match(/optin-ladder-(\d+)/);
66
+ if (!optInNumber) {
67
+ this.logger.error(`No optin number found in ${header.innerText.trim()}`);
68
+ return;
69
+ }
70
+ const optInIndex = optInNumber[1];
71
+ // Get the checkbox FormBlock
72
+ const formBlock = document.querySelector(`.en__component--formblock.optin-ladder:has(.en__field--${optInIndex})`);
73
+ if (!formBlock) {
74
+ this.logger.log(`No form block found for ${header.innerText.trim()}`);
75
+ // Remove the header if there is no form block
76
+ header.remove();
77
+ // Increment the current step
78
+ currentStep++;
79
+ continue;
80
+ }
81
+ // Check if the optInIndex is in sessionStorage
82
+ if (sessionStorageCheckboxValues[optInIndex] === "Y") {
83
+ // If the checkbox is checked, remove the header and form block
84
+ header.remove();
85
+ formBlock.remove();
86
+ // Increment the current step
87
+ currentStep++;
88
+ continue;
89
+ }
90
+ // If there's a header and a form block, end the loop
91
+ currentHeader = header;
92
+ currentFormBlock = formBlock;
93
+ currentStep++;
94
+ break;
95
+ }
96
+ if (!currentHeader || !currentFormBlock) {
97
+ this.logger.log("No optin-ladder elements found");
98
+ // Set the current step to the total steps to avoid redirecting to the first page
99
+ currentStep = totalSteps;
100
+ this.saveStepToSessionStorage(currentStep, totalSteps);
101
+ // hide the page
102
+ this.hidePage();
103
+ return;
104
+ }
105
+ // Show the current header and form block, while removing the rest
106
+ optInHeaders.forEach((header) => {
107
+ if (header !== currentHeader) {
108
+ header.remove();
109
+ }
110
+ else {
111
+ header.style.display = "block";
112
+ }
113
+ });
114
+ optInFormBlocks.forEach((formBlock) => {
115
+ if (formBlock !== currentFormBlock) {
116
+ formBlock.remove();
117
+ }
118
+ else {
119
+ formBlock.style.display = "block";
120
+ }
121
+ });
122
+ // Save the current step to sessionStorage
123
+ this.saveStepToSessionStorage(currentStep, totalSteps);
124
+ // On form submit, save the checkbox values to sessionStorage
125
+ this._form.onSubmit.subscribe(() => {
126
+ this.saveOptInsToSessionStorage();
127
+ // Save the current step to sessionStorage
128
+ currentStep++;
129
+ this.saveStepToSessionStorage(currentStep, totalSteps);
130
+ });
131
+ }
132
+ runAsChildThankYou() {
133
+ if (!this.isEmbeddedThankYouPage()) {
134
+ this.logger.log("Not Embedded on a Thank You Page");
135
+ return;
136
+ }
137
+ const sessionStorageOptInLadder = JSON.parse(sessionStorage.getItem("engrid.optin-ladder") || "{}");
138
+ const currentStep = sessionStorageOptInLadder.step || 0;
139
+ const totalSteps = sessionStorageOptInLadder.totalSteps || 0;
140
+ if (currentStep < totalSteps) {
141
+ this.logger.log(`Current step ${currentStep} is less than total steps ${totalSteps}`);
142
+ // Redirect to the first page
143
+ window.location.href = this.getFirstPageUrl();
144
+ return;
145
+ }
146
+ else {
147
+ this.logger.log(`Current step ${currentStep} is equal to total steps ${totalSteps}`);
148
+ // Remove the session storage
149
+ sessionStorage.removeItem("engrid.optin-ladder");
150
+ }
151
+ }
152
+ inIframe() {
153
+ try {
154
+ return window.self !== window.top;
155
+ }
156
+ catch (e) {
157
+ return true;
158
+ }
159
+ }
160
+ saveStepToSessionStorage(step, totalSteps) {
161
+ sessionStorage.setItem("engrid.optin-ladder", JSON.stringify({ step, totalSteps }));
162
+ this.logger.log(`Saved step ${step} of ${totalSteps} to sessionStorage`);
163
+ }
164
+ getFirstPageUrl() {
165
+ // Get the current URL and replace the last path with 1?chain
166
+ const url = new URL(window.location.href);
167
+ const path = url.pathname.split("/");
168
+ path.pop();
169
+ path.push("1");
170
+ return url.origin + path.join("/") + "?chain";
171
+ }
172
+ saveOptInsToSessionStorage() {
173
+ // Grab all the checkboxes with the name starting with "supporter.questions"
174
+ const checkboxes = document.querySelectorAll('input[name^="supporter.questions"]');
175
+ if (checkboxes.length === 0) {
176
+ this.logger.log("No checkboxes found");
177
+ return;
178
+ }
179
+ const sessionStorageCheckboxValues = JSON.parse(sessionStorage.getItem("engrid.supporter.questions") || "{}");
180
+ // Loop through all the checkboxes and store the value in sessionStorage
181
+ checkboxes.forEach((checkbox) => {
182
+ if (checkbox.checked) {
183
+ const index = checkbox.name.split(".")[2];
184
+ sessionStorageCheckboxValues[index] = "Y";
185
+ }
186
+ });
187
+ sessionStorage.setItem("engrid.supporter.questions", JSON.stringify(sessionStorageCheckboxValues));
188
+ this.logger.log(`Saved checkbox values to sessionStorage: ${JSON.stringify(sessionStorageCheckboxValues)}`);
189
+ }
190
+ isEmbeddedThankYouPage() {
191
+ return ENGrid.getBodyData("embedded") === "thank-you-page-donation";
192
+ }
193
+ hidePage() {
194
+ const engridPage = document.querySelector("#engrid");
195
+ if (engridPage) {
196
+ engridPage.classList.add("hide");
197
+ }
198
+ }
199
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AppVersion = "0.19.22";
1
+ export declare const AppVersion = "0.20.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const AppVersion = "0.19.22";
1
+ export const AppVersion = "0.20.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4site/engrid-scripts",
3
- "version": "0.19.22",
3
+ "version": "0.20.0",
4
4
  "description": "Scripts for the ENGRID Project",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "strongly-typed-events": "^2.0.9"
26
26
  },
27
27
  "homepage": "https://github.com/4site-interactive-studios/engrid#readme",
28
- "gitHead": "4065bb1ceea07ec2fffd5d08501658ec313c3214",
28
+ "gitHead": "3ad823e81402dce7592f47930bba030e9935fe9a",
29
29
  "devDependencies": {
30
30
  "nodemon": "^2.0.20"
31
31
  }