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 mw
.rcfilters
.ui
.ChangesListWrapperWidget
= function MwRcfiltersUiChangesListWrapperWidget(
21 config
= $.extend( {}, config
, {
22 $element
: $changesListRoot
26 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.parent
.call( this, config
);
28 this.filtersViewModel
= filtersViewModel
;
29 this.changesListViewModel
= changesListViewModel
;
30 this.controller
= controller
;
31 this.highlightClasses
= null;
32 this.filtersModelInitialized
= false;
35 this.filtersViewModel
.connect( this, {
36 itemUpdate
: 'onItemUpdate',
37 highlightChange
: 'onHighlightChange',
38 initialize
: 'onFiltersModelInitialize'
40 this.changesListViewModel
.connect( this, {
41 invalidate
: 'onModelInvalidate',
42 update
: 'onModelUpdate',
43 newChangesExist
: 'onNewChangesExist'
47 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
48 // We handle our own display/hide of the empty results message
49 // We keep the timeout class here and remove it later, since at this
50 // stage it is still needed to identify that the timeout occurred.
51 .removeClass( 'mw-changeslist-empty' );
53 this.setupNewChangesButtonContainer();
58 OO
.inheritClass( mw
.rcfilters
.ui
.ChangesListWrapperWidget
, OO
.ui
.Widget
);
61 * Respond to filters model initialize event
63 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onFiltersModelInitialize = function () {
64 this.filtersModelInitialized
= true;
65 // Set up highlight containers. We need to wait for the filters model
66 // to be initialized, so we can make sure we have all the css class definitions
67 // we get from the server with our filters
68 this.setupHighlightContainers( this.$element
);
72 * Get all available highlight classes
74 * @return {string[]} An array of available highlight class names
76 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
77 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
78 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
79 .map( function ( filterItem
) {
80 return filterItem
.getCssClass();
84 return this.highlightClasses
;
88 * Respond to the highlight feature being toggled on and off
90 * @param {boolean} highlightEnabled
92 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
93 if ( highlightEnabled
) {
94 this.applyHighlight();
96 this.clearHighlight();
101 * Respond to a filter item model update
103 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
104 if ( this.filtersModelInitialized
&& this.filtersViewModel
.isHighlightEnabled() ) {
105 this.clearHighlight();
106 this.applyHighlight();
111 * Respond to changes list model invalidate
113 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
114 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
118 * Respond to changes list model update
120 * @param {jQuery|string} $changesListContent The content of the updated changes list
121 * @param {jQuery} $fieldset The content of the updated fieldset
122 * @param {boolean} isDatabaseTimeout Whether this is an error state due to a database query
123 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
124 * @param {boolean} from Timestamp of the new changes
126 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelUpdate = function (
127 $changesListContent
, $fieldset
, isDatabaseTimeout
, isInitialDOM
, from
129 var conflictItem
, noResultsKey
,
130 $message
= $( '<div>' )
131 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
132 isEmpty
= $changesListContent
=== 'NO_RESULTS',
133 // For enhanced mode, we have to load these modules, which are
134 // not loaded for the 'regular' mode in the backend
135 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
136 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
137 $.Deferred().resolve(),
140 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
142 this.$element
.empty();
144 if ( this.filtersViewModel
.hasConflict() ) {
145 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
151 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
153 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
154 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
157 noResultsKey
= isDatabaseTimeout
?
158 'recentchanges-timeout' :
159 'recentchanges-noresult';
164 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
165 .text( mw
.message( noResultsKey
).text() )
168 this.$element
.removeClass( 'mw-changeslist-timeout' );
171 this.$element
.append( $message
);
173 if ( !isInitialDOM
) {
174 this.$element
.empty().append( $changesListContent
);
177 this.emphasizeNewChanges( from );
181 // Set up highlight containers
182 this.setupHighlightContainers( this.$element
);
185 this.applyHighlight();
189 loaderPromise
.done( function () {
190 if ( !isInitialDOM
&& !isEmpty
) {
191 // Make sure enhanced RC re-initializes correctly
192 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
195 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
200 * Emphasize the elements (or groups) newer than the 'from' parameter
201 * @param {string} from Anything newer than this is considered 'new'
203 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
206 $newChanges
= $( [] ),
207 selector
= this.inEnhancedMode() ?
208 'table.mw-enhanced-rc[data-mw-ts]' :
210 set = this.$element
.find( selector
),
213 set.each( function ( index
) {
214 var $this = $( this ),
215 ts
= $this.data( 'mw-ts' );
218 $newChanges
= $newChanges
.add( $this );
221 // guards against putting the marker after the last element
222 if ( index
=== ( length
- 1 ) ) {
229 $indicator
= $( '<div>' )
230 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
232 $firstNew
.after( $indicator
);
241 * Respond to changes list model newChangesExist
243 * @param {boolean} newChangesExist Whether new changes exist
245 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onNewChangesExist = function ( newChangesExist
) {
246 this.showNewChangesLink
.toggle( newChangesExist
);
250 * Respond to the user clicking the 'show new changes' button
252 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onShowNewChangesClick = function () {
253 this.controller
.showNewChanges();
257 * Setup the container for the 'new changes' button.
259 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupNewChangesButtonContainer = function () {
260 this.showNewChangesLink
= new OO
.ui
.ButtonWidget( {
262 label
: mw
.message( 'rcfilters-show-new-changes' ).text(),
263 flags
: [ 'progressive' ]
265 this.showNewChangesLink
.connect( this, { click
: 'onShowNewChangesClick' } );
266 this.showNewChangesLink
.toggle( false );
268 // HACK: Add the -newChanges div inside rcfilters-head, rather than right above us
269 // Visually it's the same place, but by putting it inside rcfilters-head we are
270 // able to use the min-height rule to prevent the page from jumping when this is added.
271 this.$element
.parent().find( '.rcfilters-head' ).append(
273 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
274 .append( this.showNewChangesLink
.$element
)
279 * Set up the highlight containers with all color circle indicators.
281 * @param {jQuery|string} $content The content of the updated changes list
283 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupHighlightContainers = function ( $content
) {
284 var $enhancedTopPageCell
, $enhancedNestedPagesCell
,
286 highlightClass
= 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
287 $highlights
= $( '<div>' )
288 .addClass( highlightClass
)
291 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
292 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
293 .prop( 'data-color', 'none' )
296 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length
) {
301 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
304 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color
)
305 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
306 .prop( 'data-color', color
)
310 if ( this.inEnhancedMode() ) {
311 $enhancedTopPageCell
= $content
.find( 'table.mw-enhanced-rc.mw-collapsible' );
312 $enhancedNestedPagesCell
= $content
.find( 'td.mw-enhanced-rc-nested' );
314 // Enhanced RC highlight containers
315 $content
.find( 'table.mw-enhanced-rc tr:first-child' )
316 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
319 .append( $highlights
.clone() )
322 // We are adding and changing cells in a table that, despite having nested rows,
323 // is actually all one big table. To prevent the highlights cell in the "nested"
324 // rows from stretching out the cell with the flags and timestamp in the top row,
325 // we give the latter colspan=2. Then to make things line up again, we add
326 // an empty <td> to the "nested" rows.
328 // Set colspan=2 on cell with flags and timestamp in top row
329 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
330 .prop( 'colspan', '2' );
331 // Add empty <td> to nested rows to compensate
332 $enhancedNestedPagesCell
.parent().prepend( $( '<td>' ) );
333 // Add highlights cell to nested rows
334 $enhancedNestedPagesCell
337 .append( $highlights
.clone().addClass( 'mw-enhanced-rc-nested' ) )
340 // We need to target the nested rows differently than the top rows so that the
341 // LESS rules applies correctly. In top rows, the rule should highlight all but
342 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
343 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
344 $enhancedNestedPagesCell
346 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
348 // Go over pages that have sub results
349 // HACK: We really only can collect those by targetting the collapsible class
350 $enhancedTopPageCell
.each( function () {
351 var collectedClasses
,
354 // Go over <tr>s and pick up all recognized classes
355 collectedClasses
= widget
.getHighlightClasses().filter( function ( className
) {
356 return $table
.find( 'tr' ).hasClass( className
);
359 $table
.find( 'tr:first-child' )
360 .addClass( collectedClasses
.join( ' ' ) );
363 $content
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
366 $content
.find( 'ul.special li' )
367 .prepend( $highlights
.clone() );
372 * In enhanced mode, we need to check whether the grouped results all have the
373 * same active highlights in order to see whether the "parent" of the group should
374 * be grey or highlighted normally.
376 * This is called every time highlights are applied.
378 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
379 var activeHighlightClasses
,
380 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
382 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
383 return 'mw-rcfilters-highlight-color-' + color
;
386 // Go over top pages and their children, and figure out if all sub-pages have the
387 // same highlights between themselves. If they do, the parent should be highlighted
388 // with all colors. If classes are different, the parent should receive a grey
390 $enhancedTopPageCell
.each( function () {
391 var firstChildClasses
, $rowsWithDifferentHighlights
,
394 // Collect the relevant classes from the first nested child
395 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
396 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
398 // Filter the non-head rows and see if they all have the same classes
400 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
401 var classesInThisRow
,
404 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
405 return $this.hasClass( className
);
408 return !OO
.compare( firstChildClasses
, classesInThisRow
);
411 // If classes are different, tag the row for using grey color
412 $table
.find( 'tr:first-child' )
413 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
418 * @return {boolean} Whether the changes are grouped by page
420 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
421 var uri
= new mw
.Uri();
422 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
423 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
427 * Apply color classes based on filters highlight configuration
429 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
430 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
434 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
435 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
437 // Add highlight class to all highlighted list items
440 'mw-rcfilters-highlighted ' +
441 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
444 // Track the filters for each item in .data( 'highlightedFilters' )
445 $elements
.each( function () {
446 var filters
= $( this ).data( 'highlightedFilters' );
449 $( this ).data( 'highlightedFilters', filters
);
451 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
452 filters
.push( filterItem
.getLabel() );
456 // Apply a title to each highlighted item, with a list of filters
457 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
458 var filters
= $( this ).data( 'highlightedFilters' );
460 if ( filters
&& filters
.length
) {
461 $( this ).attr( 'title', mw
.msg(
462 'rcfilters-highlighted-filters-list',
463 filters
.join( mw
.msg( 'comma-separator' ) )
468 if ( this.inEnhancedMode() ) {
469 this.updateEnhancedParentHighlight();
472 // Turn on highlights
473 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
477 * Remove all color classes
479 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
480 // Remove highlight classes
481 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
483 .find( '.mw-rcfilters-highlight-color-' + color
)
484 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
487 this.$element
.find( '.mw-rcfilters-highlighted' )
488 .removeAttr( 'title' )
489 .removeData( 'highlightedFilters' )
490 .removeClass( 'mw-rcfilters-highlighted' );
492 // Remove grey from enhanced rows
493 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
494 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
496 // Turn off highlights
497 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );