| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
mw.echo.ui.PageFilterWidget = function MwEchoUiPageFilterWidget( filterModel, source, config ) { |
| 18 |
config = config || {}; |
| 19 |
|
| 20 |
|
| 21 |
mw.echo.ui.PageFilterWidget.super.call( this, config ); |
| 22 |
|
| 23 |
this.model = filterModel; |
| 24 |
this.source = source; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
this.data = this.source; |
| 29 |
this.totalCount = config.unreadCount || this.model.getSourceTotalCount( this.source ); |
| 30 |
|
| 31 |
this.initialSelection = config.initialSelection; |
| 32 |
|
| 33 |
|
| 34 |
this.title = new mw.echo.ui.PageNotificationsOptionWidget( { |
| 35 |
label: config.title, |
| 36 |
title: config.title, |
| 37 |
count: this.totalCount, |
| 38 |
isCapped: true, |
| 39 |
data: null, |
| 40 |
classes: [ 'mw-echo-ui-pageFilterWidget-title' ] |
| 41 |
} ); |
| 42 |
|
| 43 |
|
| 44 |
this.populateDataFromModel(); |
| 45 |
this.$element |
| 46 |
.addClass( 'mw-echo-ui-pageFilterWidget' ) |
| 47 |
.attr( 'aria-label', mw.msg( 'echo-specialpage-pagefilterwidget-aria-label' ) ); |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
OO.inheritClass( mw.echo.ui.PageFilterWidget, OO.ui.SelectWidget ); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
mw.echo.ui.PageFilterWidget.prototype.getTotalCount = function () { |
| 60 |
return this.totalCount; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
mw.echo.ui.PageFilterWidget.prototype.populateDataFromModel = function () { |
| 67 |
const optionWidgets = [], |
| 68 |
sourcePages = this.model.getSourcePages( this.source ); |
| 69 |
|
| 70 |
for ( const title in sourcePages ) { |
| 71 |
const isUserPage = sourcePages[ title ].ns === mw.config.get( 'wgNamespaceIds' ).user; |
| 72 |
const widget = new mw.echo.ui.PageNotificationsOptionWidget( { |
| 73 |
label: isUserPage ? sourcePages[ title ].unprefixed : title, |
| 74 |
title: isUserPage ? sourcePages[ title ].unprefixed : title, |
| 75 |
icon: isUserPage ? 'userAvatar' : 'article', |
| 76 |
count: sourcePages[ title ].count, |
| 77 |
data: title, |
| 78 |
classes: [ 'mw-echo-ui-pageFilterWidget-page' ] |
| 79 |
} ); |
| 80 |
optionWidgets.push( widget ); |
| 81 |
|
| 82 |
if ( this.initialSelection === title ) { |
| 83 |
widget.setSelected( true ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
this.setItems( optionWidgets ); |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
mw.echo.ui.PageFilterWidget.prototype.getSource = function () { |
| 96 |
return this.source; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
mw.echo.ui.PageFilterWidget.prototype.getTitleItem = function () { |
| 105 |
return this.title; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
mw.echo.ui.PageFilterWidget.prototype.setItems = function ( items ) { |
| 114 |
this.clearItems(); |
| 115 |
for ( let i = 0; i < items.length; i++ ) { |
| 116 |
const index = this.findInsertionIndex( items[ i ] ); |
| 117 |
this.addItems( [ items[ i ] ], index ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
this.addItems( [ this.title ], 0 ); |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
mw.echo.ui.PageFilterWidget.prototype.findInsertionIndex = function ( item ) { |
| 132 |
return OO.binarySearch( |
| 133 |
this.items, |
| 134 |
( otherItem ) => this.sortingFunction( item, otherItem ), |
| 135 |
true |
| 136 |
); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
mw.echo.ui.PageFilterWidget.prototype.sortingFunction = function ( item, otherItem ) { |
| 148 |
return Number( otherItem.getCount() ) - Number( item.getCount() ); |
| 149 |
}; |
| 150 |
}() ); |