Show redirect fragments on Special:ListRedirects
[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
32 // Events
33 this.filtersViewModel.connect( this, {
34 itemUpdate: 'onItemUpdate',
35 highlightChange: 'onHighlightChange'
36 } );
37 this.changesListViewModel.connect( this, {
38 invalidate: 'onModelInvalidate',
39 update: 'onModelUpdate',
40 newChangesExist: 'onNewChangesExist'
41 } );
42
43 this.$element
44 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
45 // We handle our own display/hide of the empty results message
46 .removeClass( 'mw-changeslist-empty' );
47
48 // Set up highlight containers
49 this.setupHighlightContainers( this.$element );
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 the highlight feature being toggled on and off
60 *
61 * @param {boolean} highlightEnabled
62 */
63 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onHighlightChange = function ( highlightEnabled ) {
64 if ( highlightEnabled ) {
65 this.applyHighlight();
66 } else {
67 this.clearHighlight();
68 }
69 };
70
71 /**
72 * Respond to a filter item model update
73 */
74 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
75 if ( this.filtersViewModel.isHighlightEnabled() ) {
76 this.clearHighlight();
77 this.applyHighlight();
78 }
79 };
80
81 /**
82 * Respond to changes list model invalidate
83 */
84 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
85 $( '.rcfilters-spinner' ).removeClass( 'mw-rcfilters-ui-ready' );
86 this.$element.removeClass( 'mw-rcfilters-ui-ready' );
87 };
88
89 /**
90 * Respond to changes list model update
91 *
92 * @param {jQuery|string} $changesListContent The content of the updated changes list
93 * @param {jQuery} $fieldset The content of the updated fieldset
94 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
95 * @param {boolean} from Timestamp of the new changes
96 */
97 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function (
98 $changesListContent, $fieldset, isInitialDOM, from
99 ) {
100 var conflictItem,
101 $message = $( '<div>' )
102 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
103 isEmpty = $changesListContent === 'NO_RESULTS',
104 $lastSeen,
105 $indicator,
106 $newChanges = $( [] );
107
108 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
109 if ( isEmpty ) {
110 this.$element.empty();
111
112 if ( this.filtersViewModel.hasConflict() ) {
113 conflictItem = this.filtersViewModel.getFirstConflictedItem();
114
115 $message
116 .append(
117 $( '<div>' )
118 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
119 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
120 $( '<div>' )
121 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
122 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
123 );
124 } else {
125 $message
126 .append(
127 $( '<div>' )
128 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
129 .text( mw.message( 'recentchanges-noresult' ).text() )
130 );
131 }
132
133 this.$element.append( $message );
134 } else {
135 if ( !isInitialDOM ) {
136 this.$element.empty().append( $changesListContent );
137
138 if ( from ) {
139 $lastSeen = null;
140 this.$element.find( 'li[data-mw-ts]' ).each( function () {
141 var $li = $( this ),
142 ts = $li.data( 'mw-ts' );
143
144 if ( ts >= from ) {
145 $newChanges = $newChanges.add( $li );
146 } else if ( $lastSeen === null ) {
147 $lastSeen = $li;
148 return false;
149 }
150 } );
151
152 if ( $lastSeen ) {
153 $indicator = $( '<div>' )
154 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' )
155 .text( mw.message( 'rcfilters-previous-changes-label' ).text() );
156
157 $indicator.on( 'click', function () {
158 $indicator.detach();
159 } );
160
161 $lastSeen.before( $indicator );
162 }
163
164 $newChanges
165 .hide()
166 .fadeIn( 1000 );
167 }
168 }
169
170 // Set up highlight containers
171 this.setupHighlightContainers( this.$element );
172
173 // Apply highlight
174 this.applyHighlight();
175
176 if ( !isInitialDOM ) {
177 // Make sure enhanced RC re-initializes correctly
178 mw.hook( 'wikipage.content' ).fire( this.$element );
179 }
180 }
181
182 $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
183 this.$element.addClass( 'mw-rcfilters-ui-ready' );
184 };
185
186 /**
187 * Respond to changes list model newChangesExist
188 *
189 * @param {boolean} newChangesExist Whether new changes exist
190 */
191 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
192 this.showNewChangesLink.toggle( newChangesExist );
193 };
194
195 /**
196 * Respond to the user clicking the 'show new changes' button
197 */
198 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
199 this.controller.showNewChanges();
200 };
201
202 /**
203 * Setup the container for the 'new changes' button.
204 *
205 * @param {jQuery} $content
206 */
207 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
208 this.showNewChangesLink = new OO.ui.ButtonWidget( {
209 framed: false,
210 label: mw.message( 'rcfilters-show-new-changes' ).text(),
211 flags: [ 'progressive' ]
212 } );
213 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
214 this.showNewChangesLink.toggle( false );
215
216 $content.before(
217 $( '<div>' )
218 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
219 .append( this.showNewChangesLink.$element )
220 );
221 };
222
223 /**
224 * Set up the highlight containers with all color circle indicators.
225 *
226 * @param {jQuery|string} $content The content of the updated changes list
227 */
228 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
229 var uri = new mw.Uri(),
230 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
231 $highlights = $( '<div>' )
232 .addClass( highlightClass )
233 .append(
234 $( '<div>' )
235 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
236 .prop( 'data-color', 'none' )
237 );
238
239 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
240 // Already set up
241 return;
242 }
243
244 mw.rcfilters.HighlightColors.forEach( function ( color ) {
245 $highlights.append(
246 $( '<div>' )
247 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
248 .prop( 'data-color', color )
249 );
250 } );
251
252 if (
253 ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
254 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) )
255 ) {
256 // Enhanced RC
257 $content.find( 'td.mw-enhanced-rc' )
258 .parent()
259 .prepend(
260 $( '<td>' )
261 .append( $highlights.clone() )
262 );
263 } else {
264 // Regular RC
265 $content.find( 'ul.special li' )
266 .prepend( $highlights.clone() );
267 }
268 };
269
270 /**
271 * Apply color classes based on filters highlight configuration
272 */
273 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
274 if ( !this.filtersViewModel.isHighlightEnabled() ) {
275 return;
276 }
277
278 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
279 // Add highlight class to all highlighted list items
280 this.$element.find( '.' + filterItem.getCssClass() )
281 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
282 }.bind( this ) );
283
284 // Turn on highlights
285 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
286 };
287
288 /**
289 * Remove all color classes
290 */
291 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
292 // Remove highlight classes
293 mw.rcfilters.HighlightColors.forEach( function ( color ) {
294 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
295 }.bind( this ) );
296
297 // Turn off highlights
298 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
299 };
300 }( mediaWiki ) );