asyncForm.coffee 4.4 KB

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