Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Pagination model for echo notifications pages.
4 *
5 * @class
6 * @mixes OO.EventEmitter
7 *
8 * @constructor
9 * @param {Object} config Configuration object
10 * @param {string} [config.pageNext] The continue value of the next page
11 * @param {number} [config.itemsPerPage] The number of items per page
12 * @param {number} [config.currentPageItemCount] The number of items that are in the
13 * current page. If not given, the initial count defaults to the total number
14 * of items per page.
15 */
16 mw.echo.dm.PaginationModel = function MwEchoDmPaginationModel( config ) {
17 config = config || {};
18
19 // Mixin constructor
20 OO.EventEmitter.call( this );
21
22 this.pagesContinue = [];
23 this.itemsPerPage = config.itemsPerPage || 25;
24 this.currentPageItemCount = config.currentPageItemCount || this.itemsPerPage;
25
26 // Set initial page
27 this.currPageIndex = 0;
28 this.pagesContinue[ 0 ] = '';
29
30 // If a next page is given, fill it
31 if ( config.pageNext ) {
32 this.setPageContinue( 1, config.pageNext );
33 }
34 };
35
36 /* Initialization */
37
38 OO.initClass( mw.echo.dm.PaginationModel );
39 OO.mixinClass( mw.echo.dm.PaginationModel, OO.EventEmitter );
40
41 /* Events */
42
43 /**
44 * Pagination information was updated
45 *
46 * @event mw.echo.dm.PaginationModel#update
47 */
48
49 /* Methods */
50
51 /**
52 * Reset pagination data
53 *
54 * @fires mw.echo.dm.PaginationModel#update
55 */
56 mw.echo.dm.PaginationModel.prototype.reset = function () {
57 this.pagesContinue = [];
58 this.currPageIndex = 0;
59 this.currentPageItemCount = this.itemsPerPage;
60
61 this.emit( 'update' );
62 };
63 /**
64 * Set a page index with its 'continue' value, used for API fetching
65 *
66 * @param {number} page Page index
67 * @param {string} continueVal Continue string value
68 * @fires mw.echo.dm.PaginationModel#update
69 */
70 mw.echo.dm.PaginationModel.prototype.setPageContinue = function ( page, continueVal ) {
71 if ( this.pagesContinue[ page ] !== continueVal ) {
72 this.pagesContinue[ page ] = continueVal;
73 this.emit( 'update' );
74 }
75 };
76
77 /**
78 * Get the 'continue' value of a certain page
79 *
80 * @param {number} page Page index
81 * @return {string} Continue string value
82 */
83 mw.echo.dm.PaginationModel.prototype.getPageContinue = function ( page ) {
84 return this.pagesContinue[ page ];
85 };
86
87 /**
88 * Get the current page index
89 *
90 * @return {number} Current page index
91 */
92 mw.echo.dm.PaginationModel.prototype.getCurrPageIndex = function () {
93 return this.currPageIndex;
94 };
95
96 /**
97 * Set the current page index
98 *
99 * @internal
100 * @param {number} index Current page index
101 */
102 mw.echo.dm.PaginationModel.prototype.setCurrPageIndex = function ( index ) {
103 this.currPageIndex = index;
104 };
105
106 /**
107 * Move forward to the next page
108 *
109 * @fires mw.echo.dm.PaginationModel#update
110 */
111 mw.echo.dm.PaginationModel.prototype.forwards = function () {
112 if ( this.hasNextPage() ) {
113 this.setCurrPageIndex( this.currPageIndex + 1 );
114 this.emit( 'update' );
115 }
116 };
117
118 /**
119 * Move backwards to the previous page
120 *
121 * @fires mw.echo.dm.PaginationModel#update
122 */
123 mw.echo.dm.PaginationModel.prototype.backwards = function () {
124 if ( this.hasPrevPage() ) {
125 this.setCurrPageIndex( this.currPageIndex - 1 );
126 this.emit( 'update' );
127 }
128 };
129
130 /**
131 * Get the previous page continue value
132 *
133 * @return {string} Previous page continue value
134 */
135 mw.echo.dm.PaginationModel.prototype.getPrevPageContinue = function () {
136 return this.pagesContinue[ this.currPageIndex - 1 ] || '';
137 };
138
139 /**
140 * Get the current page continue value
141 *
142 * @return {string} Current page continue value
143 */
144 mw.echo.dm.PaginationModel.prototype.getCurrPageContinue = function () {
145 return this.pagesContinue[ this.currPageIndex ] || '';
146 };
147
148 /**
149 * Get the next page continue value
150 *
151 * @return {string} Next page continue value
152 */
153 mw.echo.dm.PaginationModel.prototype.getNextPageContinue = function () {
154 return this.pagesContinue[ this.currPageIndex + 1 ] || '';
155 };
156
157 /**
158 * Set the next page continue value
159 *
160 * @param {string} cont Next page continue value
161 */
162 mw.echo.dm.PaginationModel.prototype.setNextPageContinue = function ( cont ) {
163 this.setPageContinue( this.currPageIndex + 1, cont );
164 };
165
166 /**
167 * Check whether a previous page exists
168 *
169 * @return {boolean} Previous page exists
170 */
171 mw.echo.dm.PaginationModel.prototype.hasPrevPage = function () {
172 return this.currPageIndex > 0;
173 };
174
175 /**
176 * Check whether a next page exists
177 *
178 * @return {boolean} Next page exists
179 */
180 mw.echo.dm.PaginationModel.prototype.hasNextPage = function () {
181 return !!this.pagesContinue[ this.currPageIndex + 1 ];
182 };
183
184 /**
185 * Set the number of items in the current page
186 *
187 * @param {number} count Number of items
188 * @fires mw.echo.dm.PaginationModel#update
189 */
190 mw.echo.dm.PaginationModel.prototype.setCurrentPageItemCount = function ( count ) {
191 if ( this.currentPageItemCount !== count ) {
192 this.currentPageItemCount = count;
193 this.emit( 'update' );
194 }
195 };
196
197 /**
198 * Get the number of items in the current page
199 *
200 * @return {number} Number of items
201 */
202 mw.echo.dm.PaginationModel.prototype.getCurrentPageItemCount = function () {
203 return this.currentPageItemCount;
204 };
205
206 /**
207 * Set the number of items per page
208 *
209 * @param {number} count Number of items per page
210 */
211 mw.echo.dm.PaginationModel.prototype.setItemsPerPage = function ( count ) {
212 this.itemsPerPage = count;
213 };
214
215 /**
216 * Get the number of items per page
217 *
218 * @return {number} Number of items per page
219 */
220 mw.echo.dm.PaginationModel.prototype.getItemsPerPage = function () {
221 return this.itemsPerPage;
222 };
223 }() );