| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
mw.echo.ui.SubGroupListWidget = function MwEchoUiSubGroupListWidget( controller, listModel, config ) { |
| 17 |
this.$header = $( '<div>' ) |
| 18 |
.addClass( 'mw-echo-ui-subGroupListWidget-header' ); |
| 19 |
|
| 20 |
config = config || {}; |
| 21 |
|
| 22 |
this.controller = controller; |
| 23 |
this.model = listModel; |
| 24 |
|
| 25 |
|
| 26 |
mw.echo.ui.SubGroupListWidget.super.call( this, Object.assign( { data: this.getSource() }, config ) ); |
| 27 |
|
| 28 |
this.showTitle = !!config.showTitle; |
| 29 |
this.showMarkAllRead = !!config.showMarkAllRead; |
| 30 |
this.$overlay = config.$overlay || this.$element; |
| 31 |
|
| 32 |
this.listWidget = new mw.echo.ui.SortedListWidget( |
| 33 |
|
| 34 |
config.sortingCallback || ( ( a, b ) => { |
| 35 |
|
| 36 |
if ( b.getTimestamp() < a.getTimestamp() ) { |
| 37 |
return -1; |
| 38 |
} else if ( b.getTimestamp() > a.getTimestamp() ) { |
| 39 |
return 1; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
return b.getId() - a.getId(); |
| 44 |
} ), |
| 45 |
|
| 46 |
{ |
| 47 |
$overlay: this.$overlay, |
| 48 |
animated: config.animateSorting |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
const sourceURL = this.model.getSourceURL() ? |
| 53 |
this.model.getSourceURL().replace( '$1', 'Special:Notifications' ) : |
| 54 |
null; |
| 55 |
if ( sourceURL ) { |
| 56 |
this.title = new OO.ui.ButtonWidget( { |
| 57 |
framed: false, |
| 58 |
classes: [ |
| 59 |
'mw-echo-ui-subGroupListWidget-header-row-title', |
| 60 |
'mw-echo-ui-subGroupListWidget-header-row-cell' |
| 61 |
], |
| 62 |
href: sourceURL |
| 63 |
} ); |
| 64 |
} else { |
| 65 |
this.title = new OO.ui.LabelWidget( { |
| 66 |
classes: [ |
| 67 |
'mw-echo-ui-subGroupListWidget-header-row-title', |
| 68 |
'mw-echo-ui-subGroupListWidget-header-row-cell' |
| 69 |
] |
| 70 |
} ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( this.model.getTitle() ) { |
| 74 |
this.title.setLabel( this.model.getTitle() ); |
| 75 |
} |
| 76 |
this.title.toggle( this.showTitle ); |
| 77 |
|
| 78 |
|
| 79 |
this.markAllReadButton = new OO.ui.ButtonWidget( { |
| 80 |
framed: true, |
| 81 |
icon: 'checkAll', |
| 82 |
label: mw.msg( 'echo-specialpage-section-markread' ), |
| 83 |
classes: [ |
| 84 |
'mw-echo-ui-subGroupListWidget-header-row-markAllReadButton', |
| 85 |
'mw-echo-ui-subGroupListWidget-header-row-cell' |
| 86 |
] |
| 87 |
} ); |
| 88 |
|
| 89 |
|
| 90 |
this.model.connect( this, { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
discard: 'onModelDiscardItems', |
| 97 |
|
| 98 |
update: 'resetItemsFromModel' |
| 99 |
} ); |
| 100 |
this.model.connect( this, { itemUpdate: 'toggleMarkAllReadButton' } ); |
| 101 |
this.markAllReadButton.connect( this, { click: 'onMarkAllReadButtonClick' } ); |
| 102 |
|
| 103 |
|
| 104 |
this.toggleMarkAllReadButton(); |
| 105 |
|
| 106 |
this.$element |
| 107 |
.addClass( 'mw-echo-ui-subGroupListWidget' ) |
| 108 |
.append( |
| 109 |
this.$header.append( |
| 110 |
$( '<div>' ) |
| 111 |
.addClass( 'mw-echo-ui-subGroupListWidget-header-row' ) |
| 112 |
.append( |
| 113 |
this.title.$element, |
| 114 |
this.markAllReadButton.$element |
| 115 |
) |
| 116 |
), |
| 117 |
this.listWidget.$element |
| 118 |
); |
| 119 |
|
| 120 |
|
| 121 |
this.$pageContentText = $( '#mw-content-text' ); |
| 122 |
$( window ).on( 'resize', this.resizeHeader.bind( this ) ); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
setTimeout( this.resizeHeader.bind( this ), 0 ); |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
OO.inheritClass( mw.echo.ui.SubGroupListWidget, OO.ui.Widget ); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.echo.ui.SubGroupListWidget.prototype.resizeHeader = function () { |
| 139 |
const contentWidth = this.$pageContentText.width(), |
| 140 |
screenTooNarrow = this.$header.width() > contentWidth; |
| 141 |
|
| 142 |
|
| 143 |
this.title.$element.toggleClass( |
| 144 |
'mw-echo-ui-subGroupListWidget-header-row-cell', |
| 145 |
!screenTooNarrow |
| 146 |
); |
| 147 |
this.markAllReadButton.$element.toggleClass( |
| 148 |
'mw-echo-ui-subGroupListWidget-header-row-cell', |
| 149 |
!screenTooNarrow |
| 150 |
); |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
mw.echo.ui.SubGroupListWidget.prototype.destroy = function () { |
| 157 |
$( window ).off( 'resize' ); |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
mw.echo.ui.SubGroupListWidget.prototype.toggleMarkAllReadButton = function () { |
| 165 |
this.markAllReadButton.toggle( this.showMarkAllRead && this.hasUnread() ); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
mw.echo.ui.SubGroupListWidget.prototype.onMarkAllReadButtonClick = function () { |
| 172 |
this.controller.markEntireListModelRead( this.model.getName() ); |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
mw.echo.ui.SubGroupListWidget.prototype.hasUnread = function () { |
| 181 |
return this.model.getItems().some( ( item ) => !item.isRead() ); |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
mw.echo.ui.SubGroupListWidget.prototype.resetItemsFromModel = function ( items ) { |
| 191 |
const itemWidgets = []; |
| 192 |
let $elements = $(); |
| 193 |
|
| 194 |
items = items || this.model.getItems(); |
| 195 |
|
| 196 |
for ( let i = 0; i < items.length; i++ ) { |
| 197 |
const widget = new mw.echo.ui.SingleNotificationItemWidget( |
| 198 |
this.controller, |
| 199 |
items[ i ], |
| 200 |
{ |
| 201 |
$overlay: this.$overlay, |
| 202 |
bundle: items[ i ].isBundled() |
| 203 |
} |
| 204 |
); |
| 205 |
itemWidgets.push( widget ); |
| 206 |
$elements = $elements.add( widget.$element ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
this.getListWidget().clearItems(); |
| 211 |
|
| 212 |
|
| 213 |
mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements ); |
| 214 |
|
| 215 |
|
| 216 |
this.getListWidget().addItems( itemWidgets ); |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
mw.echo.ui.SubGroupListWidget.prototype.onModelDiscardItems = function ( items ) { |
| 226 |
const itemWidgets = []; |
| 227 |
|
| 228 |
for ( let i = 0; i < items.length; i++ ) { |
| 229 |
itemWidgets.push( this.listWidget.getItemFromId( items[ i ].getId() ) ); |
| 230 |
} |
| 231 |
this.listWidget.removeItems( itemWidgets ); |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
mw.echo.ui.SubGroupListWidget.prototype.getListWidget = function () { |
| 241 |
return this.listWidget; |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
mw.echo.ui.SubGroupListWidget.prototype.getTimestamp = function () { |
| 250 |
return this.model.getTimestamp(); |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
mw.echo.ui.SubGroupListWidget.prototype.toggleTitle = function ( show ) { |
| 259 |
show = show !== undefined ? show : !this.showTitle; |
| 260 |
|
| 261 |
if ( this.showTitle !== show ) { |
| 262 |
this.showTitle = show; |
| 263 |
this.title.toggle( this.showTitle ); |
| 264 |
} |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
mw.echo.ui.SubGroupListWidget.prototype.getSource = function () { |
| 273 |
return this.model.getSource(); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDs = function () { |
| 282 |
return this.model.getAllItemIds(); |
| 283 |
}; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
mw.echo.ui.SubGroupListWidget.prototype.getAllItemIDsByType = function ( type ) { |
| 293 |
return this.model.getAllItemIdsByType( type ); |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
mw.echo.ui.SubGroupListWidget.prototype.isForeign = function () { |
| 302 |
return this.model.isForeign(); |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
mw.echo.ui.SubGroupListWidget.prototype.getId = function () { |
| 313 |
return this.model.getName(); |
| 314 |
}; |
| 315 |
}() ); |