From 554eadc88d78e45d0267feb802eca378ace91751 Mon Sep 17 00:00:00 2001 From: Rushiraj Nenuji Date: Tue, 6 Aug 2024 19:06:27 -0700 Subject: [PATCH] Move bytesToSize to Utilities Ref #2484 --- src/js/common/Utilities.js | 33 ++++++++++++++++++ src/js/models/DataONEObject.js | 57 ++++++++++++------------------- src/js/models/PackageModel.js | 31 ----------------- src/js/models/SolrResult.js | 32 ----------------- src/js/views/DataPackageView.js | 4 ++- src/js/views/MetadataIndexView.js | 7 +++- src/js/views/PackageTableView.js | 6 ++-- src/js/views/StatsView.js | 35 +++---------------- 8 files changed, 71 insertions(+), 134 deletions(-) diff --git a/src/js/common/Utilities.js b/src/js/common/Utilities.js index 753674fb1..3e5798c0d 100644 --- a/src/js/common/Utilities.js +++ b/src/js/common/Utilities.js @@ -1,6 +1,11 @@ define([], () => { "use strict"; + const KIBIBYTE = 1024; + const MEBIBYTE = KIBIBYTE * 1024; + const GIBIBYTE = MEBIBYTE * 1024; + const TEBIBYTE = GIBIBYTE * 1024; + /** * @namespace Utilities * @description A generic utility object that contains functions used throughout MetacatUI to perform useful functions, @@ -156,6 +161,34 @@ define([], () => { } return 0; // No decimal places }, + + /** + * Convert number of bytes into human readable format + * + * @param integer bytes Number of bytes to convert + * @param integer precision Number of digits after the decimal separator + * @return string + */ + bytesToSize: function (bytes, precision = 0) { + if (typeof bytes === "undefined") return; + + if (bytes >= 0 && bytes < KIBIBYTE) { + return bytes + " B"; + } else if (bytes >= KIBIBYTE && bytes < MEBIBYTE) { + return (bytes / KIBIBYTE).toFixed(precision) + " KiB"; + } else if (bytes >= MEBIBYTE && bytes < GIBIBYTE) { + precision = 2; + return (bytes / MEBIBYTE).toFixed(precision) + " MiB"; + } else if (bytes >= GIBIBYTE && bytes < TEBIBYTE) { + precision = 2; + return (bytes / GIBIBYTE).toFixed(precision) + " GiB"; + } else if (bytes >= TEBIBYTE) { + precision = 2; + return (bytes / TEBIBYTE).toFixed(precision) + " TiB"; + } else { + return bytes + " B"; + } + }, }; return Utilities; diff --git a/src/js/models/DataONEObject.js b/src/js/models/DataONEObject.js index f9155fdb0..58b37eaf0 100644 --- a/src/js/models/DataONEObject.js +++ b/src/js/models/DataONEObject.js @@ -6,8 +6,19 @@ define([ "he", "collections/AccessPolicy", "collections/ObjectFormats", + "common/Utilities", "md5", -], function ($, _, Backbone, uuid, he, AccessPolicy, ObjectFormats, md5) { +], function ( + $, + _, + Backbone, + uuid, + he, + AccessPolicy, + ObjectFormats, + Utilities, + md5, +) { /** * @class DataONEObject * @classdesc A DataONEObject represents a DataONE object, such as a data file, @@ -124,8 +135,15 @@ define([ this.set("accessPolicy", this.createAccessPolicy()); - this.on("change:size", this.bytesToSize); - if (attrs.size) this.bytesToSize(); + var model = this; + this.on("change:size", function () { + var size = Utilities.bytesToSize(model.get("size")); + model.set("sizeStr", size); + }); + if (attrs.size) { + var size = Utilities.bytesToSize(model.get("size")); + model.set("sizeStr", size); + } // Cache an array of original attribute names to help in handleChange() if (this.type == "DataONEObject") @@ -1977,39 +1995,6 @@ define([ return xmlString; }, - /** - * Converts the number of bytes into a human readable format and - * updates the `sizeStr` attribute - * @returns: None - * - */ - bytesToSize: function () { - var kibibyte = 1024; - var mebibyte = kibibyte * 1024; - var gibibyte = mebibyte * 1024; - var tebibyte = gibibyte * 1024; - var precision = 0; - - var bytes = this.get("size"); - - if (bytes >= 0 && bytes < kibibyte) { - this.set("sizeStr", bytes + " B"); - } else if (bytes >= kibibyte && bytes < mebibyte) { - this.set("sizeStr", (bytes / kibibyte).toFixed(precision) + " KiB"); - } else if (bytes >= mebibyte && bytes < gibibyte) { - precision = 2; - this.set("sizeStr", (bytes / mebibyte).toFixed(precision) + " MiB"); - } else if (bytes >= gibibyte && bytes < tebibyte) { - precision = 2; - this.set("sizeStr", (bytes / gibibyte).toFixed(precision) + " GiB"); - } else if (bytes >= tebibyte) { - precision = 2; - this.set("sizeStr", (bytes / tebibyte).toFixed(precision) + " TiB"); - } else { - this.set("sizeStr", bytes + " B"); - } - }, - /** * This method will download this object while * sending the user's auth token in the request. diff --git a/src/js/models/PackageModel.js b/src/js/models/PackageModel.js index c17cd3bed..8c29e56e3 100644 --- a/src/js/models/PackageModel.js +++ b/src/js/models/PackageModel.js @@ -1816,37 +1816,6 @@ define([ this.set("totalSize", totalSize); return totalSize; }, - - /****************************/ - /** - * Convert number of bytes into human readable format - * - * @param integer bytes Number of bytes to convert - * @param integer precision Number of digits after the decimal separator - * @return string - */ - bytesToSize: function (bytes, precision) { - var kibibyte = 1024; - var mebibyte = kibibyte * 1024; - var gibibyte = mebibyte * 1024; - var tebibyte = gibibyte * 1024; - - if (typeof bytes === "undefined") var bytes = this.get("size"); - - if (bytes >= 0 && bytes < kibibyte) { - return bytes + " B"; - } else if (bytes >= kibibyte && bytes < mebibyte) { - return (bytes / kibibyte).toFixed(precision) + " KiB"; - } else if (bytes >= mebibyte && bytes < gibibyte) { - return (bytes / mebibyte).toFixed(precision) + " MiB"; - } else if (bytes >= gibibyte && bytes < tebibyte) { - return (bytes / gibibyte).toFixed(precision) + " GiB"; - } else if (bytes >= tebibyte) { - return (bytes / tebibyte).toFixed(precision) + " TiB"; - } else { - return bytes + " B"; - } - }, }, ); return PackageModel; diff --git a/src/js/models/SolrResult.js b/src/js/models/SolrResult.js index 3c55466d9..4bf2cb00a 100644 --- a/src/js/models/SolrResult.js +++ b/src/js/models/SolrResult.js @@ -859,38 +859,6 @@ define(["jquery", "underscore", "backbone"], function ($, _, Backbone) { //If nothing works so far, return an empty array return []; }, - - /****************************/ - - /** - * Convert number of bytes into human readable format - * - * @param integer bytes Number of bytes to convert - * @param integer precision Number of digits after the decimal separator - * @return string - */ - bytesToSize: function (bytes, precision) { - var kibibyte = 1024; - var mebibyte = kibibyte * 1024; - var gibibyte = mebibyte * 1024; - var tebibyte = gibibyte * 1024; - - if (typeof bytes === "undefined") var bytes = this.get("size"); - - if (bytes >= 0 && bytes < kibibyte) { - return bytes + " B"; - } else if (bytes >= kibibyte && bytes < mebibyte) { - return (bytes / kibibyte).toFixed(precision) + " KiB"; - } else if (bytes >= mebibyte && bytes < gibibyte) { - return (bytes / mebibyte).toFixed(precision) + " MiB"; - } else if (bytes >= gibibyte && bytes < tebibyte) { - return (bytes / gibibyte).toFixed(precision) + " GiB"; - } else if (bytes >= tebibyte) { - return (bytes / tebibyte).toFixed(precision) + " TiB"; - } else { - return bytes + " B"; - } - }, }, ); return SolrResult; diff --git a/src/js/views/DataPackageView.js b/src/js/views/DataPackageView.js index 5a7cd1c99..a7ba4c97d 100644 --- a/src/js/views/DataPackageView.js +++ b/src/js/views/DataPackageView.js @@ -4,6 +4,7 @@ "backbone", "localforage", "collections/DataPackage", + "common/Utilities", "models/DataONEObject", "models/PackageModel", "models/metadata/ScienceMetadata", @@ -20,6 +21,7 @@ Backbone, LocalForage, DataPackage, + Utilities, DataONEObject, PackageModel, ScienceMetadata, @@ -1036,7 +1038,7 @@ var view = this; _.each(members, function (m) { // Update the size to bytes format - m.set({ size: m.bytesToSize(m.get("size")) }); + m.set({ size: Utilities.bytesToSize(m.get("size")) }); // Add each item of this nested package to the package table view view.addOne(m, dataPackage); diff --git a/src/js/views/MetadataIndexView.js b/src/js/views/MetadataIndexView.js index 518ff79ce..b9192035b 100644 --- a/src/js/views/MetadataIndexView.js +++ b/src/js/views/MetadataIndexView.js @@ -3,6 +3,7 @@ define([ "underscore", "backbone", "gmaps", + "common/Utilities", "models/SolrResult", "views/DownloadButtonView", "text!templates/loading.html", @@ -14,6 +15,7 @@ define([ _, Backbone, gmaps, + Utilities, SolrResult, DownloadButtonView, LoadingTemplate, @@ -434,7 +436,10 @@ define([ _.each(pkg.get("members"), function (solrResult, i) { if (solrResult.get("formatType") != "DATA") return; - solrResult.set("formattedSize", solrResult.bytesToSize()); + solrResult.set( + "formattedSize", + Utilities.bytesToSize(solrResult.get("size")), + ); //Add a section for the data details, just like the other attribute sections var keys = [ diff --git a/src/js/views/PackageTableView.js b/src/js/views/PackageTableView.js index 96ce136b5..bbb934c23 100644 --- a/src/js/views/PackageTableView.js +++ b/src/js/views/PackageTableView.js @@ -2,10 +2,11 @@ define([ "jquery", "underscore", "backbone", + "common/Utilities", "models/PackageModel", "views/DownloadButtonView", "text!templates/downloadContents.html", -], function ($, _, Backbone, Package, DownloadButtonView, Template) { +], function ($, _, Backbone, Utilities, Package, DownloadButtonView, Template) { "use strict"; var PackageTable = Backbone.View.extend({ @@ -401,7 +402,8 @@ define([ //File size cell var sizeCell = $(document.createElement("td")).addClass("size"); - var size = memberModel.bytesToSize(); + var size = Utilities.bytesToSize(memberModel.get("size")); + memberModel.set("sizeStr", size); $(sizeCell).text(size); $(tr).append(sizeCell); diff --git a/src/js/views/StatsView.js b/src/js/views/StatsView.js index e655ca2a4..fb394eaab 100644 --- a/src/js/views/StatsView.js +++ b/src/js/views/StatsView.js @@ -8,6 +8,7 @@ define([ "DonutChart", "CircleBadge", "collections/Citations", + "common/Utilities", "models/MetricsModel", "models/Stats", "MetricsChart", @@ -27,6 +28,7 @@ define([ DonutChart, CircleBadge, Citations, + Utilities, MetricsModel, StatsModel, MetricsChart, @@ -923,12 +925,13 @@ define([ displayTotalSize: function () { var className = "quick-stats-count"; var count = ""; + var view = this; if (!this.model.get("totalSize")) { count = "0 bytes"; className += " no-activity"; } else { - count = this.bytesToSize(this.model.get("totalSize")); + count = Utilities.bytesToSize(view.model.get("totalSize")); } var countEl = $(document.createElement("p")) @@ -1103,36 +1106,6 @@ define([ } }, - /** - * Convert number of bytes into human readable format - * - * @param integer bytes Number of bytes to convert - * @param integer precision Number of digits after the decimal separator - * @return string - */ - bytesToSize: function (bytes, precision) { - var kibibyte = 1024; - var mebibyte = kibibyte * 1024; - var gibibyte = mebibyte * 1024; - var tebibyte = gibibyte * 1024; - - if (typeof bytes === "undefined") var bytes = this.get("size"); - - if (bytes >= 0 && bytes < kibibyte) { - return bytes + " B"; - } else if (bytes >= kibibyte && bytes < mebibyte) { - return (bytes / kibibyte).toFixed(precision) + " KiB"; - } else if (bytes >= mebibyte && bytes < gibibyte) { - return (bytes / mebibyte).toFixed(precision) + " MiB"; - } else if (bytes >= gibibyte && bytes < tebibyte) { - return (bytes / gibibyte).toFixed(precision) + " GiB"; - } else if (bytes >= tebibyte) { - return (bytes / tebibyte).toFixed(precision) + " TiB"; - } else { - return bytes + " B"; - } - }, - renderUsageMetricsError: function () { var message = "

This might take some time. Check back in 24 hours to see these results.

";