asyncForm.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define [
  2. "base"
  3. "libs/passfield"
  4. ], (App) ->
  5. App.directive "asyncForm", ($http) ->
  6. return {
  7. controller: ['$scope', ($scope) ->
  8. @getEmail = () ->
  9. return $scope.email
  10. return this
  11. ]
  12. link: (scope, element, attrs) ->
  13. formName = attrs.asyncForm
  14. scope[attrs.name].response = response = {}
  15. scope[attrs.name].inflight = false
  16. element.on "submit", (e) ->
  17. e.preventDefault()
  18. formData = {}
  19. for data in element.serializeArray()
  20. formData[data.name] = data.value
  21. scope[attrs.name].inflight = true
  22. # for asyncForm prevent automatic redirect to /login if
  23. # authentication fails, we will handle it ourselves
  24. $http
  25. .post(element.attr('action'), formData, {disableAutoLoginRedirect: true})
  26. .success (data, status, headers, config) ->
  27. scope[attrs.name].inflight = false
  28. response.success = true
  29. response.error = false
  30. if data.redir?
  31. ga('send', 'event', formName, 'success')
  32. window.location = data.redir
  33. else if data.message?
  34. response.message = data.message
  35. if data.message.type == "error"
  36. response.success = false
  37. response.error = true
  38. ga('send', 'event', formName, 'failure', data.message)
  39. else
  40. ga('send', 'event', formName, 'success')
  41. .error (data, status, headers, config) ->
  42. scope[attrs.name].inflight = false
  43. response.success = false
  44. response.error = true
  45. if status == 403 # Forbidden
  46. response.message =
  47. text: "Session error. Please check you have cookies enabled. If the problem persists, try clearing your cache and cookies."
  48. type: "error"
  49. else
  50. response.message =
  51. text: data.message?.text or data.message or "Something went wrong talking to the server :(. Please try again."
  52. type: 'error'
  53. ga('send', 'event', formName, 'failure', data.message)
  54. }
  55. App.directive "formMessages", () ->
  56. return {
  57. restrict: "E"
  58. template: """
  59. <div class="alert" ng-class="{
  60. 'alert-danger': form.response.message.type == 'error',
  61. 'alert-success': form.response.message.type != 'error'
  62. }" ng-show="!!form.response.message">
  63. {{form.response.message.text}}
  64. </div>
  65. <div ng-transclude></div>
  66. """
  67. transclude: true
  68. scope: {
  69. form: "=for"
  70. }
  71. }
  72. App.directive 'complexPassword', ->
  73. require: ['^asyncForm', 'ngModel']
  74. link: (scope, element, attrs, ctrl) ->
  75. PassField.Config.blackList = []
  76. defaultPasswordOpts =
  77. pattern: ""
  78. length:
  79. min: 1
  80. max: 50
  81. allowEmpty: false
  82. allowAnyChars: false
  83. isMasked: true
  84. showToggle: false
  85. showGenerate: false
  86. showTip:false
  87. showWarn:false
  88. checkMode : PassField.CheckModes.STRICT
  89. chars:
  90. digits: "1234567890"
  91. letters: "abcdefghijklmnopqrstuvwxyz"
  92. letters_up: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  93. symbols: "@#$%^&*()-_=+[]{};:<>/?!£€.,"
  94. opts = _.defaults(window.passwordStrengthOptions || {}, defaultPasswordOpts)
  95. if opts.length.min == 1
  96. opts.acceptRate = 0 #this allows basically anything to be a valid password
  97. passField = new PassField.Field("passwordField", opts);
  98. [asyncFormCtrl, ngModelCtrl] = ctrl
  99. ngModelCtrl.$parsers.unshift (modelValue) ->
  100. isValid = passField.validatePass()
  101. email = asyncFormCtrl.getEmail() || window.usersEmail
  102. if !isValid
  103. scope.complexPasswordErrorMessage = passField.getPassValidationMessage()
  104. else if (email? and email != "")
  105. startOfEmail = email?.split("@")?[0]
  106. if modelValue.indexOf(email) != -1 or modelValue.indexOf(startOfEmail) != -1
  107. isValid = false
  108. scope.complexPasswordErrorMessage = "Password can not contain email address"
  109. if opts.length.max? and modelValue.length == opts.length.max
  110. isValid = false
  111. scope.complexPasswordErrorMessage = "Maximum password length #{opts.length.max} reached"
  112. ngModelCtrl.$setValidity('complexPassword', isValid)
  113. return modelValue