From 3b9bf6570923dea3ed51c9fdcf7fd77b7548a7d3 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 1 Mar 2021 11:17:54 +0530 Subject: [PATCH] deduplicate 'getPrettySize' and similar variants --- customize.dist/pages/features.js | 6 +++--- www/admin/inner.js | 9 +++------ www/common/common-ui-elements.js | 12 ++++++++---- www/common/common-util.js | 13 +++---------- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/customize.dist/pages/features.js b/customize.dist/pages/features.js index ed26fad5b..ff67c6912 100644 --- a/customize.dist/pages/features.js +++ b/customize.dist/pages/features.js @@ -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 ); }; diff --git a/www/admin/inner.js b/www/admin/inner.js index 158edbcf8..bc2085681 100644 --- a/www/admin/inner.js +++ b/www/admin/inner.js @@ -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'; diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index 975810a88..8b5101fb7 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -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) { diff --git a/www/common/common-util.js b/www/common/common-util.js index 1c2bc0222..e98b37009 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -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