2016-05-31 22:32:16 +00:00
|
|
|
( function ( mw ) {
|
|
|
|
/**
|
|
|
|
* Source pages model for notification filtering
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} config Configuration object
|
|
|
|
* @cfg {string} [currentSource] The selected source for the model.
|
|
|
|
* Defaults to the current wiki.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel = function MwEchoDmSourcePagesModel( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
this.sources = {};
|
|
|
|
|
|
|
|
this.currentSource = config.currentSource || mw.config.get( 'wgDBname' );
|
|
|
|
this.currentPage = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
OO.initClass( mw.echo.dm.SourcePagesModel );
|
|
|
|
OO.mixinClass( mw.echo.dm.SourcePagesModel, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event update
|
|
|
|
*
|
|
|
|
* The state of the source page model has changed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methds */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current source and page.
|
|
|
|
*
|
|
|
|
* @param {string} source New source
|
|
|
|
* @param {string} page New page
|
|
|
|
* @fires update
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.setCurrentSourcePage = function ( source, page ) {
|
|
|
|
if (
|
|
|
|
this.currentSource !== source ||
|
|
|
|
this.currentPage !== page
|
|
|
|
) {
|
|
|
|
this.currentSource = source;
|
|
|
|
this.currentPage = page;
|
|
|
|
this.emit( 'update' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current source
|
|
|
|
*
|
|
|
|
* @return {string} Current source
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getCurrentSource = function () {
|
|
|
|
return this.currentSource;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title of the currently selected page
|
|
|
|
*
|
|
|
|
* @return {string} Page title
|
|
|
|
*/
|
2016-06-23 13:51:05 +00:00
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () {
|
|
|
|
return this.currentPage;
|
2016-05-31 22:32:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set all sources and pages. This will also reset and override any
|
|
|
|
* previously set information.
|
|
|
|
*
|
|
|
|
* @param {Object} sourceData A detailed object about sources and pages
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.setAllSources = function ( sourceData ) {
|
|
|
|
var source;
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
for ( source in sourceData ) {
|
|
|
|
if ( sourceData.hasOwnProperty( source ) ) {
|
|
|
|
this.setSourcePagesDetails( source, sourceData[ source ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.emit( 'update' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of all source names
|
|
|
|
*
|
|
|
|
* @return {string[]} Array of source names
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getSourcesArray = function () {
|
|
|
|
return Object.keys( this.sources );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title of a source
|
|
|
|
*
|
|
|
|
* @param {string} source Symbolic name of the source
|
|
|
|
* @return {string} Source title
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getSourceTitle = function ( source ) {
|
|
|
|
return this.sources[ source ] && this.sources[ source ].title;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total count of a source
|
|
|
|
*
|
|
|
|
* @param {string} source Symbolic name of the source
|
|
|
|
* @return {number} Total count
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getSourceTotalCount = function ( source ) {
|
|
|
|
return ( this.sources[ source ] && this.sources[ source ].totalCount ) || 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all pages in a source
|
|
|
|
*
|
|
|
|
* @param {string} source Symbolic name of the source
|
2016-06-23 13:51:05 +00:00
|
|
|
* @return {Object[]} Page definitions in this source
|
2016-05-31 22:32:16 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.getSourcePages = function ( source ) {
|
|
|
|
return this.sources[ source ] && this.sources[ source ].pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the data
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.reset = function () {
|
|
|
|
this.sources = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the details of a source and its page definitions
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} source Source symbolic name
|
|
|
|
* @param {Object} details Details object
|
|
|
|
*/
|
|
|
|
mw.echo.dm.SourcePagesModel.prototype.setSourcePagesDetails = function ( source, details ) {
|
|
|
|
this.sources[ source ] = {
|
|
|
|
title: details.source.title,
|
|
|
|
base: details.source.base,
|
2016-06-22 16:11:34 +00:00
|
|
|
totalCount: details.totalCount,
|
2016-06-23 13:51:05 +00:00
|
|
|
pages: details.pages
|
2016-05-31 22:32:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
} )( mediaWiki );
|