MediaWiki:Gadget-watchlist-notice-core.js
Appearance
From Wikipedia, the free encyclopedia
This page is loaded as a part of the watchlist-notice-core gadget, a hidden gadget.
/** * 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]] */ /*jslint white: true, regexp: true */ /*global jQuery, mediaWiki */ (function(mw,$){ 'use strict'; varstorageKey='hidewatchlistmessages'; functiongetDismissedNotices(){ varhiddenNotices=mw.storage.get(storageKey) ||mw.storage.session.get(storageKey); try{ returnJSON.parse(hiddenNotices)||[]; }catch(e){ return[]; } } functionsaveDismissedNotices(notices){ notices=JSON.stringify(notices); mw.storage.set(storageKey,notices) ||mw.storage.session.set(storageKey,notices); } // Remove ids which are no longer defined functionexpungeOldNotices(currentList){ vardismissedNotices=getDismissedNotices(), originalLength=dismissedNotices.length; for(vari=dismissedNotices.length-1;i>=0;i--){ if(currentList.indexOf(dismissedNotices[i])===-1){ dismissedNotices.splice(i,1); } } if(originalLength!==dismissedNotices.length){ saveDismissedNotices(dismissedNotices); } } functiondismissWatchlistMessage(event){ var$message=$(this).closest('.watchlist-message'); varcid=$(this).data('watchlistMessage').cid; varnotices=getDismissedNotices(); $message.hide(); notices.push(cid); saveDismissedNotices(notices); event.preventDefault(); } functionaddDismissButton(){ varwatchItems=$('div.watchlist-message'); varwatchItemIds=[]; vardismissedNotices=getDismissedNotices(); vardismissedNoticesLength=dismissedNotices.length; if(watchItems.length===0){ watchItems=$('li.watchlist-message'); } if(watchItems.length===0){ return; } for(vari=0;i<watchItems.length;i++){ varwatchlistCookieID=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; } varButton=document.createElement('span'); varButtonLink=document.createElement('a'); varButtonText=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));