| 1 |
const FilterGroup = require( './FilterGroup.js' ), |
| 2 |
FilterItem = require( './FilterItem.js' ), |
| 3 |
utils = require( '../utils.js' ); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
const FiltersViewModel = function MwRcfiltersDmFiltersViewModel() { |
| 14 |
|
| 15 |
OO.EventEmitter.call( this ); |
| 16 |
OO.EmitterList.call( this ); |
| 17 |
|
| 18 |
this.groups = {}; |
| 19 |
this.defaultParams = {}; |
| 20 |
this.highlightEnabled = false; |
| 21 |
this.parameterMap = {}; |
| 22 |
this.emptyParameterState = null; |
| 23 |
|
| 24 |
this.views = {}; |
| 25 |
this.currentView = 'default'; |
| 26 |
this.searchQuery = null; |
| 27 |
|
| 28 |
|
| 29 |
this.aggregate( { update: 'filterItemUpdate' } ); |
| 30 |
this.connect( this, { filterItemUpdate: [ 'emit', 'itemUpdate' ] } ); |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
OO.initClass( FiltersViewModel ); |
| 35 |
OO.mixinClass( FiltersViewModel, OO.EventEmitter ); |
| 36 |
OO.mixinClass( FiltersViewModel, OO.EmitterList ); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
FiltersViewModel.prototype.reassessFilterInteractions = function ( item ) { |
| 79 |
const model = this, |
| 80 |
iterationItems = item !== undefined ? [ item ] : this.getItems(); |
| 81 |
|
| 82 |
iterationItems.forEach( ( checkedItem ) => { |
| 83 |
const allCheckedItems = checkedItem.getSubset().concat( [ checkedItem.getName() ] ), |
| 84 |
groupModel = checkedItem.getGroupModel(); |
| 85 |
|
| 86 |
|
| 87 |
allCheckedItems.forEach( ( filterItemName ) => { |
| 88 |
const itemInSubset = model.getItemByName( filterItemName ); |
| 89 |
|
| 90 |
itemInSubset.toggleIncluded( |
| 91 |
|
| 92 |
|
| 93 |
itemInSubset.getSuperset().some( ( supersetName ) => ( model.getItemByName( supersetName ).isSelected() ) ) |
| 94 |
); |
| 95 |
} ); |
| 96 |
|
| 97 |
|
| 98 |
if ( groupModel.isFullCoverage() ) { |
| 99 |
const allSelected = groupModel.areAllSelected(); |
| 100 |
groupModel.getItems().forEach( ( filterItem ) => { |
| 101 |
filterItem.toggleFullyCovered( allSelected ); |
| 102 |
} ); |
| 103 |
} |
| 104 |
} ); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
this.getItems().forEach( ( filterItem ) => { |
| 112 |
let inConflict = false; |
| 113 |
const filterItemGroup = filterItem.getGroupModel(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
$.each( model.groups, ( groupName, groupModel ) => { |
| 118 |
if ( filterItem.getGroupName() === groupName ) { |
| 119 |
|
| 120 |
inConflict = groupModel.areAnySelectedInConflictWith( filterItem ); |
| 121 |
} else { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
inConflict = ( |
| 153 |
|
| 154 |
groupModel.areAllSelectedInConflictWith( filterItem ) && |
| 155 |
|
| 156 |
|
| 157 |
filterItemGroup.findSelectedItems().every( ( otherGroupItem ) => groupModel.areAllSelectedInConflictWith( otherGroupItem ) ) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
return !inConflict; |
| 165 |
} ); |
| 166 |
|
| 167 |
|
| 168 |
filterItem.toggleConflicted( inConflict ); |
| 169 |
} ); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
FiltersViewModel.prototype.hasConflict = function () { |
| 178 |
return this.getItems().some( ( filterItem ) => filterItem.isSelected() && filterItem.isConflicted() ); |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
FiltersViewModel.prototype.getFirstConflictedItem = function () { |
| 188 |
const items = this.getItems(); |
| 189 |
for ( let i = 0; i < items.length; i++ ) { |
| 190 |
const filterItem = items[ i ]; |
| 191 |
if ( filterItem.isSelected() && filterItem.isConflicted() ) { |
| 192 |
return filterItem; |
| 193 |
} |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
FiltersViewModel.prototype.initializeFilters = function ( filterGroups, views ) { |
| 224 |
const model = this, |
| 225 |
items = [], |
| 226 |
groupConflictMap = {}, |
| 227 |
filterConflictMap = {}, |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
expandConflictDefinitions = function ( obj ) { |
| 239 |
const result = {}; |
| 240 |
|
| 241 |
|
| 242 |
$.each( obj, ( key, conflicts ) => { |
| 243 |
const adjustedConflicts = {}; |
| 244 |
|
| 245 |
conflicts.forEach( ( conflict ) => { |
| 246 |
let filter; |
| 247 |
|
| 248 |
if ( conflict.filter ) { |
| 249 |
const filterName = model.groups[ conflict.group ].getPrefixedName( conflict.filter ); |
| 250 |
filter = model.getItemByName( filterName ); |
| 251 |
|
| 252 |
|
| 253 |
adjustedConflicts[ filterName ] = Object.assign( |
| 254 |
{}, |
| 255 |
conflict, |
| 256 |
{ |
| 257 |
filter: filterName, |
| 258 |
item: filter |
| 259 |
} |
| 260 |
); |
| 261 |
} else { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
model.groups[ conflict.group ].getItems().forEach( ( groupItem ) => { |
| 267 |
|
| 268 |
adjustedConflicts[ groupItem.getName() ] = Object.assign( |
| 269 |
{}, |
| 270 |
conflict, |
| 271 |
{ |
| 272 |
filter: groupItem.getName(), |
| 273 |
item: groupItem |
| 274 |
} |
| 275 |
); |
| 276 |
} ); |
| 277 |
} |
| 278 |
} ); |
| 279 |
|
| 280 |
result[ key ] = adjustedConflicts; |
| 281 |
} ); |
| 282 |
|
| 283 |
return result; |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
this.clearItems(); |
| 288 |
this.groups = {}; |
| 289 |
this.views = {}; |
| 290 |
|
| 291 |
|
| 292 |
filterGroups = OO.copy( filterGroups ); |
| 293 |
|
| 294 |
|
| 295 |
filterGroups.forEach( ( data ) => { |
| 296 |
let i; |
| 297 |
|
| 298 |
data.whatsThis = { |
| 299 |
body: data.whatsThisBody, |
| 300 |
header: data.whatsThisHeader, |
| 301 |
linkText: data.whatsThisLinkText, |
| 302 |
url: data.whatsThisUrl |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
data.title = data.title ? mw.msg( data.title ) : data.name; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
for ( i = 0; i < data.filters.length; i++ ) { |
| 312 |
|
| 313 |
data.filters[ i ].label = data.filters[ i ].label ? mw.msg( data.filters[ i ].label ) : data.filters[ i ].name; |
| 314 |
|
| 315 |
data.filters[ i ].description = data.filters[ i ].description ? mw.msg( data.filters[ i ].description ) : ''; |
| 316 |
} |
| 317 |
} ); |
| 318 |
|
| 319 |
|
| 320 |
const allViews = $.extend( true, { |
| 321 |
default: { |
| 322 |
title: mw.msg( 'rcfilters-filterlist-title' ), |
| 323 |
groups: filterGroups |
| 324 |
} |
| 325 |
}, views ); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
$.each( allViews, ( viewName, viewData ) => { |
| 330 |
|
| 331 |
model.views[ viewName ] = { |
| 332 |
name: viewData.name, |
| 333 |
title: viewData.title, |
| 334 |
trigger: viewData.trigger |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
viewData.groups.forEach( ( groupData ) => { |
| 339 |
const group = groupData.name; |
| 340 |
|
| 341 |
if ( !model.groups[ group ] ) { |
| 342 |
model.groups[ group ] = new FilterGroup( |
| 343 |
group, |
| 344 |
$.extend( true, {}, groupData, { view: viewName } ) |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
model.groups[ group ].initializeFilters( groupData.filters, groupData.default ); |
| 349 |
items.push( ...model.groups[ group ].getItems() ); |
| 350 |
|
| 351 |
|
| 352 |
if ( groupData.conflicts ) { |
| 353 |
|
| 354 |
groupConflictMap[ group ] = groupData.conflicts; |
| 355 |
} |
| 356 |
|
| 357 |
groupData.filters.forEach( ( itemData ) => { |
| 358 |
const filterItem = model.groups[ group ].getItemByParamName( itemData.name ); |
| 359 |
|
| 360 |
if ( itemData.conflicts ) { |
| 361 |
filterConflictMap[ filterItem.getName() ] = itemData.conflicts; |
| 362 |
} |
| 363 |
} ); |
| 364 |
} ); |
| 365 |
} ); |
| 366 |
|
| 367 |
|
| 368 |
this.addItems( items ); |
| 369 |
|
| 370 |
|
| 371 |
const groupConflictResult = expandConflictDefinitions( groupConflictMap ); |
| 372 |
const filterConflictResult = expandConflictDefinitions( filterConflictMap ); |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
$.each( groupConflictResult, ( group, conflicts ) => { |
| 377 |
model.groups[ group ].setConflicts( conflicts ); |
| 378 |
} ); |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
$.each( filterConflictResult, ( filterName, conflicts ) => { |
| 383 |
const filterItem = model.getItemByName( filterName ); |
| 384 |
|
| 385 |
filterItem.setConflicts( conflicts ); |
| 386 |
} ); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
$.each( this.groups, ( group, groupModel ) => { |
| 391 |
if ( |
| 392 |
groupModel.getType() === 'send_unselected_if_any' || |
| 393 |
groupModel.getType() === 'boolean' || |
| 394 |
groupModel.getType() === 'any_value' |
| 395 |
) { |
| 396 |
|
| 397 |
groupModel.getItems().forEach( ( filterItem ) => { |
| 398 |
model.parameterMap[ filterItem.getParamName() ] = filterItem; |
| 399 |
} ); |
| 400 |
} else if ( |
| 401 |
groupModel.getType() === 'string_options' || |
| 402 |
groupModel.getType() === 'single_option' |
| 403 |
) { |
| 404 |
|
| 405 |
model.parameterMap[ groupModel.getName() ] = groupModel; |
| 406 |
} |
| 407 |
} ); |
| 408 |
|
| 409 |
this.setSearch( '' ); |
| 410 |
|
| 411 |
this.updateHighlightedState(); |
| 412 |
|
| 413 |
|
| 414 |
this.emit( 'initialize' ); |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
FiltersViewModel.prototype.updateStateFromParams = function ( params ) { |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
$.each( this.getFilterGroups(), ( groupName, groupModel ) => { |
| 427 |
params[ groupName ] = groupModel.normalizeArbitraryValue( params[ groupName ] ); |
| 428 |
} ); |
| 429 |
|
| 430 |
|
| 431 |
const filtersValue = this.getFiltersFromParameters( params ); |
| 432 |
Object.keys( filtersValue ).forEach( ( filterName ) => { |
| 433 |
this.getItemByName( filterName ).setValue( filtersValue[ filterName ] ); |
| 434 |
} ); |
| 435 |
|
| 436 |
|
| 437 |
this.getItemsSupportingHighlights().forEach( ( filterItem ) => { |
| 438 |
const color = params[ filterItem.getName() + '_color' ]; |
| 439 |
if ( color ) { |
| 440 |
filterItem.setHighlightColor( color ); |
| 441 |
} else { |
| 442 |
filterItem.clearHighlightColor(); |
| 443 |
} |
| 444 |
} ); |
| 445 |
this.updateHighlightedState(); |
| 446 |
|
| 447 |
|
| 448 |
this.reassessFilterInteractions(); |
| 449 |
}; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
FiltersViewModel.prototype.getEmptyParameterState = function () { |
| 457 |
if ( !this.emptyParameterState ) { |
| 458 |
this.emptyParameterState = $.extend( |
| 459 |
true, |
| 460 |
{}, |
| 461 |
this.getParametersFromFilters( {} ), |
| 462 |
this.getEmptyHighlightParameters() |
| 463 |
); |
| 464 |
} |
| 465 |
return this.emptyParameterState; |
| 466 |
}; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
FiltersViewModel.prototype.getMinimizedParamRepresentation = function ( parameters ) { |
| 476 |
const result = {}; |
| 477 |
|
| 478 |
parameters = parameters ? $.extend( true, {}, parameters ) : this.getCurrentParameterState(); |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
$.each( this.getEmptyParameterState(), ( param, value ) => { |
| 483 |
if ( parameters[ param ] !== undefined && parameters[ param ] !== value ) { |
| 484 |
result[ param ] = parameters[ param ]; |
| 485 |
} |
| 486 |
} ); |
| 487 |
|
| 488 |
|
| 489 |
Object.keys( this.getEmptyHighlightParameters() ).forEach( ( param ) => { |
| 490 |
if ( parameters[ param ] ) { |
| 491 |
|
| 492 |
|
| 493 |
result[ param ] = parameters[ param ]; |
| 494 |
} |
| 495 |
} ); |
| 496 |
|
| 497 |
return result; |
| 498 |
}; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
FiltersViewModel.prototype.getExpandedParamRepresentation = function () { |
| 506 |
return $.extend( |
| 507 |
true, |
| 508 |
{}, |
| 509 |
this.getEmptyParameterState(), |
| 510 |
this.getCurrentParameterState() |
| 511 |
); |
| 512 |
}; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
FiltersViewModel.prototype.getCurrentParameterState = function ( removeStickyParams ) { |
| 521 |
let state = this.getMinimizedParamRepresentation( $.extend( |
| 522 |
true, |
| 523 |
{}, |
| 524 |
this.getParametersFromFilters( this.getSelectedState() ), |
| 525 |
this.getHighlightParameters() |
| 526 |
) ); |
| 527 |
|
| 528 |
if ( removeStickyParams ) { |
| 529 |
state = this.removeStickyParams( state ); |
| 530 |
} |
| 531 |
|
| 532 |
return state; |
| 533 |
}; |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
FiltersViewModel.prototype.removeStickyParams = function ( paramState ) { |
| 542 |
this.getStickyParams().forEach( ( paramName ) => { |
| 543 |
delete paramState[ paramName ]; |
| 544 |
} ); |
| 545 |
|
| 546 |
return paramState; |
| 547 |
}; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
FiltersViewModel.prototype.updateHighlightedState = function () { |
| 553 |
this.toggleHighlight( this.getHighlightedItems().length > 0 ); |
| 554 |
}; |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
FiltersViewModel.prototype.getFilterGroups = function () { |
| 562 |
return this.groups; |
| 563 |
}; |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
FiltersViewModel.prototype.getFilterGroupsByView = function ( view ) { |
| 572 |
const result = {}; |
| 573 |
|
| 574 |
view = view || this.getCurrentView(); |
| 575 |
|
| 576 |
|
| 577 |
$.each( this.groups, ( groupName, groupModel ) => { |
| 578 |
if ( groupModel.getView() === view ) { |
| 579 |
result[ groupName ] = groupModel; |
| 580 |
} |
| 581 |
} ); |
| 582 |
|
| 583 |
return result; |
| 584 |
}; |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
FiltersViewModel.prototype.getFiltersByView = function ( view ) { |
| 594 |
const result = []; |
| 595 |
|
| 596 |
view = view || this.getCurrentView(); |
| 597 |
|
| 598 |
const groups = this.getFilterGroupsByView( view ); |
| 599 |
|
| 600 |
|
| 601 |
$.each( groups, ( groupName, groupModel ) => { |
| 602 |
result.push( ...groupModel.getItems() ); |
| 603 |
} ); |
| 604 |
|
| 605 |
return result; |
| 606 |
}; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
FiltersViewModel.prototype.getViewTrigger = function ( view ) { |
| 615 |
return ( this.views[ view ] && this.views[ view ].trigger ) || ''; |
| 616 |
}; |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
FiltersViewModel.prototype.getParamValue = function ( name ) { |
| 625 |
return this.parameters[ name ]; |
| 626 |
}; |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
FiltersViewModel.prototype.getSelectedState = function ( onlySelected ) { |
| 635 |
const items = this.getItems(), |
| 636 |
result = {}; |
| 637 |
|
| 638 |
for ( let i = 0; i < items.length; i++ ) { |
| 639 |
if ( !onlySelected || items[ i ].getValue() ) { |
| 640 |
result[ items[ i ].getName() ] = items[ i ].getValue(); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
return result; |
| 645 |
}; |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
FiltersViewModel.prototype.getFullState = function () { |
| 653 |
const items = this.getItems(), |
| 654 |
result = {}; |
| 655 |
|
| 656 |
for ( let i = 0; i < items.length; i++ ) { |
| 657 |
result[ items[ i ].getName() ] = { |
| 658 |
selected: items[ i ].isSelected(), |
| 659 |
conflicted: items[ i ].isConflicted(), |
| 660 |
included: items[ i ].isIncluded() |
| 661 |
}; |
| 662 |
} |
| 663 |
|
| 664 |
return result; |
| 665 |
}; |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
FiltersViewModel.prototype.getDefaultParams = function () { |
| 673 |
const result = {}; |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
$.each( this.groups, ( name, model ) => { |
| 678 |
if ( !model.isSticky() ) { |
| 679 |
$.extend( true, result, model.getDefaultParams() ); |
| 680 |
} |
| 681 |
} ); |
| 682 |
|
| 683 |
return result; |
| 684 |
}; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
FiltersViewModel.prototype.getStickyParams = function () { |
| 692 |
let result = []; |
| 693 |
|
| 694 |
|
| 695 |
$.each( this.groups, ( name, model ) => { |
| 696 |
if ( model.isSticky() ) { |
| 697 |
if ( model.isPerGroupRequestParameter() ) { |
| 698 |
result.push( name ); |
| 699 |
} else { |
| 700 |
|
| 701 |
result = result.concat( model.getItems().map( ( filterItem ) => filterItem.getParamName() ) ); |
| 702 |
} |
| 703 |
} |
| 704 |
} ); |
| 705 |
|
| 706 |
return result; |
| 707 |
}; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
FiltersViewModel.prototype.getStickyParamsValues = function () { |
| 715 |
const result = {}; |
| 716 |
|
| 717 |
|
| 718 |
$.each( this.groups, ( name, model ) => { |
| 719 |
if ( model.isSticky() ) { |
| 720 |
$.extend( true, result, model.getParamRepresentation() ); |
| 721 |
} |
| 722 |
} ); |
| 723 |
|
| 724 |
return result; |
| 725 |
}; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
FiltersViewModel.prototype.getParametersFromFilters = function ( filterDefinition ) { |
| 736 |
let groupItemDefinition; |
| 737 |
const result = {}, |
| 738 |
groupItems = this.getFilterGroups(); |
| 739 |
|
| 740 |
if ( filterDefinition ) { |
| 741 |
groupItemDefinition = {}; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
this.getItems().forEach( ( filterItem ) => { |
| 749 |
groupItemDefinition[ filterItem.getGroupName() ] = groupItemDefinition[ filterItem.getGroupName() ] || {}; |
| 750 |
groupItemDefinition[ filterItem.getGroupName() ][ filterItem.getName() ] = filterItem.coerceValue( filterDefinition[ filterItem.getName() ] ); |
| 751 |
} ); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
$.each( groupItems, ( group, model ) => { |
| 756 |
Object.assign( |
| 757 |
result, |
| 758 |
model.getParamRepresentation( |
| 759 |
groupItemDefinition ? |
| 760 |
groupItemDefinition[ group ] : null |
| 761 |
) |
| 762 |
); |
| 763 |
} ); |
| 764 |
|
| 765 |
return result; |
| 766 |
}; |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
FiltersViewModel.prototype.getFiltersFromParameters = function ( params ) { |
| 776 |
const groupMap = {}, |
| 777 |
model = this; |
| 778 |
let result = {}; |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
$.each( params, ( paramName, paramValue ) => { |
| 793 |
const itemOrGroup = model.parameterMap[ paramName ]; |
| 794 |
|
| 795 |
if ( itemOrGroup ) { |
| 796 |
const groupName = itemOrGroup instanceof FilterItem ? |
| 797 |
itemOrGroup.getGroupName() : itemOrGroup.getName(); |
| 798 |
|
| 799 |
groupMap[ groupName ] = groupMap[ groupName ] || {}; |
| 800 |
groupMap[ groupName ][ paramName ] = paramValue; |
| 801 |
} |
| 802 |
} ); |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
$.each( this.groups, ( groupName, groupModel ) => { |
| 808 |
result = $.extend( true, {}, result, groupModel.getFilterRepresentation( groupMap[ groupName ] ) ); |
| 809 |
} ); |
| 810 |
|
| 811 |
return result; |
| 812 |
}; |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
FiltersViewModel.prototype.getHighlightParameters = function () { |
| 821 |
const highlightEnabled = this.isHighlightEnabled(), |
| 822 |
result = {}; |
| 823 |
|
| 824 |
this.getItems().forEach( ( filterItem ) => { |
| 825 |
if ( filterItem.isHighlightSupported() ) { |
| 826 |
result[ filterItem.getName() + '_color' ] = highlightEnabled && filterItem.isHighlighted() ? |
| 827 |
filterItem.getHighlightColor() : |
| 828 |
null; |
| 829 |
} |
| 830 |
} ); |
| 831 |
|
| 832 |
return result; |
| 833 |
}; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
FiltersViewModel.prototype.getEmptyHighlightParameters = function () { |
| 841 |
const result = {}; |
| 842 |
|
| 843 |
this.getItems().forEach( ( filterItem ) => { |
| 844 |
if ( filterItem.isHighlightSupported() ) { |
| 845 |
result[ filterItem.getName() + '_color' ] = null; |
| 846 |
} |
| 847 |
} ); |
| 848 |
|
| 849 |
return result; |
| 850 |
}; |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
FiltersViewModel.prototype.getCurrentlyUsedHighlightColors = function () { |
| 858 |
const result = []; |
| 859 |
|
| 860 |
if ( this.isHighlightEnabled() ) { |
| 861 |
this.getHighlightedItems().forEach( ( filterItem ) => { |
| 862 |
const color = filterItem.getHighlightColor(); |
| 863 |
|
| 864 |
if ( result.indexOf( color ) === -1 ) { |
| 865 |
result.push( color ); |
| 866 |
} |
| 867 |
} ); |
| 868 |
} |
| 869 |
|
| 870 |
return result; |
| 871 |
}; |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
FiltersViewModel.prototype.sanitizeStringOptionGroup = function ( groupName, valueArray ) { |
| 884 |
const validNames = this.getGroupFilters( groupName ).map( ( filterItem ) => filterItem.getParamName() ); |
| 885 |
|
| 886 |
return utils.normalizeParamOptions( valueArray, validNames ); |
| 887 |
}; |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
FiltersViewModel.prototype.areVisibleFiltersEmpty = function () { |
| 898 |
|
| 899 |
|
| 900 |
return !this.getItems().some( ( filterItem ) => { |
| 901 |
const visible = !filterItem.getGroupModel().isSticky() && !filterItem.getGroupModel().isHidden(), |
| 902 |
active = ( filterItem.isSelected() || filterItem.isHighlighted() ); |
| 903 |
return visible && active; |
| 904 |
} ); |
| 905 |
}; |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
FiltersViewModel.prototype.areNamespacesEffectivelyInverted = function () { |
| 918 |
return this.getNamespacesInvertModel().isSelected() && |
| 919 |
this.findSelectedItems().some( ( itemModel ) => itemModel.getGroupModel().getName() === 'namespace' ); |
| 920 |
}; |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
FiltersViewModel.prototype.areTagsEffectivelyInverted = function () { |
| 933 |
return this.getTagsInvertModel().isSelected() && |
| 934 |
this.findSelectedItems().some( ( itemModel ) => itemModel.getGroupModel().getName() === 'tagfilter' ); |
| 935 |
}; |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
FiltersViewModel.prototype.getItemByName = function ( name ) { |
| 945 |
return this.getItems().filter( ( item ) => name === item.getName() )[ 0 ]; |
| 946 |
}; |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
FiltersViewModel.prototype.emptyAllFilters = function () { |
| 953 |
this.getItems().forEach( ( filterItem ) => { |
| 954 |
if ( !filterItem.getGroupModel().isSticky() ) { |
| 955 |
this.toggleFilterSelected( filterItem.getName(), false ); |
| 956 |
} |
| 957 |
} ); |
| 958 |
}; |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
FiltersViewModel.prototype.toggleFilterSelected = function ( name, isSelected ) { |
| 967 |
const item = this.getItemByName( name ); |
| 968 |
|
| 969 |
if ( item ) { |
| 970 |
item.toggleSelected( isSelected ); |
| 971 |
} |
| 972 |
}; |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
FiltersViewModel.prototype.toggleFiltersSelected = function ( filterDef ) { |
| 980 |
Object.keys( filterDef ).forEach( ( name ) => { |
| 981 |
this.toggleFilterSelected( name, filterDef[ name ] ); |
| 982 |
} ); |
| 983 |
}; |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
FiltersViewModel.prototype.getGroup = function ( groupName ) { |
| 993 |
return this.groups[ groupName ]; |
| 994 |
}; |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
FiltersViewModel.prototype.getGroupFilters = function ( groupName ) { |
| 1004 |
return ( this.getGroup( groupName ) && this.getGroup( groupName ).getItems() ) || []; |
| 1005 |
}; |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
FiltersViewModel.prototype.findMatches = function ( query, returnFlat ) { |
| 1019 |
const result = {}, |
| 1020 |
flatResult = [], |
| 1021 |
view = this.getViewByTrigger( query.slice( 0, 1 ) ), |
| 1022 |
items = this.getFiltersByView( view ); |
| 1023 |
|
| 1024 |
|
| 1025 |
query = query.trim().toLowerCase(); |
| 1026 |
if ( view !== 'default' ) { |
| 1027 |
query = query.slice( 1 ); |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
query = query.trim(); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
const searchIsEmpty = query.length === 0; |
| 1036 |
|
| 1037 |
|
| 1038 |
for ( let i = 0; i < items.length; i++ ) { |
| 1039 |
if ( |
| 1040 |
searchIsEmpty || |
| 1041 |
items[ i ].getLabel().toLowerCase().indexOf( query ) === 0 || |
| 1042 |
( |
| 1043 |
|
| 1044 |
view === 'tags' && |
| 1045 |
items[ i ].getParamName().toLowerCase().indexOf( query ) > -1 |
| 1046 |
) |
| 1047 |
) { |
| 1048 |
result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || []; |
| 1049 |
result[ items[ i ].getGroupName() ].push( items[ i ] ); |
| 1050 |
flatResult.push( items[ i ] ); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
if ( $.isEmptyObject( result ) ) { |
| 1055 |
|
| 1056 |
for ( let i = 0; i < items.length; i++ ) { |
| 1057 |
const groupTitle = items[ i ].getGroupModel().getTitle(); |
| 1058 |
if ( |
| 1059 |
searchIsEmpty || |
| 1060 |
items[ i ].getLabel().toLowerCase().indexOf( query ) > -1 || |
| 1061 |
items[ i ].getDescription().toLowerCase().indexOf( query ) > -1 || |
| 1062 |
groupTitle.toLowerCase().indexOf( query ) > -1 || |
| 1063 |
( |
| 1064 |
|
| 1065 |
view === 'tags' && |
| 1066 |
items[ i ].getParamName().toLowerCase().indexOf( query ) > -1 |
| 1067 |
) |
| 1068 |
) { |
| 1069 |
result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || []; |
| 1070 |
result[ items[ i ].getGroupName() ].push( items[ i ] ); |
| 1071 |
flatResult.push( items[ i ] ); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
return returnFlat ? flatResult : result; |
| 1077 |
}; |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
FiltersViewModel.prototype.getHighlightedItems = function () { |
| 1086 |
return this.getItems().filter( ( filterItem ) => filterItem.isHighlightSupported() && |
| 1087 |
filterItem.getHighlightColor() ); |
| 1088 |
}; |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
FiltersViewModel.prototype.getItemsSupportingHighlights = function () { |
| 1097 |
return this.getItems().filter( ( filterItem ) => filterItem.isHighlightSupported() ); |
| 1098 |
}; |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
FiltersViewModel.prototype.findSelectedItems = function () { |
| 1107 |
let allSelected = []; |
| 1108 |
|
| 1109 |
|
| 1110 |
$.each( this.getFilterGroups(), ( groupName, groupModel ) => { |
| 1111 |
allSelected = allSelected.concat( groupModel.findSelectedItems() ); |
| 1112 |
} ); |
| 1113 |
|
| 1114 |
return allSelected; |
| 1115 |
}; |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
FiltersViewModel.prototype.getCurrentView = function () { |
| 1123 |
return this.currentView; |
| 1124 |
}; |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
FiltersViewModel.prototype.getViewTitle = function ( viewName ) { |
| 1133 |
viewName = viewName || this.getCurrentView(); |
| 1134 |
|
| 1135 |
return this.views[ viewName ] && this.views[ viewName ].title; |
| 1136 |
}; |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
FiltersViewModel.prototype.getViewByTrigger = function ( trigger ) { |
| 1145 |
let result = 'default'; |
| 1146 |
|
| 1147 |
|
| 1148 |
$.each( this.views, ( name, data ) => { |
| 1149 |
if ( data.trigger === trigger ) { |
| 1150 |
result = name; |
| 1151 |
} |
| 1152 |
} ); |
| 1153 |
|
| 1154 |
return result; |
| 1155 |
}; |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
FiltersViewModel.prototype.removeViewTriggers = function ( str ) { |
| 1165 |
if ( this.getViewFromString( str ) !== 'default' ) { |
| 1166 |
str = str.slice( 1 ); |
| 1167 |
} |
| 1168 |
|
| 1169 |
return str; |
| 1170 |
}; |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
FiltersViewModel.prototype.getViewFromString = function ( str ) { |
| 1179 |
return this.getViewByTrigger( str.slice( 0, 1 ) ); |
| 1180 |
}; |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
FiltersViewModel.prototype.setSearch = function ( searchQuery ) { |
| 1191 |
let visibleGroups, visibleGroupNames; |
| 1192 |
|
| 1193 |
if ( this.searchQuery !== searchQuery ) { |
| 1194 |
|
| 1195 |
this.switchView( this.getViewFromString( searchQuery ) ); |
| 1196 |
|
| 1197 |
visibleGroups = this.findMatches( searchQuery ); |
| 1198 |
visibleGroupNames = Object.keys( visibleGroups ); |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
$.each( this.getFilterGroups(), ( groupName, groupModel ) => { |
| 1203 |
|
| 1204 |
groupModel.toggleVisible( visibleGroupNames.indexOf( groupName ) !== -1 ); |
| 1205 |
groupModel.setVisibleItems( visibleGroups[ groupName ] || [] ); |
| 1206 |
} ); |
| 1207 |
|
| 1208 |
this.searchQuery = searchQuery; |
| 1209 |
this.emit( 'searchChange', this.searchQuery ); |
| 1210 |
} |
| 1211 |
}; |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
FiltersViewModel.prototype.getSearch = function () { |
| 1219 |
return this.searchQuery; |
| 1220 |
}; |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
FiltersViewModel.prototype.switchView = function ( view ) { |
| 1229 |
if ( this.views[ view ] && this.currentView !== view ) { |
| 1230 |
this.currentView = view; |
| 1231 |
} |
| 1232 |
}; |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
FiltersViewModel.prototype.toggleHighlight = function ( enable ) { |
| 1242 |
enable = enable === undefined ? !this.highlightEnabled : enable; |
| 1243 |
|
| 1244 |
if ( this.highlightEnabled !== enable ) { |
| 1245 |
this.highlightEnabled = enable; |
| 1246 |
this.emit( 'highlightChange', this.highlightEnabled ); |
| 1247 |
} |
| 1248 |
}; |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
FiltersViewModel.prototype.isHighlightEnabled = function () { |
| 1256 |
return !!this.highlightEnabled; |
| 1257 |
}; |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
FiltersViewModel.prototype.toggleInvertedTags = function ( enable ) { |
| 1266 |
this.toggleFilterSelected( this.getTagsInvertModel().getName(), enable ); |
| 1267 |
}; |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
FiltersViewModel.prototype.toggleInvertedNamespaces = function ( enable ) { |
| 1276 |
this.toggleFilterSelected( this.getNamespacesInvertModel().getName(), enable ); |
| 1277 |
}; |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
FiltersViewModel.prototype.getInvertModel = function ( view ) { |
| 1287 |
if ( view === 'namespaces' ) { |
| 1288 |
return this.getNamespacesInvertModel(); |
| 1289 |
} |
| 1290 |
if ( view === 'tags' ) { |
| 1291 |
return this.getTagsInvertModel(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
return null; |
| 1295 |
}; |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
FiltersViewModel.prototype.getNamespacesInvertModel = function () { |
| 1304 |
return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' ); |
| 1305 |
}; |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
FiltersViewModel.prototype.getTagsInvertModel = function () { |
| 1314 |
return this.getGroup( 'invertTagsGroup' ).getItemByParamName( 'inverttags' ); |
| 1315 |
}; |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
FiltersViewModel.prototype.setHighlightColor = function ( filterName, color ) { |
| 1324 |
this.getItemByName( filterName ).setHighlightColor( color ); |
| 1325 |
}; |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
FiltersViewModel.prototype.clearHighlightColor = function ( filterName ) { |
| 1333 |
this.getItemByName( filterName ).clearHighlightColor(); |
| 1334 |
}; |
| 1335 |
|
| 1336 |
module.exports = FiltersViewModel; |