edit_db.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. //
  3. // Updates database username dynamically, showing its prefix
  4. App.Actions.DB.update_db_username_hint = function(elm, hint) {
  5. if (hint.trim() == '') {
  6. $(elm).parent().find('.hint').html('');
  7. }
  8. // remove prefix from value in order to eliminate duplicates
  9. if (hint.indexOf(GLOBAL.DB_USER_PREFIX) == 0) {
  10. hint = hint.slice(GLOBAL.DB_USER_PREFIX.length, hint.length);
  11. }
  12. $(elm).parent().find('.hint').text(GLOBAL.DB_USER_PREFIX + hint);
  13. }
  14. //
  15. //
  16. // Updates database name dynamically, showing its prefix
  17. App.Actions.DB.update_db_databasename_hint = function(elm, hint) {
  18. if (hint.trim() == '') {
  19. $(elm).parent().find('.hint').html('');
  20. }
  21. // remove prefix from value in order to eliminate duplicates
  22. if (hint.indexOf(GLOBAL.DB_DBNAME_PREFIX) == 0) {
  23. hint = hint.slice(GLOBAL.DB_DBNAME_PREFIX.length, hint.length);
  24. }
  25. $(elm).parent().find('.hint').text(GLOBAL.DB_DBNAME_PREFIX + hint);
  26. }
  27. //
  28. // listener that triggers database user hint updating
  29. App.Listeners.DB.keypress_db_username = function() {
  30. var ref = $('input[name="v_dbuser"]');
  31. var current_val = ref.val();
  32. if (current_val.trim() != '') {
  33. App.Actions.DB.update_db_username_hint(ref, current_val);
  34. }
  35. ref.bind('keypress input', function(evt) {
  36. clearTimeout(window.frp_usr_tmt);
  37. window.frp_usr_tmt = setTimeout(function() {
  38. var elm = $(evt.target);
  39. App.Actions.DB.update_db_username_hint(elm, $(elm).val());
  40. }, 100);
  41. });
  42. }
  43. //
  44. // listener that triggers database name hint updating
  45. App.Listeners.DB.keypress_db_databasename = function() {
  46. var ref = $('input[name="v_database"]');
  47. var current_val = ref.val();
  48. if (current_val.trim() != '') {
  49. App.Actions.DB.update_db_databasename_hint(ref, current_val);
  50. }
  51. ref.bind('keypress input', function(evt) {
  52. clearTimeout(window.frp_dbn_tmt);
  53. window.frp_dbn_tmt = setTimeout(function() {
  54. var elm = $(evt.target);
  55. App.Actions.DB.update_db_databasename_hint(elm, $(elm).val());
  56. }, 100);
  57. });
  58. }
  59. App.Actions.DB.update_v_password = function (){
  60. var password = $('input[name="v_password"]').val();
  61. var min_small = new RegExp(/^(?=.*[a-z]).+$/);
  62. var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
  63. var min_num = new RegExp(/^(?=.*\d).+$/);
  64. var min_length = 8;
  65. var score = 0;
  66. if(password.length >= min_length) { score = score + 1; }
  67. if(min_small.test(password)) { score = score + 1;}
  68. if(min_cap.test(password)) { score = score + 1;}
  69. if(min_num.test(password)) { score = score+ 1; }
  70. $('#meter').val(score);
  71. }
  72. App.Listeners.DB.keypress_v_password = function() {
  73. var ref = $('input[name="v_password"]');
  74. ref.bind('keypress input', function(evt) {
  75. clearTimeout(window.frp_usr_tmt);
  76. window.frp_usr_tmt = setTimeout(function() {
  77. var elm = $(evt.target);
  78. App.Actions.DB.update_v_password(elm, $(elm).val());
  79. }, 100);
  80. });
  81. }
  82. App.Listeners.DB.keypress_v_password();
  83. //
  84. // Page entry point
  85. // Trigger listeners
  86. App.Listeners.DB.keypress_db_username();
  87. App.Listeners.DB.keypress_db_databasename();
  88. randomString = function(min_length = 16) {
  89. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
  90. var string_length = min_length;
  91. var randomstring = '';
  92. for (var i = 0; i < string_length; i++) {
  93. var rnum = Math.floor(Math.random() * chars.length);
  94. randomstring += chars.substr(rnum, 1);
  95. }
  96. var regex = new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\d)[a-zA-Z\d]{8,}$/);
  97. if(!regex.test(randomstring)){
  98. randomString();
  99. }else{
  100. $('input[name=v_password]').val(randomstring);
  101. App.Actions.DB.update_v_password();
  102. }
  103. }