Merge "Live Preview: Cope with the edit summary being an OOjs UI widget"
[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 // For enhanced mode, we have to load these modules, which are
108 // not loaded for the 'regular' mode in the backend
109 loaderPromise = mw.user.options.get( 'usenewrc' ) ?
110 mw.loader.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
111 $.Deferred().resolve(),
112 widget = this;
113
114 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
115 if ( isEmpty ) {
116 this.$element.empty();
117
118 if ( this.filtersViewModel.hasConflict() ) {
119 conflictItem = this.filtersViewModel.getFirstConflictedItem();
120
121 $message
122 .append(
123 $( '<div>' )
124 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
125 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
126 $( '<div>' )
127 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
128 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
129 );
130 } else {
131 $message
132 .append(
133 $( '<div>' )
134 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
135 .text( mw.message( 'recentchanges-noresult' ).text() )
136 );
137 }
138
139 this.$element.append( $message );
140 } else {
141 if ( !isInitialDOM ) {
142 this.$element.empty().append( $changesListContent );
143
144 if ( from ) {
145 $lastSeen = null;
146 this.$element.find( 'li[data-mw-ts]' ).each( function () {
147 var $li = $( this ),
148 ts = $li.data( 'mw-ts' );
149
150 if ( ts >= from ) {
151 $newChanges = $newChanges.add( $li );
152 } else if ( $lastSeen === null ) {
153 $lastSeen = $li;
154 return false;
155 }
156 } );
157
158 if ( $lastSeen ) {
159 $indicator = $( '<div>' )
160 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' )
161 .text( mw.message( 'rcfilters-previous-changes-label' ).text() );
162
163 $indicator.on( 'click', function () {
164 $indicator.detach();
165 } );
166
167 $lastSeen.before( $indicator );
168 }
169
170 $newChanges
171 .hide()
172 .fadeIn( 1000 );
173 }
174 }
175
176 // Set up highlight containers
177 this.setupHighlightContainers( this.$element );
178
179 // Apply highlight
180 this.applyHighlight();
181
182 }
183
184 loaderPromise.done( function () {
185 if ( !isInitialDOM && !isEmpty ) {
186 // Make sure enhanced RC re-initializes correctly
187 mw.hook( 'wikipage.content' ).fire( widget.$element );
188 }
189
190 $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
191 widget.$element.addClass( 'mw-rcfilters-ui-ready' );
192 } );
193 };
194
195 /**
196 * Respond to changes list model newChangesExist
197 *
198 * @param {boolean} newChangesExist Whether new changes exist
199 */
200 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
201 this.showNewChangesLink.toggle( newChangesExist );
202 };
203
204 /**
205 * Respond to the user clicking the 'show new changes' button
206 */
207 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
208 this.controller.showNewChanges();
209 };
210
211 /**
212 * Setup the container for the 'new changes' button.
213 *
214 * @param {jQuery} $content
215 */
216 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
217 this.showNewChangesLink = new OO.ui.ButtonWidget( {
218 framed: false,
219 label: mw.message( 'rcfilters-show-new-changes' ).text(),
220 flags: [ 'progressive' ]
221 } );
222 this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
223 this.showNewChangesLink.toggle( false );
224
225 $content.before(
226 $( '<div>' )
227 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
228 .append( this.showNewChangesLink.$element )
229 );
230 };
231
232 /**
233 * Set up the highlight containers with all color circle indicators.
234 *
235 * @param {jQuery|string} $content The content of the updated changes list
236 */
237 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
238 var uri = new mw.Uri(),
239 highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
240 $highlights = $( '<div>' )
241 .addClass( highlightClass )
242 .append(
243 $( '<div>' )
244 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
245 .prop( 'data-color', 'none' )
246 );
247
248 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
249 // Already set up
250 return;
251 }
252
253 mw.rcfilters.HighlightColors.forEach( function ( color ) {
254 $highlights.append(
255 $( '<div>' )
256 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
257 .prop( 'data-color', color )
258 );
259 } );
260
261 if (
262 ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
263 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) )
264 ) {
265 // Enhanced RC
266 $content.find( 'td.mw-enhanced-rc' )
267 .parent()
268 .prepend(
269 $( '<td>' )
270 .append( $highlights.clone() )
271 );
272 } else {
273 // Regular RC
274 $content.find( 'ul.special li' )
275 .prepend( $highlights.clone() );
276 }
277 };
278
279 /**
280 * Apply color classes based on filters highlight configuration
281 */
282 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
283 if ( !this.filtersViewModel.isHighlightEnabled() ) {
284 return;
285 }
286
287 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
288 // Add highlight class to all highlighted list items
289 this.$element.find( '.' + filterItem.getCssClass() )
290 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
291 }.bind( this ) );
292
293 // Turn on highlights
294 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
295 };
296
297 /**
298 * Remove all color classes
299 */
300 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
301 // Remove highlight classes
302 mw.rcfilters.HighlightColors.forEach( function ( color ) {
303 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
304 }.bind( this ) );
305
306 // Turn off highlights
307 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
308 };
309 }( mediaWiki ) );