deduplicate 'getPrettySize' and similar variants

This commit is contained in:
ansuz 2021-03-01 11:17:54 +05:30
parent 792c05874e
commit 3b9bf65709
4 changed files with 17 additions and 23 deletions

View file

@ -1,13 +1,13 @@
define([
'jquery',
'/common/hyperscript.js',
'/common/common-util.js',
'/customize/messages.js',
'/customize/application_config.js',
'/common/outer/local-store.js',
'/customize/pages.js',
'/api/config',
], function ($, h, Util, Msg, AppConfig, LocalStore, Pages, Config) {
'/common/common-ui-elements.js',
], function ($, h, Msg, AppConfig, LocalStore, Pages, Config, UIElements) {
var accounts = {
donateURL: AppConfig.donateURL || "https://opencollective.com/cryptpad/",
upgradeURL: AppConfig.upgradeURL
@ -56,7 +56,7 @@ define([
};
SPECIAL_GROUP_ITEMS.storage1 = function (f) {
return groupItemTemplate(
Msg._getKey('features_f_' + f, [Util.getPrettySize(Config.defaultStorageLimit, Msg)]), // .features_f_storage1
Msg._getKey('features_f_' + f, [UIElements.prettySize(Config.defaultStorageLimit)]), // .features_f_storage1
Msg['features_f_' + f + '_note'] // .features_f_storage1_note
);
};

View file

@ -8,6 +8,7 @@ define([
'/common/hyperscript.js',
'/customize/messages.js',
'/common/common-interface.js',
'/common/common-ui-elements.js',
'/common/common-util.js',
'/common/common-hash.js',
'/common/common-signing-keys.js',
@ -26,6 +27,7 @@ define([
h,
Messages,
UI,
UIElements,
Util,
Hash,
Keys,
@ -273,12 +275,7 @@ define([
return $div;
};
var getPrettySize = function (bytes) { // XXX duplicate of UIElements.prettySize ?
var unit = Util.magnitudeOfBytes(bytes);
var value = unit === 'GB' ? Util.bytesToGigabytes(bytes) : Util.bytesToMegabytes(bytes);
return unit === 'GB' ? Messages._getKey('formattedGB', [value])
: Messages._getKey('formattedMB', [value]);
};
var getPrettySize = UIElements.prettySize;
create['defaultlimit'] = function () {
var key = 'defaultlimit';

View file

@ -34,10 +34,14 @@ define([
};
UIElements.prettySize = function (bytes) {
var kB = Util.bytesToKilobytes(bytes);
if (kB < 1024) { return kB + Messages.KB; } // XXX replace with Msg.formattedKB ?
var mB = Util.bytesToMegabytes(bytes);
return mB + Messages.MB; // XXX replace with Msg.formattedMB
var unit = Util.magnitudeOfBytes(bytes);
if (unit === 'GB') {
return Messages._getKey('formattedGB', [ Util.bytesToGigabytes(bytes)]);
} else if (unit === 'MB') {
return Messages._getKey('formattedMB', [ Util.bytesToMegabytes(bytes)]);
} else {
return Messages._getKey('formattedKB', [ Util.bytesToKilobytes(bytes)]);
}
};
UIElements.updateTags = function (common, hrefs) {

View file

@ -269,18 +269,11 @@
Util.magnitudeOfBytes = function (bytes) {
if (bytes >= oneGigabyte) { return 'GB'; }
else if (bytes >= oneMegabyte) { return 'MB'; }
// smallest supported format is MB to preserve existing behaviour
else /* if (bytes >= oneMegabyte) */ { return 'MB'; }
//else { return 'KB'; }
};
Util.getPrettySize = function (bytes, Messages) { // XXX not used anywhere?
var unit = Util.magnitudeOfBytes(bytes);
if (unit === 'GB') {
return Messages._getKey('formattedGB', [Util.bytesToGigabytes(bytes)]);
}
return Messages._getKey('formattedMB', [Util.bytesToMegabytes(bytes)]);
};
// given a path, asynchronously return an arraybuffer
var getCacheKey = function (src) {
var _src = src.replace(/(\/)*$/, ''); // Remove trailing slashes