| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
$( () => { |
| 9 |
|
| 10 |
const config = require( './config.json' ), |
| 11 |
$tempUserBannerEl = $( '.mw-temp-user-banner ' ), |
| 12 |
$tempUserBannerTooltipEl = $( '.mw-temp-user-banner-tooltip ' ), |
| 13 |
$tempUserBannerTooltipButtonEl = $( '#mw-temp-user-banner-tooltip-button' ), |
| 14 |
TTL_DAY_MS = 86400000; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
function getTemporaryAccountDurationMs() { |
| 23 |
return Date.now() - mw.user.getFirstRegistration().getTime(); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
function isWithinExpirationNotificationPeriod() { |
| 34 |
const expirationDurationMs = TTL_DAY_MS * config.AutoCreateTempUser.expireAfterDays; |
| 35 |
const notificationDurationMs = TTL_DAY_MS * config.AutoCreateTempUser.notifyBeforeExpirationDays; |
| 36 |
return getTemporaryAccountDurationMs() > ( expirationDurationMs - notificationDurationMs ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
function getTimeToExpirationMs() { |
| 46 |
return ( TTL_DAY_MS * config.AutoCreateTempUser.expireAfterDays ) - getTemporaryAccountDurationMs(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
function shouldShowExpirationAlert() { |
| 62 |
const tempUserExpirationAlertDismissed = localStorage.getItem( 'tempUserExpirationAlertDismissed' ); |
| 63 |
const expirationIsSet = typeof config.AutoCreateTempUser.expireAfterDays === 'number'; |
| 64 |
const notifyBeforeExpirationIsSet = typeof config.AutoCreateTempUser.notifyBeforeExpirationDays === 'number'; |
| 65 |
return expirationIsSet && |
| 66 |
notifyBeforeExpirationIsSet && |
| 67 |
!tempUserExpirationAlertDismissed && |
| 68 |
isWithinExpirationNotificationPeriod(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
function getExpirationDescriptionText() { |
| 78 |
const timeToExpirationDays = Math.floor( getTimeToExpirationMs() / TTL_DAY_MS ); |
| 79 |
const params = [ timeToExpirationDays ]; |
| 80 |
let key = 'temp-user-banner-tooltip-description-expiration-soon'; |
| 81 |
if ( timeToExpirationDays < 1 ) { |
| 82 |
key += '-day'; |
| 83 |
params.pop(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
return mw.message( key, params ).parseDom(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
function getTooltipContent( shouldShowExpiration ) { |
| 99 |
const descriptionText = shouldShowExpiration ? |
| 100 |
getExpirationDescriptionText() : |
| 101 |
mw.message( 'temp-user-banner-tooltip-description-learn-more' ).parseDom(); |
| 102 |
return $( '<div>' ).append( |
| 103 |
$( '<p>' ).append( descriptionText ), |
| 104 |
$( '<p>' ).append( mw.message( 'temp-user-banner-tooltip-description-login' ).parseDom() ) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
function initTempUserBannerTooltip( $bannerEl, $tooltipEl, $buttonEl ) { |
| 117 |
if ( !$bannerEl.length || !$tooltipEl.length || !$buttonEl.length ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
let popup; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
function showTooltip() { |
| 129 |
return mw.loader.using( [ |
| 130 |
'codex-styles', |
| 131 |
'oojs-ui-core', |
| 132 |
'oojs-ui-widgets', |
| 133 |
'oojs-ui.styles.icons-interactions' |
| 134 |
] ).then( () => { |
| 135 |
const shouldShowExpiration = shouldShowExpirationAlert(); |
| 136 |
if ( !popup ) { |
| 137 |
popup = new OO.ui.PopupWidget( { |
| 138 |
icon: 'clock', |
| 139 |
padded: true, |
| 140 |
head: true, |
| 141 |
label: mw.msg( 'temp-user-banner-tooltip-title' ), |
| 142 |
$content: getTooltipContent( shouldShowExpiration ), |
| 143 |
autoClose: !shouldShowExpiration, |
| 144 |
$autoCloseIgnore: $buttonEl, |
| 145 |
$floatableContainer: $tooltipEl, |
| 146 |
position: 'below' |
| 147 |
} ); |
| 148 |
$tooltipEl.append( popup.$element ); |
| 149 |
|
| 150 |
if ( shouldShowExpiration ) { |
| 151 |
popup.on( 'toggle', ( visible ) => { |
| 152 |
if ( !visible ) { |
| 153 |
localStorage.setItem( 'tempUserExpirationAlertDismissed', true ); |
| 154 |
popup = null; |
| 155 |
} |
| 156 |
} ); |
| 157 |
} |
| 158 |
} |
| 159 |
popup.toggle(); |
| 160 |
} ); |
| 161 |
} |
| 162 |
|
| 163 |
$buttonEl.on( 'click', showTooltip ); |
| 164 |
|
| 165 |
if ( shouldShowExpirationAlert() ) { |
| 166 |
showTooltip(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
initTempUserBannerTooltip( $tempUserBannerEl, $tempUserBannerTooltipEl, $tempUserBannerTooltipButtonEl ); |
| 171 |
|
| 172 |
} ); |