1 const FilterItemHighlightButton = require( './FilterItemHighlightButton.js' ),
2 CheckboxInputWidget = require( './CheckboxInputWidget.js' );
5 * A widget representing a base toggle item.
7 * @class mw.rcfilters.ui.ItemMenuOptionWidget
9 * @extends OO.ui.MenuOptionWidget
11 * @param {mw.rcfilters.Controller} controller RCFilters controller
12 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel
13 * @param {mw.rcfilters.dm.ItemModel|null} invertModel
14 * @param {mw.rcfilters.dm.ItemModel} itemModel Item model
15 * @param {mw.rcfilters.ui.HighlightPopupWidget} highlightPopup Shared highlight color picker
16 * @param {Object} config Configuration object
18 const ItemMenuOptionWidget = function MwRcfiltersUiItemMenuOptionWidget(
19 controller, filtersViewModel, invertModel, itemModel, highlightPopup, config
21 const $label = $( '<div>' )
22 .addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label' );
24 config = config || {};
26 this.controller = controller;
27 this.filtersViewModel = filtersViewModel;
28 this.invertModel = invertModel;
29 this.itemModel = itemModel;
32 ItemMenuOptionWidget.super.call( this, Object.assign( {
33 // Override the 'check' icon that OOUI defines
35 data: this.itemModel.getName(),
36 label: this.itemModel.getLabel()
39 this.checkboxWidget = new CheckboxInputWidget( {
40 value: this.itemModel.getName(),
41 selected: this.itemModel.isSelected()
46 .addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label-title' )
47 .append( $( '<bdi>' ).append( this.$label ) )
49 if ( this.itemModel.getDescription() ) {
52 .addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label-desc' )
53 .append( $( '<bdi>' ).text( this.itemModel.getDescription() ) )
58 if ( this.itemModel.getHelpLink() ) {
59 this.helpLink = new OO.ui.ButtonWidget( {
62 title: mw.msg( 'rcfilters-tag-help', this.itemModel.getLabel() ),
63 href: this.itemModel.getHelpLink(),
66 // Prevent clicks on the help link from toggling the option
67 this.helpLink.$button.on( 'mousedown', ( e ) => e.stopPropagation() );
70 this.highlightButton = new FilterItemHighlightButton(
75 $overlay: config.$overlay || this.$element,
76 title: mw.msg( 'rcfilters-highlightmenu-help' )
79 this.highlightButton.toggle( this.filtersViewModel.isHighlightEnabled() );
81 this.excludeLabel = new OO.ui.LabelWidget( {
82 label: mw.msg( 'rcfilters-filter-excluded' )
84 this.excludeLabel.toggle(
86 this.invertModel.isSelected() &&
87 this.itemModel.isSelected()
90 const layout = new OO.ui.FieldLayout( this.checkboxWidget, {
96 this.filtersViewModel.connect( this, { highlightChange: 'updateUiBasedOnState' } );
97 if ( this.invertModel ) {
98 this.invertModel.connect( this, { update: 'updateUiBasedOnState' } );
100 this.itemModel.connect( this, { update: 'updateUiBasedOnState' } );
101 // HACK: Prevent defaults on 'click' for the label so it
102 // doesn't steal the focus away from the input. This means
103 // we can continue arrow-movement after we click the label
104 // and is consistent with the checkbox *itself* also preventing
105 // defaults on 'click' as well.
106 layout.$label.on( 'click', false );
108 const $widgetRow = $( '<div>' )
109 .addClass( 'mw-rcfilters-ui-table' )
112 .addClass( 'mw-rcfilters-ui-row' )
115 .addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-itemCheckbox' )
116 .append( layout.$element )
120 if ( this.helpLink ) {
121 $widgetRow.find( '.mw-rcfilters-ui-row' ).append(
123 .addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-helpLink' )
124 .append( this.helpLink.$element )
127 if ( !OO.ui.isMobile() ) {
128 $widgetRow.find( '.mw-rcfilters-ui-row' ).append(
130 .addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-excludeLabel' )
131 .append( this.excludeLabel.$element ),
133 .addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-highlightButton' )
134 .append( this.highlightButton.$element )
138 const classes = this.itemModel.getIdentifiers().map( ( ident ) => 'mw-rcfilters-ui-itemMenuOptionWidget-identifier-' + ident ).concat(
139 'mw-rcfilters-ui-itemMenuOptionWidget',
140 'mw-rcfilters-ui-itemMenuOptionWidget-view-' + this.itemModel.getGroupModel().getView()
143 // The following classes are used here:
144 // * mw-rcfilters-ui-itemMenuOptionWidget-identifier-subject
145 // * mw-rcfilters-ui-itemMenuOptionWidget-identifier-talk
146 // * mw-rcfilters-ui-itemMenuOptionWidget
147 // * mw-rcfilters-ui-itemMenuOptionWidget-view-default
148 // * mw-rcfilters-ui-itemMenuOptionWidget-view-namespaces
149 // * mw-rcfilters-ui-itemMenuOptionWidget-view-tags
152 .append( $widgetRow );
154 this.updateUiBasedOnState();
159 OO.inheritClass( ItemMenuOptionWidget, OO.ui.MenuOptionWidget );
161 /* Static properties */
163 // We do our own scrolling to top
164 ItemMenuOptionWidget.static.scrollIntoViewOnSelect = false;
169 * Respond to item model update event
171 ItemMenuOptionWidget.prototype.updateUiBasedOnState = function () {
172 this.checkboxWidget.setSelected( this.itemModel.isSelected() );
174 this.highlightButton.toggle( this.filtersViewModel.isHighlightEnabled() );
175 this.excludeLabel.toggle(
177 this.invertModel.isSelected() &&
178 this.itemModel.isSelected()
180 this.toggle( this.itemModel.isVisible() );
184 * Get the name of this filter
186 * @return {string} Filter name
188 ItemMenuOptionWidget.prototype.getName = function () {
189 return this.itemModel.getName();
192 ItemMenuOptionWidget.prototype.getModel = function () {
193 return this.itemModel;
196 module.exports = ItemMenuOptionWidget;