| 1 |
const FilterItem = require( './FilterItem.js' ), |
| 2 |
utils = require( '../utils.js' ); |
| 3 |
|
| 4 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
const FilterGroup = function MwRcfiltersDmFilterGroup( name, config ) { |
| 45 |
config = config || {}; |
| 46 |
|
| 47 |
|
| 48 |
OO.EventEmitter.call( this ); |
| 49 |
OO.EmitterList.call( this ); |
| 50 |
|
| 51 |
this.name = name; |
| 52 |
this.type = config.type || 'send_unselected_if_any'; |
| 53 |
this.view = config.view || 'default'; |
| 54 |
this.sticky = !!config.sticky; |
| 55 |
this.title = config.title || name; |
| 56 |
this.hidden = !!config.hidden; |
| 57 |
this.allowArbitrary = !!config.allowArbitrary; |
| 58 |
this.numericRange = config.range; |
| 59 |
this.separator = config.separator || '|'; |
| 60 |
this.supportsAll = config.supportsAll === undefined ? true : !!config.supportsAll; |
| 61 |
this.labelPrefixKey = config.labelPrefixKey; |
| 62 |
this.visible = config.visible === undefined ? true : !!config.visible; |
| 63 |
|
| 64 |
this.currSelected = null; |
| 65 |
this.active = !!config.active; |
| 66 |
this.fullCoverage = !!config.fullCoverage; |
| 67 |
|
| 68 |
this.whatsThis = config.whatsThis || {}; |
| 69 |
|
| 70 |
this.conflicts = config.conflicts || {}; |
| 71 |
this.defaultParams = {}; |
| 72 |
this.defaultFilters = {}; |
| 73 |
|
| 74 |
this.aggregate( { update: 'filterItemUpdate' } ); |
| 75 |
this.connect( this, { filterItemUpdate: 'onFilterItemUpdate' } ); |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
OO.initClass( FilterGroup ); |
| 80 |
OO.mixinClass( FilterGroup, OO.EventEmitter ); |
| 81 |
OO.mixinClass( FilterGroup, OO.EmitterList ); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
FilterGroup.prototype.initializeFilters = function ( filterDefinition, groupDefault ) { |
| 101 |
let defaultParam; |
| 102 |
const supersetMap = {}, |
| 103 |
model = this, |
| 104 |
items = []; |
| 105 |
|
| 106 |
filterDefinition.forEach( ( filter ) => { |
| 107 |
|
| 108 |
const filterItem = new FilterItem( filter.name, model, { |
| 109 |
group: model.getName(), |
| 110 |
label: filter.label || filter.name, |
| 111 |
description: filter.description || '', |
| 112 |
labelPrefixKey: model.labelPrefixKey, |
| 113 |
cssClass: filter.cssClass, |
| 114 |
helpLink: filter.helpLink, |
| 115 |
identifiers: filter.identifiers, |
| 116 |
defaultHighlightColor: filter.defaultHighlightColor |
| 117 |
} ); |
| 118 |
|
| 119 |
if ( filter.subset ) { |
| 120 |
filter.subset = filter.subset.map( ( el ) => el.filter ); |
| 121 |
|
| 122 |
const subsetNames = []; |
| 123 |
|
| 124 |
filter.subset.forEach( ( subsetFilterName ) => { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
const subsetName = model.getPrefixedName( subsetFilterName ); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
supersetMap[ subsetName ] = supersetMap[ subsetName ] || []; |
| 134 |
utils.addArrayElementsUnique( |
| 135 |
supersetMap[ subsetName ], |
| 136 |
filterItem.getName() |
| 137 |
); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
subsetNames.push( subsetName ); |
| 143 |
} ); |
| 144 |
|
| 145 |
|
| 146 |
filterItem.setSubset( subsetNames ); |
| 147 |
} |
| 148 |
|
| 149 |
items.push( filterItem ); |
| 150 |
|
| 151 |
|
| 152 |
if ( |
| 153 |
model.getType() === 'send_unselected_if_any' || |
| 154 |
model.getType() === 'boolean' |
| 155 |
) { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
model.defaultParams[ filter.name ] = String( Number( filter.default || 0 ) ); |
| 160 |
} else if ( model.getType() === 'any_value' ) { |
| 161 |
model.defaultParams[ filter.name ] = filter.default; |
| 162 |
} |
| 163 |
} ); |
| 164 |
|
| 165 |
|
| 166 |
this.addItems( items ); |
| 167 |
|
| 168 |
|
| 169 |
this.getItems().forEach( ( filterItem ) => { |
| 170 |
filterItem.setSuperset( supersetMap[ filterItem.getName() ] ); |
| 171 |
} ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if ( this.getType() === 'string_options' ) { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
this.defaultParams[ this.getName() ] = utils.normalizeParamOptions( |
| 180 |
|
| 181 |
groupDefault ? |
| 182 |
groupDefault.split( this.getSeparator() ) : |
| 183 |
[], |
| 184 |
|
| 185 |
this.getItems().map( ( item ) => item.getParamName() ) |
| 186 |
).join( this.getSeparator() ); |
| 187 |
} else if ( this.getType() === 'single_option' ) { |
| 188 |
defaultParam = groupDefault !== undefined ? |
| 189 |
groupDefault : this.getItems()[ 0 ].getParamName(); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
this.defaultParams[ this.getName() ] = defaultParam; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
this.getItems().forEach( ( filterItem ) => { |
| 198 |
if ( filterItem.isHighlighted() ) { |
| 199 |
this.defaultParams[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor(); |
| 200 |
} |
| 201 |
} ); |
| 202 |
|
| 203 |
|
| 204 |
this.defaultFilters = this.getFilterRepresentation( this.getDefaultParams() ); |
| 205 |
|
| 206 |
|
| 207 |
if ( this.isSticky() ) { |
| 208 |
const defaultFilters = this.defaultFilters; |
| 209 |
for ( const filterName in defaultFilters ) { |
| 210 |
const filterValue = defaultFilters[ filterName ]; |
| 211 |
model.getItemByName( filterName ).toggleSelected( filterValue ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
if ( |
| 217 |
this.getType() === 'single_option' && |
| 218 |
this.findSelectedItems().length === 0 |
| 219 |
) { |
| 220 |
defaultParam = groupDefault !== undefined ? |
| 221 |
groupDefault : this.getItems()[ 0 ].getParamName(); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
this.selectItemByParamName( defaultParam ); |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
FilterGroup.prototype.onFilterItemUpdate = function ( item ) { |
| 237 |
|
| 238 |
let changed = false; |
| 239 |
const active = this.areAnySelected(), |
| 240 |
model = this; |
| 241 |
|
| 242 |
if ( this.getType() === 'single_option' ) { |
| 243 |
|
| 244 |
|
| 245 |
if ( this.findSelectedItems().length === 0 ) { |
| 246 |
|
| 247 |
|
| 248 |
this.currSelected = this.getItemByParamName( this.defaultParams[ this.getName() ] ) || |
| 249 |
this.getItems()[ 0 ]; |
| 250 |
this.currSelected.toggleSelected( true ); |
| 251 |
changed = true; |
| 252 |
} else if ( this.findSelectedItems().length > 1 ) { |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
this.findSelectedItems().forEach( ( itemModel ) => { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
const selected = itemModel.getName() === item.getName() && |
| 263 |
item.isSelected(); |
| 264 |
|
| 265 |
itemModel.toggleSelected( selected ); |
| 266 |
if ( selected ) { |
| 267 |
model.currSelected = itemModel; |
| 268 |
} |
| 269 |
} ); |
| 270 |
changed = true; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if ( this.isSticky() ) { |
| 275 |
|
| 276 |
|
| 277 |
this.defaultParams = this.getParamRepresentation( this.getSelectedState() ); |
| 278 |
} |
| 279 |
|
| 280 |
if ( |
| 281 |
changed || |
| 282 |
this.active !== active || |
| 283 |
this.currSelected !== item |
| 284 |
) { |
| 285 |
this.active = active; |
| 286 |
this.currSelected = item; |
| 287 |
|
| 288 |
this.emit( 'update' ); |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
FilterGroup.prototype.isActive = function () { |
| 298 |
return this.active; |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
FilterGroup.prototype.isHidden = function () { |
| 307 |
return this.hidden; |
| 308 |
}; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
FilterGroup.prototype.isAllowArbitrary = function () { |
| 316 |
return this.allowArbitrary; |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
FilterGroup.prototype.getMaxValue = function () { |
| 325 |
return this.numericRange && this.numericRange.max !== undefined ? |
| 326 |
this.numericRange.max : null; |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
FilterGroup.prototype.getMinValue = function () { |
| 335 |
return this.numericRange && this.numericRange.min !== undefined ? |
| 336 |
this.numericRange.min : null; |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
FilterGroup.prototype.getName = function () { |
| 345 |
return this.name; |
| 346 |
}; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
FilterGroup.prototype.getDefaultParams = function () { |
| 354 |
return this.defaultParams; |
| 355 |
}; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
FilterGroup.prototype.getDefaultFilters = function () { |
| 363 |
return this.defaultFilters; |
| 364 |
}; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
FilterGroup.prototype.getWhatsThis = function () { |
| 372 |
return this.whatsThis; |
| 373 |
}; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
FilterGroup.prototype.hasWhatsThis = function () { |
| 381 |
return !!this.whatsThis.body; |
| 382 |
}; |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
FilterGroup.prototype.getConflicts = function () { |
| 409 |
return this.conflicts; |
| 410 |
}; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
FilterGroup.prototype.setConflicts = function ( conflicts ) { |
| 419 |
this.conflicts = conflicts; |
| 420 |
}; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
FilterGroup.prototype.existsInConflicts = function ( filterItem ) { |
| 433 |
return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() ); |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
FilterGroup.prototype.areAnySelected = function () { |
| 442 |
return this.getItems().some( ( filterItem ) => filterItem.isSelected() ); |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
FilterGroup.prototype.areAllSelected = function () { |
| 451 |
const selected = [], |
| 452 |
unselected = []; |
| 453 |
|
| 454 |
this.getItems().forEach( ( filterItem ) => { |
| 455 |
if ( filterItem.isSelected() ) { |
| 456 |
selected.push( filterItem ); |
| 457 |
} else { |
| 458 |
unselected.push( filterItem ); |
| 459 |
} |
| 460 |
} ); |
| 461 |
|
| 462 |
if ( unselected.length === 0 ) { |
| 463 |
return true; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
return unselected.every( ( unselectedFilterItem ) => selected.some( ( selectedFilterItem ) => selectedFilterItem.existsInSubset( unselectedFilterItem.getName() ) ) ); |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
FilterGroup.prototype.findSelectedItems = function ( excludeItem ) { |
| 478 |
const excludeName = ( excludeItem && excludeItem.getName() ) || ''; |
| 479 |
|
| 480 |
return this.getItems().filter( ( item ) => item.getName() !== excludeName && item.isSelected() ); |
| 481 |
}; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
FilterGroup.prototype.areAllSelectedInConflictWith = function ( filterItem ) { |
| 490 |
const selectedItems = this.findSelectedItems( filterItem ); |
| 491 |
|
| 492 |
return selectedItems.length > 0 && |
| 493 |
( |
| 494 |
|
| 495 |
this.existsInConflicts( filterItem ) || |
| 496 |
|
| 497 |
selectedItems.every( ( selectedFilter ) => selectedFilter.existsInConflicts( filterItem ) ) |
| 498 |
); |
| 499 |
}; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
FilterGroup.prototype.areAnySelectedInConflictWith = function ( filterItem ) { |
| 508 |
const selectedItems = this.findSelectedItems( filterItem ); |
| 509 |
|
| 510 |
return selectedItems.length > 0 && ( |
| 511 |
|
| 512 |
this.existsInConflicts( filterItem ) || |
| 513 |
|
| 514 |
selectedItems.some( ( selectedFilter ) => selectedFilter.existsInConflicts( filterItem ) ) |
| 515 |
); |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
FilterGroup.prototype.getParamRepresentation = function ( filterRepresentation ) { |
| 527 |
let areAnySelected = false; |
| 528 |
const buildFromCurrentState = !filterRepresentation, |
| 529 |
defaultFilters = this.getDefaultFilters(), |
| 530 |
result = {}, |
| 531 |
model = this, |
| 532 |
filterParamNames = {}, |
| 533 |
getSelectedParameter = function ( filters ) { |
| 534 |
const selected = []; |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
$.each( filters, ( name, value ) => { |
| 539 |
if ( value ) { |
| 540 |
selected.push( name ); |
| 541 |
} |
| 542 |
} ); |
| 543 |
|
| 544 |
const item = model.getItemByName( selected[ 0 ] ); |
| 545 |
return ( item && item.getParamName() ) || ''; |
| 546 |
}; |
| 547 |
|
| 548 |
filterRepresentation = filterRepresentation || {}; |
| 549 |
|
| 550 |
|
| 551 |
this.getItems().forEach( ( item ) => { |
| 552 |
|
| 553 |
filterParamNames[ item.getName() ] = item.getParamName(); |
| 554 |
|
| 555 |
if ( buildFromCurrentState ) { |
| 556 |
|
| 557 |
|
| 558 |
filterRepresentation[ item.getName() ] = item.getValue(); |
| 559 |
} else if ( filterRepresentation[ item.getName() ] === undefined ) { |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
if ( model.isSticky() ) { |
| 564 |
filterRepresentation[ item.getName() ] = !!defaultFilters[ item.getName() ]; |
| 565 |
} else { |
| 566 |
filterRepresentation[ item.getName() ] = false; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if ( filterRepresentation[ item.getName() ] ) { |
| 571 |
areAnySelected = true; |
| 572 |
} |
| 573 |
} ); |
| 574 |
|
| 575 |
|
| 576 |
if ( |
| 577 |
this.getType() === 'send_unselected_if_any' || |
| 578 |
this.getType() === 'boolean' || |
| 579 |
this.getType() === 'any_value' |
| 580 |
) { |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
$.each( filterRepresentation, ( name, value ) => { |
| 588 |
|
| 589 |
if ( model.getType() === 'send_unselected_if_any' ) { |
| 590 |
result[ filterParamNames[ name ] ] = areAnySelected ? |
| 591 |
String( Number( !value ) ) : |
| 592 |
'0'; |
| 593 |
} else if ( model.getType() === 'boolean' ) { |
| 594 |
|
| 595 |
|
| 596 |
result[ filterParamNames[ name ] ] = String( Number( !!value ) ); |
| 597 |
} else if ( model.getType() === 'any_value' ) { |
| 598 |
result[ filterParamNames[ name ] ] = value; |
| 599 |
} |
| 600 |
} ); |
| 601 |
} else if ( this.getType() === 'string_options' ) { |
| 602 |
const values = []; |
| 603 |
|
| 604 |
|
| 605 |
$.each( filterRepresentation, ( name, value ) => { |
| 606 |
|
| 607 |
if ( value ) { |
| 608 |
values.push( filterParamNames[ name ] ); |
| 609 |
} |
| 610 |
} ); |
| 611 |
|
| 612 |
result[ this.getName() ] = this.getSupportsAll() && |
| 613 |
values.length === Object.keys( filterRepresentation ).length ? |
| 614 |
'all' : values.join( this.getSeparator() ); |
| 615 |
} else if ( this.getType() === 'single_option' ) { |
| 616 |
result[ this.getName() ] = getSelectedParameter( filterRepresentation ); |
| 617 |
} |
| 618 |
|
| 619 |
return result; |
| 620 |
}; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
FilterGroup.prototype.getFilterRepresentation = function ( paramRepresentation ) { |
| 633 |
let areAnySelected, |
| 634 |
oneWasSelected = false; |
| 635 |
const defaultParams = this.getDefaultParams(), |
| 636 |
expandedParams = $.extend( true, {}, paramRepresentation ), |
| 637 |
model = this, |
| 638 |
paramToFilterMap = {}, |
| 639 |
result = {}; |
| 640 |
|
| 641 |
if ( this.isSticky() ) { |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
paramRepresentation = $.extend( true, {}, this.getDefaultParams(), paramRepresentation ); |
| 646 |
} |
| 647 |
|
| 648 |
paramRepresentation = paramRepresentation || {}; |
| 649 |
if ( |
| 650 |
this.getType() === 'send_unselected_if_any' || |
| 651 |
this.getType() === 'boolean' || |
| 652 |
this.getType() === 'any_value' |
| 653 |
) { |
| 654 |
|
| 655 |
this.getItems().forEach( ( filterItem ) => { |
| 656 |
const paramName = filterItem.getParamName(); |
| 657 |
|
| 658 |
expandedParams[ paramName ] = paramRepresentation[ paramName ] || '0'; |
| 659 |
paramToFilterMap[ paramName ] = filterItem; |
| 660 |
|
| 661 |
if ( Number( paramRepresentation[ filterItem.getParamName() ] ) ) { |
| 662 |
areAnySelected = true; |
| 663 |
} |
| 664 |
} ); |
| 665 |
|
| 666 |
|
| 667 |
$.each( expandedParams, ( paramName, paramValue ) => { |
| 668 |
const filterItem = paramToFilterMap[ paramName ]; |
| 669 |
|
| 670 |
if ( model.getType() === 'send_unselected_if_any' ) { |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
result[ filterItem.getName() ] = areAnySelected ? |
| 675 |
!Number( paramValue ) : |
| 676 |
|
| 677 |
|
| 678 |
false; |
| 679 |
} else if ( model.getType() === 'boolean' ) { |
| 680 |
|
| 681 |
result[ filterItem.getName() ] = !!Number( paramRepresentation[ filterItem.getParamName() ] ); |
| 682 |
} else if ( model.getType() === 'any_value' ) { |
| 683 |
result[ filterItem.getName() ] = paramRepresentation[ filterItem.getParamName() ]; |
| 684 |
} |
| 685 |
} ); |
| 686 |
} else if ( this.getType() === 'string_options' ) { |
| 687 |
const currentValue = paramRepresentation[ this.getName() ] || ''; |
| 688 |
|
| 689 |
|
| 690 |
const paramValues = utils.normalizeParamOptions( |
| 691 |
|
| 692 |
currentValue.split( |
| 693 |
this.getSeparator() |
| 694 |
), |
| 695 |
|
| 696 |
this.getItems().map( ( filterItem ) => filterItem.getParamName() ), |
| 697 |
this.getSupportsAll() |
| 698 |
); |
| 699 |
|
| 700 |
this.getItems().forEach( ( filterItem ) => { |
| 701 |
|
| 702 |
result[ filterItem.getName() ] = ( |
| 703 |
this.getSupportsAll() && paramValues.length === 1 && paramValues[ 0 ] === 'all' |
| 704 |
) ? |
| 705 |
true : |
| 706 |
|
| 707 |
paramValues.indexOf( filterItem.getParamName() ) > -1; |
| 708 |
} ); |
| 709 |
} else if ( this.getType() === 'single_option' ) { |
| 710 |
|
| 711 |
this.getItems().forEach( ( filterItem ) => { |
| 712 |
const selected = filterItem.getParamName() === paramRepresentation[ model.getName() ]; |
| 713 |
|
| 714 |
result[ filterItem.getName() ] = selected; |
| 715 |
oneWasSelected = oneWasSelected || selected; |
| 716 |
} ); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
this.getItems().forEach( ( filterItem ) => { |
| 722 |
if ( result[ filterItem.getName() ] === undefined ) { |
| 723 |
result[ filterItem.getName() ] = this.getFalsyValue(); |
| 724 |
} |
| 725 |
} ); |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
if ( |
| 733 |
this.getType() === 'single_option' && |
| 734 |
!oneWasSelected |
| 735 |
) { |
| 736 |
let item = this.getItems()[ 0 ]; |
| 737 |
if ( defaultParams[ this.getName() ] ) { |
| 738 |
item = this.getItemByParamName( defaultParams[ this.getName() ] ); |
| 739 |
} |
| 740 |
|
| 741 |
result[ item.getName() ] = true; |
| 742 |
} |
| 743 |
|
| 744 |
return result; |
| 745 |
}; |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
FilterGroup.prototype.getFalsyValue = function () { |
| 751 |
return this.getType() === 'any_value' ? '' : false; |
| 752 |
}; |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
FilterGroup.prototype.getSelectedState = function () { |
| 760 |
const state = {}; |
| 761 |
|
| 762 |
this.getItems().forEach( ( filterItem ) => { |
| 763 |
state[ filterItem.getName() ] = filterItem.getValue(); |
| 764 |
} ); |
| 765 |
|
| 766 |
return state; |
| 767 |
}; |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
FilterGroup.prototype.getItemByName = function ( filterName ) { |
| 777 |
return this.getItems().filter( ( item ) => item.getName() === filterName )[ 0 ]; |
| 778 |
}; |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
FilterGroup.prototype.selectItemByParamName = function ( paramName ) { |
| 786 |
this.getItems().forEach( ( item ) => { |
| 787 |
item.toggleSelected( item.getParamName() === String( paramName ) ); |
| 788 |
} ); |
| 789 |
}; |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
FilterGroup.prototype.getItemByParamName = function ( paramName ) { |
| 799 |
return this.getItems().filter( ( item ) => item.getParamName() === String( paramName ) )[ 0 ]; |
| 800 |
}; |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
FilterGroup.prototype.getType = function () { |
| 808 |
return this.type; |
| 809 |
}; |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
FilterGroup.prototype.isPerGroupRequestParameter = function () { |
| 818 |
return ( |
| 819 |
this.getType() === 'string_options' || |
| 820 |
this.getType() === 'single_option' |
| 821 |
); |
| 822 |
}; |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
FilterGroup.prototype.getView = function () { |
| 830 |
return this.view; |
| 831 |
}; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
FilterGroup.prototype.getNamePrefix = function () { |
| 839 |
return this.getName() + '__'; |
| 840 |
}; |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
FilterGroup.prototype.getPrefixedName = function ( name ) { |
| 849 |
return this.getNamePrefix() + name; |
| 850 |
}; |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
FilterGroup.prototype.getTitle = function () { |
| 858 |
return this.title; |
| 859 |
}; |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
FilterGroup.prototype.getSeparator = function () { |
| 867 |
return this.separator; |
| 868 |
}; |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
FilterGroup.prototype.getSupportsAll = function () { |
| 876 |
return this.supportsAll; |
| 877 |
}; |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
FilterGroup.prototype.isFullCoverage = function () { |
| 885 |
return this.fullCoverage; |
| 886 |
}; |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
FilterGroup.prototype.isSticky = function () { |
| 894 |
return this.sticky; |
| 895 |
}; |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
FilterGroup.prototype.normalizeArbitraryValue = function ( value ) { |
| 906 |
if ( |
| 907 |
this.getType() === 'single_option' && |
| 908 |
this.isAllowArbitrary() |
| 909 |
) { |
| 910 |
if ( |
| 911 |
this.getMaxValue() !== null && |
| 912 |
value > this.getMaxValue() |
| 913 |
) { |
| 914 |
|
| 915 |
return String( this.getMaxValue() ); |
| 916 |
} else if ( |
| 917 |
this.getMinValue() !== null && |
| 918 |
value < this.getMinValue() |
| 919 |
) { |
| 920 |
|
| 921 |
return String( this.getMinValue() ); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
return value; |
| 926 |
}; |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
FilterGroup.prototype.toggleVisible = function ( isVisible ) { |
| 934 |
isVisible = isVisible === undefined ? !this.visible : isVisible; |
| 935 |
|
| 936 |
if ( this.visible !== isVisible ) { |
| 937 |
this.visible = isVisible; |
| 938 |
this.emit( 'update' ); |
| 939 |
} |
| 940 |
}; |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
FilterGroup.prototype.isVisible = function () { |
| 948 |
return this.visible; |
| 949 |
}; |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
FilterGroup.prototype.setVisibleItems = function ( visibleItems ) { |
| 957 |
this.getItems().forEach( ( itemModel ) => { |
| 958 |
itemModel.toggleVisible( visibleItems.indexOf( itemModel ) !== -1 ); |
| 959 |
} ); |
| 960 |
}; |
| 961 |
|
| 962 |
module.exports = FilterGroup; |