mediawiki-skins-Vector/resources/skins.vector.js/watchstar.js
bwang 27e821a486 Consolidate watchstar icon updating logic under watchstar.js
Depends-on: Ib11177df52d46ecda2ace50ac78672ed3d5fd5c9
Bug: T336640
Bug: T336641
Change-Id: If2573409cd1af4580f89b33c32cd0441e7a80735
2023-05-16 13:18:47 -05:00

46 lines
1.2 KiB
JavaScript

/**
* @param {HTMLElement} watchIcon
* @param {boolean} isWatched
* @param {string} expiry
*/
const updateWatchIcon = ( watchIcon, isWatched, expiry ) => {
watchIcon.classList.remove(
// Vector attaches two icon classes to the element.
// Remove the mw-ui-icon one rather than managing both.
'mw-ui-icon-star',
'mw-ui-icon-unStar',
'mw-ui-icon-wikimedia-unStar',
'mw-ui-icon-wikimedia-star',
'mw-ui-icon-wikimedia-halfStar'
);
if ( isWatched ) {
if ( expiry === 'infinity' ) {
watchIcon.classList.add( 'mw-ui-icon-wikimedia-unStar' );
} else {
watchIcon.classList.add( 'mw-ui-icon-wikimedia-halfStar' );
}
} else {
watchIcon.classList.add( 'mw-ui-icon-wikimedia-star' );
}
};
const init = () => {
mw.hook( 'wikipage.watchlistChange' ).add(
function ( /** @type {boolean} */ isWatched, /** @type {string} */ expiry ) {
const watchIcons = document.querySelectorAll( '.mw-watchlink .mw-ui-icon' );
if ( !watchIcons ) {
return;
}
Array.from( watchIcons ).forEach( ( watchIcon ) => {
updateWatchIcon( /** @type {HTMLElement} */ ( watchIcon ), isWatched, expiry );
} );
}
);
};
module.exports = {
updateWatchIcon,
init
};