| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
mw.echo.ui.CrossWikiUnreadFilterWidget = function MwEchoUiCrossWikiUnreadFilterWidget( controller, filtersModel, config ) { |
| 15 |
config = config || {}; |
| 16 |
|
| 17 |
|
| 18 |
mw.echo.ui.CrossWikiUnreadFilterWidget.super.call( this, |
| 19 |
|
| 20 |
( a, b ) => { |
| 21 |
|
| 22 |
if ( a.getSource() === 'local' ) { |
| 23 |
return -1; |
| 24 |
} else if ( b.getSource() === 'local' ) { |
| 25 |
return 1; |
| 26 |
} |
| 27 |
|
| 28 |
const diff = Number( b.getTotalCount() ) - Number( a.getTotalCount() ); |
| 29 |
if ( diff !== 0 ) { |
| 30 |
return diff; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
return b.getSource() - a.getSource(); |
| 35 |
}, |
| 36 |
|
| 37 |
config |
| 38 |
); |
| 39 |
|
| 40 |
OO.ui.mixin.PendingElement.call( this, config ); |
| 41 |
|
| 42 |
this.controller = controller; |
| 43 |
this.model = filtersModel; |
| 44 |
this.previousPageSelected = null; |
| 45 |
|
| 46 |
const titleWidget = new OO.ui.LabelWidget( { |
| 47 |
classes: [ 'mw-echo-ui-crossWikiUnreadFilterWidget-title' ], |
| 48 |
label: mw.msg( 'echo-specialpage-pagefilters-title' ) |
| 49 |
} ); |
| 50 |
const subtitleWidget = new OO.ui.LabelWidget( { |
| 51 |
classes: [ 'mw-echo-ui-crossWikiUnreadFilterWidget-subtitle' ], |
| 52 |
label: mw.msg( 'echo-specialpage-pagefilters-subtitle' ) |
| 53 |
} ); |
| 54 |
|
| 55 |
|
| 56 |
this.aggregate( { choose: 'pageFilterChoose' } ); |
| 57 |
this.connect( this, { pageFilterChoose: 'onPageFilterChoose' } ); |
| 58 |
|
| 59 |
this.$element |
| 60 |
.addClass( 'mw-echo-ui-crossWikiUnreadFilterWidget' ) |
| 61 |
.append( |
| 62 |
titleWidget.$element, |
| 63 |
subtitleWidget.$element, |
| 64 |
this.$group |
| 65 |
.addClass( 'mw-echo-ui-crossWikiUnreadFilterWidget-group' ) |
| 66 |
); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
OO.inheritClass( mw.echo.ui.CrossWikiUnreadFilterWidget, mw.echo.ui.SortedListWidget ); |
| 72 |
OO.mixinClass( mw.echo.ui.CrossWikiUnreadFilterWidget, OO.ui.mixin.PendingElement ); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.onPageFilterChoose = function ( widget, item ) { |
| 94 |
const source = widget.getSource(), |
| 95 |
page = item && item.getData(); |
| 96 |
|
| 97 |
if ( item ) { |
| 98 |
this.setItemSelected( item ); |
| 99 |
|
| 100 |
this.emit( 'filter', source, page ); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.setItemSelected = function ( item ) { |
| 110 |
|
| 111 |
if ( this.previousPageSelected ) { |
| 112 |
this.previousPageSelected.setSelected( false ); |
| 113 |
} |
| 114 |
item.setSelected( true ); |
| 115 |
this.previousPageSelected = item; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.populateSources = function () { |
| 122 |
this.pushPending(); |
| 123 |
this.controller.fetchUnreadPagesByWiki() |
| 124 |
.then( this.populateDataFromModel.bind( this ) ) |
| 125 |
.always( this.popPending.bind( this ) ); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.populateDataFromModel = function () { |
| 132 |
const widgets = [], |
| 133 |
sourcePageModel = this.model.getSourcePagesModel(), |
| 134 |
selectedSource = sourcePageModel.getCurrentSource(), |
| 135 |
selectedPage = sourcePageModel.getCurrentPage(), |
| 136 |
sources = sourcePageModel.getSourcesArray(); |
| 137 |
|
| 138 |
for ( let i = 0; i < sources.length; i++ ) { |
| 139 |
const source = sources[ i ]; |
| 140 |
const widget = new mw.echo.ui.PageFilterWidget( |
| 141 |
sourcePageModel, |
| 142 |
source, |
| 143 |
{ |
| 144 |
title: sourcePageModel.getSourceTitle( source ), |
| 145 |
unreadCount: sourcePageModel.getSourceTotalCount( source ), |
| 146 |
initialSelection: this.previousPageSelected && this.previousPageSelected.getData() |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
widgets.push( widget ); |
| 151 |
} |
| 152 |
|
| 153 |
this.clearItems(); |
| 154 |
this.addItems( widgets ); |
| 155 |
|
| 156 |
|
| 157 |
const selectedWidget = this.findItemFromData( selectedSource ); |
| 158 |
let item; |
| 159 |
if ( selectedPage ) { |
| 160 |
|
| 161 |
item = selectedWidget.findItemFromData( selectedPage ); |
| 162 |
} else { |
| 163 |
|
| 164 |
item = selectedWidget.getTitleItem(); |
| 165 |
} |
| 166 |
this.setItemSelected( item ); |
| 167 |
}; |
| 168 |
|
| 169 |
}() ); |