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;
34 this.filtersViewModel
.connect( this, {
35 itemUpdate
: 'onItemUpdate',
36 highlightChange
: 'onHighlightChange'
38 this.changesListViewModel
.connect( this, {
39 invalidate
: 'onModelInvalidate',
40 update
: 'onModelUpdate'
44 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
45 // We handle our own display/hide of the empty results message
46 // We keep the timeout class here and remove it later, since at this
47 // stage it is still needed to identify that the timeout occurred.
48 .removeClass( 'mw-changeslist-empty' );
53 OO
.inheritClass( mw
.rcfilters
.ui
.ChangesListWrapperWidget
, OO
.ui
.Widget
);
56 * Get all available highlight classes
58 * @return {string[]} An array of available highlight class names
60 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
61 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
62 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
63 .map( function ( filterItem
) {
64 return filterItem
.getCssClass();
68 return this.highlightClasses
;
72 * Respond to the highlight feature being toggled on and off
74 * @param {boolean} highlightEnabled
76 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
77 if ( highlightEnabled
) {
78 this.applyHighlight();
80 this.clearHighlight();
85 * Respond to a filter item model update
87 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
88 if ( this.controller
.isInitialized() && this.filtersViewModel
.isHighlightEnabled() ) {
89 // this.controller.isInitialized() is still false during page load,
90 // we don't want to clear/apply highlights at this stage.
91 this.clearHighlight();
92 this.applyHighlight();
97 * Respond to changes list model invalidate
99 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
100 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
104 * Respond to changes list model update
106 * @param {jQuery|string} $changesListContent The content of the updated changes list
107 * @param {jQuery} $fieldset The content of the updated fieldset
108 * @param {string} noResultsDetails Type of no result error
109 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
110 * @param {boolean} from Timestamp of the new changes
112 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelUpdate = function (
113 $changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, from
116 $message
= $( '<div>' )
117 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
118 isEmpty
= $changesListContent
=== 'NO_RESULTS',
119 // For enhanced mode, we have to load these modules, which are
120 // not loaded for the 'regular' mode in the backend
121 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
122 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
123 $.Deferred().resolve(),
126 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
128 this.$element
.empty();
130 if ( this.filtersViewModel
.hasConflict() ) {
131 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
136 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
137 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
139 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
140 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
146 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
147 .text( mw
.msg( this.getMsgKeyForNoResults( noResultsDetails
) ) )
150 // remove all classes matching mw-changeslist-*
151 this.$element
.removeClass( function ( elementIndex
, allClasses
) {
154 .filter( function ( className
) {
155 return className
.indexOf( 'mw-changeslist-' ) === 0;
161 this.$element
.append( $message
);
163 if ( !isInitialDOM
) {
164 this.$element
.empty().append( $changesListContent
);
167 this.emphasizeNewChanges( from );
172 this.applyHighlight();
176 this.$element
.prepend( $( '<div>' ).addClass( 'mw-changeslist-overlay' ) );
178 loaderPromise
.done( function () {
179 if ( !isInitialDOM
&& !isEmpty
) {
180 // Make sure enhanced RC re-initializes correctly
181 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
184 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
188 /** Toggles overlay class on changes list
190 * @param {boolean} isVisible True if overlay should be visible
192 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.toggleOverlay = function ( isVisible
) {
193 this.$element
.toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget--overlaid', isVisible
);
197 * Map a reason for having no results to its message key
199 * @param {string} reason One of the NO_RESULTS_* "constant" that represent
200 * a reason for having no results
201 * @return {string} Key for the message that explains why there is no results in this case
203 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getMsgKeyForNoResults = function ( reason
) {
204 var reasonMsgKeyMap
= {
205 NO_RESULTS_NORMAL
: 'recentchanges-noresult',
206 NO_RESULTS_TIMEOUT
: 'recentchanges-timeout',
207 NO_RESULTS_NETWORK_ERROR
: 'recentchanges-network',
208 NO_RESULTS_NO_TARGET_PAGE
: 'recentchanges-notargetpage',
209 NO_RESULTS_INVALID_TARGET_PAGE
: 'allpagesbadtitle'
211 return reasonMsgKeyMap
[ reason
];
215 * Emphasize the elements (or groups) newer than the 'from' parameter
216 * @param {string} from Anything newer than this is considered 'new'
218 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
221 $newChanges
= $( [] ),
222 selector
= this.inEnhancedMode() ?
223 'table.mw-enhanced-rc[data-mw-ts]' :
225 set = this.$element
.find( selector
),
228 set.each( function ( index
) {
229 var $this = $( this ),
230 ts
= $this.data( 'mw-ts' );
233 $newChanges
= $newChanges
.add( $this );
236 // guards against putting the marker after the last element
237 if ( index
=== ( length
- 1 ) ) {
244 $indicator
= $( '<div>' )
245 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
247 $firstNew
.after( $indicator
);
256 * In enhanced mode, we need to check whether the grouped results all have the
257 * same active highlights in order to see whether the "parent" of the group should
258 * be grey or highlighted normally.
260 * This is called every time highlights are applied.
262 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
263 var activeHighlightClasses
,
264 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
266 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
267 return 'mw-rcfilters-highlight-color-' + color
;
270 // Go over top pages and their children, and figure out if all sub-pages have the
271 // same highlights between themselves. If they do, the parent should be highlighted
272 // with all colors. If classes are different, the parent should receive a grey
274 $enhancedTopPageCell
.each( function () {
275 var firstChildClasses
, $rowsWithDifferentHighlights
,
278 // Collect the relevant classes from the first nested child
279 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
280 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
282 // Filter the non-head rows and see if they all have the same classes
284 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
285 var classesInThisRow
,
288 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
289 return $this.hasClass( className
);
292 return !OO
.compare( firstChildClasses
, classesInThisRow
);
295 // If classes are different, tag the row for using grey color
296 $table
.find( 'tr:first-child' )
297 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
302 * @return {boolean} Whether the changes are grouped by page
304 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
305 var uri
= new mw
.Uri();
306 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
307 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
311 * Apply color classes based on filters highlight configuration
313 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
314 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
318 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
319 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
321 // Add highlight class to all highlighted list items
324 'mw-rcfilters-highlighted ' +
325 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
328 // Track the filters for each item in .data( 'highlightedFilters' )
329 $elements
.each( function () {
330 var filters
= $( this ).data( 'highlightedFilters' );
333 $( this ).data( 'highlightedFilters', filters
);
335 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
336 filters
.push( filterItem
.getLabel() );
340 // Apply a title to each highlighted item, with a list of filters
341 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
342 var filters
= $( this ).data( 'highlightedFilters' );
344 if ( filters
&& filters
.length
) {
345 $( this ).attr( 'title', mw
.msg(
346 'rcfilters-highlighted-filters-list',
347 filters
.join( mw
.msg( 'comma-separator' ) )
352 if ( this.inEnhancedMode() ) {
353 this.updateEnhancedParentHighlight();
356 // Turn on highlights
357 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
361 * Remove all color classes
363 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
364 // Remove highlight classes
365 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
367 .find( '.mw-rcfilters-highlight-color-' + color
)
368 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
371 this.$element
.find( '.mw-rcfilters-highlighted' )
372 .removeAttr( 'title' )
373 .removeData( 'highlightedFilters' )
374 .removeClass( 'mw-rcfilters-highlighted' );
376 // Remove grey from enhanced rows
377 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
378 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
380 // Turn off highlights
381 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );