RCFilters: Redo the way spinners and ready/loading states are managed
[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 .removeClass( 'mw-changeslist-empty' );
50
51 this.setupNewChangesButtonContainer( this.$element );
52 };
53
54 /* Initialization */
55
56 OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
57
58 /**
59 * Respond to filters model initialize event
60 */
61 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onFiltersModelInitialize = function () {
62 this.filtersModelInitialized = true;
63 // Set up highlight containers. We need to wait for the filters model
64 // to be initialized, so we can make sure we have all the css class definitions
65 // we get from the server with our filters
66 this.setupHighlightContainers( this.$element );
67 };
68
69 /**
70 * Get all available highlight classes
71 *
72 * @return {string[]} An array of available highlight class names
73 */
74 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.getHighlightClasses = function () {
75 if ( !this.highlightClasses || !this.highlightClasses.length ) {
76 this.highlightClasses = this.filtersViewModel.getItemsSupportingHighlights()
77 .map( function ( filterItem ) {
78 return filterItem.getCssClass();
79 } );
80 }
81
82 return this.highlightClasses;
83 };
84
85 /**
86 * Respond to the highlight feature being toggled on and off
87 *
88 * @param {boolean} highlightEnabled
89 */
90 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onHighlightChange = function ( highlightEnabled ) {
91 if ( highlightEnabled ) {
92 this.applyHighlight();
93 } else {
94 this.clearHighlight();
95 }
96 };
97
98 /**
99 * Respond to a filter item model update
100 */
101 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
102 if ( this.filtersModelInitialized && this.filtersViewModel.isHighlightEnabled() ) {
103 this.clearHighlight();
104 this.applyHighlight();
105 }
106 };
107
108 /**
109 * Respond to changes list model invalidate
110 */
111 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
112 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
113 };
114
115 /**
116 * Respond to changes list model update
117 *
118 * @param {jQuery|string} $changesListContent The content of the updated changes list
119 * @param {jQuery} $fieldset The content of the updated fieldset
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, isInitialDOM, from
125 ) {
126 var conflictItem,
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 $message
155 .append(
156 $( '<div>' )
157 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
158 .text( mw.message( 'recentchanges-noresult' ).text() )
159 );
160 }
161
162 this.$element.append( $message );
163 } else {
164 if ( !isInitialDOM ) {
165 this.$element.empty().append( $changesListContent );
166
167 if ( from ) {
168 this.emphasizeNewChanges( from );
169 }
170 }
171
172 // Set up highlight containers
173 this.setupHighlightContainers( this.$element );
174
175 // Apply highlight
176 this.applyHighlight();
177
178 }
179
180 loaderPromise.done( function () {
181 if ( !isInitialDOM && !isEmpty ) {
182 // Make sure enhanced RC re-initializes correctly
183 mw.hook( 'wikipage.content' ).fire( widget.$element );
184 }
185
186 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
187 } );
188 };
189
190 /**
191 * Emphasize the elements (or groups) newer than the 'from' parameter
192 * @param {string} from Anything newer than this is considered 'new'
193 */
194 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.emphasizeNewChanges = function ( from ) {
195 var $firstNew,
196 $indicator,
197 $newChanges = $( [] ),
198 selector = this.inEnhancedMode() ?
199 'table.mw-enhanced-rc[data-mw-ts]' :
200 'li[data-mw-ts]',
201 set = this.$element.find( selector ),
202 length = set.length;
203
204 set.each( function ( index ) {
205 var $this = $( this ),
206 ts = $this.data( 'mw-ts' );
207
208 if ( ts >= from ) {
209 $newChanges = $newChanges.add( $this );
210 $firstNew = $this;
211
212 // guards against putting the marker after the last element
213 if ( index === ( length - 1 ) ) {
214 $firstNew = null;
215 }
216 }
217 } );
218
219 if ( $firstNew ) {
220 $indicator = $( '<div>' )
221 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
222
223 $firstNew.after( $indicator );
224 }
225
226 $newChanges
227 .hide()
228 .fadeIn( 1000 );
229 };
230
231 /**
232 * Respond to changes list model newChangesExist
233 *
234 * @param {boolean} newChangesExist Whether new changes exist
235 */
236 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
237 this.showNewChangesLink.toggle( newChangesExist );
238 };
239
240 /**
241 * Respond to the user clicking the 'show new changes' button
242 */
243 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
244 this.controller.showNewChanges();
245 };
246
247 /**
248 * Setup the container for the 'new changes' button.
249 *
250 * @param {jQuery} $content
251 */
252 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
253 this.showNewChangesLink = new OO.ui.ButtonWidget( {
254 framed: false,
255 label: mw.message( 'rcfilters-show-new-changes' ).text(),
256 flags: [ 'progressive' ]
257 } );
258 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
259 this.showNewChangesLink.toggle( false );
260
261 $content.before(
262 $( '<div>' )
263 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
264 .append( this.showNewChangesLink.$element )
265 );
266 };
267
268 /**
269 * Set up the highlight containers with all color circle indicators.
270 *
271 * @param {jQuery|string} $content The content of the updated changes list
272 */
273 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
274 var $enhancedTopPageCell, $enhancedNestedPagesCell,
275 widget = this,
276 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
277 $highlights = $( '<div>' )
278 .addClass( highlightClass )
279 .append(
280 $( '<div>' )
281 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
282 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
283 .prop( 'data-color', 'none' )
284 );
285
286 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
287 // Already set up
288 return;
289 }
290
291 mw.rcfilters.HighlightColors.forEach( function ( color ) {
292 $highlights.append(
293 $( '<div>' )
294 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
295 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
296 .prop( 'data-color', color )
297 );
298 } );
299
300 if ( this.inEnhancedMode() ) {
301 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
302 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
303
304 // Enhanced RC highlight containers
305 $content.find( 'table.mw-enhanced-rc tr:first-child' )
306 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
307 .prepend(
308 $( '<td>' )
309 .append( $highlights.clone() )
310 );
311
312 // We are adding and changing cells in a table that, despite having nested rows,
313 // is actually all one big table. To do that right, we want to remove the 'placeholder'
314 // cell from the top row, because we're actually adding that placeholder in the children
315 // with the highlights.
316 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-changeslist-line-prefix' )
317 .detach();
318 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
319 .prop( 'colspan', '2' );
320
321 $enhancedNestedPagesCell
322 .before(
323 $( '<td>' )
324 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
325 );
326
327 // We need to target the nested rows differently than the top rows so that the
328 // LESS rules applies correctly. In top rows, the rule should highlight all but
329 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
330 // should highlight all but the first 3 cells td:not( :nth-child( -n+3 )
331 $enhancedNestedPagesCell
332 .closest( 'tr' )
333 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
334
335 // Go over pages that have sub results
336 // HACK: We really only can collect those by targetting the collapsible class
337 $enhancedTopPageCell.each( function () {
338 var collectedClasses,
339 $table = $( this );
340
341 // Go over <tr>s and pick up all recognized classes
342 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
343 return $table.find( 'tr' ).hasClass( className );
344 } );
345
346 $table.find( 'tr:first-child' )
347 .addClass( collectedClasses.join( ' ' ) );
348 } );
349
350 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
351 } else {
352 // Regular RC
353 $content.find( 'ul.special li' )
354 .prepend( $highlights.clone() );
355 }
356 };
357
358 /**
359 * In enhanced mode, we need to check whether the grouped results all have the
360 * same active highlights in order to see whether the "parent" of the group should
361 * be grey or highlighted normally.
362 *
363 * This is called every time highlights are applied.
364 */
365 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
366 var activeHighlightClasses,
367 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
368
369 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
370 return 'mw-rcfilters-highlight-color-' + color;
371 } );
372
373 // Go over top pages and their children, and figure out if all sub-pages have the
374 // same highlights between themselves. If they do, the parent should be highlighted
375 // with all colors. If classes are different, the parent should receive a grey
376 // background
377 $enhancedTopPageCell.each( function () {
378 var firstChildClasses, $rowsWithDifferentHighlights,
379 $table = $( this );
380
381 // Collect the relevant classes from the first nested child
382 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
383 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
384 } );
385 // Filter the non-head rows and see if they all have the same classes
386 // to the first row
387 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
388 var classesInThisRow,
389 $this = $( this );
390
391 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
392 return $this.hasClass( className );
393 } );
394
395 return !OO.compare( firstChildClasses, classesInThisRow );
396 } );
397
398 // If classes are different, tag the row for using grey color
399 $table.find( 'tr:first-child' )
400 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
401 } );
402 };
403
404 /**
405 * @return {boolean} Whether the changes are grouped by page
406 */
407 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
408 var uri = new mw.Uri();
409 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
410 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
411 };
412
413 /**
414 * Apply color classes based on filters highlight configuration
415 */
416 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
417 if ( !this.filtersViewModel.isHighlightEnabled() ) {
418 return;
419 }
420
421 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
422 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
423
424 // Add highlight class to all highlighted list items
425 $elements
426 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
427
428 $elements.each( function () {
429 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
430 filters = filterString ? filterString.split( '|' ) : [];
431
432 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
433 filters.push( filterItem.getLabel() );
434 }
435
436 $( this )
437 .attr( 'data-highlightedFilters', filters.join( '|' ) );
438 } );
439 }.bind( this ) );
440 // Apply a title for relevant filters
441 this.$element.find( '[data-highlightedFilters]' ).each( function () {
442 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
443 filters = filterString ? filterString.split( '|' ) : [];
444
445 if ( filterString ) {
446 $( this ).attr( 'title', mw.msg( 'rcfilters-highlighted-filters-list', filters.join( ', ' ) ) );
447 }
448 } );
449
450 if ( this.inEnhancedMode() ) {
451 this.updateEnhancedParentHighlight();
452 }
453
454 // Turn on highlights
455 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
456 };
457
458 /**
459 * Remove all color classes
460 */
461 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
462 // Remove highlight classes
463 mw.rcfilters.HighlightColors.forEach( function ( color ) {
464 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
465 }.bind( this ) );
466
467 this.$element.find( '[data-highlightedFilters]' )
468 .removeAttr( 'title' )
469 .removeAttr( 'data-highlightedFilters' );
470
471 // Remove grey from enhanced rows
472 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
473 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
474
475 // Turn off highlights
476 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
477 };
478 }( mediaWiki ) );