Follow-up 873d3c9ff: move new RC filters UI below the custom links
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.init.js
1 /*!
2 * JavaScript for Special:RecentChanges
3 */
4 ( function ( mw, $ ) {
5 /**
6 * @class mw.rcfilters
7 * @singleton
8 */
9 var rcfilters = {
10 /** */
11 init: function () {
12 var model = new mw.rcfilters.dm.FiltersViewModel(),
13 controller = new mw.rcfilters.Controller( model ),
14 widget = new mw.rcfilters.ui.FilterWrapperWidget( controller, model );
15
16 model.initializeFilters( {
17 authorship: {
18 title: mw.msg( 'rcfilters-filtergroup-authorship' ),
19 // Type 'send_unselected_if_any' means that the controller will go over
20 // all unselected filters in the group and use their parameters
21 // as truthy in the query string.
22 // This is to handle the "negative" filters. We are showing users
23 // a positive message ("Show xxx") but the filters themselves are
24 // based on "hide YYY". The purpose of this is to correctly map
25 // the functionality to the UI, whether we are dealing with 2
26 // parameters in the group or more.
27 type: 'send_unselected_if_any',
28 filters: [
29 {
30 name: 'hidemyself',
31 label: mw.msg( 'rcfilters-filter-editsbyself-label' ),
32 description: mw.msg( 'rcfilters-filter-editsbyself-description' )
33 },
34 {
35 name: 'hidebyothers',
36 label: mw.msg( 'rcfilters-filter-editsbyother-label' ),
37 description: mw.msg( 'rcfilters-filter-editsbyother-description' )
38 }
39 ]
40 }
41 } );
42
43 $( '.rcoptions' ).before( widget.$element );
44
45 // Initialize values
46 controller.initialize();
47
48 $( '.rcoptions form' ).submit( function () {
49 var $form = $( this );
50
51 // Get current filter values
52 $.each( model.getParametersFromFilters(), function ( paramName, paramValue ) {
53 var $existingInput = $form.find( 'input[name=' + paramName + ']' );
54 // Check if the hidden input already exists
55 // This happens if the parameter was already given
56 // on load
57 if ( $existingInput.length ) {
58 // Update the value
59 $existingInput.val( paramValue );
60 } else {
61 // Append hidden fields with filter values
62 $form.append(
63 $( '<input>' )
64 .attr( 'type', 'hidden' )
65 .attr( 'name', paramName )
66 .val( paramValue )
67 );
68 }
69 } );
70
71 // Continue the submission process
72 return true;
73 } );
74 }
75 };
76
77 $( rcfilters.init );
78
79 module.exports = rcfilters;
80
81 }( mediaWiki, jQuery ) );