| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( controller, model, config ) { |
| 19 |
config = config || {}; |
| 20 |
|
| 21 |
|
| 22 |
mw.echo.ui.NotificationItemWidget.super.call( this, Object.assign( { data: model.getId() }, config ) ); |
| 23 |
|
| 24 |
this.controller = controller; |
| 25 |
this.model = model; |
| 26 |
|
| 27 |
this.$overlay = config.$overlay || this.$element; |
| 28 |
this.bundle = !!config.bundle; |
| 29 |
|
| 30 |
this.$content = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content' ); |
| 31 |
this.$actions = $( '<div>' ) |
| 32 |
.addClass( 'mw-echo-ui-notificationItemWidget-content-actions' ); |
| 33 |
|
| 34 |
|
| 35 |
this.markAsReadButton = new mw.echo.ui.ToggleReadCircleButtonWidget( { |
| 36 |
framed: false, |
| 37 |
classes: [ 'mw-echo-ui-notificationItemWidget-markAsReadButton' ], |
| 38 |
markAsRead: !this.model.isRead() |
| 39 |
} ); |
| 40 |
|
| 41 |
let $icon; |
| 42 |
const iconUrl = this.model.getIconUrl(); |
| 43 |
|
| 44 |
if ( iconUrl ) { |
| 45 |
|
| 46 |
|
| 47 |
const invertIconClass = |
| 48 |
iconUrl.includes( 'progressive' ) || |
| 49 |
iconUrl.includes( 'constructive' ) || |
| 50 |
iconUrl.includes( 'failure' ) ? '' : 'skin-invert'; |
| 51 |
|
| 52 |
$icon = $( '<div>' ) |
| 53 |
.addClass( `mw-echo-ui-notificationItemWidget-icon ${ invertIconClass }` ) |
| 54 |
.append( $( '<img>' ).attr( { |
| 55 |
src: this.model.getIconUrl(), |
| 56 |
role: 'presentation', |
| 57 |
alt: ' ' |
| 58 |
} ) ); |
| 59 |
} |
| 60 |
|
| 61 |
const $message = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content-message' ); |
| 62 |
|
| 63 |
$message.append( |
| 64 |
$( '<div>' ) |
| 65 |
.addClass( 'mw-echo-ui-notificationItemWidget-content-message-header-wrapper' ) |
| 66 |
.append( |
| 67 |
$( '<div>' ) |
| 68 |
.addClass( 'mw-echo-ui-notificationItemWidget-content-message-header' ) |
| 69 |
.append( this.model.getContentHeader() ) |
| 70 |
) |
| 71 |
); |
| 72 |
if ( !this.bundle && this.model.getContentBody() ) { |
| 73 |
$message.append( |
| 74 |
$( '<div>' ) |
| 75 |
.addClass( 'mw-echo-ui-notificationItemWidget-content-message-body' ) |
| 76 |
.append( this.model.getContentBody() ) |
| 77 |
|
| 78 |
|
| 79 |
.attr( 'dir', 'auto' ) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
this.actionsButtonSelectWidget = new OO.ui.ButtonSelectWidget( { |
| 85 |
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-buttons' ], |
| 86 |
tabIndex: -1 |
| 87 |
} ); |
| 88 |
|
| 89 |
|
| 90 |
this.menuPopupButtonWidget = new mw.echo.ui.ActionMenuPopupWidget( { |
| 91 |
framed: false, |
| 92 |
icon: 'ellipsis', |
| 93 |
$overlay: this.$overlay, |
| 94 |
horizontalPosition: this.bundle ? 'end' : 'auto', |
| 95 |
title: mw.msg( 'echo-notification-more-options-tooltip' ), |
| 96 |
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-menu' ] |
| 97 |
} ); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
const echoMoment = moment.utc( this.model.getTimestamp() ); |
| 103 |
echoMoment.locale( 'echo-shortRelativeTime' ); |
| 104 |
echoMoment.local(); |
| 105 |
|
| 106 |
this.timestampWidget = new OO.ui.LabelWidget( { |
| 107 |
classes: [ 'mw-echo-ui-notificationItemWidget-content-actions-timestamp' ], |
| 108 |
|
| 109 |
label: echoMoment.fromNow( true ) |
| 110 |
} ); |
| 111 |
|
| 112 |
|
| 113 |
if ( this.bundle ) { |
| 114 |
|
| 115 |
this.$actions.append( |
| 116 |
$( '<div>' ) |
| 117 |
|
| 118 |
|
| 119 |
.addClass( 'mw-echo-ui-notificationItemWidget-content-actions-row' ) |
| 120 |
.append( |
| 121 |
this.actionsButtonSelectWidget.$element, |
| 122 |
this.timestampWidget.$element, |
| 123 |
this.menuPopupButtonWidget.$element |
| 124 |
) |
| 125 |
); |
| 126 |
} else { |
| 127 |
this.$actions.append( |
| 128 |
this.actionsButtonSelectWidget.$element, |
| 129 |
this.menuPopupButtonWidget.$element, |
| 130 |
this.timestampWidget.$element |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
let outsideMenuItemCounter = 0; |
| 136 |
const secondaryUrls = this.model.getSecondaryUrls(); |
| 137 |
for ( let i = 0; i < secondaryUrls.length; i++ ) { |
| 138 |
const urlObj = secondaryUrls[ i ]; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
const isOutsideMenu = !this.bundle && |
| 145 |
( |
| 146 |
( |
| 147 |
|
| 148 |
urlObj.prioritized && |
| 149 |
outsideMenuItemCounter < mw.echo.config.maxPrioritizedActions |
| 150 |
) || |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
secondaryUrls.length <= mw.echo.config.maxPrioritizedActions |
| 155 |
); |
| 156 |
|
| 157 |
const linkButton = new mw.echo.ui.MenuItemWidget( { |
| 158 |
type: urlObj.type, |
| 159 |
actionData: urlObj.data, |
| 160 |
icon: urlObj.icon || 'next', |
| 161 |
label: urlObj.label, |
| 162 |
tooltip: urlObj.tooltip, |
| 163 |
description: urlObj.description, |
| 164 |
url: urlObj.url, |
| 165 |
prioritized: isOutsideMenu |
| 166 |
} ); |
| 167 |
|
| 168 |
|
| 169 |
if ( isOutsideMenu ) { |
| 170 |
this.actionsButtonSelectWidget.addItems( [ linkButton ] ); |
| 171 |
this.actionsButtonSelectWidget.setTabIndex( 0 ); |
| 172 |
outsideMenuItemCounter++; |
| 173 |
} else { |
| 174 |
this.menuPopupButtonWidget.getMenu().addItems( [ linkButton ] ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if ( this.bundle ) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
this.$content.append( |
| 183 |
$icon, |
| 184 |
$message, |
| 185 |
this.$actions, |
| 186 |
this.markAsReadButton.$element |
| 187 |
); |
| 188 |
this.$element.append( this.$content ); |
| 189 |
} else { |
| 190 |
this.$content.append( |
| 191 |
this.markAsReadButton.$element, |
| 192 |
$message, |
| 193 |
this.$actions |
| 194 |
); |
| 195 |
this.$element.append( $icon, this.$content ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
this.actionsButtonSelectWidget.connect( this, { choose: 'onPopupButtonWidgetChoose' } ); |
| 200 |
this.menuPopupButtonWidget.getMenu().connect( this, { choose: 'onPopupButtonWidgetChoose' } ); |
| 201 |
this.markAsReadButton.connect( this, { click: 'onMarkAsReadButtonClick' } ); |
| 202 |
|
| 203 |
this.$element |
| 204 |
.addClass( 'mw-echo-ui-notificationItemWidget' ) |
| 205 |
.toggleClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen', !this.model.isSeen() && !this.bundle ) |
| 206 |
.toggleClass( 'mw-echo-ui-notificationItemWidget-bundled', this.bundle ); |
| 207 |
|
| 208 |
if ( this.model.getPrimaryUrl() ) { |
| 209 |
this.$element |
| 210 |
.attr( 'href', this.model.getPrimaryUrl() ) |
| 211 |
.on( 'click', this.onPrimaryLinkClick.bind( this ) ) |
| 212 |
.on( 'auxclick', this.onPrimaryLinkAuxclick.bind( this ) ); |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
OO.inheritClass( mw.echo.ui.NotificationItemWidget, OO.ui.Widget ); |
| 217 |
|
| 218 |
|
| 219 |
mw.echo.ui.NotificationItemWidget.static.tagName = 'a'; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
mw.echo.ui.NotificationItemWidget.prototype.onPrimaryLinkClick = function () { |
| 228 |
return true; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
mw.echo.ui.NotificationItemWidget.prototype.onPrimaryLinkAuxclick = function ( event ) { |
| 241 |
if ( event.button === 1 ) { |
| 242 |
this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' ); |
| 243 |
this.markRead( true ); |
| 244 |
} |
| 245 |
|
| 246 |
return true; |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
mw.echo.ui.NotificationItemWidget.prototype.onPopupButtonWidgetChoose = function ( item ) { |
| 261 |
if ( !( item instanceof mw.echo.ui.MenuItemWidget ) ) { |
| 262 |
|
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
const actionData = item && item.getActionData(), |
| 267 |
messages = item && item.getConfirmationMessages(); |
| 268 |
|
| 269 |
|
| 270 |
item.pushPending(); |
| 271 |
this.controller.performDynamicAction( actionData, this.getModel().getSource() ) |
| 272 |
.then( () => { |
| 273 |
const $title = $( '<p>' ) |
| 274 |
.addClass( 'mw-echo-ui-notificationItemWidget-notify-title' ) |
| 275 |
.append( $.parseHTML( messages.title ) ), |
| 276 |
$description = $( '<p>' ) |
| 277 |
.addClass( 'mw-echo-ui-notificationItemWidget-notify-description' ) |
| 278 |
.append( $.parseHTML( messages.description ) ); |
| 279 |
|
| 280 |
|
| 281 |
if ( item.isPrioritized() ) { |
| 282 |
this.actionsButtonSelectWidget.removeItems( [ item ] ); |
| 283 |
} else { |
| 284 |
|
| 285 |
this.menuPopupButtonWidget.getMenu().removeItems( [ item ] ); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
$title.toggle( !!$title.text() ); |
| 290 |
$description.toggle( !!$description.text() ); |
| 291 |
|
| 292 |
|
| 293 |
const $confirmation = $( '<div>' ) |
| 294 |
.append( $title, $description ); |
| 295 |
|
| 296 |
|
| 297 |
mw.notify( $confirmation ); |
| 298 |
} ); |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
mw.echo.ui.NotificationItemWidget.prototype.onMarkAsReadButtonClick = function () { |
| 305 |
|
| 306 |
|
| 307 |
this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' ); |
| 308 |
this.markRead( !this.model.isRead() ); |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
mw.echo.ui.NotificationItemWidget.prototype.markRead = null; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
mw.echo.ui.NotificationItemWidget.prototype.getPrimaryUrl = function () { |
| 326 |
return this.model.getPrimaryUrl(); |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
mw.echo.ui.NotificationItemWidget.prototype.getTimestamp = function () { |
| 335 |
return this.model.getTimestamp(); |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
mw.echo.ui.NotificationItemWidget.prototype.getId = function () { |
| 344 |
return this.model.getId(); |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
mw.echo.ui.NotificationItemWidget.prototype.isSeen = function () { |
| 353 |
return this.model.isSeen(); |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
mw.echo.ui.NotificationItemWidget.prototype.isRead = function () { |
| 362 |
return this.model.isRead(); |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
mw.echo.ui.NotificationItemWidget.prototype.isForeign = function () { |
| 371 |
return this.model.isForeign(); |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
mw.echo.ui.NotificationItemWidget.prototype.toggleMarkAsReadButtons = function ( showMarkAsRead ) { |
| 385 |
showMarkAsRead = showMarkAsRead !== undefined ? showMarkAsRead : !this.model.isRead(); |
| 386 |
|
| 387 |
this.markAsReadButton.toggleState( showMarkAsRead ); |
| 388 |
this.menuPopupButtonWidget.toggle( !this.menuPopupButtonWidget.getMenu().isEmpty() ); |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
mw.echo.ui.NotificationItemWidget.prototype.toggleRead = function ( read ) { |
| 398 |
this.read = read !== undefined ? read : !this.read; |
| 399 |
|
| 400 |
this.$element.toggleClass( 'mw-echo-ui-notificationItemWidget-unread', !this.read ); |
| 401 |
this.toggleMarkAsReadButtons( !this.read ); |
| 402 |
}; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
mw.echo.ui.NotificationItemWidget.prototype.toggleSeen = function ( seen ) { |
| 411 |
this.seen = seen !== undefined ? seen : !this.seen; |
| 412 |
|
| 413 |
this.$element |
| 414 |
.toggleClass( 'mw-echo-ui-notificationItemWidget-unseen', !this.seen ); |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
mw.echo.ui.NotificationItemWidget.prototype.getModel = function () { |
| 423 |
return this.model; |
| 424 |
}; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
mw.echo.ui.NotificationItemWidget.prototype.destroy = function () { |
| 430 |
this.model.disconnect( this ); |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
mw.echo.ui.NotificationItemWidget.prototype.resetInitiallyUnseen = function () { |
| 438 |
this.$element.removeClass( 'mw-echo-ui-notificationItemWidget-initiallyUnseen' ); |
| 439 |
}; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
mw.echo.ui.NotificationItemWidget.prototype.isFake = function () { |
| 447 |
return false; |
| 448 |
}; |
| 449 |
}() ); |