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