4 * @class mw.rcfilters.ui.ChangesListWrapperWidget
5 * @extends OO.ui.Widget
8 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel View model
9 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListViewModel View model
10 * @param {mw.rcfilters.Controller} controller
11 * @param {jQuery} $changesListRoot Root element of the changes list to attach to
12 * @param {Object} [config] Configuration object
14 var ChangesListWrapperWidget
= function MwRcfiltersUiChangesListWrapperWidget(
21 config
= $.extend( {}, config
, {
22 $element
: $changesListRoot
26 ChangesListWrapperWidget
.parent
.call( this, config
);
28 this.filtersViewModel
= filtersViewModel
;
29 this.changesListViewModel
= changesListViewModel
;
30 this.controller
= controller
;
33 this.filtersViewModel
.connect( this, {
34 itemUpdate
: 'onItemUpdate',
35 highlightChange
: 'onHighlightChange'
37 this.changesListViewModel
.connect( this, {
38 invalidate
: 'onModelInvalidate',
39 update
: 'onModelUpdate'
43 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
44 // We handle our own display/hide of the empty results message
45 // We keep the timeout class here and remove it later, since at this
46 // stage it is still needed to identify that the timeout occurred.
47 .removeClass( 'mw-changeslist-empty' );
52 OO
.inheritClass( ChangesListWrapperWidget
, OO
.ui
.Widget
);
55 * Respond to the highlight feature being toggled on and off
57 * @param {boolean} highlightEnabled
59 ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
60 if ( highlightEnabled
) {
61 this.applyHighlight();
63 this.clearHighlight();
68 * Respond to a filter item model update
70 ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
71 if ( this.controller
.isInitialized() && this.filtersViewModel
.isHighlightEnabled() ) {
72 // this.controller.isInitialized() is still false during page load,
73 // we don't want to clear/apply highlights at this stage.
74 this.clearHighlight();
75 this.applyHighlight();
80 * Respond to changes list model invalidate
82 ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
83 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
87 * Respond to changes list model update
89 * @param {jQuery|string} $changesListContent The content of the updated changes list
90 * @param {jQuery} $fieldset The content of the updated fieldset
91 * @param {string} noResultsDetails Type of no result error
92 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
93 * @param {boolean} from Timestamp of the new changes
95 ChangesListWrapperWidget
.prototype.onModelUpdate = function (
96 $changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, from
99 $message
= $( '<div>' )
100 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
101 isEmpty
= $changesListContent
=== 'NO_RESULTS',
102 // For enhanced mode, we have to load these modules, which are
103 // not loaded for the 'regular' mode in the backend
104 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
105 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
106 $.Deferred().resolve(),
109 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
111 this.$element
.empty();
113 if ( this.filtersViewModel
.hasConflict() ) {
114 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
119 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
120 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
122 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
123 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
129 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
130 .text( mw
.msg( this.getMsgKeyForNoResults( noResultsDetails
) ) )
133 // remove all classes matching mw-changeslist-*
134 this.$element
.removeClass( function ( elementIndex
, allClasses
) {
137 .filter( function ( className
) {
138 return className
.indexOf( 'mw-changeslist-' ) === 0;
144 this.$element
.append( $message
);
146 if ( !isInitialDOM
) {
147 this.$element
.empty().append( $changesListContent
);
150 this.emphasizeNewChanges( from );
155 this.applyHighlight();
159 this.$element
.prepend( $( '<div>' ).addClass( 'mw-changeslist-overlay' ) );
161 loaderPromise
.done( function () {
162 if ( !isInitialDOM
&& !isEmpty
) {
163 // Make sure enhanced RC re-initializes correctly
164 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
167 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
171 /** Toggles overlay class on changes list
173 * @param {boolean} isVisible True if overlay should be visible
175 ChangesListWrapperWidget
.prototype.toggleOverlay = function ( isVisible
) {
176 this.$element
.toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget--overlaid', isVisible
);
180 * Map a reason for having no results to its message key
182 * @param {string} reason One of the NO_RESULTS_* "constant" that represent
183 * a reason for having no results
184 * @return {string} Key for the message that explains why there is no results in this case
186 ChangesListWrapperWidget
.prototype.getMsgKeyForNoResults = function ( reason
) {
187 var reasonMsgKeyMap
= {
188 NO_RESULTS_NORMAL
: 'recentchanges-noresult',
189 NO_RESULTS_TIMEOUT
: 'recentchanges-timeout',
190 NO_RESULTS_NETWORK_ERROR
: 'recentchanges-network',
191 NO_RESULTS_NO_TARGET_PAGE
: 'recentchanges-notargetpage',
192 NO_RESULTS_INVALID_TARGET_PAGE
: 'allpagesbadtitle'
194 return reasonMsgKeyMap
[ reason
];
198 * Emphasize the elements (or groups) newer than the 'from' parameter
199 * @param {string} from Anything newer than this is considered 'new'
201 ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
204 $newChanges
= $( [] ),
205 selector
= this.inEnhancedMode() ?
206 'table.mw-enhanced-rc[data-mw-ts]' :
208 set = this.$element
.find( selector
),
211 set.each( function ( index
) {
212 var $this = $( this ),
213 ts
= $this.data( 'mw-ts' );
216 $newChanges
= $newChanges
.add( $this );
219 // guards against putting the marker after the last element
220 if ( index
=== ( length
- 1 ) ) {
227 $indicator
= $( '<div>' )
228 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
230 $firstNew
.after( $indicator
);
233 // FIXME: Use CSS transition
234 // eslint-disable-next-line no-jquery/no-fade
241 * In enhanced mode, we need to check whether the grouped results all have the
242 * same active highlights in order to see whether the "parent" of the group should
243 * be grey or highlighted normally.
245 * This is called every time highlights are applied.
247 ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
248 var activeHighlightClasses
,
249 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
251 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
252 return 'mw-rcfilters-highlight-color-' + color
;
255 // Go over top pages and their children, and figure out if all sub-pages have the
256 // same highlights between themselves. If they do, the parent should be highlighted
257 // with all colors. If classes are different, the parent should receive a grey
259 $enhancedTopPageCell
.each( function () {
260 var firstChildClasses
, $rowsWithDifferentHighlights
,
263 // Collect the relevant classes from the first nested child
264 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
265 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
267 // Filter the non-head rows and see if they all have the same classes
269 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
270 var classesInThisRow
,
273 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
274 return $this.hasClass( className
);
277 return !OO
.compare( firstChildClasses
, classesInThisRow
);
280 // If classes are different, tag the row for using grey color
281 $table
.find( 'tr:first-child' )
282 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
287 * @return {boolean} Whether the changes are grouped by page
289 ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
290 var uri
= new mw
.Uri();
291 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
292 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
296 * Apply color classes based on filters highlight configuration
298 ChangesListWrapperWidget
.prototype.applyHighlight = function () {
299 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
303 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
304 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
306 // Add highlight class to all highlighted list items
309 'mw-rcfilters-highlighted ' +
310 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
313 // Track the filters for each item in .data( 'highlightedFilters' )
314 $elements
.each( function () {
315 var filters
= $( this ).data( 'highlightedFilters' );
318 $( this ).data( 'highlightedFilters', filters
);
320 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
321 filters
.push( filterItem
.getLabel() );
325 // Apply a title to each highlighted item, with a list of filters
326 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
327 var filters
= $( this ).data( 'highlightedFilters' );
329 if ( filters
&& filters
.length
) {
330 $( this ).attr( 'title', mw
.msg(
331 'rcfilters-highlighted-filters-list',
332 filters
.join( mw
.msg( 'comma-separator' ) )
337 if ( this.inEnhancedMode() ) {
338 this.updateEnhancedParentHighlight();
341 // Turn on highlights
342 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
346 * Remove all color classes
348 ChangesListWrapperWidget
.prototype.clearHighlight = function () {
349 // Remove highlight classes
350 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
352 .find( '.mw-rcfilters-highlight-color-' + color
)
353 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
356 this.$element
.find( '.mw-rcfilters-highlighted' )
357 .removeAttr( 'title' )
358 .removeData( 'highlightedFilters' )
359 .removeClass( 'mw-rcfilters-highlighted' );
361 // Remove grey from enhanced rows
362 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
363 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
365 // Turn off highlights
366 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
369 module
.exports
= ChangesListWrapperWidget
;