| 1 |
const ItemModel = require( './ItemModel.js' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const FilterItem = function MwRcfiltersDmFilterItem( param, groupModel, config ) { |
| 20 |
config = config || {}; |
| 21 |
|
| 22 |
this.groupModel = groupModel; |
| 23 |
|
| 24 |
|
| 25 |
FilterItem.super.call( this, param, Object.assign( { |
| 26 |
namePrefix: this.groupModel.getNamePrefix() |
| 27 |
}, config ) ); |
| 28 |
|
| 29 |
OO.EventEmitter.call( this ); |
| 30 |
|
| 31 |
|
| 32 |
this.subset = config.subset || []; |
| 33 |
this.conflicts = config.conflicts || {}; |
| 34 |
this.superset = []; |
| 35 |
this.visible = config.visible === undefined ? true : !!config.visible; |
| 36 |
|
| 37 |
|
| 38 |
this.included = false; |
| 39 |
this.conflicted = false; |
| 40 |
this.fullyCovered = false; |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
OO.inheritClass( FilterItem, ItemModel ); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
FilterItem.prototype.getState = function () { |
| 55 |
return { |
| 56 |
selected: this.isSelected(), |
| 57 |
included: this.isIncluded(), |
| 58 |
conflicted: this.isConflicted(), |
| 59 |
fullyCovered: this.isFullyCovered() |
| 60 |
}; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
FilterItem.prototype.getCurrentConflictResultMessage = function () { |
| 69 |
let details; |
| 70 |
|
| 71 |
|
| 72 |
details = this.getConflictDetails( this.getOwnConflicts(), 'globalDescription' ); |
| 73 |
if ( !details.message ) { |
| 74 |
|
| 75 |
details = this.getConflictDetails( this.getGroupModel().getConflicts(), 'globalDescription' ); |
| 76 |
} |
| 77 |
|
| 78 |
return details.message; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
FilterItem.prototype.getConflictDetails = function ( conflicts, key ) { |
| 92 |
let group, |
| 93 |
conflictMessage = ''; |
| 94 |
const itemLabels = []; |
| 95 |
|
| 96 |
key = key || 'contextDescription'; |
| 97 |
|
| 98 |
|
| 99 |
$.each( conflicts, ( filterName, conflict ) => { |
| 100 |
if ( !conflict.item.isSelected() ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if ( !conflictMessage ) { |
| 105 |
conflictMessage = conflict[ key ]; |
| 106 |
group = conflict.group; |
| 107 |
} |
| 108 |
|
| 109 |
if ( group === conflict.group ) { |
| 110 |
itemLabels.push( mw.msg( 'quotation-marks', conflict.item.getLabel() ) ); |
| 111 |
} |
| 112 |
} ); |
| 113 |
|
| 114 |
return { |
| 115 |
message: conflictMessage, |
| 116 |
names: itemLabels |
| 117 |
}; |
| 118 |
|
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
FilterItem.prototype.getStateMessage = function () { |
| 125 |
let messageKey, details, superset, |
| 126 |
affectingItems = []; |
| 127 |
|
| 128 |
if ( this.isSelected() ) { |
| 129 |
if ( this.isConflicted() ) { |
| 130 |
|
| 131 |
details = this.getConflictDetails( this.getOwnConflicts() ); |
| 132 |
if ( !details.message ) { |
| 133 |
|
| 134 |
details = this.getConflictDetails( this.getGroupModel().getConflicts() ); |
| 135 |
} |
| 136 |
|
| 137 |
messageKey = details.message; |
| 138 |
affectingItems = details.names; |
| 139 |
} else if ( this.isIncluded() && !this.isHighlighted() ) { |
| 140 |
|
| 141 |
|
| 142 |
superset = this.getSuperset(); |
| 143 |
|
| 144 |
affectingItems = this.getGroupModel().findSelectedItems( this ) |
| 145 |
.filter( ( item ) => superset.indexOf( item.getName() ) !== -1 ) |
| 146 |
.map( ( item ) => mw.msg( 'quotation-marks', item.getLabel() ) ); |
| 147 |
|
| 148 |
messageKey = 'rcfilters-state-message-subset'; |
| 149 |
} else if ( this.isFullyCovered() && !this.isHighlighted() ) { |
| 150 |
affectingItems = this.getGroupModel().findSelectedItems( this ) |
| 151 |
.map( ( item ) => mw.msg( 'quotation-marks', item.getLabel() ) ); |
| 152 |
|
| 153 |
messageKey = 'rcfilters-state-message-fullcoverage'; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( messageKey ) { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
return mw.msg( |
| 164 |
messageKey, |
| 165 |
mw.language.listToText( affectingItems ), |
| 166 |
affectingItems.length |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
return this.getDescription(); |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
FilterItem.prototype.getGroupModel = function () { |
| 181 |
return this.groupModel; |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
FilterItem.prototype.getGroupName = function () { |
| 190 |
return this.groupModel.getName(); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
FilterItem.prototype.getSubset = function () { |
| 201 |
return this.subset; |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
FilterItem.prototype.getSuperset = function () { |
| 212 |
return this.superset; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
FilterItem.prototype.isConflicted = function () { |
| 221 |
return this.conflicted; |
| 222 |
}; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
FilterItem.prototype.isIncluded = function () { |
| 230 |
return this.included; |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
FilterItem.prototype.isFullyCovered = function () { |
| 239 |
return this.fullyCovered; |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
FilterItem.prototype.getConflicts = function () { |
| 266 |
return Object.assign( {}, this.conflicts, this.getGroupModel().getConflicts() ); |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
FilterItem.prototype.getOwnConflicts = function () { |
| 275 |
return this.conflicts; |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
FilterItem.prototype.setConflicts = function ( conflicts ) { |
| 285 |
this.conflicts = conflicts || {}; |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
FilterItem.prototype.setSuperset = function ( superset ) { |
| 294 |
this.superset = superset || []; |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
FilterItem.prototype.setSubset = function ( subset ) { |
| 303 |
this.subset = subset || []; |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
FilterItem.prototype.existsInSubset = function ( filterName ) { |
| 313 |
return this.subset.indexOf( filterName ) > -1; |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
FilterItem.prototype.existsInConflicts = function ( filterItem ) { |
| 327 |
return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() ); |
| 328 |
}; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
FilterItem.prototype.toggleConflicted = function ( conflicted ) { |
| 338 |
conflicted = conflicted === undefined ? !this.conflicted : conflicted; |
| 339 |
|
| 340 |
if ( this.conflicted !== conflicted ) { |
| 341 |
this.conflicted = conflicted; |
| 342 |
this.emit( 'update' ); |
| 343 |
} |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
FilterItem.prototype.toggleIncluded = function ( included ) { |
| 354 |
included = included === undefined ? !this.included : included; |
| 355 |
|
| 356 |
if ( this.included !== included ) { |
| 357 |
this.included = included; |
| 358 |
this.emit( 'update' ); |
| 359 |
} |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
FilterItem.prototype.toggleFullyCovered = function ( isFullyCovered ) { |
| 369 |
isFullyCovered = isFullyCovered === undefined ? !this.fullycovered : isFullyCovered; |
| 370 |
|
| 371 |
if ( this.fullyCovered !== isFullyCovered ) { |
| 372 |
this.fullyCovered = isFullyCovered; |
| 373 |
this.emit( 'update' ); |
| 374 |
} |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
FilterItem.prototype.toggleVisible = function ( isVisible ) { |
| 383 |
isVisible = isVisible === undefined ? !this.visible : !!isVisible; |
| 384 |
|
| 385 |
if ( this.visible !== isVisible ) { |
| 386 |
this.visible = isVisible; |
| 387 |
this.emit( 'update' ); |
| 388 |
} |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
FilterItem.prototype.isVisible = function () { |
| 397 |
return this.visible; |
| 398 |
}; |
| 399 |
|
| 400 |
module.exports = FilterItem; |