5 * @class mw.rcfilters.ui.ChangesListWrapperWidget
6 * @extends OO.ui.Widget
9 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel View model
10 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListViewModel View model
11 * @param {mw.rcfilters.Controller} controller
12 * @param {jQuery} $changesListRoot Root element of the changes list to attach to
13 * @param {Object} [config] Configuration object
15 var ChangesListWrapperWidget
= function MwRcfiltersUiChangesListWrapperWidget(
22 config
= $.extend( {}, config
, {
23 $element
: $changesListRoot
27 ChangesListWrapperWidget
.parent
.call( this, config
);
29 this.filtersViewModel
= filtersViewModel
;
30 this.changesListViewModel
= changesListViewModel
;
31 this.controller
= controller
;
32 this.highlightClasses
= null;
35 this.filtersViewModel
.connect( this, {
36 itemUpdate
: 'onItemUpdate',
37 highlightChange
: 'onHighlightChange'
39 this.changesListViewModel
.connect( this, {
40 invalidate
: 'onModelInvalidate',
41 update
: 'onModelUpdate'
45 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
46 // We handle our own display/hide of the empty results message
47 // We keep the timeout class here and remove it later, since at this
48 // stage it is still needed to identify that the timeout occurred.
49 .removeClass( 'mw-changeslist-empty' );
54 OO
.inheritClass( ChangesListWrapperWidget
, OO
.ui
.Widget
);
57 * Get all available highlight classes
59 * @return {string[]} An array of available highlight class names
61 ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
62 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
63 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
64 .map( function ( filterItem
) {
65 return filterItem
.getCssClass();
69 return this.highlightClasses
;
73 * Respond to the highlight feature being toggled on and off
75 * @param {boolean} highlightEnabled
77 ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
78 if ( highlightEnabled
) {
79 this.applyHighlight();
81 this.clearHighlight();
86 * Respond to a filter item model update
88 ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
89 if ( this.controller
.isInitialized() && this.filtersViewModel
.isHighlightEnabled() ) {
90 // this.controller.isInitialized() is still false during page load,
91 // we don't want to clear/apply highlights at this stage.
92 this.clearHighlight();
93 this.applyHighlight();
98 * Respond to changes list model invalidate
100 ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
101 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
105 * Respond to changes list model update
107 * @param {jQuery|string} $changesListContent The content of the updated changes list
108 * @param {jQuery} $fieldset The content of the updated fieldset
109 * @param {string} noResultsDetails Type of no result error
110 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
111 * @param {boolean} from Timestamp of the new changes
113 ChangesListWrapperWidget
.prototype.onModelUpdate = function (
114 $changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, from
117 $message
= $( '<div>' )
118 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
119 isEmpty
= $changesListContent
=== 'NO_RESULTS',
120 // For enhanced mode, we have to load these modules, which are
121 // not loaded for the 'regular' mode in the backend
122 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
123 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
124 $.Deferred().resolve(),
127 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
129 this.$element
.empty();
131 if ( this.filtersViewModel
.hasConflict() ) {
132 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
137 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
138 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
140 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
141 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
147 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
148 .text( mw
.msg( this.getMsgKeyForNoResults( noResultsDetails
) ) )
151 // remove all classes matching mw-changeslist-*
152 this.$element
.removeClass( function ( elementIndex
, allClasses
) {
155 .filter( function ( className
) {
156 return className
.indexOf( 'mw-changeslist-' ) === 0;
162 this.$element
.append( $message
);
164 if ( !isInitialDOM
) {
165 this.$element
.empty().append( $changesListContent
);
168 this.emphasizeNewChanges( from );
173 this.applyHighlight();
177 this.$element
.prepend( $( '<div>' ).addClass( 'mw-changeslist-overlay' ) );
179 loaderPromise
.done( function () {
180 if ( !isInitialDOM
&& !isEmpty
) {
181 // Make sure enhanced RC re-initializes correctly
182 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
185 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
189 /** Toggles overlay class on changes list
191 * @param {boolean} isVisible True if overlay should be visible
193 ChangesListWrapperWidget
.prototype.toggleOverlay = function ( isVisible
) {
194 this.$element
.toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget--overlaid', isVisible
);
198 * Map a reason for having no results to its message key
200 * @param {string} reason One of the NO_RESULTS_* "constant" that represent
201 * a reason for having no results
202 * @return {string} Key for the message that explains why there is no results in this case
204 ChangesListWrapperWidget
.prototype.getMsgKeyForNoResults = function ( reason
) {
205 var reasonMsgKeyMap
= {
206 NO_RESULTS_NORMAL
: 'recentchanges-noresult',
207 NO_RESULTS_TIMEOUT
: 'recentchanges-timeout',
208 NO_RESULTS_NETWORK_ERROR
: 'recentchanges-network',
209 NO_RESULTS_NO_TARGET_PAGE
: 'recentchanges-notargetpage',
210 NO_RESULTS_INVALID_TARGET_PAGE
: 'allpagesbadtitle'
212 return reasonMsgKeyMap
[ reason
];
216 * Emphasize the elements (or groups) newer than the 'from' parameter
217 * @param {string} from Anything newer than this is considered 'new'
219 ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
222 $newChanges
= $( [] ),
223 selector
= this.inEnhancedMode() ?
224 'table.mw-enhanced-rc[data-mw-ts]' :
226 set = this.$element
.find( selector
),
229 set.each( function ( index
) {
230 var $this = $( this ),
231 ts
= $this.data( 'mw-ts' );
234 $newChanges
= $newChanges
.add( $this );
237 // guards against putting the marker after the last element
238 if ( index
=== ( length
- 1 ) ) {
245 $indicator
= $( '<div>' )
246 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
248 $firstNew
.after( $indicator
);
251 // FIXME: Use CSS transition
252 // eslint-disable-next-line no-jquery/no-fade
259 * In enhanced mode, we need to check whether the grouped results all have the
260 * same active highlights in order to see whether the "parent" of the group should
261 * be grey or highlighted normally.
263 * This is called every time highlights are applied.
265 ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
266 var activeHighlightClasses
,
267 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
269 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
270 return 'mw-rcfilters-highlight-color-' + color
;
273 // Go over top pages and their children, and figure out if all sub-pages have the
274 // same highlights between themselves. If they do, the parent should be highlighted
275 // with all colors. If classes are different, the parent should receive a grey
277 $enhancedTopPageCell
.each( function () {
278 var firstChildClasses
, $rowsWithDifferentHighlights
,
281 // Collect the relevant classes from the first nested child
282 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
283 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
285 // Filter the non-head rows and see if they all have the same classes
287 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
288 var classesInThisRow
,
291 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
292 return $this.hasClass( className
);
295 return !OO
.compare( firstChildClasses
, classesInThisRow
);
298 // If classes are different, tag the row for using grey color
299 $table
.find( 'tr:first-child' )
300 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
305 * @return {boolean} Whether the changes are grouped by page
307 ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
308 var uri
= new mw
.Uri();
309 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
310 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
314 * Apply color classes based on filters highlight configuration
316 ChangesListWrapperWidget
.prototype.applyHighlight = function () {
317 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
321 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
322 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
324 // Add highlight class to all highlighted list items
327 'mw-rcfilters-highlighted ' +
328 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
331 // Track the filters for each item in .data( 'highlightedFilters' )
332 $elements
.each( function () {
333 var filters
= $( this ).data( 'highlightedFilters' );
336 $( this ).data( 'highlightedFilters', filters
);
338 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
339 filters
.push( filterItem
.getLabel() );
343 // Apply a title to each highlighted item, with a list of filters
344 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
345 var filters
= $( this ).data( 'highlightedFilters' );
347 if ( filters
&& filters
.length
) {
348 $( this ).attr( 'title', mw
.msg(
349 'rcfilters-highlighted-filters-list',
350 filters
.join( mw
.msg( 'comma-separator' ) )
355 if ( this.inEnhancedMode() ) {
356 this.updateEnhancedParentHighlight();
359 // Turn on highlights
360 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
364 * Remove all color classes
366 ChangesListWrapperWidget
.prototype.clearHighlight = function () {
367 // Remove highlight classes
368 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
370 .find( '.mw-rcfilters-highlight-color-' + color
)
371 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
374 this.$element
.find( '.mw-rcfilters-highlighted' )
375 .removeAttr( 'title' )
376 .removeData( 'highlightedFilters' )
377 .removeClass( 'mw-rcfilters-highlighted' );
379 // Remove grey from enhanced rows
380 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
381 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
383 // Turn off highlights
384 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
387 module
.exports
= ChangesListWrapperWidget
;