mediawiki-extensions-Popups/resources/ext.popups/counts.js
Sam Smith b7effdc000 Include user's preview count in BOOT action
Action changes:
* Include the user's preview count in the user property of the action.

Reducer changes:
* Make the eventLogging reducer add the bucketed user's preview count to
  the state tree.

Changes:
* Extract mw.popups.UserSettings#getPreviewCount and #setPreviewCount
  and associated tests from the ext.popups.core module.

Bug: T152225
Change-Id: I6b7afef31311be8fede685deb536f577845cb9cf
2016-12-12 13:01:44 +00:00

69 lines
1.6 KiB
JavaScript

( function ( mw ) {
mw.popups.counts = {};
/**
* Return count bucket for the number of edits a user has made.
*
* The buckets are defined as part of
* [the Popups schema](https://meta.wikimedia.org/wiki/Schema:Popups).
*
* Extracted from `mw.popups.schemaPopups.getEditCountBucket`.
*
* @param {Number} count
* @return {String}
*/
mw.popups.counts.getEditCountBucket = function ( count ) {
var bucket;
if ( count === 0 ) {
bucket = '0';
} else if ( count >= 1 && count <= 4 ) {
bucket = '1-4';
} else if ( count >= 5 && count <= 99 ) {
bucket = '5-99';
} else if ( count >= 100 && count <= 999 ) {
bucket = '100-999';
} else if ( count >= 1000 ) {
bucket = '1000+';
}
return bucket + ' edits';
};
/**
* Return count bucket for the number of previews a user has seen.
*
* If local storage isn't available - because the user has disabled it
* or the browser doesn't support it - then then "unknown" is returned.
*
* The buckets are defined as part of
* [the Popups schema](https://meta.wikimedia.org/wiki/Schema:Popups).
*
* Extracted from `mw.popups.getPreviewCountBucket`.
*
* @param {Number} count
* @return {String}
*/
mw.popups.counts.getPreviewCountBucket = function ( count ) {
var bucket;
if ( count === -1 ) {
return 'unknown';
}
if ( count === 0 ) {
bucket = '0';
} else if ( count >= 1 && count <= 4 ) {
bucket = '1-4';
} else if ( count >= 5 && count <= 20 ) {
bucket = '5-20';
} else if ( count >= 21 ) {
bucket = '21+';
}
return bucket + ' previews';
};
}( mediaWiki ) );