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

125 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
2015-04-24 09:33:01 -04:00
"libs/passfield"
2014-07-08 07:02:26 -04:00
], (App) ->
App.directive "asyncForm", ($http) ->
return {
controller: ['$scope', ($scope) ->
@getEmail = () ->
return $scope.email
return this
]
2014-07-08 07:02:26 -04:00
link: (scope, element, attrs) ->
formName = attrs.asyncForm
scope[attrs.name].response = response = {}
scope[attrs.name].inflight = false
2014-07-08 07:02:26 -04:00
element.on "submit", (e) ->
e.preventDefault()
formData = {}
for data in element.serializeArray()
formData[data.name] = data.value
scope[attrs.name].inflight = true
2014-07-08 07:02:26 -04:00
$http
.post(element.attr('action'), formData)
.success (data, status, headers, config) ->
scope[attrs.name].inflight = false
2014-07-08 07:02:26 -04:00
response.success = true
response.error = false
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')
2014-07-08 07:02:26 -04:00
.error (data, status, headers, config) ->
scope[attrs.name].inflight = false
2014-07-08 07:02:26 -04:00
response.success = false
response.error = true
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'
2014-07-08 07:02:26 -04:00
ga('send', 'event', formName, 'failure', data.message)
}
App.directive "formMessages", () ->
return {
restrict: "E"
template: """
<div class="alert" ng-class="{
'alert-danger': form.response.message.type == 'error',
'alert-success': form.response.message.type != 'error'
}" ng-show="!!form.response.message">
{{form.response.message.text}}
</div>
<div ng-transclude></div>
"""
transclude: true
scope: {
form: "=for"
}
}
2015-04-24 09:33:01 -04:00
App.directive 'complexPassword', ->
require: ['^asyncForm', 'ngModel']
link: (scope, element, attrs, ctrl) ->
2015-04-24 09:33:01 -04:00
passwordStrengthOptions = {
pattern: "aA$3",
allowEmpty: false,
allowAnyChars: false,
isMasked: true,
showToggle: false,
showGenerate: false,
checkMode:PassField.CheckModes.STRICT,
length: { min: 8, max: 50 },
showTip:false,
showWarn:false
}
passwordStrengthOptions.chars = {
digits: "1234567890",
letters: "abcdefghijklmnopqrstuvwxyz",
letters_up: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
symbols: "@#$%^&*()-_=+[]{};:<>/?!£€.,"
2015-04-24 09:33:01 -04:00
}
PassField.Config.blackList = []
2015-04-24 09:33:01 -04:00
passField = new PassField.Field("passwordFeild", passwordStrengthOptions);
[asyncFormCtrl, ngModelCtrl] = ctrl
ngModelCtrl.$parsers.unshift (modelValue) ->
2015-04-24 09:33:01 -04:00
isValid = passField.validatePass()
if !isValid
scope.complexPasswordErrorMessage = passField.getPassValidationMessage()
else
email = asyncFormCtrl.getEmail()
startOfEmail = email.split("@")?[0]
if modelValue.indexOf(email) != -1 or modelValue.indexOf(startOfEmail) != -1
isValid = false
scope.complexPasswordErrorMessage = "Password can not contain email address"
ngModelCtrl.$setValidity('complexPassword', isValid)
return modelValue