Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.special / mediawiki.special.watchlist.js
blob7cc9b9bb5ad3240f368f930fcf3c03fea3e6885e
1 /*!
2  * JavaScript for Special:Watchlist
3  */
4 ( function ( mw, $, OO ) {
5         $( function () {
6                 var $progressBar, $resetForm = $( '#mw-watchlist-resetbutton' );
8                 // If the user wants to reset their watchlist, use an API call to do so (no reload required)
9                 // Adapted from a user script by User:NQ of English Wikipedia
10                 // (User:NQ/WatchlistResetConfirm.js)
11                 $resetForm.submit( function ( event ) {
12                         var $button = $resetForm.find( 'input[name=mw-watchlist-reset-submit]' );
14                         event.preventDefault();
16                         // Disable reset button to prevent multiple concurrent requests
17                         $button.prop( 'disabled', true );
19                         if ( !$progressBar ) {
20                                 $progressBar = new OO.ui.ProgressBarWidget( { progress: false } ).$element;
21                                 $progressBar.css( {
22                                         position: 'absolute',
23                                         width: '100%'
24                                 } );
25                         }
26                         // Show progress bar
27                         $resetForm.append( $progressBar );
29                         // Use action=setnotificationtimestamp to mark all as visited,
30                         // then set all watchlist lines accordingly
31                         new mw.Api().postWithToken( 'csrf', {
32                                 formatversion: 2,
33                                 action: 'setnotificationtimestamp',
34                                 entirewatchlist: true
35                         } ).done( function () {
36                                 // Enable button again
37                                 $button.prop( 'disabled', false );
38                                 // Hide the button because further clicks can not generate any visual changes
39                                 $button.css( 'visibility', 'hidden' );
40                                 $progressBar.detach();
41                                 $( '.mw-changeslist-line-watched' )
42                                         .removeClass( 'mw-changeslist-line-watched' )
43                                         .addClass( 'mw-changeslist-line-not-watched' );
44                         } ).fail( function () {
45                                 // On error, fall back to server-side reset
46                                 // First remove this submit listener and then re-submit the form
47                                 $resetForm.off( 'submit' ).submit();
48                         } );
49                 } );
51                 // if the user wishes to reload the watchlist whenever a filter changes
52                 if ( mw.user.options.get( 'watchlistreloadautomatically' ) ) {
53                         // add a listener on all form elements in the header form
54                         $( '#mw-watchlist-form input, #mw-watchlist-form select' ).on( 'change', function () {
55                                 // submit the form when one of the input fields is modified
56                                 $( '#mw-watchlist-form' ).submit();
57                         } );
58                 }
59         } );
61 }( mediaWiki, jQuery, OO ) );