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 .removeClass( 'mw-changeslist-empty' );
51 this.setupNewChangesButtonContainer( this.$element
);
56 OO
.inheritClass( mw
.rcfilters
.ui
.ChangesListWrapperWidget
, OO
.ui
.Widget
);
59 * Respond to filters model initialize event
61 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onFiltersModelInitialize = function () {
62 this.filtersModelInitialized
= true;
63 // Set up highlight containers. We need to wait for the filters model
64 // to be initialized, so we can make sure we have all the css class definitions
65 // we get from the server with our filters
66 this.setupHighlightContainers( this.$element
);
70 * Get all available highlight classes
72 * @return {string[]} An array of available highlight class names
74 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
75 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
76 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
77 .map( function ( filterItem
) {
78 return filterItem
.getCssClass();
82 return this.highlightClasses
;
86 * Respond to the highlight feature being toggled on and off
88 * @param {boolean} highlightEnabled
90 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
91 if ( highlightEnabled
) {
92 this.applyHighlight();
94 this.clearHighlight();
99 * Respond to a filter item model update
101 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
102 if ( this.filtersModelInitialized
&& this.filtersViewModel
.isHighlightEnabled() ) {
103 this.clearHighlight();
104 this.applyHighlight();
109 * Respond to changes list model invalidate
111 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
112 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
116 * Respond to changes list model update
118 * @param {jQuery|string} $changesListContent The content of the updated changes list
119 * @param {jQuery} $fieldset The content of the updated fieldset
120 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
121 * @param {boolean} from Timestamp of the new changes
123 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelUpdate = function (
124 $changesListContent
, $fieldset
, isInitialDOM
, from
127 $message
= $( '<div>' )
128 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
129 isEmpty
= $changesListContent
=== 'NO_RESULTS',
130 // For enhanced mode, we have to load these modules, which are
131 // not loaded for the 'regular' mode in the backend
132 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
133 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
134 $.Deferred().resolve(),
137 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
139 this.$element
.empty();
141 if ( this.filtersViewModel
.hasConflict() ) {
142 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
147 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
148 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
151 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
157 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
158 .text( mw
.message( 'recentchanges-noresult' ).text() )
162 this.$element
.append( $message
);
164 if ( !isInitialDOM
) {
165 this.$element
.empty().append( $changesListContent
);
168 this.emphasizeNewChanges( from );
172 // Set up highlight containers
173 this.setupHighlightContainers( this.$element
);
176 this.applyHighlight();
180 loaderPromise
.done( function () {
181 if ( !isInitialDOM
&& !isEmpty
) {
182 // Make sure enhanced RC re-initializes correctly
183 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
186 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
191 * Emphasize the elements (or groups) newer than the 'from' parameter
192 * @param {string} from Anything newer than this is considered 'new'
194 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
197 $newChanges
= $( [] ),
198 selector
= this.inEnhancedMode() ?
199 'table.mw-enhanced-rc[data-mw-ts]' :
201 set = this.$element
.find( selector
),
204 set.each( function ( index
) {
205 var $this = $( this ),
206 ts
= $this.data( 'mw-ts' );
209 $newChanges
= $newChanges
.add( $this );
212 // guards against putting the marker after the last element
213 if ( index
=== ( length
- 1 ) ) {
220 $indicator
= $( '<div>' )
221 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
223 $firstNew
.after( $indicator
);
232 * Respond to changes list model newChangesExist
234 * @param {boolean} newChangesExist Whether new changes exist
236 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onNewChangesExist = function ( newChangesExist
) {
237 this.showNewChangesLink
.toggle( newChangesExist
);
241 * Respond to the user clicking the 'show new changes' button
243 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onShowNewChangesClick = function () {
244 this.controller
.showNewChanges();
248 * Setup the container for the 'new changes' button.
250 * @param {jQuery} $content
252 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupNewChangesButtonContainer = function ( $content
) {
253 this.showNewChangesLink
= new OO
.ui
.ButtonWidget( {
255 label
: mw
.message( 'rcfilters-show-new-changes' ).text(),
256 flags
: [ 'progressive' ]
258 this.showNewChangesLink
.connect( this, { click
: 'onShowNewChangesClick' } );
259 this.showNewChangesLink
.toggle( false );
263 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
264 .append( this.showNewChangesLink
.$element
)
269 * Set up the highlight containers with all color circle indicators.
271 * @param {jQuery|string} $content The content of the updated changes list
273 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupHighlightContainers = function ( $content
) {
274 var $enhancedTopPageCell
, $enhancedNestedPagesCell
,
276 highlightClass
= 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
277 $highlights
= $( '<div>' )
278 .addClass( highlightClass
)
281 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
282 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
283 .prop( 'data-color', 'none' )
286 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length
) {
291 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
294 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color
)
295 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
296 .prop( 'data-color', color
)
300 if ( this.inEnhancedMode() ) {
301 $enhancedTopPageCell
= $content
.find( 'table.mw-enhanced-rc.mw-collapsible' );
302 $enhancedNestedPagesCell
= $content
.find( 'td.mw-enhanced-rc-nested' );
304 // Enhanced RC highlight containers
305 $content
.find( 'table.mw-enhanced-rc tr:first-child' )
306 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
309 .append( $highlights
.clone() )
312 // We are adding and changing cells in a table that, despite having nested rows,
313 // is actually all one big table. To do that right, we want to remove the 'placeholder'
314 // cell from the top row, because we're actually adding that placeholder in the children
315 // with the highlights.
316 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-changeslist-line-prefix' )
318 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
319 .prop( 'colspan', '2' );
321 $enhancedNestedPagesCell
324 .append( $highlights
.clone().addClass( 'mw-enhanced-rc-nested' ) )
327 // We need to target the nested rows differently than the top rows so that the
328 // LESS rules applies correctly. In top rows, the rule should highlight all but
329 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
330 // should highlight all but the first 3 cells td:not( :nth-child( -n+3 )
331 $enhancedNestedPagesCell
333 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
335 // Go over pages that have sub results
336 // HACK: We really only can collect those by targetting the collapsible class
337 $enhancedTopPageCell
.each( function () {
338 var collectedClasses
,
341 // Go over <tr>s and pick up all recognized classes
342 collectedClasses
= widget
.getHighlightClasses().filter( function ( className
) {
343 return $table
.find( 'tr' ).hasClass( className
);
346 $table
.find( 'tr:first-child' )
347 .addClass( collectedClasses
.join( ' ' ) );
350 $content
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
353 $content
.find( 'ul.special li' )
354 .prepend( $highlights
.clone() );
359 * In enhanced mode, we need to check whether the grouped results all have the
360 * same active highlights in order to see whether the "parent" of the group should
361 * be grey or highlighted normally.
363 * This is called every time highlights are applied.
365 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
366 var activeHighlightClasses
,
367 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
369 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
370 return 'mw-rcfilters-highlight-color-' + color
;
373 // Go over top pages and their children, and figure out if all sub-pages have the
374 // same highlights between themselves. If they do, the parent should be highlighted
375 // with all colors. If classes are different, the parent should receive a grey
377 $enhancedTopPageCell
.each( function () {
378 var firstChildClasses
, $rowsWithDifferentHighlights
,
381 // Collect the relevant classes from the first nested child
382 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
383 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
385 // Filter the non-head rows and see if they all have the same classes
387 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
388 var classesInThisRow
,
391 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
392 return $this.hasClass( className
);
395 return !OO
.compare( firstChildClasses
, classesInThisRow
);
398 // If classes are different, tag the row for using grey color
399 $table
.find( 'tr:first-child' )
400 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
405 * @return {boolean} Whether the changes are grouped by page
407 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
408 var uri
= new mw
.Uri();
409 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
410 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
414 * Apply color classes based on filters highlight configuration
416 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
417 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
421 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
422 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
424 // Add highlight class to all highlighted list items
426 .addClass( 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor() );
428 $elements
.each( function () {
429 var filterString
= $( this ).attr( 'data-highlightedFilters' ) || '',
430 filters
= filterString
? filterString
.split( '|' ) : [];
432 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
433 filters
.push( filterItem
.getLabel() );
437 .attr( 'data-highlightedFilters', filters
.join( '|' ) );
440 // Apply a title for relevant filters
441 this.$element
.find( '[data-highlightedFilters]' ).each( function () {
442 var filterString
= $( this ).attr( 'data-highlightedFilters' ) || '',
443 filters
= filterString
? filterString
.split( '|' ) : [];
445 if ( filterString
) {
446 $( this ).attr( 'title', mw
.msg( 'rcfilters-highlighted-filters-list', filters
.join( ', ' ) ) );
450 if ( this.inEnhancedMode() ) {
451 this.updateEnhancedParentHighlight();
454 // Turn on highlights
455 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
459 * Remove all color classes
461 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
462 // Remove highlight classes
463 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
464 this.$element
.find( '.mw-rcfilters-highlight-color-' + color
).removeClass( 'mw-rcfilters-highlight-color-' + color
);
467 this.$element
.find( '[data-highlightedFilters]' )
468 .removeAttr( 'title' )
469 .removeAttr( 'data-highlightedFilters' );
471 // Remove grey from enhanced rows
472 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
473 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
475 // Turn off highlights
476 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );