You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull-view-file.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {diffTreeStore} from '../modules/stores.js';
  2. import {setFileFolding} from './file-fold.js';
  3. import {POST} from '../modules/fetch.js';
  4. const {pageData} = window.config;
  5. const prReview = pageData.prReview || {};
  6. const viewedStyleClass = 'viewed-file-checked-form';
  7. const viewedCheckboxSelector = '.viewed-file-form'; // Selector under which all "Viewed" checkbox forms can be found
  8. const expandFilesBtnSelector = '#expand-files-btn';
  9. const collapseFilesBtnSelector = '#collapse-files-btn';
  10. // Refreshes the summary of viewed files if present
  11. // The data used will be window.config.pageData.prReview.numberOf{Viewed}Files
  12. function refreshViewedFilesSummary() {
  13. const viewedFilesProgress = document.getElementById('viewed-files-summary');
  14. viewedFilesProgress?.setAttribute('value', prReview.numberOfViewedFiles);
  15. const summaryLabel = document.getElementById('viewed-files-summary-label');
  16. if (summaryLabel) summaryLabel.innerHTML = summaryLabel.getAttribute('data-text-changed-template')
  17. .replace('%[1]d', prReview.numberOfViewedFiles)
  18. .replace('%[2]d', prReview.numberOfFiles);
  19. }
  20. // Explicitly recounts how many files the user has currently reviewed by counting the number of checked "viewed" checkboxes
  21. // Additionally, the viewed files summary will be updated if it exists
  22. export function countAndUpdateViewedFiles() {
  23. // The number of files is constant, but the number of viewed files can change because files can be loaded dynamically
  24. prReview.numberOfViewedFiles = document.querySelectorAll(`${viewedCheckboxSelector} > input[type=checkbox][checked]`).length;
  25. refreshViewedFilesSummary();
  26. }
  27. // Initializes a listener for all children of the given html element
  28. // (for example 'document' in the most basic case)
  29. // to watch for changes of viewed-file checkboxes
  30. export function initViewedCheckboxListenerFor() {
  31. for (const form of document.querySelectorAll(`${viewedCheckboxSelector}:not([data-has-viewed-checkbox-listener="true"])`)) {
  32. // To prevent double addition of listeners
  33. form.setAttribute('data-has-viewed-checkbox-listener', true);
  34. // The checkbox consists of a div containing the real checkbox with its label and the CSRF token,
  35. // hence the actual checkbox first has to be found
  36. const checkbox = form.querySelector('input[type=checkbox]');
  37. checkbox.addEventListener('input', function() {
  38. // Mark the file as viewed visually - will especially change the background
  39. if (this.checked) {
  40. form.classList.add(viewedStyleClass);
  41. checkbox.setAttribute('checked', '');
  42. prReview.numberOfViewedFiles++;
  43. } else {
  44. form.classList.remove(viewedStyleClass);
  45. checkbox.removeAttribute('checked');
  46. prReview.numberOfViewedFiles--;
  47. }
  48. // Update viewed-files summary and remove "has changed" label if present
  49. refreshViewedFilesSummary();
  50. const hasChangedLabel = form.parentNode.querySelector('.changed-since-last-review');
  51. hasChangedLabel?.remove();
  52. const fileName = checkbox.getAttribute('name');
  53. // check if the file is in our difftreestore and if we find it -> change the IsViewed status
  54. const fileInPageData = diffTreeStore().files.find((x) => x.Name === fileName);
  55. if (fileInPageData) {
  56. fileInPageData.IsViewed = this.checked;
  57. }
  58. // Unfortunately, actual forms cause too many problems, hence another approach is needed
  59. const files = {};
  60. files[fileName] = this.checked;
  61. const data = {files};
  62. const headCommitSHA = form.getAttribute('data-headcommit');
  63. if (headCommitSHA) data.headCommitSHA = headCommitSHA;
  64. POST(form.getAttribute('data-link'), {data});
  65. // Fold the file accordingly
  66. const parentBox = form.closest('.diff-file-header');
  67. setFileFolding(parentBox.closest('.file-content'), parentBox.querySelector('.fold-file'), this.checked);
  68. });
  69. }
  70. }
  71. export function initExpandAndCollapseFilesButton() {
  72. // expand btn
  73. document.querySelector(expandFilesBtnSelector)?.addEventListener('click', () => {
  74. for (const box of document.querySelectorAll('.file-content[data-folded="true"]')) {
  75. setFileFolding(box, box.querySelector('.fold-file'), false);
  76. }
  77. });
  78. // collapse btn, need to exclude the div of “show more”
  79. document.querySelector(collapseFilesBtnSelector)?.addEventListener('click', () => {
  80. for (const box of document.querySelectorAll('.file-content:not([data-folded="true"])')) {
  81. if (box.getAttribute('id') === 'diff-incomplete') continue;
  82. setFileFolding(box, box.querySelector('.fold-file'), true);
  83. }
  84. });
  85. }