overleaf/services/web/public/coffee/app/directives/asyncForm.coffee

63 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-06-17 11:36:08 -04:00
define [
"base"
], (App) ->
App.directive "asyncForm", ($http) ->
return {
link: (scope, element, attrs) ->
formName = attrs.asyncForm
2014-06-20 06:15:25 -04:00
scope[attrs.name].response = response = {}
2014-06-17 11:36:08 -04:00
element.on "submit", (e) ->
e.preventDefault()
formData = {}
for data in element.serializeArray()
formData[data.name] = data.value
$http
.post(element.attr('action'), formData)
.success (data, status, headers, config) ->
2014-06-20 06:15:25 -04:00
response.success = true
response.error = false
2014-06-17 11:36:08 -04:00
if data.redir?
ga('send', 'event', formName, 'success')
window.location = data.redir
else if data.message?
2014-06-20 06:15:25 -04:00
response.message = data.message
2014-06-17 11:36:08 -04:00
if data.message.type == "error"
2014-06-20 06:15:25 -04:00
response.success = false
response.error = true
2014-06-17 11:36:08 -04:00
ga('send', 'event', formName, 'failure', data.message)
else
ga('send', 'event', formName, 'success')
.error (data, status, headers, config) ->
2014-06-20 06:15:25 -04:00
response.success = false
response.error = true
response.message =
2014-06-17 11:36:08 -04:00
text: data.message or "Something went wrong talking to the server :(. Please try again."
type: 'error'
2014-06-20 06:15:25 -04:00
ga('send', 'event', formName, 'failure', data.message)
2014-06-17 11:36:08 -04:00
}
App.directive "formMessages", () ->
return {
restrict: "E"
template: """
<div class="alert" ng-class="{
2014-06-20 06:15:25 -04:00
'alert-danger': form.response.message.type == 'error',
'alert-success': form.response.message.type != 'error'
}" ng-show="!!form.response.message">
{{form.response.message.text}}
2014-06-17 11:36:08 -04:00
</div>
<div ng-transclude></div>
"""
transclude: true
2014-06-20 06:15:25 -04:00
scope: {
form: "=for"
}
2014-06-17 11:36:08 -04:00
}