mediawiki.special.watchlist: Optimize JavaScript code
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.watchlist.js
1 /*!
2 * JavaScript for Special:Watchlist
3 */
4 ( function ( mw, $, OO ) {
5 $( function () {
6 var $progressBar, $resetForm = $( '#mw-watchlist-resetbutton' );
7
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 event.preventDefault();
13
14 OO.ui.confirm( mw.msg( 'watchlist-mark-all-visited' ) )
15 .done( function ( confirmed ) {
16 var $button;
17
18 if ( !confirmed ) {
19 return;
20 }
21
22 // Disable reset button to prevent multiple requests
23 $button = $resetForm.find( 'input[name=mw-watchlist-reset-submit]' );
24 $button.prop( 'disabled', true );
25
26 // Show progress bar
27 if ( $progressBar ) {
28 $progressBar.css( 'visibility', 'visible' );
29 } else {
30 $progressBar = new OO.ui.ProgressBarWidget( { progress: false } ).$element;
31 $progressBar.css( {
32 position: 'absolute',
33 width: '100%'
34 } );
35 $resetForm.append( $progressBar );
36 }
37
38 // Use action=setnotificationtimestamp to mark all as visited,
39 // then set all watchlist lines accordingly
40 new mw.Api().postWithToken( 'csrf', {
41 formatversion: 2,
42 action: 'setnotificationtimestamp',
43 entirewatchlist: true
44 } ).done( function () {
45 // Enable button again
46 $button.prop( 'disabled', false );
47 // Hide the button because further clicks can not generate any visual changes
48 $button.css( 'visibility', 'hidden' );
49 $progressBar.css( 'visibility', 'hidden' );
50 $( '.mw-changeslist-line-watched' )
51 .removeClass( 'mw-changeslist-line-watched' )
52 .addClass( 'mw-changeslist-line-not-watched' );
53 } ).fail( function () {
54 // On error, fall back to server-side reset
55 // First remove this submit listener and then re-submit the form
56 $resetForm.off( 'submit' ).submit();
57 } );
58 } );
59 } );
60
61 // if the user wishes to reload the watchlist whenever a filter changes
62 if ( mw.user.options.get( 'watchlistreloadautomatically' ) ) {
63 // add a listener on all form elements in the header form
64 $( '#mw-watchlist-form input, #mw-watchlist-form select' ).on( 'change', function () {
65 // submit the form when one of the input fields is modified
66 $( '#mw-watchlist-form' ).submit();
67 } );
68 }
69 } );
70
71 }( mediaWiki, jQuery, OO ) );