Refactor front end code into validateCaptcha service

This commit is contained in:
James Allen 2017-12-11 12:32:43 +00:00
parent 0b03bbc7c3
commit 69499847e4
6 changed files with 36 additions and 31 deletions

View file

@ -53,9 +53,6 @@ html(itemscope, itemtype='http://schema.org/Product')
- else
script(type='text/javascript').
window.ga = function() { console.log("would send to GA", arguments) };
- if (settings.recaptcha)
script(src="https://www.google.com/recaptcha/api.js")
script(type="text/javascript").
window.csrfToken = "#{csrfToken}";
@ -100,6 +97,15 @@ html(itemscope, itemtype='http://schema.org/Product')
}
body
if(settings.recaptcha)
script(src="https://www.google.com/recaptcha/api.js?render=explicit")
div(
id="recaptcha"
class="g-recaptcha"
data-sitekey=settings.recaptcha.siteKey
data-size="invisible"
data-badge="inline"
)
- if(typeof(suppressSystemMessages) == "undefined")
.system-messages(

View file

@ -102,14 +102,6 @@ script(type='text/ng-template', id='shareProjectModalTemplate')
i.fa.fa-times
.row.invite-controls
form(ng-show="canAddCollaborators")
- if (settings.recaptcha)
div(
id="recaptcha"
class="g-recaptcha"
data-sitekey=settings.recaptcha.siteKey
data-size="invisible"
data-badge="inline"
)
.small #{translate("share_with_your_collabs")}
.form-group
tags-input(

View file

@ -33,6 +33,7 @@ define [
"directives/expandableTextArea"
"directives/videoPlayState"
"services/queued-http"
"services/validateCaptcha"
"filters/formatDate"
"main/event"
"main/account-upgrade"

View file

@ -1,7 +1,7 @@
define [
"base"
], (App) ->
App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http, ide) ->
App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, projectInvites, $modal, $http, ide, validateCaptcha) ->
$scope.inputs = {
privileges: "readAndWrite"
contacts: []
@ -55,25 +55,6 @@ define [
getCurrentInviteEmails = () ->
($scope.project.invites || []).map (u) -> u.email
_recaptchaCallbacks = []
onRecaptchaSubmit = (token) ->
for cb in _recaptchaCallbacks
cb(token)
_recaptchaCallbacks = []
recaptchaId = null
validateCaptcha = (callback = (response) ->) =>
if !grecaptcha?
return callback()
reset = () ->
grecaptcha.reset()
_recaptchaCallbacks.push callback
_recaptchaCallbacks.push reset
if !recaptchaId?
el = $('.g-recaptcha')[0]
recaptchaId = grecaptcha.render(el, {callback: onRecaptchaSubmit})
grecaptcha.execute(recaptchaId)
$scope.filterAutocompleteUsers = ($query) ->
currentMemberEmails = getCurrentMemberEmails()
return $scope.autocompleteContacts.filter (contact) ->

View file

@ -30,6 +30,7 @@ define [
"directives/maxHeight"
"directives/creditCards"
"services/queued-http"
"services/validateCaptcha"
"filters/formatDate"
"__MAIN_CLIENTSIDE_INCLUDES__"
], () ->

View file

@ -0,0 +1,24 @@
define [
"base"
], (App) ->
App.factory "validateCaptcha", () ->
_recaptchaCallbacks = []
onRecaptchaSubmit = (token) ->
for cb in _recaptchaCallbacks
cb(token)
_recaptchaCallbacks = []
recaptchaId = null
validateCaptcha = (callback = (response) ->) =>
if !grecaptcha?
return callback()
reset = () ->
grecaptcha.reset()
_recaptchaCallbacks.push callback
_recaptchaCallbacks.push reset
if !recaptchaId?
el = $('.g-recaptcha')[0]
recaptchaId = grecaptcha.render(el, {callback: onRecaptchaSubmit})
grecaptcha.execute(recaptchaId)
return validateCaptcha