Merge "RCFilters: Don't remove mw-changeslist-line-prefix in enhanced mode"
[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 prevent the highlights cell in the "nested"
314 // rows from stretching out the cell with the flags and timestamp in the top row,
315 // we give the latter colspan=2. Then to make things line up again, we add
316 // an empty <td> to the "nested" rows.
317
318 // Set colspan=2 on cell with flags and timestamp in top row
319 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
320 .prop( 'colspan', '2' );
321 // Add empty <td> to nested rows to compensate
322 $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
323 // Add highlights cell to nested rows
324 $enhancedNestedPagesCell
325 .before(
326 $( '<td>' )
327 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
328 );
329
330 // We need to target the nested rows differently than the top rows so that the
331 // LESS rules applies correctly. In top rows, the rule should highlight all but
332 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
333 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
334 $enhancedNestedPagesCell
335 .closest( 'tr' )
336 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
337
338 // Go over pages that have sub results
339 // HACK: We really only can collect those by targetting the collapsible class
340 $enhancedTopPageCell.each( function () {
341 var collectedClasses,
342 $table = $( this );
343
344 // Go over <tr>s and pick up all recognized classes
345 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
346 return $table.find( 'tr' ).hasClass( className );
347 } );
348
349 $table.find( 'tr:first-child' )
350 .addClass( collectedClasses.join( ' ' ) );
351 } );
352
353 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
354 } else {
355 // Regular RC
356 $content.find( 'ul.special li' )
357 .prepend( $highlights.clone() );
358 }
359 };
360
361 /**
362 * In enhanced mode, we need to check whether the grouped results all have the
363 * same active highlights in order to see whether the "parent" of the group should
364 * be grey or highlighted normally.
365 *
366 * This is called every time highlights are applied.
367 */
368 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
369 var activeHighlightClasses,
370 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
371
372 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
373 return 'mw-rcfilters-highlight-color-' + color;
374 } );
375
376 // Go over top pages and their children, and figure out if all sub-pages have the
377 // same highlights between themselves. If they do, the parent should be highlighted
378 // with all colors. If classes are different, the parent should receive a grey
379 // background
380 $enhancedTopPageCell.each( function () {
381 var firstChildClasses, $rowsWithDifferentHighlights,
382 $table = $( this );
383
384 // Collect the relevant classes from the first nested child
385 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
386 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
387 } );
388 // Filter the non-head rows and see if they all have the same classes
389 // to the first row
390 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
391 var classesInThisRow,
392 $this = $( this );
393
394 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
395 return $this.hasClass( className );
396 } );
397
398 return !OO.compare( firstChildClasses, classesInThisRow );
399 } );
400
401 // If classes are different, tag the row for using grey color
402 $table.find( 'tr:first-child' )
403 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
404 } );
405 };
406
407 /**
408 * @return {boolean} Whether the changes are grouped by page
409 */
410 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
411 var uri = new mw.Uri();
412 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
413 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
414 };
415
416 /**
417 * Apply color classes based on filters highlight configuration
418 */
419 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
420 if ( !this.filtersViewModel.isHighlightEnabled() ) {
421 return;
422 }
423
424 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
425 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
426
427 // Add highlight class to all highlighted list items
428 $elements
429 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
430
431 $elements.each( function () {
432 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
433 filters = filterString ? filterString.split( '|' ) : [];
434
435 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
436 filters.push( filterItem.getLabel() );
437 }
438
439 $( this )
440 .attr( 'data-highlightedFilters', filters.join( '|' ) );
441 } );
442 }.bind( this ) );
443 // Apply a title for relevant filters
444 this.$element.find( '[data-highlightedFilters]' ).each( function () {
445 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
446 filters = filterString ? filterString.split( '|' ) : [];
447
448 if ( filterString ) {
449 $( this ).attr( 'title', mw.msg( 'rcfilters-highlighted-filters-list', filters.join( ', ' ) ) );
450 }
451 } );
452
453 if ( this.inEnhancedMode() ) {
454 this.updateEnhancedParentHighlight();
455 }
456
457 // Turn on highlights
458 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
459 };
460
461 /**
462 * Remove all color classes
463 */
464 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
465 // Remove highlight classes
466 mw.rcfilters.HighlightColors.forEach( function ( color ) {
467 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
468 }.bind( this ) );
469
470 this.$element.find( '[data-highlightedFilters]' )
471 .removeAttr( 'title' )
472 .removeAttr( 'data-highlightedFilters' );
473
474 // Remove grey from enhanced rows
475 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
476 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
477
478 // Turn off highlights
479 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
480 };
481 }( mediaWiki ) );