| 1 |
const FilterItemHighlightButton = require( './FilterItemHighlightButton.js' ), |
| 2 |
CheckboxInputWidget = require( './CheckboxInputWidget.js' ); |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const ItemMenuOptionWidget = function MwRcfiltersUiItemMenuOptionWidget( |
| 19 |
controller, filtersViewModel, invertModel, itemModel, highlightPopup, config |
| 20 |
) { |
| 21 |
const $label = $( '<div>' ) |
| 22 |
.addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label' ); |
| 23 |
|
| 24 |
config = config || {}; |
| 25 |
|
| 26 |
this.controller = controller; |
| 27 |
this.filtersViewModel = filtersViewModel; |
| 28 |
this.invertModel = invertModel; |
| 29 |
this.itemModel = itemModel; |
| 30 |
|
| 31 |
|
| 32 |
ItemMenuOptionWidget.super.call( this, Object.assign( { |
| 33 |
|
| 34 |
icon: '', |
| 35 |
data: this.itemModel.getName(), |
| 36 |
label: this.itemModel.getLabel() |
| 37 |
}, config ) ); |
| 38 |
|
| 39 |
this.checkboxWidget = new CheckboxInputWidget( { |
| 40 |
value: this.itemModel.getName(), |
| 41 |
selected: this.itemModel.isSelected() |
| 42 |
} ); |
| 43 |
|
| 44 |
$label.append( |
| 45 |
$( '<div>' ) |
| 46 |
.addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label-title' ) |
| 47 |
.append( $( '<bdi>' ).append( this.$label ) ) |
| 48 |
); |
| 49 |
if ( this.itemModel.getDescription() ) { |
| 50 |
$label.append( |
| 51 |
$( '<div>' ) |
| 52 |
.addClass( 'mw-rcfilters-ui-itemMenuOptionWidget-label-desc' ) |
| 53 |
.append( $( '<bdi>' ).text( this.itemModel.getDescription() ) ) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
this.helpLink = null; |
| 58 |
if ( this.itemModel.getHelpLink() ) { |
| 59 |
this.helpLink = new OO.ui.ButtonWidget( { |
| 60 |
icon: 'helpNotice', |
| 61 |
framed: false, |
| 62 |
title: mw.msg( 'rcfilters-tag-help', this.itemModel.getLabel() ), |
| 63 |
href: this.itemModel.getHelpLink(), |
| 64 |
target: '_blank' |
| 65 |
} ); |
| 66 |
|
| 67 |
this.helpLink.$button.on( 'mousedown', ( e ) => e.stopPropagation() ); |
| 68 |
} |
| 69 |
|
| 70 |
this.highlightButton = new FilterItemHighlightButton( |
| 71 |
this.controller, |
| 72 |
this.itemModel, |
| 73 |
highlightPopup, |
| 74 |
{ |
| 75 |
$overlay: config.$overlay || this.$element, |
| 76 |
title: mw.msg( 'rcfilters-highlightmenu-help' ) |
| 77 |
} |
| 78 |
); |
| 79 |
this.highlightButton.toggle( this.filtersViewModel.isHighlightEnabled() ); |
| 80 |
|
| 81 |
this.excludeLabel = new OO.ui.LabelWidget( { |
| 82 |
label: mw.msg( 'rcfilters-filter-excluded' ) |
| 83 |
} ); |
| 84 |
this.excludeLabel.toggle( |
| 85 |
this.invertModel && |
| 86 |
this.invertModel.isSelected() && |
| 87 |
this.itemModel.isSelected() |
| 88 |
); |
| 89 |
|
| 90 |
const layout = new OO.ui.FieldLayout( this.checkboxWidget, { |
| 91 |
label: $label, |
| 92 |
align: 'inline' |
| 93 |
} ); |
| 94 |
|
| 95 |
|
| 96 |
this.filtersViewModel.connect( this, { highlightChange: 'updateUiBasedOnState' } ); |
| 97 |
if ( this.invertModel ) { |
| 98 |
this.invertModel.connect( this, { update: 'updateUiBasedOnState' } ); |
| 99 |
} |
| 100 |
this.itemModel.connect( this, { update: 'updateUiBasedOnState' } ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
layout.$label.on( 'click', false ); |
| 107 |
|
| 108 |
const $widgetRow = $( '<div>' ) |
| 109 |
.addClass( 'mw-rcfilters-ui-table' ) |
| 110 |
.append( |
| 111 |
$( '<div>' ) |
| 112 |
.addClass( 'mw-rcfilters-ui-row' ) |
| 113 |
.append( |
| 114 |
$( '<div>' ) |
| 115 |
.addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-itemCheckbox' ) |
| 116 |
.append( layout.$element ) |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
if ( this.helpLink ) { |
| 121 |
$widgetRow.find( '.mw-rcfilters-ui-row' ).append( |
| 122 |
$( '<div>' ) |
| 123 |
.addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-helpLink' ) |
| 124 |
.append( this.helpLink.$element ) |
| 125 |
); |
| 126 |
} |
| 127 |
if ( !OO.ui.isMobile() ) { |
| 128 |
$widgetRow.find( '.mw-rcfilters-ui-row' ).append( |
| 129 |
$( '<div>' ) |
| 130 |
.addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-excludeLabel' ) |
| 131 |
.append( this.excludeLabel.$element ), |
| 132 |
$( '<div>' ) |
| 133 |
.addClass( 'mw-rcfilters-ui-cell mw-rcfilters-ui-itemMenuOptionWidget-highlightButton' ) |
| 134 |
.append( this.highlightButton.$element ) |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 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() |
| 141 |
); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
this.$element |
| 151 |
.addClass( classes ) |
| 152 |
.append( $widgetRow ); |
| 153 |
|
| 154 |
this.updateUiBasedOnState(); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
OO.inheritClass( ItemMenuOptionWidget, OO.ui.MenuOptionWidget ); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
ItemMenuOptionWidget.static.scrollIntoViewOnSelect = false; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
ItemMenuOptionWidget.prototype.updateUiBasedOnState = function () { |
| 172 |
this.checkboxWidget.setSelected( this.itemModel.isSelected() ); |
| 173 |
|
| 174 |
this.highlightButton.toggle( this.filtersViewModel.isHighlightEnabled() ); |
| 175 |
this.excludeLabel.toggle( |
| 176 |
this.invertModel && |
| 177 |
this.invertModel.isSelected() && |
| 178 |
this.itemModel.isSelected() |
| 179 |
); |
| 180 |
this.toggle( this.itemModel.isVisible() ); |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
ItemMenuOptionWidget.prototype.getName = function () { |
| 189 |
return this.itemModel.getName(); |
| 190 |
}; |
| 191 |
|
| 192 |
ItemMenuOptionWidget.prototype.getModel = function () { |
| 193 |
return this.itemModel; |
| 194 |
}; |
| 195 |
|
| 196 |
module.exports = ItemMenuOptionWidget; |