Merge "Skin: Make skins aware of their registered skin name"
[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();
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 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function () {
251 this.showNewChangesLink = new OO.ui.ButtonWidget( {
252 framed: false,
253 label: mw.message( 'rcfilters-show-new-changes' ).text(),
254 flags: [ 'progressive' ]
255 } );
256 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
257 this.showNewChangesLink.toggle( false );
258
259 // HACK: Add the -newChanges div inside rcfilters-head, rather than right above us
260 // Visually it's the same place, but by putting it inside rcfilters-head we are
261 // able to use the min-height rule to prevent the page from jumping when this is added.
262 this.$element.parent().find( '.rcfilters-head' ).append(
263 $( '<div>' )
264 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
265 .append( this.showNewChangesLink.$element )
266 );
267 };
268
269 /**
270 * Set up the highlight containers with all color circle indicators.
271 *
272 * @param {jQuery|string} $content The content of the updated changes list
273 */
274 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
275 var $enhancedTopPageCell, $enhancedNestedPagesCell,
276 widget = this,
277 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
278 $highlights = $( '<div>' )
279 .addClass( highlightClass )
280 .append(
281 $( '<div>' )
282 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
283 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
284 .prop( 'data-color', 'none' )
285 );
286
287 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
288 // Already set up
289 return;
290 }
291
292 mw.rcfilters.HighlightColors.forEach( function ( color ) {
293 $highlights.append(
294 $( '<div>' )
295 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
296 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
297 .prop( 'data-color', color )
298 );
299 } );
300
301 if ( this.inEnhancedMode() ) {
302 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
303 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
304
305 // Enhanced RC highlight containers
306 $content.find( 'table.mw-enhanced-rc tr:first-child' )
307 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
308 .prepend(
309 $( '<td>' )
310 .append( $highlights.clone() )
311 );
312
313 // We are adding and changing cells in a table that, despite having nested rows,
314 // is actually all one big table. To prevent the highlights cell in the "nested"
315 // rows from stretching out the cell with the flags and timestamp in the top row,
316 // we give the latter colspan=2. Then to make things line up again, we add
317 // an empty <td> to the "nested" rows.
318
319 // Set colspan=2 on cell with flags and timestamp in top row
320 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
321 .prop( 'colspan', '2' );
322 // Add empty <td> to nested rows to compensate
323 $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
324 // Add highlights cell to nested rows
325 $enhancedNestedPagesCell
326 .before(
327 $( '<td>' )
328 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
329 );
330
331 // We need to target the nested rows differently than the top rows so that the
332 // LESS rules applies correctly. In top rows, the rule should highlight all but
333 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
334 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
335 $enhancedNestedPagesCell
336 .closest( 'tr' )
337 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
338
339 // Go over pages that have sub results
340 // HACK: We really only can collect those by targetting the collapsible class
341 $enhancedTopPageCell.each( function () {
342 var collectedClasses,
343 $table = $( this );
344
345 // Go over <tr>s and pick up all recognized classes
346 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
347 return $table.find( 'tr' ).hasClass( className );
348 } );
349
350 $table.find( 'tr:first-child' )
351 .addClass( collectedClasses.join( ' ' ) );
352 } );
353
354 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
355 } else {
356 // Regular RC
357 $content.find( 'ul.special li' )
358 .prepend( $highlights.clone() );
359 }
360 };
361
362 /**
363 * In enhanced mode, we need to check whether the grouped results all have the
364 * same active highlights in order to see whether the "parent" of the group should
365 * be grey or highlighted normally.
366 *
367 * This is called every time highlights are applied.
368 */
369 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
370 var activeHighlightClasses,
371 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
372
373 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
374 return 'mw-rcfilters-highlight-color-' + color;
375 } );
376
377 // Go over top pages and their children, and figure out if all sub-pages have the
378 // same highlights between themselves. If they do, the parent should be highlighted
379 // with all colors. If classes are different, the parent should receive a grey
380 // background
381 $enhancedTopPageCell.each( function () {
382 var firstChildClasses, $rowsWithDifferentHighlights,
383 $table = $( this );
384
385 // Collect the relevant classes from the first nested child
386 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
387 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
388 } );
389 // Filter the non-head rows and see if they all have the same classes
390 // to the first row
391 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
392 var classesInThisRow,
393 $this = $( this );
394
395 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
396 return $this.hasClass( className );
397 } );
398
399 return !OO.compare( firstChildClasses, classesInThisRow );
400 } );
401
402 // If classes are different, tag the row for using grey color
403 $table.find( 'tr:first-child' )
404 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
405 } );
406 };
407
408 /**
409 * @return {boolean} Whether the changes are grouped by page
410 */
411 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
412 var uri = new mw.Uri();
413 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
414 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
415 };
416
417 /**
418 * Apply color classes based on filters highlight configuration
419 */
420 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
421 if ( !this.filtersViewModel.isHighlightEnabled() ) {
422 return;
423 }
424
425 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
426 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
427
428 // Add highlight class to all highlighted list items
429 $elements
430 .addClass(
431 'mw-rcfilters-highlighted ' +
432 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor()
433 );
434
435 // Track the filters for each item in .data( 'highlightedFilters' )
436 $elements.each( function () {
437 var filters = $( this ).data( 'highlightedFilters' );
438 if ( !filters ) {
439 filters = [];
440 $( this ).data( 'highlightedFilters', filters );
441 }
442 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
443 filters.push( filterItem.getLabel() );
444 }
445 } );
446 }.bind( this ) );
447 // Apply a title to each highlighted item, with a list of filters
448 this.$element.find( '.mw-rcfilters-highlighted' ).each( function () {
449 var filters = $( this ).data( 'highlightedFilters' );
450
451 if ( filters && filters.length ) {
452 $( this ).attr( 'title', mw.msg(
453 'rcfilters-highlighted-filters-list',
454 filters.join( mw.msg( 'comma-separator' ) )
455 ) );
456 }
457
458 } );
459 if ( this.inEnhancedMode() ) {
460 this.updateEnhancedParentHighlight();
461 }
462
463 // Turn on highlights
464 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
465 };
466
467 /**
468 * Remove all color classes
469 */
470 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
471 // Remove highlight classes
472 mw.rcfilters.HighlightColors.forEach( function ( color ) {
473 this.$element
474 .find( '.mw-rcfilters-highlight-color-' + color )
475 .removeClass( 'mw-rcfilters-highlight-color-' + color );
476 }.bind( this ) );
477
478 this.$element.find( '.mw-rcfilters-highlighted' )
479 .removeAttr( 'title' )
480 .removeData( 'highlightedFilters' )
481 .removeClass( 'mw-rcfilters-highlighted' );
482
483 // Remove grey from enhanced rows
484 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
485 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
486
487 // Turn off highlights
488 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
489 };
490 }( mediaWiki ) );