| 1 |
const byteLength = require( 'mediawiki.String' ).byteLength, |
| 2 |
UriProcessor = require( './UriProcessor.js' ); |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
const Controller = function MwRcfiltersController( filtersModel, changesListModel, savedQueriesModel, config ) { |
| 25 |
this.filtersModel = filtersModel; |
| 26 |
this.changesListModel = changesListModel; |
| 27 |
this.savedQueriesModel = savedQueriesModel; |
| 28 |
this.savedQueriesPreferenceName = config.savedQueriesPreferenceName; |
| 29 |
this.daysPreferenceName = config.daysPreferenceName; |
| 30 |
this.limitPreferenceName = config.limitPreferenceName; |
| 31 |
this.collapsedPreferenceName = config.collapsedPreferenceName; |
| 32 |
this.normalizeTarget = !!config.normalizeTarget; |
| 33 |
|
| 34 |
|
| 35 |
this.pollingRate = require( './dmConfig.json' ).StructuredChangeFiltersLiveUpdatePollingRate; |
| 36 |
|
| 37 |
this.requestCounter = {}; |
| 38 |
this.uriProcessor = null; |
| 39 |
this.initialized = false; |
| 40 |
this.wereSavedQueriesSaved = false; |
| 41 |
|
| 42 |
this.prevLoggedItems = []; |
| 43 |
|
| 44 |
this.FILTER_CHANGE = 'filterChange'; |
| 45 |
this.SHOW_NEW_CHANGES = 'showNewChanges'; |
| 46 |
this.LIVE_UPDATE = 'liveUpdate'; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
OO.initClass( Controller ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList, conditionalViews ) { |
| 61 |
const displayConfig = mw.config.get( 'StructuredChangeFiltersDisplayConfig' ), |
| 62 |
defaultSavedQueryExists = mw.config.get( 'wgStructuredChangeFiltersDefaultSavedQueryExists' ), |
| 63 |
controller = this, |
| 64 |
views = $.extend( true, {}, conditionalViews ), |
| 65 |
url = new URL( location.href ); |
| 66 |
|
| 67 |
|
| 68 |
const nsAllContents = { |
| 69 |
name: 'all-contents', |
| 70 |
label: mw.msg( 'rcfilters-allcontents-label' ), |
| 71 |
description: '', |
| 72 |
identifiers: [ 'subject' ], |
| 73 |
cssClass: 'mw-changeslist-ns-subject', |
| 74 |
subset: [] |
| 75 |
}; |
| 76 |
const nsAllDiscussions = { |
| 77 |
name: 'all-discussions', |
| 78 |
label: mw.msg( 'rcfilters-alldiscussions-label' ), |
| 79 |
description: '', |
| 80 |
identifiers: [ 'talk' ], |
| 81 |
cssClass: 'mw-changeslist-ns-talk', |
| 82 |
subset: [] |
| 83 |
}; |
| 84 |
const items = [ nsAllContents, nsAllDiscussions ]; |
| 85 |
for ( const namespaceID in namespaceStructure ) { |
| 86 |
const label = namespaceStructure[ namespaceID ]; |
| 87 |
|
| 88 |
const isTalk = mw.Title.isTalkNamespace( namespaceID ), |
| 89 |
nsFilter = { |
| 90 |
name: namespaceID, |
| 91 |
label: label || mw.msg( 'blanknamespace' ), |
| 92 |
description: '', |
| 93 |
identifiers: [ |
| 94 |
isTalk ? 'talk' : 'subject' |
| 95 |
], |
| 96 |
cssClass: 'mw-changeslist-ns-' + namespaceID |
| 97 |
}; |
| 98 |
items.push( nsFilter ); |
| 99 |
( isTalk ? nsAllDiscussions : nsAllContents ).subset.push( { filter: namespaceID } ); |
| 100 |
} |
| 101 |
|
| 102 |
views.namespaces = { |
| 103 |
title: mw.msg( 'namespaces' ), |
| 104 |
trigger: ':', |
| 105 |
groups: [ { |
| 106 |
|
| 107 |
name: 'namespace', |
| 108 |
type: 'string_options', |
| 109 |
title: mw.msg( 'namespaces' ), |
| 110 |
labelPrefixKey: { |
| 111 |
default: 'rcfilters-tag-prefix-namespace', |
| 112 |
inverted: 'rcfilters-tag-prefix-namespace-inverted' |
| 113 |
}, |
| 114 |
separator: ';', |
| 115 |
supportsAll: false, |
| 116 |
fullCoverage: true, |
| 117 |
filters: items |
| 118 |
} ] |
| 119 |
}; |
| 120 |
views.invertNamespaces = { |
| 121 |
groups: [ |
| 122 |
{ |
| 123 |
|
| 124 |
|
| 125 |
name: 'invertGroup', |
| 126 |
type: 'boolean', |
| 127 |
hidden: true, |
| 128 |
filters: [ { |
| 129 |
name: 'invert', |
| 130 |
default: '0' |
| 131 |
} ] |
| 132 |
} ] |
| 133 |
}; |
| 134 |
|
| 135 |
views.tags = { |
| 136 |
title: mw.msg( 'rcfilters-view-tags' ), |
| 137 |
trigger: '#', |
| 138 |
groups: [ { |
| 139 |
|
| 140 |
name: 'tagfilter', |
| 141 |
type: 'string_options', |
| 142 |
title: 'rcfilters-view-tags', |
| 143 |
labelPrefixKey: { |
| 144 |
default: 'rcfilters-tag-prefix-tags', |
| 145 |
inverted: 'rcfilters-tag-prefix-tags-inverted' |
| 146 |
}, |
| 147 |
separator: '|', |
| 148 |
supportsAll: false, |
| 149 |
fullCoverage: false, |
| 150 |
filters: tagList |
| 151 |
} ] |
| 152 |
}; |
| 153 |
views.invertTags = { |
| 154 |
groups: [ |
| 155 |
{ |
| 156 |
name: 'invertTagsGroup', |
| 157 |
type: 'boolean', |
| 158 |
hidden: true, |
| 159 |
filters: [ { |
| 160 |
name: 'inverttags', |
| 161 |
default: '0' |
| 162 |
} ] |
| 163 |
} ] |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
views.range = { |
| 168 |
groups: [ |
| 169 |
{ |
| 170 |
name: 'limit', |
| 171 |
type: 'single_option', |
| 172 |
title: '', |
| 173 |
hidden: true, |
| 174 |
allowArbitrary: true, |
| 175 |
|
| 176 |
validate: $.isNumeric, |
| 177 |
range: { |
| 178 |
min: 0, |
| 179 |
max: 1000 |
| 180 |
}, |
| 181 |
sortFunc: function ( a, b ) { |
| 182 |
return Number( a.name ) - Number( b.name ); |
| 183 |
}, |
| 184 |
default: mw.user.options.get( this.limitPreferenceName, displayConfig.limitDefault ), |
| 185 |
sticky: true, |
| 186 |
filters: displayConfig.limitArray.map( ( num ) => controller._createFilterDataFromNumber( num, num ) ) |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: 'days', |
| 190 |
type: 'single_option', |
| 191 |
title: '', |
| 192 |
hidden: true, |
| 193 |
allowArbitrary: true, |
| 194 |
|
| 195 |
validate: $.isNumeric, |
| 196 |
range: { |
| 197 |
min: 0, |
| 198 |
max: displayConfig.maxDays |
| 199 |
}, |
| 200 |
sortFunc: function ( a, b ) { |
| 201 |
return Number( a.name ) - Number( b.name ); |
| 202 |
}, |
| 203 |
numToLabelFunc: function ( i ) { |
| 204 |
return Number( i ) < 1 ? |
| 205 |
( Number( i ) * 24 ).toFixed( 2 ) : |
| 206 |
Number( i ); |
| 207 |
}, |
| 208 |
default: mw.user.options.get( this.daysPreferenceName, displayConfig.daysDefault ), |
| 209 |
sticky: true, |
| 210 |
filters: [ |
| 211 |
|
| 212 |
0.04166, 0.0833, 0.25, 0.5 |
| 213 |
|
| 214 |
].concat( displayConfig.daysArray ) |
| 215 |
.map( ( num ) => controller._createFilterDataFromNumber( |
| 216 |
num, |
| 217 |
|
| 218 |
num < 1 ? Math.round( num * 24 ) : num |
| 219 |
) ) |
| 220 |
} |
| 221 |
] |
| 222 |
}; |
| 223 |
|
| 224 |
views.display = { |
| 225 |
groups: [ |
| 226 |
{ |
| 227 |
name: 'display', |
| 228 |
type: 'boolean', |
| 229 |
title: '', |
| 230 |
hidden: true, |
| 231 |
sticky: true, |
| 232 |
filters: [ |
| 233 |
{ |
| 234 |
name: 'enhanced', |
| 235 |
default: String( mw.user.options.get( 'usenewrc', 0 ) ) |
| 236 |
} |
| 237 |
] |
| 238 |
} |
| 239 |
] |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
for ( const viewName in views ) { |
| 246 |
const viewData = views[ viewName ]; |
| 247 |
viewData.groups.forEach( ( groupData ) => { |
| 248 |
const extraValues = []; |
| 249 |
if ( groupData.allowArbitrary ) { |
| 250 |
|
| 251 |
if ( url.searchParams.get( groupData.name ) !== null ) { |
| 252 |
extraValues.push( url.searchParams.get( groupData.name ) ); |
| 253 |
} |
| 254 |
|
| 255 |
if ( groupData.default !== undefined ) { |
| 256 |
extraValues.push( String( groupData.default ) ); |
| 257 |
} |
| 258 |
controller.addNumberValuesToGroup( groupData, extraValues ); |
| 259 |
} |
| 260 |
} ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
this.filtersModel.initializeFilters( filterStructure, views ); |
| 265 |
|
| 266 |
this.uriProcessor = new UriProcessor( |
| 267 |
this.filtersModel, |
| 268 |
{ normalizeTarget: this.normalizeTarget } |
| 269 |
); |
| 270 |
|
| 271 |
let parsedSavedQueries; |
| 272 |
if ( !mw.user.isAnon() ) { |
| 273 |
try { |
| 274 |
parsedSavedQueries = JSON.parse( mw.user.options.get( this.savedQueriesPreferenceName ) || '{}' ); |
| 275 |
} catch ( err ) { |
| 276 |
parsedSavedQueries = {}; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
this.savedQueriesModel.initialize( parsedSavedQueries ); |
| 281 |
if ( this.savedQueriesModel.isConverted() ) { |
| 282 |
|
| 283 |
|
| 284 |
this._saveSavedQueries(); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
if ( defaultSavedQueryExists ) { |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
this.applySavedQuery( this.savedQueriesModel.getDefault() ); |
| 294 |
} else { |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
this.updateStateFromUrl( false ); |
| 301 |
|
| 302 |
const pieces = this._extractChangesListInfo( $( '#mw-content-text' ) ); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
this.changesListModel.update( |
| 307 |
pieces.changes, |
| 308 |
pieces.fieldset, |
| 309 |
pieces.noResultsDetails, |
| 310 |
true |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
this.initialized = true; |
| 315 |
this.switchView( 'default' ); |
| 316 |
|
| 317 |
if ( this.pollingRate ) { |
| 318 |
this._scheduleLiveUpdate(); |
| 319 |
} |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
Controller.prototype.isInitialized = function () { |
| 328 |
return this.initialized; |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
Controller.prototype._extractChangesListInfo = function ( $root, statusCode ) { |
| 344 |
const $changesListContents = $root.find( '.mw-changeslist' ).first().contents(), |
| 345 |
areResults = !!$changesListContents.length, |
| 346 |
checkForLogout = !areResults && statusCode === 200; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if ( checkForLogout && !!$root.find( '#wpName1' ).length ) { |
| 354 |
location.reload( false ); |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
const info = { |
| 359 |
changes: $changesListContents.length ? $changesListContents : 'NO_RESULTS', |
| 360 |
fieldset: $root.find( 'fieldset.cloptions' ).first() |
| 361 |
}; |
| 362 |
|
| 363 |
if ( !areResults ) { |
| 364 |
if ( $root.find( '.mw-changeslist-timeout' ).length ) { |
| 365 |
info.noResultsDetails = 'NO_RESULTS_TIMEOUT'; |
| 366 |
} else if ( $root.find( '.mw-changeslist-notargetpage' ).length ) { |
| 367 |
info.noResultsDetails = 'NO_RESULTS_NO_TARGET_PAGE'; |
| 368 |
} else if ( $root.find( '.mw-changeslist-invalidtargetpage' ).length ) { |
| 369 |
info.noResultsDetails = 'NO_RESULTS_INVALID_TARGET_PAGE'; |
| 370 |
} else { |
| 371 |
info.noResultsDetails = 'NO_RESULTS_NORMAL'; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
return info; |
| 376 |
}; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
Controller.prototype._createFilterDataFromNumber = function ( num, numForDisplay ) { |
| 386 |
return { |
| 387 |
name: String( num ), |
| 388 |
label: mw.language.convertNumber( numForDisplay ) |
| 389 |
}; |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
Controller.prototype.addNumberValuesToGroup = function ( groupData, arbitraryValues ) { |
| 399 |
const controller = this, |
| 400 |
normalizeWithinRange = function ( range, val ) { |
| 401 |
if ( val < range.min ) { |
| 402 |
return range.min; |
| 403 |
} else if ( val >= range.max ) { |
| 404 |
return range.max; |
| 405 |
} |
| 406 |
return val; |
| 407 |
}; |
| 408 |
|
| 409 |
arbitraryValues = Array.isArray( arbitraryValues ) ? arbitraryValues : [ arbitraryValues ]; |
| 410 |
|
| 411 |
|
| 412 |
if ( groupData.range ) { |
| 413 |
arbitraryValues = arbitraryValues.map( ( val ) => normalizeWithinRange( groupData.range, val ) ); |
| 414 |
|
| 415 |
|
| 416 |
if ( groupData.default !== undefined ) { |
| 417 |
groupData.default = String( normalizeWithinRange( groupData.range, groupData.default ) ); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
arbitraryValues.forEach( ( val ) => { |
| 426 |
if ( |
| 427 |
|
| 428 |
groupData.allowArbitrary && |
| 429 |
|
| 430 |
|
| 431 |
groupData.type === 'single_option' && |
| 432 |
|
| 433 |
|
| 434 |
( !groupData.validate || groupData.validate( val ) ) && |
| 435 |
|
| 436 |
groupData.filters |
| 437 |
.map( ( filterData ) => String( filterData.name ) ) |
| 438 |
.indexOf( String( val ) ) === -1 |
| 439 |
) { |
| 440 |
|
| 441 |
groupData.filters.push( controller._createFilterDataFromNumber( |
| 442 |
val, |
| 443 |
groupData.numToLabelFunc ? |
| 444 |
groupData.numToLabelFunc( val ) : |
| 445 |
val |
| 446 |
) ); |
| 447 |
|
| 448 |
|
| 449 |
if ( groupData.sortFunc ) { |
| 450 |
groupData.filters.sort( groupData.sortFunc ); |
| 451 |
} |
| 452 |
} |
| 453 |
} ); |
| 454 |
}; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
Controller.prototype.resetToDefaults = function () { |
| 460 |
const params = this._getDefaultParams(); |
| 461 |
if ( this.applyParamChange( params ) ) { |
| 462 |
|
| 463 |
this.updateChangesList(); |
| 464 |
} else { |
| 465 |
this.uriProcessor.updateURL( params ); |
| 466 |
} |
| 467 |
}; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
Controller.prototype.areDefaultsEmpty = function () { |
| 475 |
return $.isEmptyObject( this._getDefaultParams() ); |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
Controller.prototype.emptyFilters = function () { |
| 482 |
if ( this.applyParamChange( {} ) ) { |
| 483 |
|
| 484 |
this.updateChangesList(); |
| 485 |
} else { |
| 486 |
this.uriProcessor.updateURL(); |
| 487 |
} |
| 488 |
}; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
Controller.prototype.toggleFilterSelect = function ( filterName, isSelected ) { |
| 497 |
const filterItem = this.filtersModel.getItemByName( filterName ); |
| 498 |
|
| 499 |
if ( !filterItem ) { |
| 500 |
|
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
isSelected = isSelected === undefined ? !filterItem.isSelected() : isSelected; |
| 505 |
|
| 506 |
if ( filterItem.isSelected() !== isSelected ) { |
| 507 |
this.filtersModel.toggleFilterSelected( filterName, isSelected ); |
| 508 |
|
| 509 |
this.updateChangesList(); |
| 510 |
|
| 511 |
|
| 512 |
this.filtersModel.reassessFilterInteractions( filterItem ); |
| 513 |
} |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
Controller.prototype.clearFilter = function ( filterName ) { |
| 522 |
const filterItem = this.filtersModel.getItemByName( filterName ), |
| 523 |
isHighlighted = filterItem.isHighlighted(), |
| 524 |
isSelected = filterItem.isSelected(); |
| 525 |
|
| 526 |
if ( isSelected || isHighlighted ) { |
| 527 |
this.filtersModel.clearHighlightColor( filterName ); |
| 528 |
this.filtersModel.toggleFilterSelected( filterName, false ); |
| 529 |
|
| 530 |
if ( isSelected ) { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
this.updateChangesList(); |
| 535 |
} |
| 536 |
|
| 537 |
this.filtersModel.reassessFilterInteractions( filterItem ); |
| 538 |
} |
| 539 |
}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
Controller.prototype.toggleHighlight = function () { |
| 545 |
this.filtersModel.toggleHighlight(); |
| 546 |
this.uriProcessor.updateURL(); |
| 547 |
|
| 548 |
if ( this.filtersModel.isHighlightEnabled() ) { |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
mw.hook( 'RcFilters.highlight.enable' ).fire(); |
| 556 |
} |
| 557 |
}; |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
Controller.prototype.toggleInvertedTags = function () { |
| 563 |
this.filtersModel.toggleInvertedTags(); |
| 564 |
|
| 565 |
if ( |
| 566 |
this.filtersModel.getFiltersByView( 'tags' ).filter( |
| 567 |
( filterItem ) => filterItem.isSelected() |
| 568 |
).length |
| 569 |
) { |
| 570 |
|
| 571 |
this.updateChangesList(); |
| 572 |
} else { |
| 573 |
this.uriProcessor.updateURL(); |
| 574 |
} |
| 575 |
}; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
Controller.prototype.toggleInvertedNamespaces = function () { |
| 581 |
this.filtersModel.toggleInvertedNamespaces(); |
| 582 |
|
| 583 |
if ( |
| 584 |
this.filtersModel.getFiltersByView( 'namespaces' ).filter( |
| 585 |
( filterItem ) => filterItem.isSelected() |
| 586 |
).length |
| 587 |
) { |
| 588 |
|
| 589 |
this.updateChangesList(); |
| 590 |
} else { |
| 591 |
this.uriProcessor.updateURL(); |
| 592 |
} |
| 593 |
}; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
Controller.prototype.setShowLinkedTo = function ( value ) { |
| 601 |
const targetItem = this.filtersModel.getGroup( 'page' ).getItemByParamName( 'target' ), |
| 602 |
showLinkedToItem = this.filtersModel.getGroup( 'toOrFrom' ).getItemByParamName( 'showlinkedto' ); |
| 603 |
|
| 604 |
this.filtersModel.toggleFilterSelected( showLinkedToItem.getName(), value ); |
| 605 |
this.uriProcessor.updateURL(); |
| 606 |
|
| 607 |
if ( targetItem.getValue() ) { |
| 608 |
this.updateChangesList(); |
| 609 |
} |
| 610 |
}; |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
Controller.prototype.setTargetPage = function ( page ) { |
| 618 |
const targetItem = this.filtersModel.getGroup( 'page' ).getItemByParamName( 'target' ); |
| 619 |
targetItem.setValue( page ); |
| 620 |
this.uriProcessor.updateURL(); |
| 621 |
this.updateChangesList(); |
| 622 |
}; |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
Controller.prototype.setHighlightColor = function ( filterName, color ) { |
| 631 |
this.filtersModel.setHighlightColor( filterName, color ); |
| 632 |
this.uriProcessor.updateURL(); |
| 633 |
}; |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
Controller.prototype.clearHighlightColor = function ( filterName ) { |
| 641 |
this.filtersModel.clearHighlightColor( filterName ); |
| 642 |
this.uriProcessor.updateURL(); |
| 643 |
}; |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
Controller.prototype.toggleLiveUpdate = function ( enable ) { |
| 651 |
this.changesListModel.toggleLiveUpdate( enable ); |
| 652 |
if ( this.changesListModel.getLiveUpdate() && this.changesListModel.getNewChangesExist() ) { |
| 653 |
this.updateChangesList( null, this.LIVE_UPDATE ); |
| 654 |
} |
| 655 |
}; |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
Controller.prototype._scheduleLiveUpdate = function () { |
| 663 |
setTimeout( this._doLiveUpdate.bind( this ), this.pollingRate * 1000 ); |
| 664 |
}; |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
Controller.prototype._doLiveUpdate = function () { |
| 672 |
if ( !this._shouldCheckForNewChanges() ) { |
| 673 |
|
| 674 |
this._scheduleLiveUpdate(); |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
this._checkForNewChanges() |
| 679 |
.then( ( statusCode ) => { |
| 680 |
|
| 681 |
|
| 682 |
const newChanges = statusCode === 200; |
| 683 |
|
| 684 |
if ( !this._shouldCheckForNewChanges() ) { |
| 685 |
|
| 686 |
|
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
if ( statusCode === 205 ) { |
| 695 |
location.reload( false ); |
| 696 |
return; |
| 697 |
} |
| 698 |
|
| 699 |
if ( newChanges ) { |
| 700 |
if ( this.changesListModel.getLiveUpdate() ) { |
| 701 |
return this.updateChangesList( null, this.LIVE_UPDATE ); |
| 702 |
} else { |
| 703 |
this.changesListModel.setNewChangesExist( true ); |
| 704 |
} |
| 705 |
} |
| 706 |
} ) |
| 707 |
.always( this._scheduleLiveUpdate.bind( this ) ); |
| 708 |
}; |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
Controller.prototype._shouldCheckForNewChanges = function () { |
| 715 |
return !document.hidden && |
| 716 |
!this.filtersModel.hasConflict() && |
| 717 |
!this.changesListModel.getNewChangesExist() && |
| 718 |
!this.updatingChangesList && |
| 719 |
this.changesListModel.getNextFrom(); |
| 720 |
}; |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
Controller.prototype._checkForNewChanges = function () { |
| 731 |
const params = { |
| 732 |
limit: 1, |
| 733 |
peek: 1, |
| 734 |
from: this.changesListModel.getNextFrom(), |
| 735 |
isAnon: mw.user.isAnon() |
| 736 |
}; |
| 737 |
return this._queryChangesList( 'liveUpdate', params ).then( |
| 738 |
( data ) => data.status |
| 739 |
); |
| 740 |
}; |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
Controller.prototype.showNewChanges = function () { |
| 749 |
return this.updateChangesList( null, this.SHOW_NEW_CHANGES ); |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
Controller.prototype.saveCurrentQuery = function ( label, setAsDefault ) { |
| 759 |
|
| 760 |
this.savedQueriesModel.addNewQuery( |
| 761 |
label || mw.msg( 'rcfilters-savedqueries-defaultlabel' ), |
| 762 |
this.filtersModel.getCurrentParameterState( true ), |
| 763 |
setAsDefault |
| 764 |
); |
| 765 |
|
| 766 |
|
| 767 |
this._saveSavedQueries(); |
| 768 |
}; |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
Controller.prototype.removeSavedQuery = function ( queryID ) { |
| 776 |
this.savedQueriesModel.removeQuery( queryID ); |
| 777 |
|
| 778 |
this._saveSavedQueries(); |
| 779 |
}; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
Controller.prototype.renameSavedQuery = function ( queryID, newLabel ) { |
| 788 |
const queryItem = this.savedQueriesModel.getItemByID( queryID ); |
| 789 |
|
| 790 |
if ( queryItem ) { |
| 791 |
queryItem.updateLabel( newLabel ); |
| 792 |
} |
| 793 |
this._saveSavedQueries(); |
| 794 |
}; |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
Controller.prototype.setDefaultSavedQuery = function ( queryID ) { |
| 803 |
this.savedQueriesModel.setDefault( queryID ); |
| 804 |
this._saveSavedQueries(); |
| 805 |
}; |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
Controller.prototype.applySavedQuery = function ( queryID ) { |
| 813 |
const params = this.savedQueriesModel.getItemParams( queryID ); |
| 814 |
|
| 815 |
const currentMatchingQuery = this.findQueryMatchingCurrentState(); |
| 816 |
|
| 817 |
if ( |
| 818 |
currentMatchingQuery && |
| 819 |
currentMatchingQuery.getID() === queryID |
| 820 |
) { |
| 821 |
|
| 822 |
|
| 823 |
return; |
| 824 |
} |
| 825 |
|
| 826 |
if ( this.applyParamChange( params ) ) { |
| 827 |
|
| 828 |
this.updateChangesList(); |
| 829 |
} else { |
| 830 |
this.uriProcessor.updateURL( params ); |
| 831 |
} |
| 832 |
}; |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
Controller.prototype.findQueryMatchingCurrentState = function () { |
| 842 |
return this.savedQueriesModel.findMatchingQuery( |
| 843 |
this.filtersModel.getCurrentParameterState( true ) |
| 844 |
); |
| 845 |
}; |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
Controller.prototype._saveSavedQueries = function () { |
| 852 |
const backupPrefName = this.savedQueriesPreferenceName + '-versionbackup', |
| 853 |
state = this.savedQueriesModel.getState(); |
| 854 |
|
| 855 |
|
| 856 |
const stringified = JSON.stringify( state ); |
| 857 |
|
| 858 |
if ( byteLength( stringified ) > 65535 ) { |
| 859 |
|
| 860 |
return; |
| 861 |
} |
| 862 |
|
| 863 |
if ( !this.wereSavedQueriesSaved && this.savedQueriesModel.isConverted() ) { |
| 864 |
|
| 865 |
|
| 866 |
const oldPrefValue = mw.user.options.get( this.savedQueriesPreferenceName ); |
| 867 |
|
| 868 |
|
| 869 |
new mw.Api().saveOption( backupPrefName, oldPrefValue ); |
| 870 |
|
| 871 |
mw.user.options.set( backupPrefName, oldPrefValue ); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
new mw.Api().saveOption( this.savedQueriesPreferenceName, stringified ); |
| 876 |
|
| 877 |
mw.user.options.set( this.savedQueriesPreferenceName, stringified ); |
| 878 |
|
| 879 |
|
| 880 |
this.wereSavedQueriesSaved = true; |
| 881 |
}; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
Controller.prototype.updateStickyPreferences = function () { |
| 887 |
|
| 888 |
|
| 889 |
this.updateDaysDefault( this.filtersModel.getGroup( 'days' ).findSelectedItems()[ 0 ].getParamName() ); |
| 890 |
this.updateLimitDefault( this.filtersModel.getGroup( 'limit' ).findSelectedItems()[ 0 ].getParamName() ); |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
}; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
Controller.prototype.updateLimitDefault = function ( newValue ) { |
| 902 |
this.updateNumericPreference( this.limitPreferenceName, newValue ); |
| 903 |
}; |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
Controller.prototype.updateDaysDefault = function ( newValue ) { |
| 911 |
this.updateNumericPreference( this.daysPreferenceName, newValue ); |
| 912 |
}; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
Controller.prototype.updateGroupByPageDefault = function ( newValue ) { |
| 920 |
this.updateNumericPreference( 'usenewrc', Number( newValue ) ); |
| 921 |
}; |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
Controller.prototype.updateCollapsedState = function ( isCollapsed ) { |
| 929 |
this.updateNumericPreference( this.collapsedPreferenceName, Number( isCollapsed ) ); |
| 930 |
}; |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
Controller.prototype.updateNumericPreference = function ( prefName, newValue ) { |
| 939 |
|
| 940 |
|
| 941 |
if ( !$.isNumeric( newValue ) ) { |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
if ( String( mw.user.options.get( prefName ) ) !== String( newValue ) ) { |
| 946 |
|
| 947 |
new mw.Api().saveOption( prefName, newValue ); |
| 948 |
|
| 949 |
mw.user.options.set( prefName, newValue ); |
| 950 |
} |
| 951 |
}; |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
Controller.prototype.replaceUrl = function () { |
| 958 |
this.uriProcessor.updateURL(); |
| 959 |
}; |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
Controller.prototype.updateStateFromUrl = function ( fetchChangesList ) { |
| 969 |
fetchChangesList = fetchChangesList === undefined ? true : !!fetchChangesList; |
| 970 |
|
| 971 |
this.uriProcessor.updateModelBasedOnQuery(); |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
this.updateStickyPreferences(); |
| 976 |
|
| 977 |
|
| 978 |
if ( fetchChangesList ) { |
| 979 |
this.updateChangesList(); |
| 980 |
} |
| 981 |
}; |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
Controller.prototype.updateChangesList = function ( params, updateMode ) { |
| 991 |
updateMode = updateMode === undefined ? this.FILTER_CHANGE : updateMode; |
| 992 |
|
| 993 |
if ( updateMode === this.FILTER_CHANGE ) { |
| 994 |
this.uriProcessor.updateURL( params ); |
| 995 |
} |
| 996 |
if ( updateMode === this.FILTER_CHANGE || updateMode === this.SHOW_NEW_CHANGES ) { |
| 997 |
this.changesListModel.invalidate(); |
| 998 |
} |
| 999 |
this.changesListModel.setNewChangesExist( false ); |
| 1000 |
this.updatingChangesList = true; |
| 1001 |
return this._fetchChangesList() |
| 1002 |
.then( |
| 1003 |
|
| 1004 |
( pieces ) => { |
| 1005 |
const $changesListContent = pieces.changes, |
| 1006 |
$fieldset = pieces.fieldset; |
| 1007 |
this.changesListModel.update( |
| 1008 |
$changesListContent, |
| 1009 |
$fieldset, |
| 1010 |
pieces.noResultsDetails, |
| 1011 |
false, |
| 1012 |
|
| 1013 |
updateMode === this.SHOW_NEW_CHANGES || updateMode === this.LIVE_UPDATE |
| 1014 |
); |
| 1015 |
} |
| 1016 |
|
| 1017 |
) |
| 1018 |
.always( () => { |
| 1019 |
this.updatingChangesList = false; |
| 1020 |
} ); |
| 1021 |
}; |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
Controller.prototype._getDefaultParams = function () { |
| 1030 |
if ( this.savedQueriesModel.getDefault() ) { |
| 1031 |
return this.savedQueriesModel.getDefaultParams(); |
| 1032 |
} else { |
| 1033 |
return this.filtersModel.getDefaultParams(); |
| 1034 |
} |
| 1035 |
}; |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
Controller.prototype._queryChangesList = function ( counterId, params ) { |
| 1047 |
const uri = this.uriProcessor.getUpdatedUri(), |
| 1048 |
stickyParams = this.filtersModel.getStickyParamsValues(); |
| 1049 |
|
| 1050 |
params = params || {}; |
| 1051 |
params.action = 'render'; |
| 1052 |
|
| 1053 |
uri.extend( params ); |
| 1054 |
|
| 1055 |
this.requestCounter[ counterId ] = this.requestCounter[ counterId ] || 0; |
| 1056 |
const requestId = ++this.requestCounter[ counterId ]; |
| 1057 |
const latestRequest = function () { |
| 1058 |
return requestId === this.requestCounter[ counterId ]; |
| 1059 |
}.bind( this ); |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
uri.extend( stickyParams ); |
| 1068 |
|
| 1069 |
return $.ajax( uri.toString() ) |
| 1070 |
.then( |
| 1071 |
( content, message, jqXHR ) => { |
| 1072 |
if ( !latestRequest() ) { |
| 1073 |
return $.Deferred().reject(); |
| 1074 |
} |
| 1075 |
return { |
| 1076 |
content: content, |
| 1077 |
status: jqXHR.status |
| 1078 |
}; |
| 1079 |
}, |
| 1080 |
|
| 1081 |
( jqXHR ) => { |
| 1082 |
if ( latestRequest() ) { |
| 1083 |
return $.Deferred().resolve( |
| 1084 |
{ |
| 1085 |
content: jqXHR.responseText, |
| 1086 |
status: jqXHR.status |
| 1087 |
} |
| 1088 |
).promise(); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
); |
| 1092 |
}; |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
Controller.prototype._fetchChangesList = function () { |
| 1101 |
return this._queryChangesList( 'updateChangesList' ) |
| 1102 |
.then( |
| 1103 |
( data ) => { |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
if ( data && data.status === 0 ) { |
| 1109 |
return { |
| 1110 |
changes: 'NO_RESULTS', |
| 1111 |
|
| 1112 |
fieldset: $( [] ), |
| 1113 |
noResultsDetails: 'NO_RESULTS_NETWORK_ERROR' |
| 1114 |
}; |
| 1115 |
} |
| 1116 |
|
| 1117 |
const $parsed = $( '<div>' ).append( $( $.parseHTML( |
| 1118 |
data ? data.content : '' |
| 1119 |
) ) ); |
| 1120 |
|
| 1121 |
return this._extractChangesListInfo( $parsed, data.status ); |
| 1122 |
} |
| 1123 |
); |
| 1124 |
}; |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
Controller.prototype.applyParamChange = function ( newParamState ) { |
| 1134 |
const before = this.filtersModel.getSelectedState(); |
| 1135 |
|
| 1136 |
this.filtersModel.updateStateFromParams( newParamState ); |
| 1137 |
|
| 1138 |
const after = this.filtersModel.getSelectedState(); |
| 1139 |
|
| 1140 |
return !OO.compare( before, after ); |
| 1141 |
}; |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
Controller.prototype.markAllChangesAsSeen = function () { |
| 1147 |
const api = new mw.Api(); |
| 1148 |
api.postWithToken( 'csrf', { |
| 1149 |
formatversion: 2, |
| 1150 |
action: 'setnotificationtimestamp', |
| 1151 |
entirewatchlist: true |
| 1152 |
} ).then( () => { |
| 1153 |
this.updateChangesList( null, 'markSeen' ); |
| 1154 |
} ); |
| 1155 |
}; |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
Controller.prototype.setSearch = function ( searchQuery ) { |
| 1163 |
this.filtersModel.setSearch( searchQuery ); |
| 1164 |
}; |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
Controller.prototype.switchView = function ( view ) { |
| 1173 |
this.setSearch( |
| 1174 |
this.filtersModel.getViewTrigger( view ) + |
| 1175 |
this.filtersModel.removeViewTriggers( this.filtersModel.getSearch() ) |
| 1176 |
); |
| 1177 |
}; |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
Controller.prototype.resetSearchForView = function ( view ) { |
| 1186 |
view = view || 'default'; |
| 1187 |
|
| 1188 |
this.setSearch( |
| 1189 |
this.filtersModel.getViewTrigger( view ) |
| 1190 |
); |
| 1191 |
}; |
| 1192 |
|
| 1193 |
module.exports = Controller; |