User:NQ/WatchlistResetConfirm.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*Adds a confirmation dialogue to the Watchlist Reset button
https://en.wikipedia.org/w/index.php?title=Wikipedia:Village_pump_(technical)&oldid=746997333#Watchlist:_.22Are_you_sure_you_want_to_mark_all_pages_as_visited.3F.22

* Licensed under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. See the
* GNU General Public License for more details.
* A copy of the GPL is available at https://www.gnu.org/licenses/gpl-2.0.txt 
 
 /* ############################################################################
$(function() {
    $watchlistReset = $('#mw-watchlist-resetbutton');
    $watchlistReset.css('display', 'inline'); //already hidden in css
    $watchlistReset.on('click', function(event) {
        if (!confirm('Are you sure you want to mark all pages as visited?' )) {
            event.preventDefault();
        }
    });
}); ############################################################################ */

/* Using [[:mw:OOjs_UI/Windows/Message_Dialogs]] */

mw.loader.using(['mediawiki.api', 'oojs-ui'], function() {

  $watchlistReset = $('#mw-watchlist-resetbutton');
  $watchlistReset.css('display', 'inline'); //if already hidden in css; recommended

  var messageDialog = new OO.ui.MessageDialog();
  var windowManager = new OO.ui.WindowManager();
  $('body').append(windowManager.$element);
  windowManager.addWindows([messageDialog]);

  $watchlistReset.submit(function(event) {
    event.preventDefault();

    windowManager.openWindow(messageDialog, {
      title: 'Confirm',
      message: 'Mark all pages as visited?',
      actions: [{ action: 'reject', label: 'Cancel', flags: ['safe', 'destructive'] },
               { action: 'reset', label: 'Confirm', flags: ['primary', 'progressive']}]
    }).then(function(opened) {
      opened.then(function(closing, data) {
        if (data && data.action === 'reset') {
        	
/* Uses [[:mw:API:SetNotificationTimestamp]] to reset the watchlist without refreshing the page */
             
          // Code adapted from [[User:HueSatLum/common.js]]
          new mw.Api().post({
            action: 'setnotificationtimestamp',
            entirewatchlist: '',
            token: mw.user.tokens.get('editToken')
          }).done(function() {
            $('.mw-changeslist-line-watched').removeClass('mw-changeslist-line-watched').addClass('mw-changeslist-line-not-watched');
          });
          
        } 
      });
    });
  });
});