RCFilters: Display specific error if query times out
[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 newChangesExist: 'onNewChangesExist'
44 } );
45
46 this.$element
47 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
48 // We handle our own display/hide of the empty results message
49 // We keep the timeout class here and remove it later, since at this
50 // stage it is still needed to identify that the timeout occurred.
51 .removeClass( 'mw-changeslist-empty' );
52
53 this.setupNewChangesButtonContainer();
54 };
55
56 /* Initialization */
57
58 OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
59
60 /**
61 * Respond to filters model initialize event
62 */
63 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onFiltersModelInitialize = function () {
64 this.filtersModelInitialized = true;
65 // Set up highlight containers. We need to wait for the filters model
66 // to be initialized, so we can make sure we have all the css class definitions
67 // we get from the server with our filters
68 this.setupHighlightContainers( this.$element );
69 };
70
71 /**
72 * Get all available highlight classes
73 *
74 * @return {string[]} An array of available highlight class names
75 */
76 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.getHighlightClasses = function () {
77 if ( !this.highlightClasses || !this.highlightClasses.length ) {
78 this.highlightClasses = this.filtersViewModel.getItemsSupportingHighlights()
79 .map( function ( filterItem ) {
80 return filterItem.getCssClass();
81 } );
82 }
83
84 return this.highlightClasses;
85 };
86
87 /**
88 * Respond to the highlight feature being toggled on and off
89 *
90 * @param {boolean} highlightEnabled
91 */
92 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onHighlightChange = function ( highlightEnabled ) {
93 if ( highlightEnabled ) {
94 this.applyHighlight();
95 } else {
96 this.clearHighlight();
97 }
98 };
99
100 /**
101 * Respond to a filter item model update
102 */
103 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
104 if ( this.filtersModelInitialized && this.filtersViewModel.isHighlightEnabled() ) {
105 this.clearHighlight();
106 this.applyHighlight();
107 }
108 };
109
110 /**
111 * Respond to changes list model invalidate
112 */
113 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
114 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
115 };
116
117 /**
118 * Respond to changes list model update
119 *
120 * @param {jQuery|string} $changesListContent The content of the updated changes list
121 * @param {jQuery} $fieldset The content of the updated fieldset
122 * @param {boolean} isDatabaseTimeout Whether this is an error state due to a database query
123 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
124 * @param {boolean} from Timestamp of the new changes
125 */
126 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function (
127 $changesListContent, $fieldset, isDatabaseTimeout, isInitialDOM, from
128 ) {
129 var conflictItem, noResultsKey,
130 $message = $( '<div>' )
131 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
132 isEmpty = $changesListContent === 'NO_RESULTS',
133 // For enhanced mode, we have to load these modules, which are
134 // not loaded for the 'regular' mode in the backend
135 loaderPromise = mw.user.options.get( 'usenewrc' ) ?
136 mw.loader.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
137 $.Deferred().resolve(),
138 widget = this;
139
140 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
141 if ( isEmpty ) {
142 this.$element.empty();
143
144 if ( this.filtersViewModel.hasConflict() ) {
145 conflictItem = this.filtersViewModel.getFirstConflictedItem();
146
147 $message
148 .append(
149 $( '<div>' )
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
151 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
152 $( '<div>' )
153 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
154 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
155 );
156 } else {
157 noResultsKey = isDatabaseTimeout ?
158 'recentchanges-timeout' :
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 * Respond to changes list model newChangesExist
242 *
243 * @param {boolean} newChangesExist Whether new changes exist
244 */
245 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
246 this.showNewChangesLink.toggle( newChangesExist );
247 };
248
249 /**
250 * Respond to the user clicking the 'show new changes' button
251 */
252 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
253 this.controller.showNewChanges();
254 };
255
256 /**
257 * Setup the container for the 'new changes' button.
258 */
259 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function () {
260 this.showNewChangesLink = new OO.ui.ButtonWidget( {
261 framed: false,
262 label: mw.message( 'rcfilters-show-new-changes' ).text(),
263 flags: [ 'progressive' ]
264 } );
265 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
266 this.showNewChangesLink.toggle( false );
267
268 // HACK: Add the -newChanges div inside rcfilters-head, rather than right above us
269 // Visually it's the same place, but by putting it inside rcfilters-head we are
270 // able to use the min-height rule to prevent the page from jumping when this is added.
271 this.$element.parent().find( '.rcfilters-head' ).append(
272 $( '<div>' )
273 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
274 .append( this.showNewChangesLink.$element )
275 );
276 };
277
278 /**
279 * Set up the highlight containers with all color circle indicators.
280 *
281 * @param {jQuery|string} $content The content of the updated changes list
282 */
283 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
284 var $enhancedTopPageCell, $enhancedNestedPagesCell,
285 widget = this,
286 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
287 $highlights = $( '<div>' )
288 .addClass( highlightClass )
289 .append(
290 $( '<div>' )
291 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
292 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
293 .prop( 'data-color', 'none' )
294 );
295
296 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
297 // Already set up
298 return;
299 }
300
301 mw.rcfilters.HighlightColors.forEach( function ( color ) {
302 $highlights.append(
303 $( '<div>' )
304 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
305 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
306 .prop( 'data-color', color )
307 );
308 } );
309
310 if ( this.inEnhancedMode() ) {
311 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
312 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
313
314 // Enhanced RC highlight containers
315 $content.find( 'table.mw-enhanced-rc tr:first-child' )
316 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
317 .prepend(
318 $( '<td>' )
319 .append( $highlights.clone() )
320 );
321
322 // We are adding and changing cells in a table that, despite having nested rows,
323 // is actually all one big table. To prevent the highlights cell in the "nested"
324 // rows from stretching out the cell with the flags and timestamp in the top row,
325 // we give the latter colspan=2. Then to make things line up again, we add
326 // an empty <td> to the "nested" rows.
327
328 // Set colspan=2 on cell with flags and timestamp in top row
329 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
330 .prop( 'colspan', '2' );
331 // Add empty <td> to nested rows to compensate
332 $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
333 // Add highlights cell to nested rows
334 $enhancedNestedPagesCell
335 .before(
336 $( '<td>' )
337 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
338 );
339
340 // We need to target the nested rows differently than the top rows so that the
341 // LESS rules applies correctly. In top rows, the rule should highlight all but
342 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
343 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
344 $enhancedNestedPagesCell
345 .closest( 'tr' )
346 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
347
348 // Go over pages that have sub results
349 // HACK: We really only can collect those by targetting the collapsible class
350 $enhancedTopPageCell.each( function () {
351 var collectedClasses,
352 $table = $( this );
353
354 // Go over <tr>s and pick up all recognized classes
355 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
356 return $table.find( 'tr' ).hasClass( className );
357 } );
358
359 $table.find( 'tr:first-child' )
360 .addClass( collectedClasses.join( ' ' ) );
361 } );
362
363 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
364 } else {
365 // Regular RC
366 $content.find( 'ul.special li' )
367 .prepend( $highlights.clone() );
368 }
369 };
370
371 /**
372 * In enhanced mode, we need to check whether the grouped results all have the
373 * same active highlights in order to see whether the "parent" of the group should
374 * be grey or highlighted normally.
375 *
376 * This is called every time highlights are applied.
377 */
378 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
379 var activeHighlightClasses,
380 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
381
382 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
383 return 'mw-rcfilters-highlight-color-' + color;
384 } );
385
386 // Go over top pages and their children, and figure out if all sub-pages have the
387 // same highlights between themselves. If they do, the parent should be highlighted
388 // with all colors. If classes are different, the parent should receive a grey
389 // background
390 $enhancedTopPageCell.each( function () {
391 var firstChildClasses, $rowsWithDifferentHighlights,
392 $table = $( this );
393
394 // Collect the relevant classes from the first nested child
395 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
396 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
397 } );
398 // Filter the non-head rows and see if they all have the same classes
399 // to the first row
400 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
401 var classesInThisRow,
402 $this = $( this );
403
404 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
405 return $this.hasClass( className );
406 } );
407
408 return !OO.compare( firstChildClasses, classesInThisRow );
409 } );
410
411 // If classes are different, tag the row for using grey color
412 $table.find( 'tr:first-child' )
413 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
414 } );
415 };
416
417 /**
418 * @return {boolean} Whether the changes are grouped by page
419 */
420 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
421 var uri = new mw.Uri();
422 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
423 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
424 };
425
426 /**
427 * Apply color classes based on filters highlight configuration
428 */
429 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
430 if ( !this.filtersViewModel.isHighlightEnabled() ) {
431 return;
432 }
433
434 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
435 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
436
437 // Add highlight class to all highlighted list items
438 $elements
439 .addClass(
440 'mw-rcfilters-highlighted ' +
441 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor()
442 );
443
444 // Track the filters for each item in .data( 'highlightedFilters' )
445 $elements.each( function () {
446 var filters = $( this ).data( 'highlightedFilters' );
447 if ( !filters ) {
448 filters = [];
449 $( this ).data( 'highlightedFilters', filters );
450 }
451 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
452 filters.push( filterItem.getLabel() );
453 }
454 } );
455 }.bind( this ) );
456 // Apply a title to each highlighted item, with a list of filters
457 this.$element.find( '.mw-rcfilters-highlighted' ).each( function () {
458 var filters = $( this ).data( 'highlightedFilters' );
459
460 if ( filters && filters.length ) {
461 $( this ).attr( 'title', mw.msg(
462 'rcfilters-highlighted-filters-list',
463 filters.join( mw.msg( 'comma-separator' ) )
464 ) );
465 }
466
467 } );
468 if ( this.inEnhancedMode() ) {
469 this.updateEnhancedParentHighlight();
470 }
471
472 // Turn on highlights
473 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
474 };
475
476 /**
477 * Remove all color classes
478 */
479 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
480 // Remove highlight classes
481 mw.rcfilters.HighlightColors.forEach( function ( color ) {
482 this.$element
483 .find( '.mw-rcfilters-highlight-color-' + color )
484 .removeClass( 'mw-rcfilters-highlight-color-' + color );
485 }.bind( this ) );
486
487 this.$element.find( '.mw-rcfilters-highlighted' )
488 .removeAttr( 'title' )
489 .removeData( 'highlightedFilters' )
490 .removeClass( 'mw-rcfilters-highlighted' );
491
492 // Remove grey from enhanced rows
493 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
494 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
495
496 // Turn off highlights
497 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
498 };
499 }( mediaWiki ) );