This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
263 lines (183 loc) · 6.87 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
jQuery(function ($) {
//Promise polyfill
//var RSVP = require('rsvp'); //Using the RSVP library for Promise functions in IE11: https://github.com/tildeio/rsvp.js/ and https://developers.google.com/web/fundamentals/primers/promises
//Globals
var increase_amount = false;
// JS
window.Counter = {
/**
* init - Runs first
*
*/
init: function () {
this.cookie = script_localise.cookie_name; //The name of the cookie to be set/checked
this.cookie_duration = parseInt(script_localise.cookie_duration); //The duration of the cookie
this.content_wrapper = jQuery("#" + script_localise.content_wrapper);
this.content_messages = jQuery("#" + script_localise.content_messages);
/* Disable standard form submissions on form submit button */
this.content_wrapper.find('form').submit(function (event) {
event.preventDefault();
});
/* Debugging */
Cookies.remove(this.cookie);
/* Run functions dependant on Cookie */
cookie = Cookies.get(this.cookie);
//If no cookie is set, continue displaying the interactive elements
if (typeof cookie === 'undefined') {
//Every 5 seconds, execute 'ajax_counter_current' function
window.setInterval(function () {
Counter.ajax_counter_current();
}, 5000);
//Run once
Counter.form_submission();
//Else, cookie is set. Display message stating no form submissions can be made
} else {
//Set error message
Counter.status_message('alert-error', 'You have already submitted the form.', this.content_messages);
//Hide the content wrapper
this.content_wrapper.hide();
}
},
/* AJAX functions ---------------------------------------------------- */
/**
* ajax_counter_increase
* Increase the counter via AJAX
* =====================================================================
*/
ajax_counter_increase: function (increase_value) {
/* Create Variables */
var increase_value = increase_value;
if (increase_value < 1) {
increase_value = false;
}
/* Run AJAX via Promise - https://www.taniarascia.com/how-to-promisify-an-ajax-call/ */
return new RSVP.Promise(function (resolve, reject) {
jQuery.ajax({
type: "POST",
async: true,
url: script_localise.ajaxurl, //URL from functions.php
data: {
action: script_localise.action_counter_increase, //Action name
increase_value: increase_value,
nonce: script_localise.nonce
},
//On success
success: function (data) {
//Set data to repsonse variable
response = data;
resolve(data)
//resolve(data.data['value'])
//console.log(data.data['value']);
//return data.data['value'];
console.log("done");
},
//On error
error: function (xhr, status) {
//console.log("'ajax_counter_increase' error.");
reject("'ajax_counter_increase' error.")
},
//On completion of AJAX call
complete: function (xhr, status) {
//console.log("'ajax_counter_increase' complete.");
}
});
});
},
/**
* ajax_counter_current
* Retrieve the current value of the counter via AJAX
* =====================================================================
*/
ajax_counter_current: function () {
/* Run AJAX */
jQuery.ajax({
type: "POST",
url: script_localise.ajaxurl, //URL from functions.php
data: {
action: script_localise.action_counter_current, //Action name
nonce: script_localise.nonce
},
//On success
success: function (data) {
Counter.callback_refresh_counter_current_value(data); //Run the update function to display current up-to-date value in HTML
},
//On error
error: function (xhr, status) {
console.log("'ajax_counter_current' error.");
},
//On completion of AJAX call
complete: function (xhr, status) {
//console.log("'ajax_counter_current' completed.");
}
});
},
/* Callbacks ---------------------------------------------------- */
/**
* callback_refresh_counter_current_value
* Retrieve the current counter value and update HTML
* =====================================================================
*/
callback_refresh_counter_current_value: function (response) {
//If success
if (response.success === true) {
//Update the HTML content of Div ID '#counter'
$('#counter span').html(response.data['value']);
//Else, failure
} else if (response.success === false) {
//console.log("failure");
}
},
/* Page functions ---------------------------------------------------- */
/**
* statusMessages
* The status messages to be displayed, either success or error
* type = either 'success' or 'error'
* text = the text to display
* element = in which element to display
* =====================================================================
*/
status_message: function (type, text, element) {
var element = jQuery(element);
element.html('<div class="alert ' + type + '">' + text + '</div>');
},
/**
* form_submission
* Submission of the form
* =====================================================================
*/
form_submission: function () {
/* Create Variables */
var cookie = this.cookie; //Cookie to be set
var cookie_duration = this.cookie_duration //Cookie duration
var content_wrapper = this.content_wrapper; //The content wrapper
var content_messages = this.content_messages; //The content message
/* On submission of form */
content_wrapper.find('form').submit(function (event) {
/* Reset the amount to increase */
increase_amount = 0;
/* Run AJAX call to increase the counter */
Counter.ajax_counter_increase(increase_amount).then(function (value) {
//If success
if (response.success === true) {
//console.log("'ajax_counter_increase' complete.");
/* Get the lastest counter value - refresh */
Counter.ajax_counter_current();
/* Set cookie for completion of form */
Cookies.set(cookie, cookie, { expires: cookie_duration });
/* Set success message */
Counter.status_message('alert-success', 'Thankyou for submitting.' + answer_feedback, content_messages);
content_wrapper.find('form').trigger("reset");
//Else, failure
} else if (response.success === false) {
//console.log("'ajax_counter_increase' error.");
/* Set error message */
Counter.status_message('alert-error', 'Unable to submit.', content_messages);
}
});
});
},
}
$(function () {
Counter.init(); //Run init
});
});