Merge "Change php extract() to explicit code"
[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,
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.msg( this.getMsgKeyForNoResults( noResultsDetails ) ) )
159 );
160
161 this.$element.removeClass( 'mw-changeslist-timeout' );
162 this.$element.removeClass( 'mw-changeslist-notargetpage' );
163 }
164
165 this.$element.append( $message );
166 } else {
167 if ( !isInitialDOM ) {
168 this.$element.empty().append( $changesListContent );
169
170 if ( from ) {
171 this.emphasizeNewChanges( from );
172 }
173 }
174
175 // Set up highlight containers
176 this.setupHighlightContainers( this.$element );
177
178 // Apply highlight
179 this.applyHighlight();
180
181 }
182
183 this.$element.prepend( $( '<div>' ).addClass( 'mw-changeslist-overlay' ) );
184
185 loaderPromise.done( function () {
186 if ( !isInitialDOM && !isEmpty ) {
187 // Make sure enhanced RC re-initializes correctly
188 mw.hook( 'wikipage.content' ).fire( widget.$element );
189 }
190
191 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
192 } );
193 };
194
195 /** Toggles overlay class on changes list
196 *
197 * @param {boolean} isVisible True if overlay should be visible
198 */
199 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.toggleOverlay = function ( isVisible ) {
200 this.$element.toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget--overlaid', isVisible );
201 };
202
203 /**
204 * Map a reason for having no results to its message key
205 *
206 * @param {string} reason One of the NO_RESULTS_* "constant" that represent
207 * a reason for having no results
208 * @return {string} Key for the message that explains why there is no results in this case
209 */
210 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.getMsgKeyForNoResults = function ( reason ) {
211 var reasonMsgKeyMap = {
212 NO_RESULTS_NORMAL: 'recentchanges-noresult',
213 NO_RESULTS_TIMEOUT: 'recentchanges-timeout',
214 NO_RESULTS_NETWORK_ERROR: 'recentchanges-network',
215 NO_RESULTS_NO_TARGET_PAGE: 'recentchanges-notargetpage'
216 };
217 return reasonMsgKeyMap[ reason ];
218 };
219
220 /**
221 * Emphasize the elements (or groups) newer than the 'from' parameter
222 * @param {string} from Anything newer than this is considered 'new'
223 */
224 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.emphasizeNewChanges = function ( from ) {
225 var $firstNew,
226 $indicator,
227 $newChanges = $( [] ),
228 selector = this.inEnhancedMode() ?
229 'table.mw-enhanced-rc[data-mw-ts]' :
230 'li[data-mw-ts]',
231 set = this.$element.find( selector ),
232 length = set.length;
233
234 set.each( function ( index ) {
235 var $this = $( this ),
236 ts = $this.data( 'mw-ts' );
237
238 if ( ts >= from ) {
239 $newChanges = $newChanges.add( $this );
240 $firstNew = $this;
241
242 // guards against putting the marker after the last element
243 if ( index === ( length - 1 ) ) {
244 $firstNew = null;
245 }
246 }
247 } );
248
249 if ( $firstNew ) {
250 $indicator = $( '<div>' )
251 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
252
253 $firstNew.after( $indicator );
254 }
255
256 $newChanges
257 .hide()
258 .fadeIn( 1000 );
259 };
260
261 /**
262 * Set up the highlight containers with all color circle indicators.
263 *
264 * @param {jQuery|string} $content The content of the updated changes list
265 */
266 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
267 var $enhancedTopPageCell, $enhancedNestedPagesCell,
268 widget = this,
269 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
270 $highlights = $( '<div>' )
271 .addClass( highlightClass )
272 .append(
273 $( '<div>' )
274 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
275 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
276 .prop( 'data-color', 'none' )
277 );
278
279 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
280 // Already set up
281 return;
282 }
283
284 mw.rcfilters.HighlightColors.forEach( function ( color ) {
285 $highlights.append(
286 $( '<div>' )
287 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
288 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
289 .prop( 'data-color', color )
290 );
291 } );
292
293 if ( this.inEnhancedMode() ) {
294 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
295 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
296
297 // Enhanced RC highlight containers
298 $content.find( 'table.mw-enhanced-rc tr:first-child' )
299 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
300 .prepend(
301 $( '<td>' )
302 .append( $highlights.clone() )
303 );
304
305 // We are adding and changing cells in a table that, despite having nested rows,
306 // is actually all one big table. To prevent the highlights cell in the "nested"
307 // rows from stretching out the cell with the flags and timestamp in the top row,
308 // we give the latter colspan=2. Then to make things line up again, we add
309 // an empty <td> to the "nested" rows.
310
311 // Set colspan=2 on cell with flags and timestamp in top row
312 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
313 .prop( 'colspan', '2' );
314 // Add empty <td> to nested rows to compensate
315 $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
316 // Add highlights cell to nested rows
317 $enhancedNestedPagesCell
318 .before(
319 $( '<td>' )
320 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
321 );
322
323 // We need to target the nested rows differently than the top rows so that the
324 // LESS rules applies correctly. In top rows, the rule should highlight all but
325 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
326 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
327 $enhancedNestedPagesCell
328 .closest( 'tr' )
329 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
330
331 // Go over pages that have sub results
332 // HACK: We really only can collect those by targetting the collapsible class
333 $enhancedTopPageCell.each( function () {
334 var collectedClasses,
335 $table = $( this );
336
337 // Go over <tr>s and pick up all recognized classes
338 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
339 return $table.find( 'tr' ).hasClass( className );
340 } );
341
342 $table.find( 'tr:first-child' )
343 .addClass( collectedClasses.join( ' ' ) );
344 } );
345
346 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
347 } else {
348 // Regular RC
349 $content.find( 'ul.special li' )
350 .prepend( $highlights.clone() );
351
352 $content.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
353 }
354 };
355
356 /**
357 * In enhanced mode, we need to check whether the grouped results all have the
358 * same active highlights in order to see whether the "parent" of the group should
359 * be grey or highlighted normally.
360 *
361 * This is called every time highlights are applied.
362 */
363 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
364 var activeHighlightClasses,
365 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
366
367 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
368 return 'mw-rcfilters-highlight-color-' + color;
369 } );
370
371 // Go over top pages and their children, and figure out if all sub-pages have the
372 // same highlights between themselves. If they do, the parent should be highlighted
373 // with all colors. If classes are different, the parent should receive a grey
374 // background
375 $enhancedTopPageCell.each( function () {
376 var firstChildClasses, $rowsWithDifferentHighlights,
377 $table = $( this );
378
379 // Collect the relevant classes from the first nested child
380 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
381 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
382 } );
383 // Filter the non-head rows and see if they all have the same classes
384 // to the first row
385 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
386 var classesInThisRow,
387 $this = $( this );
388
389 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
390 return $this.hasClass( className );
391 } );
392
393 return !OO.compare( firstChildClasses, classesInThisRow );
394 } );
395
396 // If classes are different, tag the row for using grey color
397 $table.find( 'tr:first-child' )
398 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
399 } );
400 };
401
402 /**
403 * @return {boolean} Whether the changes are grouped by page
404 */
405 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
406 var uri = new mw.Uri();
407 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
408 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
409 };
410
411 /**
412 * Apply color classes based on filters highlight configuration
413 */
414 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
415 if ( !this.filtersViewModel.isHighlightEnabled() ) {
416 return;
417 }
418
419 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
420 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
421
422 // Add highlight class to all highlighted list items
423 $elements
424 .addClass(
425 'mw-rcfilters-highlighted ' +
426 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor()
427 );
428
429 // Track the filters for each item in .data( 'highlightedFilters' )
430 $elements.each( function () {
431 var filters = $( this ).data( 'highlightedFilters' );
432 if ( !filters ) {
433 filters = [];
434 $( this ).data( 'highlightedFilters', filters );
435 }
436 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
437 filters.push( filterItem.getLabel() );
438 }
439 } );
440 }.bind( this ) );
441 // Apply a title to each highlighted item, with a list of filters
442 this.$element.find( '.mw-rcfilters-highlighted' ).each( function () {
443 var filters = $( this ).data( 'highlightedFilters' );
444
445 if ( filters && filters.length ) {
446 $( this ).attr( 'title', mw.msg(
447 'rcfilters-highlighted-filters-list',
448 filters.join( mw.msg( 'comma-separator' ) )
449 ) );
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
468 .find( '.mw-rcfilters-highlight-color-' + color )
469 .removeClass( 'mw-rcfilters-highlight-color-' + color );
470 }.bind( this ) );
471
472 this.$element.find( '.mw-rcfilters-highlighted' )
473 .removeAttr( 'title' )
474 .removeData( 'highlightedFilters' )
475 .removeClass( 'mw-rcfilters-highlighted' );
476
477 // Remove grey from enhanced rows
478 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
479 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
480
481 // Turn off highlights
482 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
483 };
484 }( mediaWiki ) );