@@ -90,23 +90,62 @@ $(function () {
9090 const element = $ ( this ) ;
9191 const fileId = element . data ( 'id' ) ;
9292 const fileViewable = element . data ( 'viewable' ) ;
93- const fileUrl = element . data ( 'url' ) ;
93+ let fileUrl = element . data ( 'url' ) ;
9494 if ( fileUrl && fileViewable && ! loadedFiles . has ( fileId ) ) {
9595 loadedFiles . add ( fileId ) ;
96- $ . get ( fileUrl , function ( data ) {
97- const text = $ ( '<pre/>' ) . text ( data ) ;
98- text . attr ( 'data-url' , fileUrl ) ;
99- text . attr ( 'data-filename' , element . data ( 'filename' ) ) ;
100- element . find ( '.submitted-file-data' ) . html ( text ) ;
101- const extraButtons = element . addStickyButton ( 'submissionSticky' ) ;
102- text . highlightCode ( { extraButtons, compareMode : fileUrl . includes ( 'compare_to=' ) } ) ;
103- } )
104- . fail ( function ( ) {
105- element . find ( '.submitted-file-error' ) . removeClass ( 'd-none' ) ;
106- } )
107- . always ( function ( ) {
108- element . find ( '.submitted-file-progress' ) . addClass ( 'd-none' ) ;
109- } ) ;
96+
97+ const loadFileContent = ( url ) => {
98+ element . find ( '.submitted-file-progress' ) . removeClass ( 'd-none' ) ;
99+ element . find ( '.submitted-file-error' ) . addClass ( 'd-none' ) ;
100+
101+ $ . get ( url , function ( data ) {
102+ const text = $ ( '<pre/>' ) . text ( data ) ;
103+ text . attr ( 'data-url' , url ) ;
104+ text . attr ( 'data-filename' , element . data ( 'filename' ) ) ;
105+ element . find ( '.submitted-file-data' ) . html ( text ) ;
106+ const extraButtons = element . addStickyButton ( 'submissionSticky' ) ;
107+
108+ // Add ignore whitespace toggle button if comparing files
109+ if ( url . includes ( 'compare_to=' ) ) {
110+ const urlObj = new URL ( url , window . location . origin ) ;
111+ const isIgnoringWhitespace = urlObj . searchParams . get ( 'ignore_trailing_whitespace' ) === 'yes' ;
112+
113+ const whitespaceButton = {
114+ action : ( ) => {
115+ // Toggle the parameter in the current file URL
116+ const newFileUrl = new URL ( url , window . location . origin ) ;
117+ const currentIgnoring = newFileUrl . searchParams . get ( 'ignore_trailing_whitespace' ) === 'yes' ;
118+
119+ if ( currentIgnoring ) {
120+ newFileUrl . searchParams . delete ( 'ignore_trailing_whitespace' ) ;
121+ } else {
122+ newFileUrl . searchParams . set ( 'ignore_trailing_whitespace' , 'yes' ) ;
123+ }
124+
125+ // Update the stored URL and reload this specific file
126+ fileUrl = newFileUrl . toString ( ) ;
127+ element . data ( 'url' , fileUrl ) ;
128+ loadFileContent ( fileUrl ) ;
129+ } ,
130+ icon : isIgnoringWhitespace ? 'check-square' : 'square' ,
131+ text : _ ( 'Ignore trailing whitespace' ) ,
132+ toggle : true ,
133+ } ;
134+
135+ extraButtons . push ( whitespaceButton ) ;
136+ }
137+
138+ text . highlightCode ( { extraButtons, compareMode : url . includes ( 'compare_to=' ) } ) ;
139+ } )
140+ . fail ( function ( ) {
141+ element . find ( '.submitted-file-error' ) . removeClass ( 'd-none' ) ;
142+ } )
143+ . always ( function ( ) {
144+ element . find ( '.submitted-file-progress' ) . addClass ( 'd-none' ) ;
145+ } ) ;
146+ } ;
147+
148+ loadFileContent ( fileUrl ) ;
110149 }
111150 } ) ;
112151
0 commit comments