Localisation updates from https://translatewiki.net.
[mediawiki.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.js
blob5cadcaad107a49ddd4ec28fc01da2c90c28971bc
1 /**
2  * Components for use on the RecentChanges page. Provided by the `mediawiki.rcfilters.filters.ui` module.
3  *
4  * @namespace rcfilters
5  * @private
6  * @singleton
7  */
8 const rcfilters = {
9         Controller: require( './Controller.js' ),
10         HighlightColors: require( './HighlightColors.js' ),
11         UriProcessor: require( './UriProcessor.js' ),
12         /**
13          * Models used by RecentChanges Filters.
14          *
15          * @namespace rcfilters.dm
16          * @private
17          */
18         dm: {
19                 ChangesListViewModel: require( './dm/ChangesListViewModel.js' ),
20                 FilterGroup: require( './dm/FilterGroup.js' ),
21                 FilterItem: require( './dm/FilterItem.js' ),
22                 FiltersViewModel: require( './dm/FiltersViewModel.js' ),
23                 ItemModel: require( './dm/ItemModel.js' ),
24                 SavedQueriesModel: require( './dm/SavedQueriesModel.js' ),
25                 SavedQueryItemModel: require( './dm/SavedQueryItemModel.js' )
26         },
27         /**
28          * Widgets used by RecentChanges Filters.
29          *
30          * @namespace rcfilters.ui
31          * @private
32          */
33         ui: {
34                 MainWrapperWidget: require( './ui/MainWrapperWidget.js' )
35         },
36         /**
37          * Utils used by RecentChanges Filters.
38          *
39          * @namespace rcfilters.ui
40          * @private
41          */
42         utils: require( './utils.js' )
45 /**
46  * Get list of namespaces and remove unused ones
47  *
48  * @private
49  *
50  * @param {Array} unusedNamespaces Names of namespaces to remove
51  * @return {Array} Filtered array of namespaces
52  */
53 function getNamespaces( unusedNamespaces ) {
54         const namespaceIds = mw.config.get( 'wgNamespaceIds' ),
55                 namespaces = mw.config.get( 'wgFormattedNamespaces' );
57         for ( let i = 0, length = unusedNamespaces.length; i < length; i++ ) {
58                 const name = unusedNamespaces[ i ];
59                 const id = namespaceIds[ name.toLowerCase() ];
60                 delete namespaces[ id ];
61         }
63         return namespaces;
66 /**
67  * @private
68  */
69 function init() {
70         const conditionalViews = {},
71                 $initialFieldset = $( 'fieldset.cloptions' ),
72                 savedQueriesPreferenceName = mw.config.get( 'wgStructuredChangeFiltersSavedQueriesPreferenceName' ),
73                 daysPreferenceName = mw.config.get( 'wgStructuredChangeFiltersDaysPreferenceName' ),
74                 limitPreferenceName = mw.config.get( 'wgStructuredChangeFiltersLimitPreferenceName' ),
75                 activeFiltersCollapsedName = mw.config.get( 'wgStructuredChangeFiltersCollapsedPreferenceName' ),
76                 initialCollapsedState = mw.config.get( 'wgStructuredChangeFiltersCollapsedState' ),
77                 filtersModel = new rcfilters.dm.FiltersViewModel(),
78                 changesListModel = new rcfilters.dm.ChangesListViewModel( $initialFieldset ),
79                 savedQueriesModel = new rcfilters.dm.SavedQueriesModel( filtersModel ),
80                 specialPage = mw.config.get( 'wgCanonicalSpecialPageName' ),
81                 controller = new rcfilters.Controller(
82                         filtersModel, changesListModel, savedQueriesModel,
83                         {
84                                 savedQueriesPreferenceName: savedQueriesPreferenceName,
85                                 daysPreferenceName: daysPreferenceName,
86                                 limitPreferenceName: limitPreferenceName,
87                                 collapsedPreferenceName: activeFiltersCollapsedName,
88                                 normalizeTarget: specialPage === 'Recentchangeslinked'
89                         }
90                 );
92         // TODO: The changesListWrapperWidget should be able to initialize
93         // after the model is ready.
95         let $topSection;
96         if ( specialPage === 'Recentchanges' ) {
97                 $topSection = $( '.mw-recentchanges-toplinks' ).detach();
98         } else if ( specialPage === 'Watchlist' ) {
99                 $( '.mw-watchlist-owner, .mw-watchlist-toollinks, form#mw-watchlist-resetbutton' ).remove();
100                 $topSection = $( '.watchlistDetails' ).detach().contents();
101         } else if ( specialPage === 'Recentchangeslinked' ) {
102                 conditionalViews.recentChangesLinked = {
103                         groups: [
104                                 {
105                                         name: 'page',
106                                         type: 'any_value',
107                                         title: '',
108                                         hidden: true,
109                                         sticky: true,
110                                         filters: [
111                                                 {
112                                                         name: 'target',
113                                                         default: ''
114                                                 }
115                                         ]
116                                 },
117                                 {
118                                         name: 'toOrFrom',
119                                         type: 'boolean',
120                                         title: '',
121                                         hidden: true,
122                                         sticky: true,
123                                         filters: [
124                                                 {
125                                                         name: 'showlinkedto',
126                                                         default: false
127                                                 }
128                                         ]
129                                 }
130                         ]
131                 };
132         }
134         const mainWrapperWidget = new rcfilters.ui.MainWrapperWidget(
135                 controller,
136                 filtersModel,
137                 savedQueriesModel,
138                 changesListModel,
139                 {
140                         $wrapper: $( document.body ),
141                         $topSection: $topSection,
142                         $filtersContainer: $( '.mw-rcfilters-container' ),
143                         $changesListContainer: $( '.mw-changeslist, .mw-changeslist-empty' ),
144                         $formContainer: $initialFieldset,
145                         collapsed: initialCollapsedState
146                 }
147         );
149         // Remove the -loading class that may have been added on the server side.
150         // If we are in fact going to load a default saved query, this .initialize()
151         // call will do that and add the -loading class right back.
152         $( document.body ).removeClass( 'mw-rcfilters-ui-loading' );
154         controller.initialize(
155                 mw.config.get( 'wgStructuredChangeFilters' ),
156                 // All namespaces without Media namespace
157                 getNamespaces( [ 'Media' ] ),
158                 require( './config.json' ).RCFiltersChangeTags,
159                 conditionalViews
160         );
162         mainWrapperWidget.initFormWidget( specialPage );
164         $( 'a.mw-helplink' ).attr(
165                 'href',
166                 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:New_filters_for_edit_review'
167         );
169         controller.replaceUrl();
171         mainWrapperWidget.setTopSection( specialPage );
173         /**
174          * Fired when initialization of the filtering interface for changes list is complete.
175          *
176          * @event ~'structuredChangeFilters.ui.initialized'
177          * @memberof Hooks
178          */
179         mw.hook( 'structuredChangeFilters.ui.initialized' ).fire();
182 // Import i18n messages from config
183 mw.messages.set( mw.config.get( 'wgStructuredChangeFiltersMessages' ) );
185 // Don't try to run init during QUnit tests, some of the code depends on mw.config variables
186 // that are not set, and the ui code here isn't even being tested.
187 if ( !window.QUnit ) {
188         if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
189                 // Early execute of init
190                 init();
191         } else {
192                 $( init );
193         }
196 module.exports = rcfilters;