MediaWiki:Gadget-watchlist-notice-core.js
/*jslint white: true, regexp: true */ /*global jQuery, mediaWiki */ ( function ( mw, $ ) { 'use strict';
/**
* Add dismiss buttons to watchlist-message, then unhide it * Allows multiple dismiss buttons on MediaWiki:Watchlist-messages with bumpable cookie IDs (now used for LS). * Note: HTML is backwards compatible with old version, new version ignores old syntax, except for dismissed IDs. * @author: en:User:Ruud Koot * @author: en:User:MZMcBride */
var storageKey = 'hidewatchlistmessages';
function getDismissedNotices () { var hiddenNotices = mw.storage.get( storageKey ) || mw.storage.session.get( storageKey );
try { return JSON.parse( hiddenNotices ) || []; } catch (e) { return []; } }
function saveDismissedNotices ( notices ) { notices = JSON.stringify( notices ); mw.storage.set( storageKey, notices ) || mw.storage.session.set( storageKey, notices ); }
// Remove ids which are no longer defined function expungeOldNotices ( currentList ) { var dismissedNotices = getDismissedNotices(), originalLength = dismissedNotices.length; for ( var i = dismissedNotices.length - 1; i >= 0; i--) { if( currentList.indexOf( dismissedNotices[i] ) === -1 ) { dismissedNotices.splice( i, 1 ); } } if( originalLength !== dismissedNotices.length ) { saveDismissedNotices( dismissedNotices ); } }
function dismissWatchlistMessage( event ) { var $message = $( this ).closest( '.watchlist-message' ); var cid = $( this ).data( 'watchlistMessage' ).cid; var notices = getDismissedNotices();
$message.hide();
notices.push( cid ); saveDismissedNotices( notices );
event.preventDefault(); }
function addDismissButton() { var watchItems = $( 'div.watchlist-message' ); var watchItemIds = []; var dismissedNotices = getDismissedNotices(); var dismissedNoticesLength = dismissedNotices.length;
if ( watchItems.length === 0) { watchItems = $( 'li.watchlist-message' ); } if ( watchItems.length === 0) { return; } for ( var i = 0; i < watchItems.length; i++ ) { var watchlistCookieID = parseInt( watchItems[i].className.replace( /.*cookie\-ID\_(\d*).*/ig, '$1' ) ); if ( isNaN( watchlistCookieID ) ) { continue; } watchItemIds.push( watchlistCookieID ); if ( dismissedNotices.indexOf( watchlistCookieID ) !== -1 ) { watchItems[i].style.display = 'none'; continue; } var Button = document.createElement( 'span' ); var ButtonLink = document.createElement( 'a' ); var ButtonText = document.createTextNode( 'dismiss' );
ButtonLink.className = 'dismissButton'; ButtonLink.setAttribute( 'href', '#' ); ButtonLink.setAttribute( 'title', 'Hide this message' ); ButtonLink.appendChild( ButtonText ); $( ButtonLink ).data( 'watchlistMessage', { index: i, cid: watchlistCookieID } ); $( ButtonLink ).click( dismissWatchlistMessage );
Button.appendChild( document.createTextNode(' [' ) ); Button.appendChild( ButtonLink ); Button.appendChild( document.createTextNode( ']' ) ); watchItems[i].appendChild( Button ); } expungeOldNotices( watchItemIds );
$( '#watchlist-message' ).show(); }
$( addDismissButton );
}( mediaWiki, jQuery ) );