Debian Codegraph @

extensions/Echo/modules/model/mw.echo.dm.SourcePagesModel.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Source pages model for notification filtering
4 *
5 * @class
6 * @mixes OO.EventEmitter
7 *
8 * @constructor
9 * @param {Object} config Configuration object
10 * @param {string} [config.currentSource] The selected source for the model.
11 * Defaults to the current wiki.
12 */
13 mw.echo.dm.SourcePagesModel = function MwEchoDmSourcePagesModel( config ) {
14 config = config || {};
15
16 // Mixin constructor
17 OO.EventEmitter.call( this );
18
19 this.sources = {};
20
21 this.currentSource = config.currentSource || 'local';
22 this.currentPage = null;
23 };
24
25 /* Initialization */
26 OO.initClass( mw.echo.dm.SourcePagesModel );
27 OO.mixinClass( mw.echo.dm.SourcePagesModel, OO.EventEmitter );
28
29 /* Events */
30
31 /**
32 * The state of the source page model has changed
33 *
34 * @event mw.echo.dm.SourcePagesModel#update
35 */
36
37 /* Methods */
38
39 /**
40 * Set the current source and page.
41 *
42 * @param {string} source New source
43 * @param {string} page New page
44 * @fires mw.echo.dm.SourcePagesModel#update
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 * Get the current source
59 *
60 * @return {string} Current source
61 */
62 mw.echo.dm.SourcePagesModel.prototype.getCurrentSource = function () {
63 return this.currentSource;
64 };
65
66 /**
67 * Get the title of the currently selected page
68 *
69 * @return {string} Page title
70 */
71 mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () {
72 return this.currentPage;
73 };
74
75 /**
76 * Set all sources and pages. This will also reset and override any
77 * previously set information.
78 *
79 * @param {Object} sourceData A detailed object about sources and pages
80 * @fires mw.echo.dm.SourcePagesModel#update
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 * Get an array of all source names
94 *
95 * @return {string[]} Array of source names
96 */
97 mw.echo.dm.SourcePagesModel.prototype.getSourcesArray = function () {
98 return Object.keys( this.sources );
99 };
100
101 /**
102 * Get the title of a source
103 *
104 * @param {string} source Symbolic name of the source
105 * @return {string} Source title
106 */
107 mw.echo.dm.SourcePagesModel.prototype.getSourceTitle = function ( source ) {
108 return this.sources[ source ] && this.sources[ source ].title;
109 };
110
111 /**
112 * Get the total count of a source
113 *
114 * @param {string} source Symbolic name of the source
115 * @return {number} Total count
116 */
117 mw.echo.dm.SourcePagesModel.prototype.getSourceTotalCount = function ( source ) {
118 return ( this.sources[ source ] && this.sources[ source ].totalCount ) || 0;
119 };
120
121 /**
122 * Get all pages in a source
123 *
124 * @param {string} source Symbolic name of the source
125 * @return {Object} Page definitions in this source
126 */
127 mw.echo.dm.SourcePagesModel.prototype.getSourcePages = function ( source ) {
128 return this.sources[ source ] && this.sources[ source ].pages;
129 };
130
131 /**
132 * Get the list of page titles associated with one group title.
133 *
134 * @param {string} source Symbolic name of the source
135 * @param {string} title Group title
136 * @return {string[]} Page titles
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 * Get the list of page titles associated with the current group title.
144 *
145 * @return {string[]} Page titles
146 */
147 mw.echo.dm.SourcePagesModel.prototype.getGroupedPagesForCurrentTitle = function () {
148 return this.getGroupedPagesForTitle( this.getCurrentSource(), this.getCurrentPage() );
149 };
150
151 /**
152 * Reset the data
153 */
154 mw.echo.dm.SourcePagesModel.prototype.reset = function () {
155 this.sources = {};
156 };
157
158 /**
159 * Set the details of a source and its page definitions
160 *
161 * @private
162 * @param {string} source Source symbolic name
163 * @param {Object} details Details object
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 }() );