2024-07-07 18:32:30 +03:00
import { diffTreeStore } from '../modules/stores.ts' ;
import { setFileFolding } from './file-fold.ts' ;
import { POST } from '../modules/fetch.ts' ;
2022-05-07 21:28:10 +03:00
2023-09-19 03:50:30 +03:00
const { pageData } = window . config ;
2022-05-07 21:28:10 +03:00
const prReview = pageData . prReview || { } ;
const viewedStyleClass = 'viewed-file-checked-form' ;
const viewedCheckboxSelector = '.viewed-file-form' ; // Selector under which all "Viewed" checkbox forms can be found
2023-04-09 16:11:02 +03:00
const expandFilesBtnSelector = '#expand-files-btn' ;
const collapseFilesBtnSelector = '#collapse-files-btn' ;
2022-05-07 21:28:10 +03:00
// Refreshes the summary of viewed files if present
// The data used will be window.config.pageData.prReview.numberOf{Viewed}Files
function refreshViewedFilesSummary() {
2024-06-10 23:49:33 +03:00
const viewedFilesProgress = document . querySelector ( '#viewed-files-summary' ) ;
2022-06-15 07:28:24 +03:00
viewedFilesProgress ? . setAttribute ( 'value' , prReview . numberOfViewedFiles ) ;
2024-06-10 23:49:33 +03:00
const summaryLabel = document . querySelector ( '#viewed-files-summary-label' ) ;
2022-05-07 21:28:10 +03:00
if ( summaryLabel ) summaryLabel . innerHTML = summaryLabel . getAttribute ( 'data-text-changed-template' )
. replace ( '%[1]d' , prReview . numberOfViewedFiles )
. replace ( '%[2]d' , prReview . numberOfFiles ) ;
}
// Explicitly recounts how many files the user has currently reviewed by counting the number of checked "viewed" checkboxes
// Additionally, the viewed files summary will be updated if it exists
export function countAndUpdateViewedFiles() {
// The number of files is constant, but the number of viewed files can change because files can be loaded dynamically
prReview . numberOfViewedFiles = document . querySelectorAll ( ` ${ viewedCheckboxSelector } > input[type=checkbox][checked] ` ) . length ;
refreshViewedFilesSummary ( ) ;
}
// Initializes a listener for all children of the given html element
// (for example 'document' in the most basic case)
// to watch for changes of viewed-file checkboxes
export function initViewedCheckboxListenerFor() {
for ( const form of document . querySelectorAll ( ` ${ viewedCheckboxSelector } :not([data-has-viewed-checkbox-listener="true"]) ` ) ) {
// To prevent double addition of listeners
form . setAttribute ( 'data-has-viewed-checkbox-listener' , true ) ;
// The checkbox consists of a div containing the real checkbox with its label and the CSRF token,
// hence the actual checkbox first has to be found
const checkbox = form . querySelector ( 'input[type=checkbox]' ) ;
2023-05-21 23:47:41 +03:00
checkbox . addEventListener ( 'input' , function ( ) {
2022-05-07 21:28:10 +03:00
// Mark the file as viewed visually - will especially change the background
if ( this . checked ) {
form . classList . add ( viewedStyleClass ) ;
2024-02-28 23:23:49 +03:00
checkbox . setAttribute ( 'checked' , '' ) ;
2022-05-07 21:28:10 +03:00
prReview . numberOfViewedFiles ++ ;
} else {
form . classList . remove ( viewedStyleClass ) ;
2024-02-28 23:23:49 +03:00
checkbox . removeAttribute ( 'checked' ) ;
2022-05-07 21:28:10 +03:00
prReview . numberOfViewedFiles -- ;
}
// Update viewed-files summary and remove "has changed" label if present
refreshViewedFilesSummary ( ) ;
const hasChangedLabel = form . parentNode . querySelector ( '.changed-since-last-review' ) ;
2023-05-09 05:35:49 +03:00
hasChangedLabel ? . remove ( ) ;
2022-05-07 21:28:10 +03:00
2023-06-25 03:46:30 +03:00
const fileName = checkbox . getAttribute ( 'name' ) ;
// check if the file is in our difftreestore and if we find it -> change the IsViewed status
const fileInPageData = diffTreeStore ( ) . files . find ( ( x ) = > x . Name === fileName ) ;
if ( fileInPageData ) {
fileInPageData . IsViewed = this . checked ;
}
2022-05-07 21:28:10 +03:00
// Unfortunately, actual forms cause too many problems, hence another approach is needed
const files = { } ;
2023-06-25 03:46:30 +03:00
files [ fileName ] = this . checked ;
2022-05-07 21:28:10 +03:00
const data = { files } ;
const headCommitSHA = form . getAttribute ( 'data-headcommit' ) ;
if ( headCommitSHA ) data . headCommitSHA = headCommitSHA ;
2023-09-19 03:50:30 +03:00
POST ( form . getAttribute ( 'data-link' ) , { data } ) ;
2022-05-07 21:28:10 +03:00
// Fold the file accordingly
const parentBox = form . closest ( '.diff-file-header' ) ;
setFileFolding ( parentBox . closest ( '.file-content' ) , parentBox . querySelector ( '.fold-file' ) , this . checked ) ;
} ) ;
}
}
2023-04-09 16:11:02 +03:00
export function initExpandAndCollapseFilesButton() {
// expand btn
document . querySelector ( expandFilesBtnSelector ) ? . addEventListener ( 'click' , ( ) = > {
for ( const box of document . querySelectorAll ( '.file-content[data-folded="true"]' ) ) {
setFileFolding ( box , box . querySelector ( '.fold-file' ) , false ) ;
}
} ) ;
// collapse btn, need to exclude the div of “show more”
document . querySelector ( collapseFilesBtnSelector ) ? . addEventListener ( 'click' , ( ) = > {
for ( const box of document . querySelectorAll ( '.file-content:not([data-folded="true"])' ) ) {
if ( box . getAttribute ( 'id' ) === 'diff-incomplete' ) continue ;
setFileFolding ( box , box . querySelector ( '.fold-file' ) , true ) ;
}
} ) ;
}