asyncForm.coffee 4.9 KB

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