| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
mw.echo.ui.NotificationBadgeWidget = function MwEchoUiNotificationBadgeButtonPopupWidget( controller, manager, links, config ) { |
| 28 |
config = config || {}; |
| 29 |
|
| 30 |
|
| 31 |
mw.echo.ui.NotificationBadgeWidget.super.call( this, config ); |
| 32 |
|
| 33 |
|
| 34 |
OO.ui.mixin.PendingElement.call( this, config ); |
| 35 |
|
| 36 |
this.$overlay = config.$overlay || this.$element; |
| 37 |
|
| 38 |
this.$menuOverlay = $( '<div>' ) |
| 39 |
.addClass( 'mw-echo-ui-NotificationBadgeWidget-overlay-menu' ); |
| 40 |
this.$overlay.append( this.$menuOverlay ); |
| 41 |
|
| 42 |
|
| 43 |
this.controller = controller; |
| 44 |
this.manager = manager; |
| 45 |
|
| 46 |
const adjustedTypeString = this.controller.getTypeString() === 'message' ? 'notice' : this.controller.getTypeString(); |
| 47 |
|
| 48 |
|
| 49 |
this.types = this.manager.getTypes(); |
| 50 |
|
| 51 |
this.numItems = config.numItems || 0; |
| 52 |
this.hasRunFirstTime = false; |
| 53 |
|
| 54 |
const buttonFlags = []; |
| 55 |
if ( config.hasUnseen ) { |
| 56 |
buttonFlags.push( 'unseen' ); |
| 57 |
} |
| 58 |
|
| 59 |
this.badgeButton = new mw.echo.ui.BadgeLinkWidget( { |
| 60 |
convertedNumber: config.convertedNumber, |
| 61 |
type: this.manager.getTypeString(), |
| 62 |
numItems: this.numItems, |
| 63 |
flags: buttonFlags, |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
title: mw.msg( 'tooltip-pt-notifications-' + adjustedTypeString ), |
| 68 |
href: config.href |
| 69 |
} ); |
| 70 |
|
| 71 |
|
| 72 |
this.notificationsWidget = new mw.echo.ui.NotificationsListWidget( |
| 73 |
this.controller, |
| 74 |
this.manager, |
| 75 |
{ |
| 76 |
type: this.types, |
| 77 |
$overlay: this.$menuOverlay, |
| 78 |
animated: true |
| 79 |
} |
| 80 |
); |
| 81 |
|
| 82 |
|
| 83 |
const allNotificationsButton = new OO.ui.ButtonWidget( { |
| 84 |
icon: 'next', |
| 85 |
label: mw.msg( 'echo-overlay-link' ), |
| 86 |
href: links.notifications, |
| 87 |
classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-allnotifs' ] |
| 88 |
} ); |
| 89 |
allNotificationsButton.$element.children().first().removeAttr( 'role' ); |
| 90 |
|
| 91 |
const preferencesButton = new OO.ui.ButtonWidget( { |
| 92 |
icon: 'settings', |
| 93 |
label: mw.msg( 'mypreferences' ), |
| 94 |
href: links.preferences, |
| 95 |
classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-preferences' ] |
| 96 |
} ); |
| 97 |
preferencesButton.$element.children().first().removeAttr( 'role' ); |
| 98 |
|
| 99 |
const footerItems = [ allNotificationsButton ]; |
| 100 |
if ( !mw.user.isTemp() ) { |
| 101 |
footerItems.push( preferencesButton ); |
| 102 |
} |
| 103 |
const footerButtonGroupWidget = new OO.ui.ButtonGroupWidget( { |
| 104 |
items: footerItems, |
| 105 |
classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer-buttons' ] |
| 106 |
} ); |
| 107 |
const $footer = $( '<div>' ) |
| 108 |
.addClass( 'mw-echo-ui-notificationBadgeButtonPopupWidget-footer' ) |
| 109 |
.append( footerButtonGroupWidget.$element ); |
| 110 |
|
| 111 |
const screenWidth = $( window ).width(); |
| 112 |
|
| 113 |
|
| 114 |
const maxWidthBreakPoint = 639; |
| 115 |
const isUnderBreakpointMobile = screenWidth < maxWidthBreakPoint; |
| 116 |
const mql = window.matchMedia( `(max-width: ${ maxWidthBreakPoint }px)` ); |
| 117 |
const matchMedia = function ( event ) { |
| 118 |
if ( event.matches ) { |
| 119 |
this.popup.containerPadding = 0; |
| 120 |
} else { |
| 121 |
this.popup.containerPadding = 20; |
| 122 |
} |
| 123 |
}; |
| 124 |
this.popup = new OO.ui.PopupWidget( { |
| 125 |
$content: this.notificationsWidget.$element, |
| 126 |
$footer: $footer, |
| 127 |
width: config.popupWidth || 500, |
| 128 |
hideWhenOutOfView: false, |
| 129 |
autoFlip: false, |
| 130 |
autoClose: true, |
| 131 |
containerPadding: isUnderBreakpointMobile ? 0 : 20, |
| 132 |
$floatableContainer: this.$element, |
| 133 |
|
| 134 |
|
| 135 |
$autoCloseIgnore: this.$element.add( this.$menuOverlay ), |
| 136 |
head: true, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
label: mw.msg( |
| 141 |
'echo-notification-' + adjustedTypeString + |
| 142 |
'-text-only' |
| 143 |
), |
| 144 |
classes: [ 'mw-echo-ui-notificationBadgeButtonPopupWidget-popup' ] |
| 145 |
} ); |
| 146 |
mql.addEventListener( 'change', matchMedia.bind( this ) ); |
| 147 |
|
| 148 |
this.$overlay.append( this.popup.$element ); |
| 149 |
|
| 150 |
|
| 151 |
this.popupHeadIcon = new OO.ui.IconWidget( { icon: config.badgeIcon } ); |
| 152 |
this.popup.$head.prepend( this.popupHeadIcon.$element ); |
| 153 |
|
| 154 |
this.setPendingElement( this.popup.$head ); |
| 155 |
|
| 156 |
|
| 157 |
this.markAllReadLabel = mw.msg( 'echo-mark-all-as-read', config.convertedNumber ); |
| 158 |
this.markAllReadButton = new OO.ui.ButtonWidget( { |
| 159 |
framed: false, |
| 160 |
label: this.markAllReadLabel, |
| 161 |
classes: [ 'mw-echo-ui-notificationsWidget-markAllReadButton' ] |
| 162 |
} ); |
| 163 |
|
| 164 |
|
| 165 |
this.popup.closeButton.toggle( false ); |
| 166 |
|
| 167 |
this.popup.$head.append( this.markAllReadButton.$element ); |
| 168 |
this.markAllReadButton.toggle( false ); |
| 169 |
|
| 170 |
|
| 171 |
this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } ); |
| 172 |
this.manager.connect( this, { |
| 173 |
update: 'updateBadge' |
| 174 |
} ); |
| 175 |
this.manager.getSeenTimeModel().connect( this, { update: 'onSeenTimeModelUpdate' } ); |
| 176 |
this.manager.getUnreadCounter().connect( this, { countChange: 'updateBadge' } ); |
| 177 |
this.popup.connect( this, { toggle: 'onPopupToggle' } ); |
| 178 |
this.badgeButton.connect( this, { |
| 179 |
click: 'onBadgeButtonClick' |
| 180 |
} ); |
| 181 |
this.notificationsWidget.connect( this, { modified: 'onNotificationsListModified' } ); |
| 182 |
|
| 183 |
this.$element |
| 184 |
.prop( 'id', 'pt-notifications-' + adjustedTypeString ) |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
.addClass( |
| 189 |
'mw-echo-ui-notificationBadgeButtonPopupWidget ' + |
| 190 |
'mw-echo-ui-notificationBadgeButtonPopupWidget-' + adjustedTypeString |
| 191 |
) |
| 192 |
.append( this.badgeButton.$element ); |
| 193 |
mw.hook( 'ext.echo.NotificationBadgeWidget.onInitialize' ).fire( this ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
OO.inheritClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.Widget ); |
| 199 |
OO.mixinClass( mw.echo.ui.NotificationBadgeWidget, OO.ui.mixin.PendingElement ); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
mw.echo.ui.NotificationBadgeWidget.static.tagName = 'li'; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
mw.echo.ui.NotificationBadgeWidget.prototype.onNotificationsListModified = function () { |
| 228 |
this.popup.clip(); |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
mw.echo.ui.NotificationBadgeWidget.prototype.onBadgeButtonClick = function () { |
| 235 |
this.popup.toggle(); |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
mw.echo.ui.NotificationBadgeWidget.prototype.onSeenTimeModelUpdate = function () { |
| 242 |
this.updateBadgeSeenState( false ); |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
mw.echo.ui.NotificationBadgeWidget.prototype.updateBadgeSeenState = function ( hasUnseen ) { |
| 251 |
hasUnseen = hasUnseen === undefined ? false : !!hasUnseen; |
| 252 |
|
| 253 |
this.badgeButton.setFlags( { unseen: !!hasUnseen } ); |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
mw.echo.ui.NotificationBadgeWidget.prototype.updateBadge = function () { |
| 260 |
const unreadCount = this.manager.getUnreadCounter().getCount(); |
| 261 |
const cappedUnreadCount = this.manager.getUnreadCounter().getCappedNotificationCount( unreadCount ); |
| 262 |
const convertedCount = mw.language.convertNumber( cappedUnreadCount ); |
| 263 |
const badgeLabel = mw.msg( 'echo-badge-count', convertedCount ); |
| 264 |
this.markAllReadLabel = mw.msg( 'echo-mark-all-as-read', convertedCount ); |
| 265 |
this.markAllReadButton.setLabel( this.markAllReadLabel ); |
| 266 |
|
| 267 |
this.badgeButton.setLabel( badgeLabel ); |
| 268 |
this.badgeButton.setCount( unreadCount, badgeLabel ); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if ( unreadCount === 0 ) { |
| 273 |
this.updateBadgeSeenState( false ); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
this.markAllReadButton.toggle( this.manager.hasLocalUnread() ); |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
mw.echo.ui.NotificationBadgeWidget.prototype.onMarkAllReadButtonClick = function () { |
| 284 |
this.controller.markLocalNotificationsRead(); |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
mw.echo.ui.NotificationBadgeWidget.prototype.onPopupToggle = function ( isVisible ) { |
| 294 |
if ( this.promiseRunning ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
if ( !isVisible ) { |
| 299 |
this.notificationsWidget.resetInitiallyUnseenItems(); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
if ( this.hasRunFirstTime ) { |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
this.popup.$clippable.css( 'height', '1px' ); |
| 310 |
this.popup.clip(); |
| 311 |
} |
| 312 |
|
| 313 |
this.pushPending(); |
| 314 |
this.markAllReadButton.toggle( false ); |
| 315 |
this.promiseRunning = true; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
this.controller.fetchLocalNotifications( this.hasRunFirstTime ) |
| 320 |
.then( |
| 321 |
|
| 322 |
() => { |
| 323 |
if ( this.popup.isVisible() ) { |
| 324 |
|
| 325 |
mw.hook( 'ext.echo.popup.onInitialize' ).fire( this.manager.getTypeString(), this.controller ); |
| 326 |
|
| 327 |
|
| 328 |
return this.controller.updateSeenTime(); |
| 329 |
} |
| 330 |
}, |
| 331 |
|
| 332 |
( errorObj ) => { |
| 333 |
if ( errorObj.errCode === 'notlogin-required' ) { |
| 334 |
|
| 335 |
this.notificationsWidget.resetLoadingOption( mw.msg( 'echo-notification-loginrequired' ) ); |
| 336 |
} else { |
| 337 |
|
| 338 |
this.notificationsWidget.resetLoadingOption( mw.msg( 'echo-api-failure' ) ); |
| 339 |
} |
| 340 |
} |
| 341 |
) |
| 342 |
.then( this.emit.bind( this, 'finishLoading' ) ) |
| 343 |
.always( () => { |
| 344 |
this.popup.clip(); |
| 345 |
|
| 346 |
this.popPending(); |
| 347 |
this.promiseRunning = false; |
| 348 |
} ); |
| 349 |
this.hasRunFirstTime = true; |
| 350 |
}; |
| 351 |
}() ); |