3 * Wrapper for the RC form with hide/show links
5 * @extends OO.ui.Widget
8 * @param {mw.rcfilters.dm.ChangesListViewModel} model Changes list view model
9 * @param {mw.rcfilters.Controller} controller RCfilters controller
10 * @param {jQuery} $formRoot Root element of the form to attach to
11 * @param {Object} config Configuration object
13 mw
.rcfilters
.ui
.FormWrapperWidget
= function MwRcfiltersUiFormWrapperWidget( model
, controller
, $formRoot
, config
) {
14 config
= config
|| {};
17 mw
.rcfilters
.ui
.FormWrapperWidget
.parent
.call( this, $.extend( {}, config
, {
21 OO
.ui
.mixin
.PendingElement
.call( this, config
);
24 this.controller
= controller
;
25 this.$submitButton
= this.$element
.find( 'form input[type=submit]' );
28 .on( 'click', 'a[data-params]', this.onLinkClick
.bind( this ) );
31 .on( 'submit', 'form', this.onFormSubmit
.bind( this ) );
34 this.model
.connect( this, {
35 invalidate
: 'onModelInvalidate',
36 update
: 'onModelUpdate'
42 .addClass( 'mw-rcfilters-ui-FormWrapperWidget' )
43 .addClass( 'mw-rcfilters-ui-ready' );
48 OO
.inheritClass( mw
.rcfilters
.ui
.FormWrapperWidget
, OO
.ui
.Widget
);
49 OO
.mixinClass( mw
.rcfilters
.ui
.FormWrapperWidget
, OO
.ui
.mixin
.PendingElement
);
52 * Clean up the base form we're getting from the back-end.
53 * Remove <strong> tags and replace those with classes, so
54 * we can toggle those on click.
56 mw
.rcfilters
.ui
.FormWrapperWidget
.prototype.cleanupForm = function () {
57 this.$element
.find( '[data-keys] strong' ).each( function () {
59 .parent().addClass( 'mw-rcfilters-staticfilters-selected' );
62 .replaceWith( $( this ).contents() );
67 * Respond to link click
69 * @param {jQuery.Event} e Event
70 * @return {boolean} false
72 mw
.rcfilters
.ui
.FormWrapperWidget
.prototype.onLinkClick = function ( e
) {
73 var $element
= $( e
.target
),
74 data
= $element
.data( 'params' ),
75 keys
= $element
.data( 'keys' ),
76 $similarElements
= $element
.parent().find( '[data-keys="' + keys
+ '"]' );
78 // Only highlight choice if this link isn't a show/hide link
79 if ( !$element
.parents( '.rcshowhideoption' ).length
) {
80 // Remove the class from similar elements
81 $similarElements
.removeClass( 'mw-rcfilters-staticfilters-selected' );
82 // Add the class to this element
83 $element
.addClass( 'mw-rcfilters-staticfilters-selected' );
88 this.controller
.updateChangesList( data
);
93 * Respond to form submit event
95 * @param {jQuery.Event} e Event
96 * @return {boolean} false
98 mw
.rcfilters
.ui
.FormWrapperWidget
.prototype.onFormSubmit = function ( e
) {
101 // Collect all data from form
102 $( e
.target
).find( 'input:not([type="hidden"],[type="submit"]), select' ).each( function () {
103 if ( !$( this ).is( ':checkbox' ) || $( this ).is( ':checked' ) ) {
104 data
[ $( this ).prop( 'name' ) ] = $( this ).val();
108 this.controller
.updateChangesList( data
);
113 * Respond to model invalidate
115 mw
.rcfilters
.ui
.FormWrapperWidget
.prototype.onModelInvalidate = function () {
117 this.$submitButton
.prop( 'disabled', true );
121 * Respond to model update, replace the show/hide links with the ones from the
122 * server so they feature the correct state.
124 * @param {jQuery|string} $changesList Updated changes list
125 * @param {jQuery} $fieldset Updated fieldset
127 mw
.rcfilters
.ui
.FormWrapperWidget
.prototype.onModelUpdate = function ( $changesList
, $fieldset
) {
128 this.$submitButton
.prop( 'disabled', false );
130 // Replace the links we have in the content
131 // We don't want to replace the entire thing, because there is a big difference between
132 // the links in the backend and the links we have initialized, since we are removing
133 // the ones that are implemented in the new system
134 this.$element
.find( '.rcshowhide' ).children().each( function () {
135 // Go over existing links and replace only them
136 var classes
= $( this ).attr( 'class' ).split( ' ' ),
137 // Look for that item in the fieldset from the server
138 $remoteItem
= $fieldset
.find( '.' + classes
.join( '.' ) );
141 $( this ).replaceWith( $remoteItem
);