asyncForm.coffee 4.8 KB

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