| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
mw.echo.dm.ModelManager = function MwEchoDmModelManager( counter, config ) { |
| 31 |
config = config || {}; |
| 32 |
|
| 33 |
|
| 34 |
OO.EventEmitter.call( this ); |
| 35 |
|
| 36 |
this.counter = counter; |
| 37 |
|
| 38 |
this.types = config.type || 'message'; |
| 39 |
this.types = Array.isArray( this.types ) ? |
| 40 |
config.type : [ this.types ]; |
| 41 |
|
| 42 |
this.seenTimeModel = new mw.echo.dm.SeenTimeModel( { types: this.types } ); |
| 43 |
|
| 44 |
this.notificationModels = {}; |
| 45 |
this.paginationModel = new mw.echo.dm.PaginationModel( { |
| 46 |
itemsPerPage: config.itemsPerPage || 25 |
| 47 |
} ); |
| 48 |
this.filtersModel = new mw.echo.dm.FiltersModel( { |
| 49 |
selectedSource: 'local', |
| 50 |
readState: config.readState |
| 51 |
} ); |
| 52 |
|
| 53 |
|
| 54 |
this.seenTimeModel.connect( this, { update: 'onSeenTimeUpdate' } ); |
| 55 |
|
| 56 |
this.localCounter = config.localCounter || new mw.echo.dm.UnreadNotificationCounter(); |
| 57 |
this.localCounter.connect( this, { countChange: [ 'emit', 'localCountChange' ] } ); |
| 58 |
}; |
| 59 |
|
| 60 |
OO.initClass( mw.echo.dm.ModelManager ); |
| 61 |
OO.mixinClass( mw.echo.dm.ModelManager, OO.EventEmitter ); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
mw.echo.dm.ModelManager.prototype.onSeenTimeUpdate = function ( timestamp ) { |
| 116 |
const models = this.getAllNotificationModels(); |
| 117 |
|
| 118 |
for ( const modelId in models ) { |
| 119 |
models[ modelId ].updateSeenState( timestamp ); |
| 120 |
} |
| 121 |
|
| 122 |
this.emit( 'seen', timestamp ); |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
mw.echo.dm.ModelManager.prototype.onModelDiscardItems = function ( modelId ) { |
| 131 |
const model = this.getNotificationModel( modelId ); |
| 132 |
|
| 133 |
|
| 134 |
if ( model.isEmpty() ) { |
| 135 |
this.removeNotificationModel( modelId ); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
mw.echo.dm.ModelManager.prototype.getAllNotificationModels = function () { |
| 145 |
return this.notificationModels; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
mw.echo.dm.ModelManager.prototype.setNotificationModels = function ( modelDefinitions ) { |
| 160 |
this.resetNotificationModels(); |
| 161 |
|
| 162 |
for ( const modelId in modelDefinitions ) { |
| 163 |
this.notificationModels[ modelId ] = modelDefinitions[ modelId ]; |
| 164 |
this.notificationModels[ modelId ].connect( this, { |
| 165 |
discard: [ 'onModelDiscardItems', modelId ], |
| 166 |
itemUpdate: [ 'onModelItemUpdate', modelId ] |
| 167 |
} ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
this.updateCurrentPageItemCount(); |
| 172 |
|
| 173 |
this.emit( 'update', this.getAllNotificationModels() ); |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
mw.echo.dm.ModelManager.prototype.onModelItemUpdate = function ( modelName, item ) { |
| 184 |
this.checkLocalUnreadTalk(); |
| 185 |
|
| 186 |
this.emit( 'modelItemUpdate', modelName, item ); |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
mw.echo.dm.ModelManager.prototype.updateCurrentPageItemCount = function () { |
| 193 |
this.getPaginationModel().setCurrentPageItemCount( this.getAllNotificationCount() ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
mw.echo.dm.ModelManager.prototype.getAllNotificationCount = function () { |
| 203 |
const models = this.getAllNotificationModels(); |
| 204 |
|
| 205 |
let count = 0; |
| 206 |
for ( const model in models ) { |
| 207 |
count += models[ model ].getCount(); |
| 208 |
} |
| 209 |
|
| 210 |
return count; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
mw.echo.dm.ModelManager.prototype.getNotificationModel = function ( modelName ) { |
| 220 |
return this.notificationModels[ modelName ]; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
mw.echo.dm.ModelManager.prototype.getPaginationModel = function () { |
| 229 |
return this.paginationModel; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
mw.echo.dm.ModelManager.prototype.getFiltersModel = function () { |
| 238 |
return this.filtersModel; |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
mw.echo.dm.ModelManager.prototype.removeNotificationModel = function ( modelName ) { |
| 248 |
delete this.notificationModels[ modelName ]; |
| 249 |
|
| 250 |
this.emit( 'discard', modelName ); |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
mw.echo.dm.ModelManager.prototype.resetNotificationModels = function () { |
| 259 |
for ( const model in this.notificationModels ) { |
| 260 |
if ( Object.prototype.hasOwnProperty.call( this.notificationModels, model ) ) { |
| 261 |
this.notificationModels[ model ].disconnect( this ); |
| 262 |
delete this.notificationModels[ model ]; |
| 263 |
} |
| 264 |
} |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
mw.echo.dm.ModelManager.prototype.getUnreadCounter = function () { |
| 273 |
return this.counter; |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
mw.echo.dm.ModelManager.prototype.hasLocalUnread = function () { |
| 282 |
return this.getLocalNotifications().some( ( item ) => !item.isRead() ); |
| 283 |
}; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
mw.echo.dm.ModelManager.prototype.getLocalUnread = function () { |
| 291 |
return this.getLocalNotifications().filter( ( item ) => !item.isRead() ); |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
mw.echo.dm.ModelManager.prototype.checkLocalUnreadTalk = function () { |
| 300 |
if ( !this.hasLocalUnreadTalk() ) { |
| 301 |
this.emit( 'allTalkRead' ); |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
mw.echo.dm.ModelManager.prototype.hasLocalUnreadTalk = function () { |
| 311 |
return this.getLocalNotifications().some( |
| 312 |
( item ) => !item.isRead() && item.getCategory() === 'edit-user-talk' |
| 313 |
); |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
mw.echo.dm.ModelManager.prototype.hasUnseenInModel = function ( modelId ) { |
| 323 |
const model = this.getNotificationModel( modelId || 'local' ); |
| 324 |
|
| 325 |
return model && model.hasUnseen(); |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
mw.echo.dm.ModelManager.prototype.hasUnseenInSource = function ( source ) { |
| 335 |
source = source || 'local'; |
| 336 |
const modelNames = this.getModelsBySource( source ); |
| 337 |
|
| 338 |
for ( let i = 0; i < modelNames.length; i++ ) { |
| 339 |
if ( this.getNotificationModel( modelNames[ i ] ).hasUnseen() ) { |
| 340 |
return true; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return false; |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
mw.echo.dm.ModelManager.prototype.hasUnseenInAnyModel = function () { |
| 353 |
const models = this.getAllNotificationModels(); |
| 354 |
|
| 355 |
for ( const model in models ) { |
| 356 |
if ( models[ model ].hasUnseen() ) { |
| 357 |
return true; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return false; |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
mw.echo.dm.ModelManager.prototype.getSeenTime = function ( source ) { |
| 371 |
return this.getSeenTimeModel().getSeenTime( source ); |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
mw.echo.dm.ModelManager.prototype.getTypes = function () { |
| 380 |
return this.types; |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
mw.echo.dm.ModelManager.prototype.getTypeString = function () { |
| 389 |
return ( |
| 390 |
this.types.length === 1 ? |
| 391 |
this.types[ 0 ] : |
| 392 |
'all' |
| 393 |
); |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
mw.echo.dm.ModelManager.prototype.getLocalCounter = function () { |
| 402 |
return this.localCounter; |
| 403 |
}; |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
mw.echo.dm.ModelManager.prototype.getLocalNotifications = function () { |
| 411 |
return this.getNotificationsBySource( 'local' ); |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
mw.echo.dm.ModelManager.prototype.getNotificationsBySource = function ( source ) { |
| 421 |
source = source || 'local'; |
| 422 |
|
| 423 |
const notifications = []; |
| 424 |
Object.keys( this.getAllNotificationModels() ).forEach( ( modelName ) => { |
| 425 |
const model = this.getNotificationModel( modelName ); |
| 426 |
if ( model.getSource() === source ) { |
| 427 |
notifications.push( ...model.getItems() ); |
| 428 |
} |
| 429 |
} ); |
| 430 |
return notifications; |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
mw.echo.dm.ModelManager.prototype.getModelsBySource = function ( source ) { |
| 440 |
const modelIds = []; |
| 441 |
|
| 442 |
source = source || 'local'; |
| 443 |
|
| 444 |
Object.keys( this.getAllNotificationModels() ).forEach( ( modelName ) => { |
| 445 |
const model = this.getNotificationModel( modelName ); |
| 446 |
if ( model.getSource() === source ) { |
| 447 |
modelIds.push( modelName ); |
| 448 |
} |
| 449 |
} ); |
| 450 |
return modelIds; |
| 451 |
}; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
mw.echo.dm.ModelManager.prototype.getSeenTimeModel = function () { |
| 459 |
return this.seenTimeModel; |
| 460 |
|
| 461 |
}; |
| 462 |
|
| 463 |
}() ); |