zxcvbn.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* global zxcvbn */
  2. import Ember from 'ember';
  3. export default Ember.Service.extend({
  4. includedJs: false,
  5. loadedJs: false,
  6. scorePassword: function(password, inputs) {
  7. // 0-4 score, the more the stronger password
  8. return zxcvbn(password, inputs).score;
  9. },
  10. loadLibrary: function() {
  11. if (!this.get('includedJs')) {
  12. this._includeJs();
  13. }
  14. if (!this.get('loadedJs')) {
  15. return this._loadingPromise();
  16. } else {
  17. return this._loadedPromise();
  18. }
  19. },
  20. _includeJs: function() {
  21. Ember.$.getScript(this.get('_includedJsPath'));
  22. this.set('includedJs', true);
  23. },
  24. _includedJsPath: function() {
  25. return this.get('staticUrl') + 'misago/js/zxcvbn.js';
  26. }.property(),
  27. _loadingPromise: function() {
  28. var self = this;
  29. return new Ember.RSVP.Promise(function(resolve) {
  30. var wait = function() {
  31. if (typeof zxcvbn === "undefined") {
  32. Ember.run.later(function () {
  33. wait();
  34. }, 200);
  35. } else {
  36. self.set('loadedJs', true);
  37. resolve();
  38. }
  39. };
  40. wait();
  41. });
  42. },
  43. _loadedPromise: function() {
  44. // we have already loaded zxcvbn.js, resolve away!
  45. return new Ember.RSVP.Promise(function(resolve) {
  46. resolve();
  47. });
  48. }
  49. });