| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
mw.echo.ui.NotificationsListWidget = function MwEchoUiNotificationsListWidget( controller, manager, config ) { |
| 20 |
config = config || {}; |
| 21 |
|
| 22 |
mw.echo.ui.NotificationsListWidget.super.call( |
| 23 |
this, |
| 24 |
|
| 25 |
( a, b ) => { |
| 26 |
if ( !a.isRead() && b.isRead() ) { |
| 27 |
return -1; |
| 28 |
} else if ( a.isRead() && !b.isRead() ) { |
| 29 |
return 1; |
| 30 |
} else if ( !a.isForeign() && b.isForeign() ) { |
| 31 |
return -1; |
| 32 |
} else if ( a.isForeign() && !b.isForeign() ) { |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
if ( b.getTimestamp() < a.getTimestamp() ) { |
| 38 |
return -1; |
| 39 |
} else if ( b.getTimestamp() > a.getTimestamp() ) { |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
return b.getId() - a.getId(); |
| 45 |
}, |
| 46 |
config |
| 47 |
); |
| 48 |
|
| 49 |
|
| 50 |
this.controller = controller; |
| 51 |
this.manager = manager; |
| 52 |
this.models = {}; |
| 53 |
|
| 54 |
|
| 55 |
this.$overlay = config.$overlay || this.$element; |
| 56 |
this.timestamp = config.timestamp || 0; |
| 57 |
|
| 58 |
|
| 59 |
this.loadingOptionWidget = new mw.echo.ui.PlaceholderItemWidget(); |
| 60 |
|
| 61 |
this.resetLoadingOption(); |
| 62 |
|
| 63 |
this.manager.connect( this, { |
| 64 |
update: 'resetDataFromModel', |
| 65 |
discard: 'onModelManagerDiscard' |
| 66 |
} ); |
| 67 |
|
| 68 |
this.$element |
| 69 |
.addClass( 'mw-echo-ui-notificationsListWidget' ); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
OO.inheritClass( mw.echo.ui.NotificationsListWidget, mw.echo.ui.SortedListWidget ); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
mw.echo.ui.NotificationsListWidget.prototype.onModelManagerDiscard = function ( modelName ) { |
| 89 |
const items = this.getItems(); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if ( modelName === 'xwiki' ) { |
| 96 |
for ( let i = 0; i < items.length; i++ ) { |
| 97 |
if ( items[ i ] instanceof mw.echo.ui.CrossWikiNotificationItemWidget ) { |
| 98 |
this.removeItems( [ items[ i ] ] ); |
| 99 |
this.checkForEmptyNotificationsList(); |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
this.emit( 'modified' ); |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
mw.echo.ui.NotificationsListWidget.prototype.resetDataFromModel = function ( models ) { |
| 118 |
const itemWidgets = []; |
| 119 |
let $elements = $(); |
| 120 |
|
| 121 |
|
| 122 |
for ( const modelId in this.models ) { |
| 123 |
this.detachModel( modelId ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
for ( const modelId in models ) { |
| 128 |
const model = models[ modelId ]; |
| 129 |
this.attachModel( modelId, model ); |
| 130 |
|
| 131 |
|
| 132 |
if ( model.isGroup() ) { |
| 133 |
let widget; |
| 134 |
if ( model.isForeign() ) { |
| 135 |
|
| 136 |
widget = new mw.echo.ui.CrossWikiNotificationItemWidget( |
| 137 |
this.controller, |
| 138 |
model, |
| 139 |
{ |
| 140 |
$overlay: this.$overlay, |
| 141 |
animateSorting: this.animated |
| 142 |
} |
| 143 |
); |
| 144 |
} else { |
| 145 |
|
| 146 |
widget = new mw.echo.ui.BundleNotificationItemWidget( |
| 147 |
this.controller, |
| 148 |
model, |
| 149 |
{ |
| 150 |
$overlay: this.$overlay, |
| 151 |
bundle: false, |
| 152 |
animateSorting: this.animated |
| 153 |
} |
| 154 |
); |
| 155 |
} |
| 156 |
itemWidgets.push( widget ); |
| 157 |
$elements = $elements.add( widget.$element ); |
| 158 |
} else { |
| 159 |
const subItems = model.getItems(); |
| 160 |
|
| 161 |
for ( let i = 0; i < subItems.length; i++ ) { |
| 162 |
const subItem = subItems[ i ]; |
| 163 |
const widget = new mw.echo.ui.SingleNotificationItemWidget( |
| 164 |
this.controller, |
| 165 |
subItem, |
| 166 |
{ |
| 167 |
$overlay: this.$overlay, |
| 168 |
bundle: false |
| 169 |
} |
| 170 |
); |
| 171 |
itemWidgets.push( widget ); |
| 172 |
$elements = $elements.add( widget.$element ); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
this.clearItems(); |
| 179 |
|
| 180 |
|
| 181 |
mw.hook( 'ext.echo.notifications.beforeRender' ).fire( this.$element, $elements ); |
| 182 |
|
| 183 |
this.addItems( itemWidgets ); |
| 184 |
|
| 185 |
this.checkForEmptyNotificationsList(); |
| 186 |
|
| 187 |
this.emit( 'modified' ); |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
mw.echo.ui.NotificationsListWidget.prototype.attachModel = function ( modelId, model ) { |
| 197 |
this.models[ modelId ] = model; |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
mw.echo.ui.NotificationsListWidget.prototype.detachModel = function ( modelId ) { |
| 206 |
this.models[ modelId ].disconnect( this ); |
| 207 |
delete this.models[ modelId ]; |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
mw.echo.ui.NotificationsListWidget.prototype.resetLoadingOption = function ( label, link ) { |
| 218 |
this.loadingOptionWidget.setLabel( label || '' ); |
| 219 |
this.loadingOptionWidget.setLink( link || '' ); |
| 220 |
if ( this.isEmpty() ) { |
| 221 |
this.addItems( [ this.loadingOptionWidget ] ); |
| 222 |
} |
| 223 |
this.emit( 'modified' ); |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
mw.echo.ui.NotificationsListWidget.prototype.checkForEmptyNotificationsList = function () { |
| 231 |
this.resetLoadingOption( this.isEmpty() ? mw.msg( 'echo-notification-placeholder' ) : '' ); |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
mw.echo.ui.NotificationsListWidget.prototype.resetInitiallyUnseenItems = function () { |
| 238 |
const itemWidgets = this.getItems(); |
| 239 |
|
| 240 |
for ( let i = 0; i < itemWidgets.length; i++ ) { |
| 241 |
itemWidgets[ i ].resetInitiallyUnseen(); |
| 242 |
} |
| 243 |
}; |
| 244 |
}() ); |