Merge "Unhide rcenhancedfilters preference"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterWrapperWidget.js
1 ( function ( mw ) {
2 /**
3 * List displaying all filter groups
4 *
5 * @extends OO.ui.Widget
6 * @mixins OO.ui.mixin.PendingElement
7 *
8 * @constructor
9 * @param {mw.rcfilters.Controller} controller Controller
10 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
11 * @param {Object} [config] Configuration object
12 * @cfg {Object} [filters] A definition of the filter groups in this list
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
14 */
15 mw.rcfilters.ui.FilterWrapperWidget = function MwRcfiltersUiFilterWrapperWidget( controller, model, config ) {
16 var $footer = $( '<div>' );
17 config = config || {};
18
19 // Parent
20 mw.rcfilters.ui.FilterWrapperWidget.parent.call( this, config );
21 // Mixin constructors
22 OO.ui.mixin.PendingElement.call( this, config );
23
24 this.controller = controller;
25 this.model = model;
26 this.$overlay = config.$overlay || this.$element;
27
28 this.filterPopup = new mw.rcfilters.ui.FiltersListWidget(
29 this.controller,
30 this.model,
31 {
32 label: mw.msg( 'rcfilters-filterlist-title' ),
33 $overlay: this.$overlay
34 }
35 );
36
37 $footer.append(
38 new OO.ui.ButtonWidget( {
39 framed: false,
40 icon: 'feedback',
41 flags: [ 'progressive' ],
42 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
43 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
44 } ).$element
45 );
46
47 this.textInput = new OO.ui.TextInputWidget( {
48 classes: [ 'mw-rcfilters-ui-filterWrapperWidget-search' ],
49 icon: 'search',
50 placeholder: mw.msg( 'rcfilters-search-placeholder' )
51 } );
52
53 this.capsule = new mw.rcfilters.ui.FilterCapsuleMultiselectWidget( controller, this.model, this.textInput, {
54 $overlay: this.$overlay,
55 popup: {
56 $content: this.filterPopup.$element,
57 $footer: $footer,
58 classes: [ 'mw-rcfilters-ui-filterWrapperWidget-popup' ],
59 width: 650
60 }
61 } );
62
63 // Events
64 this.model.connect( this, {
65 initialize: 'onModelInitialize',
66 itemUpdate: 'onModelItemUpdate'
67 } );
68 this.textInput.connect( this, {
69 change: 'onTextInputChange'
70 } );
71 this.capsule.connect( this, { capsuleItemClick: 'onCapsuleItemClick' } );
72 this.capsule.popup.connect( this, { toggle: 'onCapsulePopupToggle' } );
73
74 // Initialize
75 this.$element
76 .addClass( 'mw-rcfilters-ui-filterWrapperWidget' )
77 .addClass( 'mw-rcfilters-ui-ready' )
78 .append( this.capsule.$element, this.textInput.$element );
79 };
80
81 /* Initialization */
82
83 OO.inheritClass( mw.rcfilters.ui.FilterWrapperWidget, OO.ui.Widget );
84 OO.mixinClass( mw.rcfilters.ui.FilterWrapperWidget, OO.ui.mixin.PendingElement );
85
86 /**
87 * Respond to capsule item click and make the popup scroll down to the requested item
88 *
89 * @param {mw.rcfilters.ui.CapsuleItemWidget} item Clicked item
90 */
91 mw.rcfilters.ui.FilterWrapperWidget.prototype.onCapsuleItemClick = function ( item ) {
92 var filterName = item.getData(),
93 // Find the item in the popup
94 filterWidget = this.filterPopup.getItemWidget( filterName );
95
96 // Highlight item
97 this.filterPopup.select( filterName );
98 this.capsule.select( item );
99
100 this.scrollToTop( filterWidget.$element );
101 };
102
103 /**
104 * Respond to popup toggle event. Reset selection in the list when the popup is closed.
105 *
106 * @param {boolean} isVisible Popup is visible
107 */
108 mw.rcfilters.ui.FilterWrapperWidget.prototype.onCapsulePopupToggle = function ( isVisible ) {
109 if ( !isVisible ) {
110 this.filterPopup.resetSelection();
111 this.capsule.resetSelection();
112 } else {
113 this.scrollToTop( this.capsule.$element, 10 );
114 }
115 };
116
117 /**
118 * Respond to text input change
119 *
120 * @param {string} newValue Current value
121 */
122 mw.rcfilters.ui.FilterWrapperWidget.prototype.onTextInputChange = function ( newValue ) {
123 this.filterPopup.resetSelection();
124
125 // Filter the results
126 this.filterPopup.filter( this.model.findMatches( newValue ) );
127 this.capsule.popup.clip();
128 };
129
130 /**
131 * Respond to model update event and set up the available filters to choose
132 * from.
133 */
134 mw.rcfilters.ui.FilterWrapperWidget.prototype.onModelInitialize = function () {
135 var wrapper = this;
136
137 // Add defaults to capsule. We have to do this
138 // after we added to the capsule menu, since that's
139 // how the capsule multiselect widget knows which
140 // object to add
141 this.model.getItems().forEach( function ( filterItem ) {
142 if ( filterItem.isSelected() ) {
143 wrapper.capsule.addItemByName( filterItem.getName() );
144 }
145 } );
146 };
147
148 /**
149 * Respond to item update and reset the selection. This will make it so that
150 * any actual interaction with the system resets the selection state of any item.
151 */
152 mw.rcfilters.ui.FilterWrapperWidget.prototype.onModelItemUpdate = function () {
153 this.filterPopup.resetSelection();
154 };
155
156 /**
157 * Scroll the element to top within its container
158 *
159 * @private
160 * @param {jQuery} $element Element to position
161 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
162 * much space (in pixels) above the widget.
163 */
164 mw.rcfilters.ui.FilterWrapperWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
165 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
166 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) );
167
168 // Scroll to item
169 $( container ).animate( {
170 scrollTop: $( container ).scrollTop() + pos.top + ( marginFromTop || 0 )
171 } );
172 };
173 }( mediaWiki ) );