utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. };
  101. function render_bandwidth_slider(bandwidth_slider ,action, bandwidth_unit) {
  102. var values_with_gbps = [
  103. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  104. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  105. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  106. 31, 32, 33, 34, 35
  107. ];
  108. var values_p_with_gbps = [
  109. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  110. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  111. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  112. 31, 32, 33, 34, '∞'];
  113. if (action === 'create') {
  114. bandwidth_slider.ionRangeSlider({
  115. min: 0,
  116. max: 1000,
  117. from: 200,
  118. from_min: 1,
  119. from_max: 999,
  120. step: 1,
  121. grid: true,
  122. grid_num: 10,
  123. postfix: ' Mbps'
  124. });
  125. } else if (action === 'update') {
  126. var bandwitdh_slider_instance = bandwidth_slider.data("ionRangeSlider");
  127. if (bandwidth_unit === 'g') {
  128. bandwitdh_slider_instance.update({
  129. min: 0,
  130. max: 35,
  131. from: 1,
  132. from_min: 1,
  133. from_max: 35,
  134. step: 1,
  135. grid: true,
  136. grid_num: 7,
  137. postfix: ' Gbps'
  138. // values: values_with_gbps,
  139. // prettify: function (n) {
  140. // var ind = values_with_gbps.indexOf(n);
  141. // return values_p_with_gbps[ind];
  142. // }
  143. });
  144. } else if (bandwidth_unit === 'm') {
  145. bandwitdh_slider_instance.update({
  146. min: 0,
  147. max: 1000,
  148. from: 200,
  149. from_min: 1,
  150. from_max: 999,
  151. step: 1,
  152. grid: true,
  153. grid_num: 10,
  154. postfix: ' Mbps'
  155. });
  156. } else if (bandwidth_unit === 'k') {
  157. bandwitdh_slider_instance.update({
  158. min: 0,
  159. max: 1000,
  160. from: 512,
  161. from_min: 8,
  162. from_max: 992,
  163. step: 8,
  164. grid: true,
  165. grid_num: 10,
  166. postfix: ' Kbps'
  167. });
  168. }
  169. }
  170. }