define [ "base" "libs/passfield" ], (App) -> App.directive "asyncForm", ($http, validateCaptcha) -> return { controller: ['$scope', ($scope) -> @getEmail = () -> return $scope.email return this ] link: (scope, element, attrs) -> formName = attrs.asyncForm scope[attrs.name].response = response = {} scope[attrs.name].inflight = false validateCaptchaIfEnabled = (callback = (response) ->) -> if attrs.captcha? validateCaptcha callback else callback() submitRequest = (grecaptchaResponse) -> formData = {} for data in element.serializeArray() formData[data.name] = data.value if grecaptchaResponse? formData['g-recaptcha-response'] = grecaptchaResponse scope[attrs.name].inflight = true # for asyncForm prevent automatic redirect to /login if # authentication fails, we will handle it ourselves $http .post(element.attr('action'), formData, {disableAutoLoginRedirect: true}) .then (httpResponse) -> { data, status, headers, config } = httpResponse scope[attrs.name].inflight = false response.success = true response.error = false onSuccessHandler = scope[attrs.onSuccess] if onSuccessHandler onSuccessHandler(httpResponse) return if data.redir? ga('send', 'event', formName, 'success') window.location = data.redir else if data.message? response.message = data.message if data.message.type == "error" response.success = false response.error = true ga('send', 'event', formName, 'failure', data.message) else ga('send', 'event', formName, 'success') .catch (httpResponse) -> { data, status, headers, config } = httpResponse scope[attrs.name].inflight = false response.success = false response.error = true onErrorHandler = scope[attrs.onError] if onErrorHandler onErrorHandler(httpResponse) return if status == 403 # Forbidden response.message = text: "Session error. Please check you have cookies enabled. If the problem persists, try clearing your cache and cookies." type: "error" else response.message = text: data.message?.text or data.message or "Something went wrong talking to the server :(. Please try again." type: 'error' ga('send', 'event', formName, 'failure', data.message) submit = () -> validateCaptchaIfEnabled (response) -> submitRequest response element.on "submit", (e) -> e.preventDefault() submit() if attrs.autoSubmit submit() } App.directive "formMessages", () -> return { restrict: "E" template: """