asyncForm.coffee 3.6 KB

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