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