buttons.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { createToast } from './bs';
  2. import { isTruthy, getElements, apiPatch, hasError, slugify } from './util';
  3. /**
  4. * Add onClick callback for toggling rack elevation images.
  5. */
  6. function initRackElevation() {
  7. for (const button of getElements('button.toggle-images')) {
  8. /**
  9. * Toggle the visibility of device images and update the toggle button style.
  10. */
  11. function handleClick(event: Event) {
  12. const target = event.target as HTMLButtonElement;
  13. const selected = target.getAttribute('selected');
  14. if (isTruthy(selected)) {
  15. target.innerHTML = `<i class="bi bi-file-image"></i> Show Images`;
  16. for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) {
  17. const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? [];
  18. for (const image of images) {
  19. if (!image.classList.contains('hidden')) {
  20. image && image.classList.add('hidden');
  21. }
  22. }
  23. }
  24. target.setAttribute('selected', '');
  25. } else {
  26. target.innerHTML = `<i class="bi bi-file-image"></i> Hide Images`;
  27. for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) {
  28. const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? [];
  29. for (const image of images) {
  30. image && image.classList.remove('hidden');
  31. }
  32. }
  33. target.setAttribute('selected', 'selected');
  34. }
  35. }
  36. button.addEventListener('click', handleClick);
  37. }
  38. }
  39. /**
  40. * When the toggle button is clicked, swap the connection status via the API and toggle CSS
  41. * classes to reflect the connection status.
  42. *
  43. * @param element Connection Toggle Button Element
  44. */
  45. function toggleConnection(element: HTMLButtonElement) {
  46. const id = element.getAttribute('data');
  47. const connected = element.classList.contains('connected');
  48. const status = connected ? 'planned' : 'connected';
  49. if (isTruthy(id)) {
  50. apiPatch(`/api/dcim/cables/${id}/`, { status }).then(res => {
  51. if (hasError(res)) {
  52. // If the API responds with an error, show it to the user.
  53. createToast('danger', 'Error', res.error).show();
  54. return;
  55. } else {
  56. // Get the button's row to change its styles.
  57. const row = element.parentElement?.parentElement as HTMLTableRowElement;
  58. // Get the button's icon to change its CSS class.
  59. const icon = element.querySelector('i.mdi, span.mdi') as HTMLSpanElement;
  60. if (connected) {
  61. row.classList.remove('success');
  62. row.classList.add('info');
  63. element.classList.remove('connected', 'btn-warning');
  64. element.title = 'Mark Installed';
  65. icon.classList.remove('mdi-lan-disconnect');
  66. icon.classList.add('mdi-lan-connect');
  67. } else {
  68. row.classList.remove('info');
  69. row.classList.add('success');
  70. element.classList.remove('btn-success');
  71. element.classList.add('connected', 'btn-warning');
  72. element.title = 'Mark Installed';
  73. icon.classList.remove('mdi-lan-connect');
  74. icon.classList.add('mdi-lan-disconnect');
  75. }
  76. }
  77. });
  78. }
  79. }
  80. function initConnectionToggle() {
  81. for (const element of getElements<HTMLButtonElement>('button.cable-toggle')) {
  82. element.addEventListener('click', () => toggleConnection(element));
  83. }
  84. }
  85. /**
  86. * If a slug field exists, add event listeners to handle automatically generating its value.
  87. */
  88. function initReslug(): void {
  89. const slugField = document.getElementById('id_slug') as HTMLInputElement;
  90. const slugButton = document.getElementById('reslug') as HTMLButtonElement;
  91. if (slugField === null || slugButton === null) {
  92. return;
  93. }
  94. const sourceId = slugField.getAttribute('slug-source');
  95. const sourceField = document.getElementById(`id_${sourceId}`) as HTMLInputElement;
  96. if (sourceField === null) {
  97. console.error('Unable to find field for slug field.');
  98. return;
  99. }
  100. const slugLengthAttr = slugField.getAttribute('maxlength');
  101. let slugLength = 50;
  102. if (slugLengthAttr) {
  103. slugLength = Number(slugLengthAttr);
  104. }
  105. sourceField.addEventListener('blur', () => {
  106. slugField.value = slugify(sourceField.value, slugLength);
  107. });
  108. slugButton.addEventListener('click', () => {
  109. slugField.value = slugify(sourceField.value, slugLength);
  110. });
  111. }
  112. export function initButtons() {
  113. for (const func of [initRackElevation, initConnectionToggle, initReslug]) {
  114. func();
  115. }
  116. }