script.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. document.addEventListener('DOMContentLoaded', () => {
  2. log.setDefaultLevel(log.levels.TRACE, false);
  3. const demoForm = document.getElementById('LogForm');
  4. const setLevelForm = document.getElementById('SetLevel');
  5. const setDefaultLevelForm = document.getElementById('SetDefaultLevel');
  6. const resetLevelButton = document.getElementById('ResetLevelButton');
  7. const enableAllButton = document.getElementById('EnableAllButton');
  8. const disableAllButton = document.getElementById('DisableAllButton');
  9. if (demoForm) {
  10. demoForm.addEventListener('submit', onSubmitDemoForm);
  11. }
  12. if (setLevelForm) {
  13. setLevelForm.addEventListener('submit', onSubmitSetLevelForm);
  14. }
  15. if (setDefaultLevelForm) {
  16. setDefaultLevelForm.addEventListener('submit', onSubmitSetDefaultLevelForm);
  17. }
  18. if (resetLevelButton) {
  19. resetLevelButton.addEventListener('click', () => {
  20. log.resetLevel();
  21. updateLogStateForm();
  22. });
  23. }
  24. if (enableAllButton) {
  25. enableAllButton.addEventListener('click', () => {
  26. log.enableAll();
  27. updateLogStateForm();
  28. });
  29. }
  30. if (disableAllButton) {
  31. disableAllButton.addEventListener('click', () => {
  32. log.disableAll();
  33. updateLogStateForm();
  34. });
  35. }
  36. updateLogStateForm();
  37. });
  38. function onSubmitDemoForm(event) {
  39. event.preventDefault();
  40. const form = event.currentTarget;
  41. const formData = new FormData(form)
  42. const debugMessage = formData.get('debugMessage');
  43. const logLevel = formData.get('logLevel');
  44. if (debugMessage && logLevel) {
  45. log[logLevel](debugMessage);
  46. }
  47. }
  48. function onSubmitSetLevelForm(event) {
  49. event.preventDefault();
  50. const form = event.currentTarget;
  51. const formData = new FormData(form)
  52. log.setLevel(parseInt(formData.get('level')), formData.get('persist') === 'true');
  53. updateLogStateForm();
  54. }
  55. function onSubmitSetDefaultLevelForm(event) {
  56. event.preventDefault();
  57. const form = event.currentTarget;
  58. const formData = new FormData(form)
  59. log.setDefaultLevel(parseInt(formData.get('level')));
  60. updateLogStateForm();
  61. }
  62. function updateLogStateForm() {
  63. const logState = document.getElementById('LogState');
  64. if (logState) {
  65. const currentLevel = logState.querySelector('input[name="currentLevel"]');
  66. const logLevel = log.getLevel();
  67. currentLevel.value = Object.keys(log.levels).find(key => log.levels[key] === logLevel);
  68. }
  69. }