| 1 |
const SavedQueryItemModel = require( './SavedQueryItemModel.js' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
const SavedQueriesModel = function MwRcfiltersDmSavedQueriesModel( filtersModel, config ) { |
| 16 |
config = config || {}; |
| 17 |
|
| 18 |
|
| 19 |
OO.EventEmitter.call( this ); |
| 20 |
OO.EmitterList.call( this ); |
| 21 |
|
| 22 |
this.default = config.default; |
| 23 |
this.filtersModel = filtersModel; |
| 24 |
this.converted = false; |
| 25 |
|
| 26 |
|
| 27 |
this.aggregate( { update: 'itemUpdate' } ); |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
OO.initClass( SavedQueriesModel ); |
| 33 |
OO.mixinClass( SavedQueriesModel, OO.EventEmitter ); |
| 34 |
OO.mixinClass( SavedQueriesModel, OO.EmitterList ); |
| 35 |
|
| 36 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
SavedQueriesModel.prototype.initialize = function ( savedQueries ) { |
| 87 |
const model = this; |
| 88 |
|
| 89 |
savedQueries = savedQueries || {}; |
| 90 |
|
| 91 |
this.clearItems(); |
| 92 |
this.default = null; |
| 93 |
this.converted = false; |
| 94 |
|
| 95 |
if ( savedQueries.version !== '2' ) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$.each( savedQueries.queries || {}, ( id, obj ) => { |
| 112 |
if ( obj.data && obj.data.filters ) { |
| 113 |
obj.data = model.convertToParameters( obj.data ); |
| 114 |
} |
| 115 |
} ); |
| 116 |
|
| 117 |
this.converted = true; |
| 118 |
savedQueries.version = '2'; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
$.each( savedQueries.queries || {}, ( id, obj ) => { |
| 124 |
const normalizedData = obj.data, |
| 125 |
isDefault = String( savedQueries.default ) === String( id ); |
| 126 |
|
| 127 |
if ( normalizedData && normalizedData.params ) { |
| 128 |
|
| 129 |
|
| 130 |
normalizedData.params = model.filtersModel.removeStickyParams( normalizedData.params ); |
| 131 |
|
| 132 |
|
| 133 |
if ( normalizedData.params.invert && !normalizedData.params.namespace ) { |
| 134 |
delete normalizedData.params.invert; |
| 135 |
} |
| 136 |
|
| 137 |
model.cleanupHighlights( normalizedData ); |
| 138 |
|
| 139 |
id = String( id ); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
model.addItems( [ |
| 148 |
new SavedQueryItemModel( |
| 149 |
id, |
| 150 |
obj.label, |
| 151 |
normalizedData, |
| 152 |
{ default: isDefault } |
| 153 |
) |
| 154 |
] ); |
| 155 |
|
| 156 |
if ( isDefault ) { |
| 157 |
model.default = id; |
| 158 |
} |
| 159 |
} |
| 160 |
} ); |
| 161 |
|
| 162 |
this.emit( 'initialize' ); |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
SavedQueriesModel.prototype.cleanupHighlights = function ( data ) { |
| 173 |
if ( |
| 174 |
data.params.highlight === '0' && |
| 175 |
data.highlights && Object.keys( data.highlights ).length |
| 176 |
) { |
| 177 |
data.highlights = {}; |
| 178 |
} |
| 179 |
delete data.params.highlight; |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
SavedQueriesModel.prototype.convertToParameters = function ( data ) { |
| 189 |
const newData = {}, |
| 190 |
defaultFilters = this.filtersModel.getFiltersFromParameters( this.filtersModel.getDefaultParams() ), |
| 191 |
fullFilterRepresentation = $.extend( true, {}, defaultFilters, data.filters ), |
| 192 |
highlightEnabled = data.highlights.highlight; |
| 193 |
|
| 194 |
delete data.highlights.highlight; |
| 195 |
|
| 196 |
|
| 197 |
newData.params = this.filtersModel.getMinimizedParamRepresentation( |
| 198 |
this.filtersModel.getParametersFromFilters( fullFilterRepresentation ) |
| 199 |
); |
| 200 |
|
| 201 |
|
| 202 |
newData.highlights = {}; |
| 203 |
|
| 204 |
$.each( data.highlights, ( highlightedFilterName, value ) => { |
| 205 |
if ( value ) { |
| 206 |
newData.highlights[ highlightedFilterName + '_color' ] = data.highlights[ highlightedFilterName ]; |
| 207 |
} |
| 208 |
} ); |
| 209 |
|
| 210 |
|
| 211 |
newData.params.highlight = String( Number( highlightEnabled || 0 ) ); |
| 212 |
|
| 213 |
return newData; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
SavedQueriesModel.prototype.addNewQuery = function ( label, fulldata, isDefault, id ) { |
| 227 |
const normalizedData = { params: {}, highlights: {} }, |
| 228 |
highlightParamNames = Object.keys( this.filtersModel.getEmptyHighlightParameters() ), |
| 229 |
randomID = String( id || Date.now() ), |
| 230 |
data = this.filtersModel.getMinimizedParamRepresentation( fulldata ); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
$.each( data, ( param, value ) => { |
| 235 |
if ( param !== 'highlight' && highlightParamNames.indexOf( param ) > -1 ) { |
| 236 |
normalizedData.highlights[ param ] = value; |
| 237 |
} else { |
| 238 |
normalizedData.params[ param ] = value; |
| 239 |
} |
| 240 |
} ); |
| 241 |
|
| 242 |
|
| 243 |
if ( normalizedData.params.invert && !this.filtersModel.areNamespacesEffectivelyInverted() ) { |
| 244 |
delete normalizedData.params.invert; |
| 245 |
} |
| 246 |
|
| 247 |
if ( normalizedData.params.inverttags && !this.filtersModel.areTagsEffectivelyInverted() ) { |
| 248 |
delete normalizedData.params.inverttags; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
this.addItems( [ |
| 253 |
new SavedQueryItemModel( |
| 254 |
randomID, |
| 255 |
label, |
| 256 |
normalizedData, |
| 257 |
{ default: isDefault } |
| 258 |
) |
| 259 |
] ); |
| 260 |
|
| 261 |
if ( isDefault ) { |
| 262 |
this.setDefault( randomID ); |
| 263 |
} |
| 264 |
|
| 265 |
return randomID; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
SavedQueriesModel.prototype.removeQuery = function ( queryID ) { |
| 274 |
const query = this.getItemByID( queryID ); |
| 275 |
|
| 276 |
if ( query ) { |
| 277 |
|
| 278 |
if ( String( this.getDefault() ) === String( queryID ) ) { |
| 279 |
|
| 280 |
this.setDefault( null ); |
| 281 |
} |
| 282 |
|
| 283 |
this.removeItems( [ query ] ); |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
SavedQueriesModel.prototype.findMatchingQuery = function ( fullQueryComparison ) { |
| 295 |
|
| 296 |
fullQueryComparison = this.filtersModel.getMinimizedParamRepresentation( fullQueryComparison ); |
| 297 |
|
| 298 |
|
| 299 |
if ( fullQueryComparison.invert && !this.filtersModel.areNamespacesEffectivelyInverted() ) { |
| 300 |
delete fullQueryComparison.invert; |
| 301 |
} |
| 302 |
|
| 303 |
return this.getItems().filter( ( item ) => OO.compare( |
| 304 |
item.getCombinedData(), |
| 305 |
fullQueryComparison |
| 306 |
) )[ 0 ]; |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
SavedQueriesModel.prototype.getItemByID = function ( queryID ) { |
| 318 |
return this.getItems().filter( ( item ) => item.getID() === queryID )[ 0 ]; |
| 319 |
}; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
SavedQueriesModel.prototype.getDefaultParams = function () { |
| 328 |
return ( !mw.user.isAnon() && this.getItemParams( this.getDefault() ) ) || {}; |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
SavedQueriesModel.prototype.getItemParams = function ( queryID ) { |
| 338 |
const item = this.getItemByID( queryID ), |
| 339 |
data = item ? item.getData() : {}; |
| 340 |
|
| 341 |
return !$.isEmptyObject( data ) ? this.buildParamsFromData( data ) : {}; |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
SavedQueriesModel.prototype.buildParamsFromData = function ( data ) { |
| 351 |
data = data || {}; |
| 352 |
|
| 353 |
return this.filtersModel.getMinimizedParamRepresentation( $.extend( true, {}, |
| 354 |
data.params, |
| 355 |
data.highlights |
| 356 |
) ); |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
SavedQueriesModel.prototype.getState = function () { |
| 365 |
const obj = { queries: {}, version: '2' }; |
| 366 |
|
| 367 |
|
| 368 |
this.getItems().forEach( ( item ) => { |
| 369 |
obj.queries[ item.getID() ] = item.getState(); |
| 370 |
} ); |
| 371 |
|
| 372 |
if ( this.getDefault() ) { |
| 373 |
obj.default = this.getDefault(); |
| 374 |
} |
| 375 |
|
| 376 |
return obj; |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
SavedQueriesModel.prototype.setDefault = function ( itemID ) { |
| 386 |
if ( this.default !== itemID ) { |
| 387 |
this.default = itemID; |
| 388 |
|
| 389 |
|
| 390 |
this.getItems().forEach( ( item ) => { |
| 391 |
item.toggleDefault( item.getID() === itemID ); |
| 392 |
} ); |
| 393 |
|
| 394 |
this.emit( 'default', itemID ); |
| 395 |
} |
| 396 |
}; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
SavedQueriesModel.prototype.getDefault = function () { |
| 404 |
return this.default; |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
SavedQueriesModel.prototype.isConverted = function () { |
| 414 |
return this.converted; |
| 415 |
}; |
| 416 |
|
| 417 |
module.exports = SavedQueriesModel; |