6512f04f68b2cdcbe5de867f60890ba4b12c12d9
[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 * @param {jQuery} $fieldset The content of the updated fieldset
91 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
92 */
93 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function ( $changesListContent, $fieldset, isInitialDOM ) {
94 var conflictItem,
95 $message = $( '<div>' )
96 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
97 isEmpty = $changesListContent === 'NO_RESULTS';
98
99 this.$element.toggleClass( 'mw-changeslist', !isEmpty );
100 if ( isEmpty ) {
101 this.$changesListContent = null;
102 this.$element.empty();
103
104 if ( this.filtersViewModel.hasConflict() ) {
105 conflictItem = this.filtersViewModel.getFirstConflictedItem();
106
107 $message
108 .append(
109 $( '<div>' )
110 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
111 .text( mw.message( 'rcfilters-noresults-conflict' ).text() ),
112 $( '<div>' )
113 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
114 .text( mw.message( conflictItem.getCurrentConflictResultMessage() ).text() )
115 );
116 } else {
117 $message
118 .append(
119 $( '<div>' )
120 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
121 .text( mw.message( 'recentchanges-noresult' ).text() )
122 );
123 }
124
125 this.$element.append( $message );
126 } else {
127 this.$changesListContent = $changesListContent;
128 this.$element.empty().append( this.$changesListContent );
129 // Set up highlight containers
130 this.setupHighlightContainers( this.$element );
131
132 // Apply highlight
133 this.applyHighlight();
134
135 if ( !isInitialDOM ) {
136 // Make sure enhanced RC re-initializes correctly
137 mw.hook( 'wikipage.content' ).fire( this.$element );
138 }
139 }
140 this.popPending();
141 };
142
143 /**
144 * Set up the highlight containers with all color circle indicators.
145 *
146 * @param {jQuery|string} $content The content of the updated changes list
147 */
148 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
149 var uri = new mw.Uri(),
150 $highlights = $( '<div>' )
151 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights' )
152 .append(
153 $( '<div>' )
154 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
155 .prop( 'data-color', 'none' )
156 );
157
158 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length ) {
159 // Already set up
160 return;
161 }
162
163 mw.rcfilters.HighlightColors.forEach( function ( color ) {
164 $highlights.append(
165 $( '<div>' )
166 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
167 .prop( 'data-color', color )
168 );
169 } );
170
171 if (
172 ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
173 ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) )
174 ) {
175 // Enhanced RC
176 $content.find( 'td.mw-enhanced-rc' )
177 .parent()
178 .prepend(
179 $( '<td>' )
180 .append( $highlights.clone() )
181 );
182 } else {
183 // Regular RC
184 $content.find( 'ul.special li' )
185 .prepend( $highlights.clone() );
186 }
187 };
188
189 /**
190 * Apply color classes based on filters highlight configuration
191 */
192 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.applyHighlight = function () {
193 if ( !this.filtersViewModel.isHighlightEnabled() ) {
194 return;
195 }
196
197 this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
198 // Add highlight class to all highlighted list items
199 this.$element.find( '.' + filterItem.getCssClass() )
200 .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
201 }.bind( this ) );
202
203 // Turn on highlights
204 this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
205 };
206
207 /**
208 * Remove all color classes
209 */
210 mw.rcfilters.ui.ChangesListWrapperWidget.prototype.clearHighlight = function () {
211 // Remove highlight classes
212 mw.rcfilters.HighlightColors.forEach( function ( color ) {
213 this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
214 }.bind( this ) );
215
216 // Turn off highlights
217 this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
218 };
219 }( mediaWiki ) );