utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. *
  3. * Created by James on 15/11/29.
  4. */
  5. function insert_warning_window(element, content) {
  6. // 展出警告框
  7. element.before(
  8. '<div class="alert alert-warning alert-dismissible fade in" role="alert">' +
  9. '<button id="warning_window_close_btn" type="button" class="close" data-dismiss="alert" aria-label="Close">' +
  10. '<span aria-hidden="true">&times;</span>' +
  11. '</button>' +
  12. content +
  13. '</div>');
  14. // 3秒后自动关闭
  15. setTimeout(function() {
  16. $('#warning_window_close_btn').click();
  17. }, 3000);
  18. }
  19. $.fn.hasAttr = function(name) {
  20. return this.attr(name) !== undefined;
  21. };
  22. // Public: Constructor
  23. function RandomPassword() {
  24. this.chrLower="abcdefghjkmnpqrst";
  25. this.chrUpper="ABCDEFGHJKMNPQRST";
  26. this.chrNumbers="23456789";
  27. this.chrSymbols="!#%&?+*_.,:;";
  28. this.maxLength=255;
  29. this.minLength=8;
  30. }
  31. /*
  32. Public: Create password
  33. length (optional): Password length. If value is not within minLength/maxLength (defined in constructor), length will be adjusted automatically.
  34. characters (optional): The characters the password will be composed of. Must contain at least one of the following:
  35. this.chrLower
  36. this.chrUpper
  37. this.chrNumbers
  38. this.chrSymbols
  39. Use + to combine. You can add your own sets of characters. If not at least one of the constructor defined sets of characters is found, default set of characters will be used.
  40. */
  41. RandomPassword.prototype.create = function(length, characters) {
  42. var _length=this.adjustLengthWithinLimits(length);
  43. var _characters=this.secureCharacterCombination(characters);
  44. return this.shufflePassword(this.assemblePassword(_characters, _length));
  45. };
  46. // Private: Adjusts password length to be within limits.
  47. RandomPassword.prototype.adjustLengthWithinLimits = function(length) {
  48. if(!length || length<this.minLength)
  49. return this.minLength;
  50. else if(length>this.maxLength)
  51. return this.maxLength;
  52. else
  53. return length;
  54. };
  55. // Private: Make sure characters password is build of contains meaningful set of characters.
  56. RandomPassword.prototype.secureCharacterCombination = function(characters) {
  57. var defaultCharacters=this.chrLower+this.chrUpper+this.chrNumbers;
  58. if(!characters || this.trim(characters)=="")
  59. return defaultCharacters;
  60. else if(!this.containsAtLeast(characters, [this.chrLower, this.chrUpper, this.chrNumbers, this.chrSymbols]))
  61. return defaultCharacters;
  62. else
  63. return characters;
  64. };
  65. // Private: Assemble password using a string of characters the password will consist of.
  66. RandomPassword.prototype.assemblePassword = function(characters, length) {
  67. var randMax=this.chrNumbers.length;
  68. var randMin=randMax-4;
  69. var index=this.random(0, characters.length-1);
  70. var password="";
  71. for(var i=0; i<length; i++) {
  72. var jump=this.random(randMin, randMax);
  73. index=((index+jump)>(characters.length-1)?this.random(0, characters.length-1):index+jump);
  74. password+=characters[index];
  75. }
  76. return password;
  77. };
  78. // Private: Shuffle password.
  79. RandomPassword.prototype.shufflePassword = function(password) {
  80. return password.split('').sort(function(){return 0.5-Math.random()}).join('');
  81. };
  82. // Private: Checks if string contains at least one string in an array
  83. RandomPassword.prototype.containsAtLeast = function(string, strings) {
  84. for(var i=0; i<strings.length; i++) {
  85. if(string.indexOf(strings[i])!=-1)
  86. return true;
  87. }
  88. return false;
  89. };
  90. // Private: Returns a random number between min and max.
  91. RandomPassword.prototype.random = function(min, max) {
  92. return Math.floor((Math.random() * max) + min);
  93. };
  94. // Private: Trims a string (required for compatibility with IE9 or older)
  95. RandomPassword.prototype.trim = function(s) {
  96. if(typeof String.prototype.trim !== 'function')
  97. return s.replace(/^\s+|\s+$/g, '');
  98. else
  99. return s.trim();
  100. };