| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
mw.echo.dm.SourcePagesModel = function MwEchoDmSourcePagesModel( config ) { |
| 14 |
config = config || {}; |
| 15 |
|
| 16 |
|
| 17 |
OO.EventEmitter.call( this ); |
| 18 |
|
| 19 |
this.sources = {}; |
| 20 |
|
| 21 |
this.currentSource = config.currentSource || 'local'; |
| 22 |
this.currentPage = null; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
OO.initClass( mw.echo.dm.SourcePagesModel ); |
| 27 |
OO.mixinClass( mw.echo.dm.SourcePagesModel, OO.EventEmitter ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
mw.echo.dm.SourcePagesModel.prototype.setCurrentSourcePage = function ( source, page ) { |
| 47 |
if ( |
| 48 |
this.currentSource !== source || |
| 49 |
this.currentPage !== page |
| 50 |
) { |
| 51 |
this.currentSource = source; |
| 52 |
this.currentPage = page; |
| 53 |
this.emit( 'update' ); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
mw.echo.dm.SourcePagesModel.prototype.getCurrentSource = function () { |
| 63 |
return this.currentSource; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () { |
| 72 |
return this.currentPage; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
mw.echo.dm.SourcePagesModel.prototype.setAllSources = function ( sourceData ) { |
| 83 |
this.reset(); |
| 84 |
for ( const source in sourceData ) { |
| 85 |
if ( Object.prototype.hasOwnProperty.call( sourceData, source ) ) { |
| 86 |
this.setSourcePagesDetails( source, sourceData[ source ] ); |
| 87 |
} |
| 88 |
} |
| 89 |
this.emit( 'update' ); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
mw.echo.dm.SourcePagesModel.prototype.getSourcesArray = function () { |
| 98 |
return Object.keys( this.sources ); |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
mw.echo.dm.SourcePagesModel.prototype.getSourceTitle = function ( source ) { |
| 108 |
return this.sources[ source ] && this.sources[ source ].title; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
mw.echo.dm.SourcePagesModel.prototype.getSourceTotalCount = function ( source ) { |
| 118 |
return ( this.sources[ source ] && this.sources[ source ].totalCount ) || 0; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
mw.echo.dm.SourcePagesModel.prototype.getSourcePages = function ( source ) { |
| 128 |
return this.sources[ source ] && this.sources[ source ].pages; |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.echo.dm.SourcePagesModel.prototype.getGroupedPagesForTitle = function ( source, title ) { |
| 139 |
return OO.getProp( this.sources, source, 'pages', title, 'pages' ) || []; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
mw.echo.dm.SourcePagesModel.prototype.getGroupedPagesForCurrentTitle = function () { |
| 148 |
return this.getGroupedPagesForTitle( this.getCurrentSource(), this.getCurrentPage() ); |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
mw.echo.dm.SourcePagesModel.prototype.reset = function () { |
| 155 |
this.sources = {}; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
mw.echo.dm.SourcePagesModel.prototype.setSourcePagesDetails = function ( source, details ) { |
| 166 |
this.sources[ source ] = { |
| 167 |
title: details.source.title || source, |
| 168 |
base: details.source.base, |
| 169 |
totalCount: details.totalCount || 0, |
| 170 |
pages: {} |
| 171 |
}; |
| 172 |
|
| 173 |
for ( let i = 0; i < details.pages.length; i++ ) { |
| 174 |
const page = details.pages[ i ]; |
| 175 |
this.sources[ source ].pages[ page.title ] = page; |
| 176 |
} |
| 177 |
}; |
| 178 |
}() ); |