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 $( '.rcfilters-spinner' ).removeClass( 'mw-rcfilters-ui-ready' );
113 this.$element
.removeClass( 'mw-rcfilters-ui-ready' );
117 * Respond to changes list model update
119 * @param {jQuery|string} $changesListContent The content of the updated changes list
120 * @param {jQuery} $fieldset The content of the updated fieldset
121 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
122 * @param {boolean} from Timestamp of the new changes
124 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelUpdate = function (
125 $changesListContent
, $fieldset
, isInitialDOM
, from
128 $message
= $( '<div>' )
129 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
130 isEmpty
= $changesListContent
=== 'NO_RESULTS',
131 // For enhanced mode, we have to load these modules, which are
132 // not loaded for the 'regular' mode in the backend
133 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
134 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
135 $.Deferred().resolve(),
138 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
140 this.$element
.empty();
142 if ( this.filtersViewModel
.hasConflict() ) {
143 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
148 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
149 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
151 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
152 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
158 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
159 .text( mw
.message( 'recentchanges-noresult' ).text() )
163 this.$element
.append( $message
);
165 if ( !isInitialDOM
) {
166 this.$element
.empty().append( $changesListContent
);
169 this.emphasizeNewChanges( from );
173 // Set up highlight containers
174 this.setupHighlightContainers( this.$element
);
177 this.applyHighlight();
181 loaderPromise
.done( function () {
182 if ( !isInitialDOM
&& !isEmpty
) {
183 // Make sure enhanced RC re-initializes correctly
184 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
187 $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
188 widget
.$element
.addClass( 'mw-rcfilters-ui-ready' );
193 * Emphasize the elements (or groups) newer than the 'from' parameter
194 * @param {string} from Anything newer than this is considered 'new'
196 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
199 $newChanges
= $( [] ),
200 selector
= this.inEnhancedMode() ?
201 'table.mw-enhanced-rc[data-mw-ts]' :
203 set = this.$element
.find( selector
),
206 set.each( function ( index
) {
207 var $this = $( this ),
208 ts
= $this.data( 'mw-ts' );
211 $newChanges
= $newChanges
.add( $this );
214 // guards against putting the marker after the last element
215 if ( index
=== ( length
- 1 ) ) {
222 $indicator
= $( '<div>' )
223 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
225 $firstNew
.after( $indicator
);
234 * Respond to changes list model newChangesExist
236 * @param {boolean} newChangesExist Whether new changes exist
238 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onNewChangesExist = function ( newChangesExist
) {
239 this.showNewChangesLink
.toggle( newChangesExist
);
243 * Respond to the user clicking the 'show new changes' button
245 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onShowNewChangesClick = function () {
246 this.controller
.showNewChanges();
250 * Setup the container for the 'new changes' button.
252 * @param {jQuery} $content
254 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupNewChangesButtonContainer = function ( $content
) {
255 this.showNewChangesLink
= new OO
.ui
.ButtonWidget( {
257 label
: mw
.message( 'rcfilters-show-new-changes' ).text(),
258 flags
: [ 'progressive' ]
260 this.showNewChangesLink
.connect( this, { click
: 'onShowNewChangesClick' } );
261 this.showNewChangesLink
.toggle( false );
265 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
266 .append( this.showNewChangesLink
.$element
)
271 * Set up the highlight containers with all color circle indicators.
273 * @param {jQuery|string} $content The content of the updated changes list
275 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupHighlightContainers = function ( $content
) {
276 var $enhancedTopPageCell
, $enhancedNestedPagesCell
,
278 highlightClass
= 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
279 $highlights
= $( '<div>' )
280 .addClass( highlightClass
)
283 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
284 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
285 .prop( 'data-color', 'none' )
288 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length
) {
293 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
296 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color
)
297 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
298 .prop( 'data-color', color
)
302 if ( this.inEnhancedMode() ) {
303 $enhancedTopPageCell
= $content
.find( 'table.mw-enhanced-rc.mw-collapsible' );
304 $enhancedNestedPagesCell
= $content
.find( 'td.mw-enhanced-rc-nested' );
306 // Enhanced RC highlight containers
307 $content
.find( 'table.mw-enhanced-rc tr:first-child' )
308 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
311 .append( $highlights
.clone() )
314 // We are adding and changing cells in a table that, despite having nested rows,
315 // is actually all one big table. To do that right, we want to remove the 'placeholder'
316 // cell from the top row, because we're actually adding that placeholder in the children
317 // with the highlights.
318 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-changeslist-line-prefix' )
320 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
321 .prop( 'colspan', '2' );
323 $enhancedNestedPagesCell
326 .append( $highlights
.clone().addClass( 'mw-enhanced-rc-nested' ) )
329 // We need to target the nested rows differently than the top rows so that the
330 // LESS rules applies correctly. In top rows, the rule should highlight all but
331 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
332 // should highlight all but the first 3 cells td:not( :nth-child( -n+3 )
333 $enhancedNestedPagesCell
335 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
337 // Go over pages that have sub results
338 // HACK: We really only can collect those by targetting the collapsible class
339 $enhancedTopPageCell
.each( function () {
340 var collectedClasses
,
343 // Go over <tr>s and pick up all recognized classes
344 collectedClasses
= widget
.getHighlightClasses().filter( function ( className
) {
345 return $table
.find( 'tr' ).hasClass( className
);
348 $table
.find( 'tr:first-child' )
349 .addClass( collectedClasses
.join( ' ' ) );
352 $content
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
355 $content
.find( 'ul.special li' )
356 .prepend( $highlights
.clone() );
361 * In enhanced mode, we need to check whether the grouped results all have the
362 * same active highlights in order to see whether the "parent" of the group should
363 * be grey or highlighted normally.
365 * This is called every time highlights are applied.
367 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
368 var activeHighlightClasses
,
369 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
371 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
372 return 'mw-rcfilters-highlight-color-' + color
;
375 // Go over top pages and their children, and figure out if all sub-pages have the
376 // same highlights between themselves. If they do, the parent should be highlighted
377 // with all colors. If classes are different, the parent should receive a grey
379 $enhancedTopPageCell
.each( function () {
380 var firstChildClasses
, $rowsWithDifferentHighlights
,
383 // Collect the relevant classes from the first nested child
384 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
385 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
387 // Filter the non-head rows and see if they all have the same classes
389 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
390 var classesInThisRow
,
393 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
394 return $this.hasClass( className
);
397 return !OO
.compare( firstChildClasses
, classesInThisRow
);
400 // If classes are different, tag the row for using grey color
401 $table
.find( 'tr:first-child' )
402 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
407 * @return {boolean} Whether the changes are grouped by page
409 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
410 var uri
= new mw
.Uri();
411 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
412 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
416 * Apply color classes based on filters highlight configuration
418 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
419 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
423 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
424 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
426 // Add highlight class to all highlighted list items
428 .addClass( 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor() );
430 $elements
.each( function () {
431 var filterString
= $( this ).attr( 'data-highlightedFilters' ) || '',
432 filters
= filterString
? filterString
.split( '|' ) : [];
434 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
435 filters
.push( filterItem
.getLabel() );
439 .attr( 'data-highlightedFilters', filters
.join( '|' ) );
442 // Apply a title for relevant filters
443 this.$element
.find( '[data-highlightedFilters]' ).each( function () {
444 var filterString
= $( this ).attr( 'data-highlightedFilters' ) || '',
445 filters
= filterString
? filterString
.split( '|' ) : [];
447 if ( filterString
) {
448 $( this ).attr( 'title', mw
.msg( 'rcfilters-highlighted-filters-list', filters
.join( ', ' ) ) );
452 if ( this.inEnhancedMode() ) {
453 this.updateEnhancedParentHighlight();
456 // Turn on highlights
457 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
461 * Remove all color classes
463 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
464 // Remove highlight classes
465 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
466 this.$element
.find( '.mw-rcfilters-highlight-color-' + color
).removeClass( 'mw-rcfilters-highlight-color-' + color
);
469 this.$element
.find( '[data-highlightedFilters]' )
470 .removeAttr( 'title' )
471 .removeAttr( 'data-highlightedFilters' );
473 // Remove grey from enhanced rows
474 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
475 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
477 // Turn off highlights
478 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );