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