From 32d0df1af92c8d07ae2f5f06cb66d6ec05413494 Mon Sep 17 00:00:00 2001 From: Joxit Date: Tue, 21 May 2019 00:18:24 +0200 Subject: [PATCH] New script `utils` for global static functions --- gulpfile.js | 6 ++- src/index.html | 1 + src/scripts/utils.js | 68 ++++++++++++++++++++++++++++++++ src/tags/app.tag | 11 ------ src/tags/image-date.tag | 19 +-------- src/tags/tag-history-element.tag | 43 +------------------- 6 files changed, 76 insertions(+), 72 deletions(-) create mode 100644 src/scripts/utils.js diff --git a/gulpfile.js b/gulpfile.js index 5f47739..515d254 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,14 +20,16 @@ const allTags = ['src/tags/*.tag', 'src/tags/dialogs/*.tag']; const allScripts = [ 'src/scripts/http.js', - 'src/scripts/script.js' + 'src/scripts/script.js', + 'src/scripts/utils.js' ]; const staticTags = ['src/tags/*.tag']; const staticScripts = [ 'src/scripts/http.js', - 'src/scripts/static.js' + 'src/scripts/static.js', + 'src/scripts/utils.js' ]; function html() { diff --git a/src/index.html b/src/index.html index 612ca95..d1549a0 100644 --- a/src/index.html +++ b/src/index.html @@ -61,6 +61,7 @@ + diff --git a/src/scripts/utils.js b/src/scripts/utils.js new file mode 100644 index 0000000..96067a6 --- /dev/null +++ b/src/scripts/utils.js @@ -0,0 +1,68 @@ + +registryUI.bytesToSize = function (bytes) { + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + if (bytes == undefined || isNaN(bytes)) { + return '?'; + } else if (bytes == 0) { + return '0 Byte'; + } + const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + return Math.ceil(bytes / Math.pow(1024, i)) + ' ' + sizes[i]; +}; + +registryUI.dateFormat = function(date) { + if (date === undefined) { + return ''; + } + const labels = ['a second', 'seconds', 'a minute', 'minutes', 'an hour', 'hours', 'a day', 'days', 'a month', 'months', 'a year', 'years']; + const maxSeconds = [1, 60, 3600, 86400, 2592000, 31104000, Infinity]; + const diff = (new Date() - date) / 1000; + for (var i = 0; i < maxSeconds.length - 1; i++) { + if (maxSeconds[i] * 2 >= diff) { + return labels[i * 2]; + } else if (maxSeconds[i + 1] > diff) { + return Math.floor(diff / maxSeconds[i]) + ' ' + labels[i * 2 + 1]; + } + } +}; + + +registryUI.getHistoryIcon = function(attribute) { + switch (attribute) { + case 'architecture': + return 'memory'; + case 'created': + return 'event'; + case 'docker_version': + return ''; + case 'os': + return 'developer_board'; + case 'Cmd': + return 'launch'; + case 'Entrypoint': + return 'input'; + case 'Env': + return 'notes'; + case 'Labels': + return 'label'; + case 'User': + return 'face'; + case 'Volumes': + return 'storage'; + case 'WorkingDir': + return 'home'; + case 'author': + return 'account_circle'; + case 'id': + case 'digest': + return 'settings_ethernet'; + case 'created_by': + return 'build'; + case 'size': + return 'get_app'; + case 'ExposedPorts': + return 'router'; + default: + '' + } +} \ No newline at end of file diff --git a/src/tags/app.tag b/src/tags/app.tag index 9ed3931..190a784 100644 --- a/src/tags/app.tag +++ b/src/tags/app.tag @@ -221,17 +221,6 @@ along with this program. If not, see . oReq.send(); }; - registryUI.bytesToSize = function (bytes) { - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; - if (bytes == undefined || isNaN(bytes)) { - return '?'; - } else if (bytes == 0) { - return '0 Byte'; - } - const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); - return Math.ceil(bytes / Math.pow(1024, i)) + ' ' + sizes[i]; - }; - registryUI.taglist.go = function(image) { route('taglist/' + image); }; diff --git a/src/tags/image-date.tag b/src/tags/image-date.tag index a5f5b2c..1064d70 100644 --- a/src/tags/image-date.tag +++ b/src/tags/image-date.tag @@ -15,29 +15,14 @@ along with this program. If not, see . --> -
{ this.dateFormat(this.date) } ago
+
{ registryUI.dateFormat(this.date) } ago
\ No newline at end of file diff --git a/src/tags/tag-history-element.tag b/src/tags/tag-history-element.tag index b1ece87..db92292 100644 --- a/src/tags/tag-history-element.tag +++ b/src/tags/tag-history-element.tag @@ -15,50 +15,9 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . --> -
{ this.getIcon(entry.key) } +
{ registryUI.getHistoryIcon(entry.key) }

{ entry.key.replace('_', ' ') }

{ entry.value }
{ e }
- \ No newline at end of file