summaryrefslogtreecommitdiff
blob: 2d3b2b4972df3182b8012982725039b99b80fe2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
( function () {
	/**
	 * 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 || 'local';
		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
	 */

	/* Methods */

	/**
	 * 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
	 */
	mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () {
		return this.currentPage;
	};

	/**
	 * 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 ( Object.prototype.hasOwnProperty.call( sourceData, 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
	 * @return {Object} Page definitions in this source
	 */
	mw.echo.dm.SourcePagesModel.prototype.getSourcePages = function ( source ) {
		return this.sources[ source ] && this.sources[ source ].pages;
	};

	/**
	 * Get the list of page titles associated with one group title.
	 *
	 * @param {string} source Symbolic name of the source
	 * @param {string} title Group title
	 * @return {string[]} Page titles
	 */
	mw.echo.dm.SourcePagesModel.prototype.getGroupedPagesForTitle = function ( source, title ) {
		return OO.getProp( this.sources, source, 'pages', title, 'pages' ) || [];
	};

	/**
	 * Get the list of page titles associated with the current group title.
	 *
	 * @return {string[]} Page titles
	 */
	mw.echo.dm.SourcePagesModel.prototype.getGroupedPagesForCurrentTitle = function () {
		return this.getGroupedPagesForTitle( this.getCurrentSource(), this.getCurrentPage() );
	};

	/**
	 * 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 ) {
		var i, page;
		this.sources[ source ] = {
			title: details.source.title || source,
			base: details.source.base,
			totalCount: details.totalCount || 0,
			pages: {}
		};

		for ( i = 0; i < details.pages.length; i++ ) {
			page = details.pages[ i ];
			this.sources[ source ].pages[ page.title ] = page;
		}
	};
}() );