Merge "Show a warning in edit preview when a template loop is detected"
[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 $( '.rcfilters-spinner' ).removeClass( 'mw-rcfilters-ui-ready' );
113 this.$element.removeClass( 'mw-rcfilters-ui-ready' );
114 };
115
116 /**
117 * Respond to changes list model update
118 *
119 * @param {jQuery|string} $changesListContent The content of the updated changes list
120 * @param {jQuery} $fieldset The content of the updated fieldset
121 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
122 * @param {boolean} from Timestamp of the new changes
123 */
124 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function (
125 $changesListContent, $fieldset, isInitialDOM, from
126 ) {
127 var conflictItem,
128 $message = $( '<div>' )
129 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
130 isEmpty = $changesListContent === 'NO_RESULTS',
131 // For enhanced mode, we have to load these modules, which are
132 // not loaded for the 'regular' mode in the backend
133 loaderPromise = mw.user.options.get( 'usenewrc' ) ?
134 mw.loader.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
135 $.Deferred().resolve(),
136 widget = this;
137
138 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
139 if ( isEmpty ) {
140 this.$element.empty();
141
142 if ( this.filtersViewModel.hasConflict() ) {
143 conflictItem = this.filtersViewModel.getFirstConflictedItem();
144
145 $message
146 .append(
147 $( '<div>' )
148 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
149 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
150 $( '<div>' )
151 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
152 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
153 );
154 } else {
155 $message
156 .append(
157 $( '<div>' )
158 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
159 .text( mw.message( 'recentchanges-noresult' ).text() )
160 );
161 }
162
163 this.$element.append( $message );
164 } else {
165 if ( !isInitialDOM ) {
166 this.$element.empty().append( $changesListContent );
167
168 if ( from ) {
169 this.emphasizeNewChanges( from );
170 }
171 }
172
173 // Set up highlight containers
174 this.setupHighlightContainers( this.$element );
175
176 // Apply highlight
177 this.applyHighlight();
178
179 }
180
181 loaderPromise.done( function () {
182 if ( !isInitialDOM && !isEmpty ) {
183 // Make sure enhanced RC re-initializes correctly
184 mw.hook( 'wikipage.content' ).fire( widget.$element );
185 }
186
187 $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
188 widget.$element.addClass( 'mw-rcfilters-ui-ready' );
189 } );
190 };
191
192 /**
193 * Emphasize the elements (or groups) newer than the 'from' parameter
194 * @param {string} from Anything newer than this is considered 'new'
195 */
196 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.emphasizeNewChanges = function ( from ) {
197 var $firstNew,
198 $indicator,
199 $newChanges = $( [] ),
200 selector = this.inEnhancedMode() ?
201 'table.mw-enhanced-rc[data-mw-ts]' :
202 'li[data-mw-ts]',
203 set = this.$element.find( selector ),
204 length = set.length;
205
206 set.each( function ( index ) {
207 var $this = $( this ),
208 ts = $this.data( 'mw-ts' );
209
210 if ( ts >= from ) {
211 $newChanges = $newChanges.add( $this );
212 $firstNew = $this;
213
214 // guards against putting the marker after the last element
215 if ( index === ( length - 1 ) ) {
216 $firstNew = null;
217 }
218 }
219 } );
220
221 if ( $firstNew ) {
222 $indicator = $( '<div>' )
223 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
224
225 $firstNew.after( $indicator );
226 }
227
228 $newChanges
229 .hide()
230 .fadeIn( 1000 );
231 };
232
233 /**
234 * Respond to changes list model newChangesExist
235 *
236 * @param {boolean} newChangesExist Whether new changes exist
237 */
238 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
239 this.showNewChangesLink.toggle( newChangesExist );
240 };
241
242 /**
243 * Respond to the user clicking the 'show new changes' button
244 */
245 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
246 this.controller.showNewChanges();
247 };
248
249 /**
250 * Setup the container for the 'new changes' button.
251 *
252 * @param {jQuery} $content
253 */
254 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
255 this.showNewChangesLink = new OO.ui.ButtonWidget( {
256 framed: false,
257 label: mw.message( 'rcfilters-show-new-changes' ).text(),
258 flags: [ 'progressive' ]
259 } );
260 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
261 this.showNewChangesLink.toggle( false );
262
263 $content.before(
264 $( '<div>' )
265 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
266 .append( this.showNewChangesLink.$element )
267 );
268 };
269
270 /**
271 * Set up the highlight containers with all color circle indicators.
272 *
273 * @param {jQuery|string} $content The content of the updated changes list
274 */
275 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
276 var $enhancedTopPageCell, $enhancedNestedPagesCell,
277 widget = this,
278 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
279 $highlights = $( '<div>' )
280 .addClass( highlightClass )
281 .append(
282 $( '<div>' )
283 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
284 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
285 .prop( 'data-color', 'none' )
286 );
287
288 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
289 // Already set up
290 return;
291 }
292
293 mw.rcfilters.HighlightColors.forEach( function ( color ) {
294 $highlights.append(
295 $( '<div>' )
296 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
297 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
298 .prop( 'data-color', color )
299 );
300 } );
301
302 if ( this.inEnhancedMode() ) {
303 $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
304 $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
305
306 // Enhanced RC highlight containers
307 $content.find( 'table.mw-enhanced-rc tr:first-child' )
308 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
309 .prepend(
310 $( '<td>' )
311 .append( $highlights.clone() )
312 );
313
314 // We are adding and changing cells in a table that, despite having nested rows,
315 // is actually all one big table. To do that right, we want to remove the 'placeholder'
316 // cell from the top row, because we're actually adding that placeholder in the children
317 // with the highlights.
318 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-changeslist-line-prefix' )
319 .detach();
320 $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
321 .prop( 'colspan', '2' );
322
323 $enhancedNestedPagesCell
324 .before(
325 $( '<td>' )
326 .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
327 );
328
329 // We need to target the nested rows differently than the top rows so that the
330 // LESS rules applies correctly. In top rows, the rule should highlight all but
331 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
332 // should highlight all but the first 3 cells td:not( :nth-child( -n+3 )
333 $enhancedNestedPagesCell
334 .closest( 'tr' )
335 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
336
337 // Go over pages that have sub results
338 // HACK: We really only can collect those by targetting the collapsible class
339 $enhancedTopPageCell.each( function () {
340 var collectedClasses,
341 $table = $( this );
342
343 // Go over <tr>s and pick up all recognized classes
344 collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
345 return $table.find( 'tr' ).hasClass( className );
346 } );
347
348 $table.find( 'tr:first-child' )
349 .addClass( collectedClasses.join( ' ' ) );
350 } );
351
352 $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
353 } else {
354 // Regular RC
355 $content.find( 'ul.special li' )
356 .prepend( $highlights.clone() );
357 }
358 };
359
360 /**
361 * In enhanced mode, we need to check whether the grouped results all have the
362 * same active highlights in order to see whether the "parent" of the group should
363 * be grey or highlighted normally.
364 *
365 * This is called every time highlights are applied.
366 */
367 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
368 var activeHighlightClasses,
369 $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
370
371 activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
372 return 'mw-rcfilters-highlight-color-' + color;
373 } );
374
375 // Go over top pages and their children, and figure out if all sub-pages have the
376 // same highlights between themselves. If they do, the parent should be highlighted
377 // with all colors. If classes are different, the parent should receive a grey
378 // background
379 $enhancedTopPageCell.each( function () {
380 var firstChildClasses, $rowsWithDifferentHighlights,
381 $table = $( this );
382
383 // Collect the relevant classes from the first nested child
384 firstChildClasses = activeHighlightClasses.filter( function ( className ) {
385 return $table.find( 'tr:nth-child(2)' ).hasClass( className );
386 } );
387 // Filter the non-head rows and see if they all have the same classes
388 // to the first row
389 $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
390 var classesInThisRow,
391 $this = $( this );
392
393 classesInThisRow = activeHighlightClasses.filter( function ( className ) {
394 return $this.hasClass( className );
395 } );
396
397 return !OO.compare( firstChildClasses, classesInThisRow );
398 } );
399
400 // If classes are different, tag the row for using grey color
401 $table.find( 'tr:first-child' )
402 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
403 } );
404 };
405
406 /**
407 * @return {boolean} Whether the changes are grouped by page
408 */
409 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
410 var uri = new mw.Uri();
411 return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
412 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
413 };
414
415 /**
416 * Apply color classes based on filters highlight configuration
417 */
418 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
419 if ( !this.filtersViewModel.isHighlightEnabled() ) {
420 return;
421 }
422
423 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
424 var $elements = this.$element.find( '.' + filterItem.getCssClass() );
425
426 // Add highlight class to all highlighted list items
427 $elements
428 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
429
430 $elements.each( function () {
431 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
432 filters = filterString ? filterString.split( '|' ) : [];
433
434 if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
435 filters.push( filterItem.getLabel() );
436 }
437
438 $( this )
439 .attr( 'data-highlightedFilters', filters.join( '|' ) );
440 } );
441 }.bind( this ) );
442 // Apply a title for relevant filters
443 this.$element.find( '[data-highlightedFilters]' ).each( function () {
444 var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
445 filters = filterString ? filterString.split( '|' ) : [];
446
447 if ( filterString ) {
448 $( this ).attr( 'title', mw.msg( 'rcfilters-highlighted-filters-list', filters.join( ', ' ) ) );
449 }
450 } );
451
452 if ( this.inEnhancedMode() ) {
453 this.updateEnhancedParentHighlight();
454 }
455
456 // Turn on highlights
457 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
458 };
459
460 /**
461 * Remove all color classes
462 */
463 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
464 // Remove highlight classes
465 mw.rcfilters.HighlightColors.forEach( function ( color ) {
466 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
467 }.bind( this ) );
468
469 this.$element.find( '[data-highlightedFilters]' )
470 .removeAttr( 'title' )
471 .removeAttr( 'data-highlightedFilters' );
472
473 // Remove grey from enhanced rows
474 this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
475 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
476
477 // Turn off highlights
478 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
479 };
480 }( mediaWiki ) );