Skip to content

Commit 7cefcce

Browse files
committed
allowing multiple recaptchas
1 parent 5bbdf81 commit 7cefcce

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/components/enquiry.vue

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<tcs-input :field="field"></tcs-input>
1515
</div>
1616

17-
<div :id="grecaptcha_container_id" class="grecaptcha"></div>
17+
<div :id="$root.grecaptcha_container_id" class="grecaptcha"></div>
1818
<div v-if="grecaptcha_missing" class="error-msg">
1919
{{ $root.get_text('grecaptcha_missing') }}
2020
</div>
@@ -61,8 +61,6 @@ export default {
6161
data: () => ({
6262
submitted: false,
6363
grecaptcha_missing: false,
64-
grecaptcha_container_id: 'grecaptcha_' + Math.random().toString(36).substring(2, 10),
65-
grecaptcha_id: null,
6664
}),
6765
methods: {
6866
submit () {
@@ -83,24 +81,15 @@ export default {
8381
},
8482
/* istanbul ignore next */
8583
prepare_grecaptcha () {
86-
const grecaptcha_callback = response => this.$set(this.$root.enquiry_data, 'grecaptcha_response', response)
87-
8884
if (this.$root.grecaptcha_key === null) {
89-
grecaptcha_callback('-')
85+
this.$root.grecaptcha_callback('-')
9086
return
9187
}
9288
93-
const render_grecaptcha = () => {
94-
this.grecaptcha_id = window.grecaptcha.render(this.grecaptcha_container_id, {
95-
sitekey: this.$root.grecaptcha_key,
96-
callback: grecaptcha_callback
97-
})
98-
}
9989
if (window.grecaptcha === undefined) {
100-
window._tcs_grecaptcha_loaded = render_grecaptcha
10190
add_script('https://www.google.com/recaptcha/api.js?onload=_tcs_grecaptcha_loaded&render=explicit')
10291
} else {
103-
render_grecaptcha()
92+
this.$root.render_grecaptcha()
10493
}
10594
}
10695
},

src/main.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = function (public_key, config) {
163163

164164
const ga_prefixes = init_ga(router, config)
165165

166-
return new Vue({
166+
const v = new Vue({
167167
el: config.element,
168168
router: router,
169169
render: h => h(app),
@@ -178,6 +178,7 @@ module.exports = function (public_key, config) {
178178
enquiry_form_info: {},
179179
enquiry_data: {},
180180
selected_subject_id: null,
181+
grecaptcha_container_id: 'grecaptcha_' + Math.random().toString(36).substring(2, 10),
181182
},
182183
components: {
183184
app
@@ -328,9 +329,38 @@ ${xhr.responseText}`)
328329
window.ga(prefix + 'send', 'event', category, action, label)
329330
}
330331
},
332+
grecaptcha_callback (response) {
333+
Vue.set(this.enquiry_data, 'grecaptcha_response', response)
334+
},
335+
render_grecaptcha () {
336+
const el = document.getElementById(this.grecaptcha_container_id)
337+
if (el && el.childElementCount === 0) {
338+
console.debug('rendering grecaptcha')
339+
window.grecaptcha.render(this.grecaptcha_container_id, {
340+
sitekey: this.grecaptcha_key,
341+
callback: this.grecaptcha_callback
342+
})
343+
} else {
344+
console.debug('not rendering grecaptcha', el)
345+
}
346+
},
347+
331348
goto (name, params) {
332349
this.$router.push({'name': name, params: params})
333350
}
334351
}
335352
})
353+
if (window.socket_view === undefined) {
354+
window.socket_view = [v]
355+
} else {
356+
window.socket_view.push(v)
357+
}
358+
359+
window._tcs_grecaptcha_loaded = () => {
360+
console.log('_tcs_grecaptcha_loaded', window.socket_view)
361+
for (let v of window.socket_view) {
362+
v.render_grecaptcha()
363+
}
364+
}
365+
return v
336366
}

test/unit/specs/_shared.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function generate_vm (router, vm_data_) {
100100
callback()
101101
},
102102
ga_event () { this.__record_call('ga_event') },
103+
grecaptcha_callback (r) { this.__record_call('grecaptcha_callback', r) },
103104
get_selected_subject () {
104105
this.__record_call('get_selected_subject')
105106
return null

0 commit comments

Comments
 (0)