|
1
|
|
|
GLSR.SCROLL_TIME = 468; |
|
|
|
|
|
|
2
|
|
|
GLSR.activeForm = null; |
|
3
|
|
|
GLSR.recaptcha = {}; |
|
4
|
|
|
|
|
5
|
|
|
GLSR.buildFormData = function( recaptchaToken ) |
|
6
|
|
|
{ |
|
7
|
|
|
if( recaptchaToken === undefined ) { |
|
8
|
|
|
recaptchaToken = ''; |
|
9
|
|
|
} |
|
10
|
|
|
// console.log( 'recaptchaToken: ' + recaptchaToken ); |
|
11
|
|
|
return { |
|
12
|
|
|
action: site_reviews.action, |
|
|
|
|
|
|
13
|
|
|
request: GLSR.parseFormData( GLSR.activeForm ), |
|
|
|
|
|
|
14
|
|
|
'g-recaptcha-response': recaptchaToken, |
|
15
|
|
|
}; |
|
16
|
|
|
}; |
|
17
|
|
|
|
|
18
|
|
|
GLSR.clearFieldError = function( el ) |
|
19
|
|
|
{ |
|
20
|
|
|
var fieldEl = el.closest( '.glsr-field' ); |
|
21
|
|
|
if( fieldEl === null )return; |
|
22
|
|
|
var errorEl = fieldEl.querySelector( '.glsr-field-errors' ); |
|
23
|
|
|
fieldEl.classList.remove( 'glsr-has-error' ); |
|
24
|
|
|
if( errorEl !== null ) { |
|
25
|
|
|
errorEl.parentNode.removeChild( errorEl ); |
|
26
|
|
|
} |
|
27
|
|
|
}; |
|
28
|
|
|
|
|
29
|
|
|
GLSR.clearFormErrors = function() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
var formEl = GLSR.activeForm; |
|
|
|
|
|
|
32
|
|
|
GLSR.clearFormMessages(); |
|
33
|
|
|
for( var i = 0; i < formEl.length; i++ ) { |
|
34
|
|
|
GLSR.clearFieldError( formEl[i] ); |
|
35
|
|
|
} |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
|
|
GLSR.clearFormMessages = function() |
|
39
|
|
|
{ |
|
40
|
|
|
var messageEl = GLSR.activeForm.querySelector( '.glsr-form-messages' ); |
|
|
|
|
|
|
41
|
|
|
if( messageEl ) { |
|
42
|
|
|
messageEl.innerHTML = ''; |
|
43
|
|
|
} |
|
44
|
|
|
}; |
|
45
|
|
|
|
|
46
|
|
|
GLSR.createExceprts = function( parentEl ) |
|
47
|
|
|
{ |
|
48
|
|
|
parentEl = parentEl || document; |
|
49
|
|
|
var excerpts = parentEl.querySelectorAll( '.glsr-hidden-text' ); |
|
50
|
|
|
for( var i = 0; i < excerpts.length; i++ ) { |
|
51
|
|
|
var readmore = GLSR.insertAfter( excerpts[i], 'span', { |
|
|
|
|
|
|
52
|
|
|
'class': 'glsr-read-more', |
|
53
|
|
|
}); |
|
54
|
|
|
var readmoreLink = GLSR.appendTo( readmore, 'a', { |
|
55
|
|
|
'href': '#', |
|
56
|
|
|
'data-text': excerpts[i].getAttribute( 'data-show-less' ), |
|
57
|
|
|
}); |
|
58
|
|
|
readmoreLink.innerHTML = excerpts[i].getAttribute( 'data-show-more' ); |
|
59
|
|
|
} |
|
60
|
|
|
GLSR.on( 'click', '.glsr-read-more a', GLSR.onClickReadMore ); |
|
61
|
|
|
}; |
|
62
|
|
|
|
|
63
|
|
|
GLSR.createStarRatings = function() |
|
64
|
|
|
{ |
|
65
|
|
|
new StarRating( 'select.glsr-star-rating', { |
|
|
|
|
|
|
66
|
|
|
clearable: false, |
|
67
|
|
|
showText: false, |
|
68
|
|
|
onClick: GLSR.clearFieldError, |
|
|
|
|
|
|
69
|
|
|
}); |
|
70
|
|
|
}; |
|
71
|
|
|
|
|
72
|
|
|
GLSR.enableSubmitButton = function() |
|
73
|
|
|
{ |
|
74
|
|
|
GLSR.activeForm.querySelector( '[type="submit"]' ).removeAttribute( 'disabled' ); |
|
|
|
|
|
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
GLSR.getSelectorOfElement = function( el ) |
|
78
|
|
|
{ |
|
79
|
|
|
if( !el || el.nodeType !== el.ELEMENT_NODE )return; |
|
80
|
|
|
return el.nodeName.toLowerCase() + |
|
81
|
|
|
( el.id ? '#' + el.id.trim() : '' ) + |
|
82
|
|
|
( el.className ? '.' + el.className.trim().replace( /\s+/g, '.' ) : '' ); |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
GLSR.onClickPagination = function( ev ) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
ev.preventDefault(); |
|
88
|
|
|
var parentEl = this.closest( '.glsr-reviews' ); |
|
89
|
|
|
var parentSelector = GLSR.getSelectorOfElement( parentEl ); |
|
|
|
|
|
|
90
|
|
|
parentEl.classList.add( 'glsr-hide' ); |
|
91
|
|
|
GLSR.getAjax( this.href, function( response ) { |
|
92
|
|
|
var html = document.implementation.createHTMLDocument( 'new' ); |
|
93
|
|
|
html.documentElement.innerHTML = response; |
|
94
|
|
|
var newParentEl = parentSelector ? html.querySelectorAll( parentSelector ) : ''; |
|
95
|
|
|
if( newParentEl.length === 1 ) { |
|
96
|
|
|
parentEl.innerHTML = newParentEl[0].innerHTML; |
|
97
|
|
|
GLSR.scrollToTop( parentEl ); |
|
|
|
|
|
|
98
|
|
|
parentEl.classList.remove( 'glsr-hide' ); |
|
99
|
|
|
GLSR.on( 'click', '.glsr-ajax-pagination .glsr-navigation a', GLSR.onClickPagination ); |
|
100
|
|
|
window.history.pushState( null, '', ev.target.href ); |
|
101
|
|
|
GLSR.createExceprts( parentEl ); |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
window.location = ev.target.href; |
|
105
|
|
|
}); |
|
106
|
|
|
}; |
|
107
|
|
|
|
|
108
|
|
|
GLSR.onClickReadMore = function( ev ) |
|
109
|
|
|
{ |
|
110
|
|
|
ev.preventDefault(); |
|
111
|
|
|
var el = ev.target; |
|
112
|
|
|
var hiddenNode = el.parentNode.previousSibling; |
|
113
|
|
|
var text = el.getAttribute( 'data-text' ); |
|
114
|
|
|
hiddenNode.classList.toggle( 'glsr-hidden' ); |
|
115
|
|
|
hiddenNode.classList.toggle( 'glsr-visible' ); |
|
116
|
|
|
el.setAttribute( 'data-text', el.innerText ); |
|
117
|
|
|
el.innerText = text; |
|
118
|
|
|
}; |
|
119
|
|
|
|
|
120
|
|
|
GLSR.recaptcha.addListeners = function() |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
var overlayEl = GLSR.recaptcha.overlay(); |
|
|
|
|
|
|
123
|
|
|
if( Object.prototype.toString.call( overlayEl ) === '[object HTMLDivElement]' ) { |
|
124
|
|
|
overlayEl.addEventListener( 'click', GLSR.enableSubmitButton, false ); |
|
125
|
|
|
window.addEventListener( 'keyup', GLSR.recaptcha.onKeyup.bind( overlayEl ), false ); |
|
126
|
|
|
} |
|
127
|
|
|
}; |
|
128
|
|
|
|
|
129
|
|
|
GLSR.recaptcha.execute = function() |
|
130
|
|
|
{ |
|
131
|
|
|
var recaptchaId = GLSR.recaptcha.id(); |
|
|
|
|
|
|
132
|
|
|
if( recaptchaId !== -1 ) { |
|
133
|
|
|
return grecaptcha.execute( recaptchaId ); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
// recaptcha ID not found so pass through an error |
|
136
|
|
|
return GLSR.submitForm( false ); |
|
137
|
|
|
}; |
|
138
|
|
|
|
|
139
|
|
|
GLSR.recaptcha.id = function() |
|
140
|
|
|
{ |
|
141
|
|
|
return GLSR.recaptcha.search( function( value, id ) { |
|
|
|
|
|
|
142
|
|
|
if( Object.prototype.toString.call( value ) !== '[object HTMLDivElement]' )return; |
|
143
|
|
|
if( value.closest( 'form' ) === GLSR.activeForm ) { |
|
|
|
|
|
|
144
|
|
|
return id; |
|
145
|
|
|
} |
|
146
|
|
|
}); |
|
147
|
|
|
}; |
|
148
|
|
|
|
|
149
|
|
|
GLSR.recaptcha.onKeyup = function( ev ) |
|
150
|
|
|
{ |
|
151
|
|
|
if( ev.keyCode !== 27 )return; |
|
152
|
|
|
GLSR.enableSubmitButton(); |
|
|
|
|
|
|
153
|
|
|
GLSR.recaptcha.removeListeners( this ); |
|
154
|
|
|
}; |
|
155
|
|
|
|
|
156
|
|
|
GLSR.recaptcha.overlay = function() |
|
157
|
|
|
{ |
|
158
|
|
|
return GLSR.recaptcha.search( function( value ) { |
|
|
|
|
|
|
159
|
|
|
if( Object.prototype.toString.call( value ) !== '[object Object]' )return; |
|
160
|
|
|
for( var obj in value) { |
|
161
|
|
|
if( !value.hasOwnProperty( obj ) || Object.prototype.toString.call( value[obj] ) !== '[object HTMLDivElement]' )continue; |
|
162
|
|
|
if( value[obj].className === '' ) { |
|
163
|
|
|
return value[obj].firstChild; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
return false; |
|
167
|
|
|
}); |
|
168
|
|
|
}; |
|
169
|
|
|
|
|
170
|
|
|
GLSR.recaptcha.removeListeners = function( overlayEl ) |
|
|
|
|
|
|
171
|
|
|
{ |
|
172
|
|
|
overlayEl.removeEventListener( 'click', GLSR.enableSubmitButton, false ); |
|
|
|
|
|
|
173
|
|
|
window.removeEventListener( 'keyup', GLSR.recaptcha.onKeyup, false ); |
|
174
|
|
|
}; |
|
175
|
|
|
|
|
176
|
|
|
GLSR.recaptcha.reset = function() |
|
177
|
|
|
{ |
|
178
|
|
|
var recaptchaId = GLSR.recaptcha.id(); |
|
|
|
|
|
|
179
|
|
|
if( recaptchaId !== -1 ) { |
|
180
|
|
|
grecaptcha.reset( recaptchaId ); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
}; |
|
183
|
|
|
|
|
184
|
|
|
GLSR.recaptcha.search = function( callback ) |
|
185
|
|
|
{ |
|
186
|
|
|
var result = -1; |
|
187
|
|
|
if( window.hasOwnProperty( '___grecaptcha_cfg' )) { |
|
188
|
|
|
var clients = window.___grecaptcha_cfg.clients; |
|
189
|
|
|
var i, key; |
|
190
|
|
|
for( i in clients ) { |
|
191
|
|
|
for( key in clients[i] ) { |
|
192
|
|
|
if( !( result = callback( clients[i][key], i )))continue; |
|
193
|
|
|
return result; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
return result; |
|
198
|
|
|
}; |
|
199
|
|
|
|
|
200
|
|
|
GLSR.setDirection = function() |
|
|
|
|
|
|
201
|
|
|
{ |
|
202
|
|
|
var widgets = document.querySelectorAll( '.glsr-widget, .glsr-shortcode' ); |
|
203
|
|
|
for( var i = 0; i < widgets.length; i++ ) { |
|
204
|
|
|
var direction = window.getComputedStyle( widgets[i], null ).getPropertyValue( 'direction' ); |
|
205
|
|
|
widgets[i].classList.add( 'glsr-' + direction ); |
|
206
|
|
|
} |
|
207
|
|
|
}; |
|
208
|
|
|
|
|
209
|
|
|
GLSR.scrollToTop = function( el, offset ) |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
offset = offset || 16; // 1rem |
|
212
|
|
|
var fixedElement; |
|
213
|
|
|
for( var i = 0; i < site_reviews.ajaxpagination.length; i++ ) { |
|
|
|
|
|
|
214
|
|
|
fixedElement = document.querySelector( site_reviews.ajaxpagination[i] ); |
|
215
|
|
|
if( fixedElement && window.getComputedStyle( fixedElement ).getPropertyValue( 'position' ) === 'fixed' ) { |
|
216
|
|
|
offset = offset + fixedElement.clientHeight; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
var clientBounds = el.getBoundingClientRect(); |
|
220
|
|
|
var offsetTop = clientBounds.top - offset; |
|
221
|
|
|
if( offsetTop > 0 )return; // if top is in view, don't scroll! |
|
222
|
|
|
if( 'requestAnimationFrame' in window === false ) { |
|
223
|
|
|
window.scroll( 0, window.pageYOffset + offsetTop ); |
|
224
|
|
|
return; |
|
225
|
|
|
} |
|
226
|
|
|
GLSR.scrollToTopStep({ |
|
|
|
|
|
|
227
|
|
|
endY: offsetTop, |
|
228
|
|
|
offset: window.pageYOffset, |
|
229
|
|
|
startTime: window.performance.now(), |
|
230
|
|
|
startY: el.scrollTop, |
|
231
|
|
|
}); |
|
232
|
|
|
}; |
|
233
|
|
|
|
|
234
|
|
|
GLSR.scrollToTopStep = function( context ) |
|
235
|
|
|
{ |
|
236
|
|
|
var elapsed = ( window.performance.now() - context.startTime ) / GLSR.SCROLL_TIME; |
|
|
|
|
|
|
237
|
|
|
elapsed = elapsed > 1 ? 1 : elapsed; |
|
238
|
|
|
var easedValue = 0.5 * ( 1 - Math.cos( Math.PI * elapsed )); |
|
239
|
|
|
var currentY = context.startY + ( context.endY - context.startY ) * easedValue; |
|
240
|
|
|
window.scroll( 0, context.offset + currentY ); |
|
241
|
|
|
if( currentY !== context.endY ) { |
|
242
|
|
|
window.requestAnimationFrame( GLSR.scrollToTopStep.bind( window, context )); |
|
243
|
|
|
} |
|
244
|
|
|
}; |
|
245
|
|
|
|
|
246
|
|
|
GLSR.showFormErrors = function( errors ) |
|
247
|
|
|
{ |
|
248
|
|
|
if( !errors )return; |
|
249
|
|
|
var fieldEl, errorsEl; |
|
250
|
|
|
for( var error in errors ) { |
|
251
|
|
|
if( !errors.hasOwnProperty( error ))continue; |
|
252
|
|
|
fieldEl = GLSR.activeForm.querySelector( '[name="' + error + '"]' ).closest( '.glsr-field' ); |
|
|
|
|
|
|
253
|
|
|
fieldEl.classList.add( 'glsr-has-error' ); |
|
254
|
|
|
errorsEl = fieldEl.querySelector( '.glsr-field-errors' ); |
|
255
|
|
|
if( errorsEl === null ) { |
|
256
|
|
|
errorsEl = GLSR.appendTo( fieldEl, 'span', { |
|
257
|
|
|
'class': 'glsr-field-errors', |
|
258
|
|
|
}); |
|
259
|
|
|
} |
|
260
|
|
|
for( var i = 0; i < errors[ error ].errors.length; i++ ) { |
|
261
|
|
|
if( errors[ error ].errors[i] === null )continue; |
|
262
|
|
|
errorsEl.innerHTML += '<span class="glsr-field-error">' + errors[ error ].errors[i] + '</span>'; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
}; |
|
266
|
|
|
|
|
267
|
|
|
GLSR.showFormMessage = function( response ) |
|
268
|
|
|
{ |
|
269
|
|
|
var formIdEl = GLSR.activeForm.querySelector( 'input[name="form_id"]' ); |
|
|
|
|
|
|
270
|
|
|
var messageEl = GLSR.activeForm.querySelector( '.glsr-form-messages' ); |
|
271
|
|
|
if( messageEl === null ) { |
|
272
|
|
|
messageEl = GLSR.insertAfter( formIdEl, 'div', { |
|
273
|
|
|
'class': 'glsr-form-messages', |
|
274
|
|
|
}); |
|
275
|
|
|
} |
|
276
|
|
|
if( !!response.errors ) { |
|
277
|
|
|
messageEl.classList.add( 'gslr-has-errors' ); |
|
278
|
|
|
} |
|
279
|
|
|
else { |
|
280
|
|
|
messageEl.classList.remove( 'gslr-has-errors' ); |
|
281
|
|
|
} |
|
282
|
|
|
messageEl.innerHTML = '<p>' + response.message + '</p>'; |
|
283
|
|
|
}; |
|
284
|
|
|
|
|
285
|
|
|
GLSR.submitForm = function( recaptchaToken ) |
|
286
|
|
|
{ |
|
287
|
|
|
GLSR.activeForm.querySelector( '[type="submit"]' ).setAttribute( 'disabled', '' ); |
|
|
|
|
|
|
288
|
|
|
GLSR.postAjax( site_reviews.ajaxurl, GLSR.buildFormData( recaptchaToken ), function( response ) { |
|
|
|
|
|
|
289
|
|
|
// console.log( response ); |
|
290
|
|
|
if( response.recaptcha === true ) { |
|
291
|
|
|
// console.log( 'executing recaptcha' ); |
|
292
|
|
|
return GLSR.recaptcha.execute(); |
|
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
if( response.recaptcha === 'reset' ) { |
|
295
|
|
|
// console.log( 'reseting failed recaptcha' ); |
|
296
|
|
|
GLSR.recaptcha.reset(); |
|
297
|
|
|
} |
|
298
|
|
|
if( response.errors === false ) { |
|
299
|
|
|
// console.log( 'reseting recaptcha' ); |
|
300
|
|
|
GLSR.recaptcha.reset(); |
|
301
|
|
|
GLSR.activeForm.reset(); |
|
302
|
|
|
} |
|
303
|
|
|
// console.log( 'submission finished' ); |
|
304
|
|
|
GLSR.showFormErrors( response.errors ); |
|
305
|
|
|
GLSR.showFormMessage( response ); |
|
306
|
|
|
GLSR.enableSubmitButton(); |
|
307
|
|
|
response.form = GLSR.activeForm; |
|
308
|
|
|
document.dispatchEvent( new CustomEvent( 'site-reviews/after/submission', { detail: response })); |
|
|
|
|
|
|
309
|
|
|
GLSR.activeForm = null; |
|
|
|
|
|
|
310
|
|
|
}); |
|
311
|
|
|
}; |
|
312
|
|
|
|
|
313
|
|
|
GLSR.on( 'change', 'form.glsr-submit-review-form', function( ev ) |
|
314
|
|
|
{ |
|
315
|
|
|
GLSR.clearFieldError( ev.target ); |
|
|
|
|
|
|
316
|
|
|
}); |
|
317
|
|
|
|
|
318
|
|
|
GLSR.on( 'submit', 'form.glsr-submit-review-form', function( ev ) |
|
319
|
|
|
{ |
|
320
|
|
|
if( this.classList.contains( 'no-ajax' ))return; |
|
321
|
|
|
ev.preventDefault(); |
|
322
|
|
|
GLSR.activeForm = this; |
|
|
|
|
|
|
323
|
|
|
GLSR.recaptcha.addListeners(); |
|
324
|
|
|
GLSR.clearFormErrors(); |
|
325
|
|
|
GLSR.submitForm(); |
|
326
|
|
|
}); |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* This event function exists to undo the mayhem caused by the invisible-recaptcha plugin |
|
330
|
|
|
* This function is triggered on the invisible-recaptcha callback |
|
331
|
|
|
*/ |
|
332
|
|
|
GLSR.on( 'click', '.glsr-field [type="submit"]', function() |
|
333
|
|
|
{ |
|
334
|
|
|
this.closest( 'form' ).onsubmit = null; |
|
335
|
|
|
HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit; |
|
|
|
|
|
|
336
|
|
|
HTMLFormElement.prototype.submit = function() { |
|
337
|
|
|
var token = this.querySelector( '#g-recaptcha-response' ); |
|
338
|
|
|
if( null === token || null === this.querySelector( '.glsr-field' )) { |
|
339
|
|
|
this._submit(); |
|
340
|
|
|
} |
|
341
|
|
|
else { |
|
342
|
|
|
GLSR.submitForm( token.value ); |
|
|
|
|
|
|
343
|
|
|
} |
|
344
|
|
|
}; |
|
345
|
|
|
}); |
|
346
|
|
|
|
|
347
|
|
|
GLSR.on( 'click', '.glsr-ajax-pagination .glsr-navigation a', GLSR.onClickPagination ); |
|
348
|
|
|
|
|
349
|
|
|
GLSR.ready( function() |
|
350
|
|
|
{ |
|
351
|
|
|
GLSR.setDirection(); |
|
|
|
|
|
|
352
|
|
|
GLSR.createExceprts(); |
|
353
|
|
|
GLSR.createStarRatings(); |
|
354
|
|
|
}); |
|
355
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.