Răsfoiți Sursa

Refactor front end code into validateCaptcha service

James Allen 8 ani în urmă
părinte
comite
69499847e4

+ 9 - 3
services/web/app/views/layout.pug

@@ -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(

+ 0 - 8
services/web/app/views/project/editor/share.pug

@@ -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(

+ 1 - 0
services/web/public/coffee/ide.coffee

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

+ 1 - 20
services/web/public/coffee/ide/share/controllers/ShareProjectModalController.coffee

@@ -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) ->

+ 1 - 0
services/web/public/coffee/main.coffee

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

+ 24 - 0
services/web/public/coffee/services/validateCaptcha.coffee

@@ -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