Combine changes limit and time period selectors
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.ChangesListWrapperWidget.js
1 ( function ( mw ) {
2 /**
3 * List of changes
4 *
5 * @extends OO.ui.Widget
6 *
7 * @constructor
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
13 */
14 mw.rcfilters.ui.ChangesListWrapperWidget = function MwRcfiltersUiChangesListWrapperWidget(
15 filtersViewModel,
16 changesListViewModel,
17 controller,
18 $changesListRoot,
19 config
20 ) {
21 config = $.extend( {}, config, {
22 $element: $changesListRoot
23 } );
24
25 // Parent
26 mw.rcfilters.ui.ChangesListWrapperWidget.parent.call( this, config );
27
28 this.filtersViewModel = filtersViewModel;
29 this.changesListViewModel = changesListViewModel;
30 this.controller = controller;
31 this.highlightClasses = null;
32 this.filtersModelInitialized = false;
33
34 // Events
35 this.filtersViewModel.connect( this, {
36 itemUpdate: 'onItemUpdate',
37 highlightChange: 'onHighlightChange',
38 initialize: 'onFiltersModelInitialize'
39 } );
40 this.changesListViewModel.connect( this, {
41 invalidate: 'onModelInvalidate',
42 update: 'onModelUpdate'
43 } );
44
45 this.$element
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' );
51 };
52
53 /* Initialization */
54
55 OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
56
57 /**
58 * Respond to filters model initialize event
59 */
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 );
66 };
67
68 /**
69 * Get all available highlight classes
70 *
71 * @return {string[]} An array of available highlight class names
72 */
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();
78 } );
79 }
80
81 return this.highlightClasses;
82 };
83
84 /**
85 * Respond to the highlight feature being toggled on and off
86 *
87 * @param {boolean} highlightEnabled
88 */
89 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onHighlightChange = function ( highlightEnabled ) {
90 if ( highlightEnabled ) {
91 this.applyHighlight();
92 } else {
93 this.clearHighlight();
94 }
95 };
96
97 /**
98 * Respond to a filter item model update
99 */
100 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
101 if ( this.filtersModelInitialized && this.filtersViewModel.isHighlightEnabled() ) {
102 this.clearHighlight();
103 this.applyHighlight();
104 }
105 };
106
107 /**
108 * Respond to changes list model invalidate
109 */
110 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
111 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
112 };
113
114 /**
115 * Respond to changes list model update
116 *
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
122 */
123 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function (
124 $changesListContent, $fieldset, noResultsDetails, isInitialDOM, from
125 ) {
126 var conflictItem, noResultsKey,
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(),
135 widget = this;
136
137 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
138 if ( isEmpty ) {
139 this.$element.empty();
140
141 if ( this.filtersViewModel.hasConflict() ) {
142 conflictItem = this.filtersViewModel.getFirstConflictedItem();
143
144 $message
145 .append(
146 $( '<div>' )
147 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
148 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
149 $( '<div>' )
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
151 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
152 );
153 } else {
154 noResultsKey =
155 ( noResultsDetails === 'NO_RESULTS_TIMEOUT' ) ?
156 'recentchanges-timeout' :
157 ( noResultsDetails === 'NO_RESULTS_NETWORK_ERROR' ) ?
158 'recentchanges-network' :
159 'recentchanges-noresult';
160
161 $message
162 .append(
163 $( '<div>' )
164 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
165 .text( mw.message( noResultsKey ).text() )
166 );
167
168 this.$element.removeClass( 'mw-changeslist-timeout' );
169 }
170
171 this.$element.append( $message );
172 } else {
173 if ( !isInitialDOM ) {
174 this.$element.empty().append( $changesListContent );
175
176 if ( from ) {
177 this.emphasizeNewChanges( from );
178 }
179 }
180
181 // Set up highlight containers
182 this.setupHighlightContainers( this.$element );
183
184 // Apply highlight
185 this.applyHighlight();
186
187 }
188
189 loaderPromise.done( function () {
190 if ( !isInitialDOM && !isEmpty ) {
191 // Make sure enhanced RC re-initializes correctly
192 mw.hook( 'wikipage.content' ).fire( widget.$element );
193 }
194
195 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
196 } );
197 };
198
199 /**
200 * Emphasize the elements (or groups) newer than the 'from' parameter
201 * @param {string} from Anything newer than this is considered 'new'
202 */
203 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.emphasizeNewChanges = function ( from ) {
204 var $firstNew,
205 $indicator,
206 $newChanges = $( [] ),
207 selector = this.inEnhancedMode() ?
208 'table.mw-enhanced-rc[data-mw-ts]' :
209 'li[data-mw-ts]',
210 set = this.$element.find( selector ),
211 length = set.length;
212
213 set.each( function ( index ) {
214 var $this = $( this ),
215 ts = $this.data( 'mw-ts' );
216
217 if ( ts >= from ) {
218 $newChanges = $newChanges.add( $this );
219 $firstNew = $this;
220
221 // guards against putting the marker after the last element
222 if ( index === ( length - 1 ) ) {
223 $firstNew = null;
224 }
225 }
226 } );
227
228 if ( $firstNew ) {
229 $indicator = $( '<div>' )
230 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
231
232 $firstNew.after( $indicator );
233 }
234
235 $newChanges
236 .hide()
237 .fadeIn( 1000 );
238 };
239
240 /**
241 * Set up the highlight containers with all color circle indicators.
242 *
243 * @param {jQuery|string} $content The content of the updated changes list
244 */
245 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
246 var $enhancedTopPageCell, $enhancedNestedPagesCell,
247 widget = this,
248 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
249 $highlights = $( '<div>' )
250 .addClass( highlightClass )
251 .append(
252 $( '<div>' )
253 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
254 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
255 .prop( 'data-color', 'none' )
256 );
257
258 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
259 // Already set up
260 return;
261 }
262
263 mw.rcfilters.HighlightColors.forEach( function ( color ) {
264 $highlights.append(
265 $( '<div>' )
266 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
267 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
268 .prop( 'data-color', color )
269 );
270 } );
271
272 if ( this.inEnhancedMode() ) {
273 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
274 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
275
276 // Enhanced RC highlight containers
277 $content.find( 'table.mw-enhanced-rc tr:first-child' )
278 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
279 .prepend(
280 $( '<td>' )
281 .append( $highlights.clone() )
282 );
283
284 // We are adding and changing cells in a table that, despite having nested rows,
285 // is actually all one big table. To prevent the highlights cell in the "nested"
286 // rows from stretching out the cell with the flags and timestamp in the top row,
287 // we give the latter colspan=2. Then to make things line up again, we add
288 // an empty <td> to the "nested" rows.
289
290 // Set colspan=2 on cell with flags and timestamp in top row
291 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
292 .prop( 'colspan', '2' );
293 // Add empty <td> to nested rows to compensate
294 $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
295 // Add highlights cell to nested rows
296 $enhancedNestedPagesCell
297 .before(
298 $( '<td>' )
299 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
300 );
301
302 // We need to target the nested rows differently than the top rows so that the
303 // LESS rules applies correctly. In top rows, the rule should highlight all but
304 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
305 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
306 $enhancedNestedPagesCell
307 .closest( 'tr' )
308 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
309
310 // Go over pages that have sub results
311 // HACK: We really only can collect those by targetting the collapsible class
312 $enhancedTopPageCell.each( function () {
313 var collectedClasses,
314 $table = $( this );
315
316 // Go over <tr>s and pick up all recognized classes
317 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
318 return $table.find( 'tr' ).hasClass( className );
319 } );
320
321 $table.find( 'tr:first-child' )
322 .addClass( collectedClasses.join( ' ' ) );
323 } );
324
325 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
326 } else {
327 // Regular RC
328 $content.find( 'ul.special li' )
329 .prepend( $highlights.clone() );
330 }
331 };
332
333 /**
334 * In enhanced mode, we need to check whether the grouped results all have the
335 * same active highlights in order to see whether the "parent" of the group should
336 * be grey or highlighted normally.
337 *
338 * This is called every time highlights are applied.
339 */
340 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
341 var activeHighlightClasses,
342 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
343
344 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
345 return 'mw-rcfilters-highlight-color-' + color;
346 } );
347
348 // Go over top pages and their children, and figure out if all sub-pages have the
349 // same highlights between themselves. If they do, the parent should be highlighted
350 // with all colors. If classes are different, the parent should receive a grey
351 // background
352 $enhancedTopPageCell.each( function () {
353 var firstChildClasses, $rowsWithDifferentHighlights,
354 $table = $( this );
355
356 // Collect the relevant classes from the first nested child
357 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
358 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
359 } );
360 // Filter the non-head rows and see if they all have the same classes
361 // to the first row
362 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
363 var classesInThisRow,
364 $this = $( this );
365
366 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
367 return $this.hasClass( className );
368 } );
369
370 return !OO.compare( firstChildClasses, classesInThisRow );
371 } );
372
373 // If classes are different, tag the row for using grey color
374 $table.find( 'tr:first-child' )
375 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
376 } );
377 };
378
379 /**
380 * @return {boolean} Whether the changes are grouped by page
381 */
382 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
383 var uri = new mw.Uri();
384 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
385 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
386 };
387
388 /**
389 * Apply color classes based on filters highlight configuration
390 */
391 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
392 if ( !this.filtersViewModel.isHighlightEnabled() ) {
393 return;
394 }
395
396 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
397 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
398
399 // Add highlight class to all highlighted list items
400 $elements
401 .addClass(
402 'mw-rcfilters-highlighted ' +
403 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor()
404 );
405
406 // Track the filters for each item in .data( 'highlightedFilters' )
407 $elements.each( function () {
408 var filters = $( this ).data( 'highlightedFilters' );
409 if ( !filters ) {
410 filters = [];
411 $( this ).data( 'highlightedFilters', filters );
412 }
413 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
414 filters.push( filterItem.getLabel() );
415 }
416 } );
417 }.bind( this ) );
418 // Apply a title to each highlighted item, with a list of filters
419 this.$element.find( '.mw-rcfilters-highlighted' ).each( function () {
420 var filters = $( this ).data( 'highlightedFilters' );
421
422 if ( filters && filters.length ) {
423 $( this ).attr( 'title', mw.msg(
424 'rcfilters-highlighted-filters-list',
425 filters.join( mw.msg( 'comma-separator' ) )
426 ) );
427 }
428
429 } );
430 if ( this.inEnhancedMode() ) {
431 this.updateEnhancedParentHighlight();
432 }
433
434 // Turn on highlights
435 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
436 };
437
438 /**
439 * Remove all color classes
440 */
441 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
442 // Remove highlight classes
443 mw.rcfilters.HighlightColors.forEach( function ( color ) {
444 this.$element
445 .find( '.mw-rcfilters-highlight-color-' + color )
446 .removeClass( 'mw-rcfilters-highlight-color-' + color );
447 }.bind( this ) );
448
449 this.$element.find( '.mw-rcfilters-highlighted' )
450 .removeAttr( 'title' )
451 .removeData( 'highlightedFilters' )
452 .removeClass( 'mw-rcfilters-highlighted' );
453
454 // Remove grey from enhanced rows
455 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
456 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
457
458 // Turn off highlights
459 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
460 };
461 }( mediaWiki ) );