| 1 |
const FilterMenuHeaderWidget = require( './FilterMenuHeaderWidget.js' ), |
| 2 |
HighlightPopupWidget = require( './HighlightPopupWidget.js' ), |
| 3 |
FilterMenuSectionOptionWidget = require( './FilterMenuSectionOptionWidget.js' ), |
| 4 |
FilterMenuOptionWidget = require( './FilterMenuOptionWidget.js' ); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const MenuSelectWidget = function MwRcfiltersUiMenuSelectWidget( controller, model, config ) { |
| 32 |
config = config || {}; |
| 33 |
|
| 34 |
this.controller = controller; |
| 35 |
this.model = model; |
| 36 |
this.currentView = ''; |
| 37 |
this.views = {}; |
| 38 |
this.userSelecting = false; |
| 39 |
|
| 40 |
this.menuInitialized = false; |
| 41 |
this.$overlay = config.$overlay || this.$element; |
| 42 |
this.$body = $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' ); |
| 43 |
this.footers = []; |
| 44 |
|
| 45 |
|
| 46 |
MenuSelectWidget.super.call( this, Object.assign( config, { |
| 47 |
$autoCloseIgnore: this.$overlay, |
| 48 |
width: config.isMobile ? undefined : 650, |
| 49 |
|
| 50 |
filterFromInput: false |
| 51 |
} ) ); |
| 52 |
this.setGroupElement( |
| 53 |
$( '<div>' ) |
| 54 |
.addClass( 'mw-rcfilters-ui-menuSelectWidget-group' ) |
| 55 |
); |
| 56 |
|
| 57 |
let header; |
| 58 |
if ( !config.isMobile ) { |
| 59 |
|
| 60 |
|
| 61 |
this.setClippableElement( this.$body ); |
| 62 |
this.setClippableContainer( this.$element ); |
| 63 |
|
| 64 |
header = new FilterMenuHeaderWidget( |
| 65 |
this.controller, |
| 66 |
this.model, |
| 67 |
{ |
| 68 |
$overlay: this.$overlay |
| 69 |
} |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
this.noResults = new OO.ui.LabelWidget( { |
| 74 |
label: mw.msg( 'rcfilters-filterlist-noresults' ), |
| 75 |
classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ] |
| 76 |
} ); |
| 77 |
|
| 78 |
|
| 79 |
this.model.connect( this, { |
| 80 |
initialize: 'onModelInitialize', |
| 81 |
searchChange: 'onModelSearchChange' |
| 82 |
} ); |
| 83 |
|
| 84 |
|
| 85 |
this.$element |
| 86 |
.addClass( 'mw-rcfilters-ui-menuSelectWidget' ) |
| 87 |
.attr( 'aria-label', mw.msg( 'rcfilters-filterlist-title' ) ) |
| 88 |
.append( config.isMobile ? undefined : header.$element ) |
| 89 |
.append( |
| 90 |
this.$body |
| 91 |
.append( this.$group, this.noResults.$element ) |
| 92 |
); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
config.footers = config.isMobile ? [] : config.footers || []; |
| 97 |
config.footers.forEach( ( footerData ) => { |
| 98 |
const isSticky = footerData.sticky === undefined ? true : !!footerData.sticky, |
| 99 |
adjustedData = { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
$element: $( '<div>' ) |
| 105 |
.addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' ) |
| 106 |
.addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name ) |
| 107 |
.append( footerData.$element ), |
| 108 |
views: footerData.views |
| 109 |
}; |
| 110 |
|
| 111 |
if ( !footerData.disabled ) { |
| 112 |
this.footers.push( adjustedData ); |
| 113 |
|
| 114 |
if ( isSticky ) { |
| 115 |
this.$element.append( adjustedData.$element ); |
| 116 |
} else { |
| 117 |
this.$body.append( adjustedData.$element ); |
| 118 |
} |
| 119 |
} |
| 120 |
} ); |
| 121 |
|
| 122 |
|
| 123 |
this.updateView(); |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
OO.inheritClass( MenuSelectWidget, OO.ui.MenuSelectWidget ); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
MenuSelectWidget.prototype.onModelSearchChange = function () { |
| 134 |
this.updateView(); |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
MenuSelectWidget.prototype.toggle = function ( show ) { |
| 141 |
this.lazyMenuCreation(); |
| 142 |
MenuSelectWidget.super.prototype.toggle.call( this, show ); |
| 143 |
|
| 144 |
this.setVerticalPosition( 'below' ); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
MenuSelectWidget.prototype.lazyMenuCreation = function () { |
| 151 |
const widget = this, |
| 152 |
items = [], |
| 153 |
viewGroupCount = {}, |
| 154 |
groups = this.model.getFilterGroups(); |
| 155 |
|
| 156 |
if ( this.menuInitialized ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
this.menuInitialized = true; |
| 161 |
|
| 162 |
|
| 163 |
this.highlightPopup = new HighlightPopupWidget( this.controller ); |
| 164 |
this.$overlay.append( this.highlightPopup.$element ); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
$.each( groups, ( groupName, groupModel ) => { |
| 169 |
if ( !groupModel.isHidden() ) { |
| 170 |
viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0; |
| 171 |
viewGroupCount[ groupModel.getView() ]++; |
| 172 |
} |
| 173 |
} ); |
| 174 |
|
| 175 |
|
| 176 |
$.each( groups, ( groupName, groupModel ) => { |
| 177 |
const currentItems = [], |
| 178 |
view = groupModel.getView(); |
| 179 |
|
| 180 |
if ( !groupModel.isHidden() ) { |
| 181 |
if ( viewGroupCount[ view ] > 1 ) { |
| 182 |
|
| 183 |
|
| 184 |
currentItems.push( |
| 185 |
|
| 186 |
new FilterMenuSectionOptionWidget( |
| 187 |
widget.controller, |
| 188 |
groupModel, |
| 189 |
{ |
| 190 |
$overlay: widget.$overlay |
| 191 |
} |
| 192 |
) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
widget.model.getGroupFilters( groupName ).forEach( ( filterItem ) => { |
| 198 |
currentItems.push( |
| 199 |
new FilterMenuOptionWidget( |
| 200 |
widget.controller, |
| 201 |
widget.model, |
| 202 |
widget.model.getInvertModel( view ), |
| 203 |
filterItem, |
| 204 |
widget.highlightPopup, |
| 205 |
{ |
| 206 |
$overlay: widget.$overlay |
| 207 |
} |
| 208 |
) |
| 209 |
); |
| 210 |
} ); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
widget.views[ view ] = widget.views[ view ] || []; |
| 215 |
widget.views[ view ] = widget.views[ view ].concat( currentItems ); |
| 216 |
items.push( ...currentItems ); |
| 217 |
} |
| 218 |
} ); |
| 219 |
|
| 220 |
this.addItems( items ); |
| 221 |
this.updateView(); |
| 222 |
}; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
MenuSelectWidget.prototype.onModelInitialize = function () { |
| 228 |
this.menuInitialized = false; |
| 229 |
|
| 230 |
setTimeout( this.lazyMenuCreation.bind( this ) ); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
MenuSelectWidget.prototype.updateView = function () { |
| 237 |
const viewName = this.model.getCurrentView(); |
| 238 |
|
| 239 |
if ( this.views[ viewName ] && this.currentView !== viewName ) { |
| 240 |
this.updateFooterVisibility( viewName ); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
this.$element |
| 247 |
.data( 'view', viewName ) |
| 248 |
.removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView ) |
| 249 |
.addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName ); |
| 250 |
|
| 251 |
this.currentView = viewName; |
| 252 |
this.scrollToTop(); |
| 253 |
} |
| 254 |
|
| 255 |
this.postProcessItems(); |
| 256 |
this.clip(); |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) { |
| 266 |
currentView = currentView || this.model.getCurrentView(); |
| 267 |
|
| 268 |
this.footers.forEach( ( data ) => { |
| 269 |
data.$element.toggle( |
| 270 |
|
| 271 |
|
| 272 |
!data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1 |
| 273 |
); |
| 274 |
} ); |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
MenuSelectWidget.prototype.postProcessItems = function () { |
| 283 |
let itemWasSelected = false; |
| 284 |
const items = this.getItems(); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if ( !this.userSelecting ) { |
| 289 |
|
| 290 |
for ( let i = 0; i < items.length; i++ ) { |
| 291 |
if ( |
| 292 |
!( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) && |
| 293 |
items[ i ].isVisible() |
| 294 |
) { |
| 295 |
itemWasSelected = true; |
| 296 |
this.selectItem( items[ i ] ); |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if ( !itemWasSelected ) { |
| 302 |
this.selectItem( null ); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
this.noResults.toggle( !this.getItems().some( ( item ) => item.isVisible() ) ); |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
MenuSelectWidget.prototype.getItemFromModel = function ( model ) { |
| 317 |
this.lazyMenuCreation(); |
| 318 |
return this.views[ model.getGroupModel().getView() ].filter( ( item ) => item.getName() === model.getName() )[ 0 ]; |
| 319 |
}; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
MenuSelectWidget.prototype.onDocumentKeyDown = function ( e ) { |
| 325 |
const currentItem = this.findHighlightedItem() || this.findSelectedItem(); |
| 326 |
let nextItem; |
| 327 |
|
| 328 |
|
| 329 |
MenuSelectWidget.super.prototype.onDocumentKeyDown.call( this, e ); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if ( !this.isDisabled() && this.isVisible() ) { |
| 335 |
switch ( e.keyCode ) { |
| 336 |
case OO.ui.Keys.UP: |
| 337 |
case OO.ui.Keys.LEFT: |
| 338 |
|
| 339 |
nextItem = this.findRelativeSelectableItem( currentItem, -1 ); |
| 340 |
break; |
| 341 |
case OO.ui.Keys.DOWN: |
| 342 |
case OO.ui.Keys.RIGHT: |
| 343 |
|
| 344 |
nextItem = this.findRelativeSelectableItem( currentItem, 1 ); |
| 345 |
break; |
| 346 |
} |
| 347 |
|
| 348 |
nextItem = nextItem && nextItem.constructor.static.selectable ? |
| 349 |
nextItem : null; |
| 350 |
|
| 351 |
|
| 352 |
this.selectItem( nextItem ); |
| 353 |
} |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
MenuSelectWidget.prototype.scrollToTop = function () { |
| 360 |
this.$body.scrollTop( 0 ); |
| 361 |
}; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) { |
| 372 |
this.userSelecting = !!isSelecting; |
| 373 |
}; |
| 374 |
|
| 375 |
module.exports = MenuSelectWidget; |