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'
46 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
47 // We handle our own display/hide of the empty results message
48 // We keep the timeout class here and remove it later, since at this
49 // stage it is still needed to identify that the timeout occurred.
50 .removeClass( 'mw-changeslist-empty' );
55 OO
.inheritClass( mw
.rcfilters
.ui
.ChangesListWrapperWidget
, OO
.ui
.Widget
);
58 * Respond to filters model initialize event
60 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onFiltersModelInitialize = function () {
61 this.filtersModelInitialized
= true;
62 // Set up highlight containers. We need to wait for the filters model
63 // to be initialized, so we can make sure we have all the css class definitions
64 // we get from the server with our filters
65 this.setupHighlightContainers( this.$element
);
69 * Get all available highlight classes
71 * @return {string[]} An array of available highlight class names
73 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
74 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
75 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
76 .map( function ( filterItem
) {
77 return filterItem
.getCssClass();
81 return this.highlightClasses
;
85 * Respond to the highlight feature being toggled on and off
87 * @param {boolean} highlightEnabled
89 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
90 if ( highlightEnabled
) {
91 this.applyHighlight();
93 this.clearHighlight();
98 * Respond to a filter item model update
100 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
101 if ( this.filtersModelInitialized
&& this.filtersViewModel
.isHighlightEnabled() ) {
102 this.clearHighlight();
103 this.applyHighlight();
108 * Respond to changes list model invalidate
110 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
111 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
115 * Respond to changes list model update
117 * @param {jQuery|string} $changesListContent The content of the updated changes list
118 * @param {jQuery} $fieldset The content of the updated fieldset
119 * @param {string} noResultsDetails Type of no result error
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
, noResultsDetails
, 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
.msg( this.getMsgKeyForNoResults( noResultsDetails
) ) )
161 this.$element
.removeClass( 'mw-changeslist-timeout' );
162 this.$element
.removeClass( 'mw-changeslist-notargetpage' );
165 this.$element
.append( $message
);
167 if ( !isInitialDOM
) {
168 this.$element
.empty().append( $changesListContent
);
171 this.emphasizeNewChanges( from );
175 // Set up highlight containers
176 this.setupHighlightContainers( this.$element
);
179 this.applyHighlight();
183 loaderPromise
.done( function () {
184 if ( !isInitialDOM
&& !isEmpty
) {
185 // Make sure enhanced RC re-initializes correctly
186 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
189 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
194 * Map a reason for having no results to its message key
196 * @param {string} reason One of the NO_RESULTS_* "constant" that represent
197 * a reason for having no results
198 * @return {string} Key for the message that explains why there is no results in this case
200 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getMsgKeyForNoResults = function ( reason
) {
201 var reasonMsgKeyMap
= {
202 NO_RESULTS_NORMAL
: 'recentchanges-noresult',
203 NO_RESULTS_TIMEOUT
: 'recentchanges-timeout',
204 NO_RESULTS_NETWORK_ERROR
: 'recentchanges-network',
205 NO_RESULTS_NO_TARGET_PAGE
: 'recentchanges-notargetpage'
207 return reasonMsgKeyMap
[ reason
];
210 * Emphasize the elements (or groups) newer than the 'from' parameter
211 * @param {string} from Anything newer than this is considered 'new'
213 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
216 $newChanges
= $( [] ),
217 selector
= this.inEnhancedMode() ?
218 'table.mw-enhanced-rc[data-mw-ts]' :
220 set = this.$element
.find( selector
),
223 set.each( function ( index
) {
224 var $this = $( this ),
225 ts
= $this.data( 'mw-ts' );
228 $newChanges
= $newChanges
.add( $this );
231 // guards against putting the marker after the last element
232 if ( index
=== ( length
- 1 ) ) {
239 $indicator
= $( '<div>' )
240 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
242 $firstNew
.after( $indicator
);
251 * Set up the highlight containers with all color circle indicators.
253 * @param {jQuery|string} $content The content of the updated changes list
255 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupHighlightContainers = function ( $content
) {
256 var $enhancedTopPageCell
, $enhancedNestedPagesCell
,
258 highlightClass
= 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
259 $highlights
= $( '<div>' )
260 .addClass( highlightClass
)
263 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
264 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
265 .prop( 'data-color', 'none' )
268 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length
) {
273 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
276 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color
)
277 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
278 .prop( 'data-color', color
)
282 if ( this.inEnhancedMode() ) {
283 $enhancedTopPageCell
= $content
.find( 'table.mw-enhanced-rc.mw-collapsible' );
284 $enhancedNestedPagesCell
= $content
.find( 'td.mw-enhanced-rc-nested' );
286 // Enhanced RC highlight containers
287 $content
.find( 'table.mw-enhanced-rc tr:first-child' )
288 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
291 .append( $highlights
.clone() )
294 // We are adding and changing cells in a table that, despite having nested rows,
295 // is actually all one big table. To prevent the highlights cell in the "nested"
296 // rows from stretching out the cell with the flags and timestamp in the top row,
297 // we give the latter colspan=2. Then to make things line up again, we add
298 // an empty <td> to the "nested" rows.
300 // Set colspan=2 on cell with flags and timestamp in top row
301 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
302 .prop( 'colspan', '2' );
303 // Add empty <td> to nested rows to compensate
304 $enhancedNestedPagesCell
.parent().prepend( $( '<td>' ) );
305 // Add highlights cell to nested rows
306 $enhancedNestedPagesCell
309 .append( $highlights
.clone().addClass( 'mw-enhanced-rc-nested' ) )
312 // We need to target the nested rows differently than the top rows so that the
313 // LESS rules applies correctly. In top rows, the rule should highlight all but
314 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
315 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
316 $enhancedNestedPagesCell
318 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
320 // Go over pages that have sub results
321 // HACK: We really only can collect those by targetting the collapsible class
322 $enhancedTopPageCell
.each( function () {
323 var collectedClasses
,
326 // Go over <tr>s and pick up all recognized classes
327 collectedClasses
= widget
.getHighlightClasses().filter( function ( className
) {
328 return $table
.find( 'tr' ).hasClass( className
);
331 $table
.find( 'tr:first-child' )
332 .addClass( collectedClasses
.join( ' ' ) );
335 $content
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
338 $content
.find( 'ul.special li' )
339 .prepend( $highlights
.clone() );
341 $content
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
346 * In enhanced mode, we need to check whether the grouped results all have the
347 * same active highlights in order to see whether the "parent" of the group should
348 * be grey or highlighted normally.
350 * This is called every time highlights are applied.
352 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
353 var activeHighlightClasses
,
354 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
356 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
357 return 'mw-rcfilters-highlight-color-' + color
;
360 // Go over top pages and their children, and figure out if all sub-pages have the
361 // same highlights between themselves. If they do, the parent should be highlighted
362 // with all colors. If classes are different, the parent should receive a grey
364 $enhancedTopPageCell
.each( function () {
365 var firstChildClasses
, $rowsWithDifferentHighlights
,
368 // Collect the relevant classes from the first nested child
369 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
370 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
372 // Filter the non-head rows and see if they all have the same classes
374 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
375 var classesInThisRow
,
378 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
379 return $this.hasClass( className
);
382 return !OO
.compare( firstChildClasses
, classesInThisRow
);
385 // If classes are different, tag the row for using grey color
386 $table
.find( 'tr:first-child' )
387 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
392 * @return {boolean} Whether the changes are grouped by page
394 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
395 var uri
= new mw
.Uri();
396 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
397 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
401 * Apply color classes based on filters highlight configuration
403 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
404 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
408 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
409 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
411 // Add highlight class to all highlighted list items
414 'mw-rcfilters-highlighted ' +
415 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
418 // Track the filters for each item in .data( 'highlightedFilters' )
419 $elements
.each( function () {
420 var filters
= $( this ).data( 'highlightedFilters' );
423 $( this ).data( 'highlightedFilters', filters
);
425 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
426 filters
.push( filterItem
.getLabel() );
430 // Apply a title to each highlighted item, with a list of filters
431 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
432 var filters
= $( this ).data( 'highlightedFilters' );
434 if ( filters
&& filters
.length
) {
435 $( this ).attr( 'title', mw
.msg(
436 'rcfilters-highlighted-filters-list',
437 filters
.join( mw
.msg( 'comma-separator' ) )
442 if ( this.inEnhancedMode() ) {
443 this.updateEnhancedParentHighlight();
446 // Turn on highlights
447 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
451 * Remove all color classes
453 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
454 // Remove highlight classes
455 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
457 .find( '.mw-rcfilters-highlight-color-' + color
)
458 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
461 this.$element
.find( '.mw-rcfilters-highlighted' )
462 .removeAttr( 'title' )
463 .removeData( 'highlightedFilters' )
464 .removeClass( 'mw-rcfilters-highlighted' );
466 // Remove grey from enhanced rows
467 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
468 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
470 // Turn off highlights
471 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );