RCFilters: Respect ?enhanced=0 url regardless of preference
[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 * @mixins OO.ui.mixin.PendingElement
7 *
8 * @constructor
9 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel View model
10 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListViewModel View model
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 $changesListRoot,
18 config
19 ) {
20 config = $.extend( {}, config, {
21 $element: $changesListRoot
22 } );
23
24 // Parent
25 mw.rcfilters.ui.ChangesListWrapperWidget.parent.call( this, config );
26 // Mixin constructors
27 OO.ui.mixin.PendingElement.call( this, config );
28
29 this.filtersViewModel = filtersViewModel;
30 this.changesListViewModel = changesListViewModel;
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 } );
41
42 this.$element
43 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
44 // We handle our own display/hide of the empty results message
45 .removeClass( 'mw-changeslist-empty' );
46
47 // Set up highlight containers
48 this.setupHighlightContainers( this.$element );
49 };
50
51 /* Initialization */
52
53 OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
54 OO.mixinClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.mixin.PendingElement );
55
56 /**
57 * Respond to the highlight feature being toggled on and off
58 *
59 * @param {boolean} highlightEnabled
60 */
61 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onHighlightChange = function ( highlightEnabled ) {
62 if ( highlightEnabled ) {
63 this.applyHighlight();
64 } else {
65 this.clearHighlight();
66 }
67 };
68
69 /**
70 * Respond to a filter item model update
71 */
72 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
73 if ( this.filtersViewModel.isHighlightEnabled() ) {
74 this.clearHighlight();
75 this.applyHighlight();
76 }
77 };
78
79 /**
80 * Respond to changes list model invalidate
81 */
82 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
83 this.pushPending();
84 };
85
86 /**
87 * Respond to changes list model update
88 *
89 * @param {jQuery|string} $changesListContent The content of the updated changes list
90 */
91 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function ( $changesListContent ) {
92 var conflictItem,
93 $message = $( '<div>' )
94 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
95 isEmpty = $changesListContent === 'NO_RESULTS';
96
97 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
98 if ( isEmpty ) {
99 this.$changesListContent = null;
100 this.$element.empty();
101
102 if ( this.filtersViewModel.hasConflict() ) {
103 conflictItem = this.filtersViewModel.getFirstConflictedItem();
104
105 $message
106 .append(
107 $( '<div>' )
108 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
109 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
110 $( '<div>' )
111 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
112 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
113 );
114 } else {
115 $message
116 .append(
117 $( '<div>' )
118 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
119 .text( mw.message( 'recentchanges-noresult' ).text() )
120 );
121 }
122
123 this.$element.append( $message );
124 } else {
125 this.$changesListContent = $changesListContent;
126 this.$element.empty().append( this.$changesListContent );
127 // Set up highlight containers
128 this.setupHighlightContainers( this.$element );
129
130 // Apply highlight
131 this.applyHighlight();
132
133 // Make sure enhanced RC re-initializes correctly
134 mw.hook( 'wikipage.content' ).fire( this.$element );
135 }
136 this.popPending();
137 };
138
139 /**
140 * Set up the highlight containers with all color circle indicators.
141 *
142 * @param {jQuery|string} $content The content of the updated changes list
143 */
144 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
145 var uri = new mw.Uri(),
146 $highlights = $( '<div>' )
147 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights' )
148 .append(
149 $( '<div>' )
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
151 .prop( 'data-color', 'none' )
152 );
153
154 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
155 // Already set up
156 return;
157 }
158
159 mw.rcfilters.HighlightColors.forEach( function ( color ) {
160 $highlights.append(
161 $( '<div>' )
162 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
163 .prop( 'data-color', color )
164 );
165 } );
166
167 if (
168 ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
169 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) )
170 ) {
171 // Enhanced RC
172 $content.find( 'td.mw-enhanced-rc' )
173 .parent()
174 .prepend(
175 $( '<td>' )
176 .append( $highlights.clone() )
177 );
178 } else {
179 // Regular RC
180 $content.find( 'ul.special li' )
181 .prepend( $highlights.clone() );
182 }
183 };
184
185 /**
186 * Apply color classes based on filters highlight configuration
187 */
188 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
189 if ( !this.filtersViewModel.isHighlightEnabled() ) {
190 return;
191 }
192
193 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
194 // Add highlight class to all highlighted list items
195 this.$element.find( '.' + filterItem.getCssClass() )
196 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
197 }.bind( this ) );
198
199 // Turn on highlights
200 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
201 };
202
203 /**
204 * Remove all color classes
205 */
206 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
207 // Remove highlight classes
208 mw.rcfilters.HighlightColors.forEach( function ( color ) {
209 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
210 }.bind( this ) );
211
212 // Turn off highlights
213 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
214 };
215 }( mediaWiki ) );