| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
mw.echo.dm.UnreadNotificationCounter = function mwEchoDmUnreadNotificationCounter( api, type, max, config ) { |
| 19 |
config = config || {}; |
| 20 |
|
| 21 |
|
| 22 |
OO.EventEmitter.call( this ); |
| 23 |
|
| 24 |
this.api = api; |
| 25 |
this.type = type; |
| 26 |
this.max = max; |
| 27 |
this.prioritizer = new mw.echo.api.PromisePrioritizer(); |
| 28 |
|
| 29 |
this.count = 0; |
| 30 |
this.localOnly = config.localOnly === undefined ? false : !!config.localOnly; |
| 31 |
this.source = config.source || 'local'; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
OO.mixinClass( mw.echo.dm.UnreadNotificationCounter, OO.EventEmitter ); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
mw.echo.dm.UnreadNotificationCounter.prototype.getCappedNotificationCount = function ( count ) { |
| 60 |
if ( count < 0 ) { |
| 61 |
return 0; |
| 62 |
} else if ( count <= this.max ) { |
| 63 |
return count; |
| 64 |
} else { |
| 65 |
return this.max + 1; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
mw.echo.dm.UnreadNotificationCounter.prototype.getCount = function () { |
| 75 |
return this.count; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
mw.echo.dm.UnreadNotificationCounter.prototype.setCount = function ( count, isEstimation ) { |
| 86 |
if ( isEstimation ) { |
| 87 |
if ( this.count > this.max ) { |
| 88 |
|
| 89 |
return; |
| 90 |
} |
| 91 |
if ( count < 0 ) { |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
count = this.getCappedNotificationCount( count ); |
| 99 |
|
| 100 |
if ( count !== this.count ) { |
| 101 |
this.count = count; |
| 102 |
this.emit( 'countChange', this.count ); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
mw.echo.dm.UnreadNotificationCounter.prototype.estimateChange = function ( delta ) { |
| 112 |
this.setCount( this.count + delta, true ); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
mw.echo.dm.UnreadNotificationCounter.prototype.update = function () { |
| 122 |
if ( !this.api ) { |
| 123 |
return $.Deferred().reject(); |
| 124 |
} |
| 125 |
|
| 126 |
return this.prioritizer.prioritize( this.api.fetchUnreadCount( |
| 127 |
this.source, |
| 128 |
this.type, |
| 129 |
this.localOnly |
| 130 |
) ).then( ( actualCount ) => { |
| 131 |
this.setCount( actualCount, false ); |
| 132 |
|
| 133 |
return actualCount; |
| 134 |
} ); |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
mw.echo.dm.UnreadNotificationCounter.prototype.setSource = function ( source ) { |
| 143 |
this.source = source; |
| 144 |
}; |
| 145 |
|
| 146 |
}() ); |