From f7b9384d261beaa70994340eb83d6b77ac6535af Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Fri, 15 Apr 2016 11:27:01 +0200 Subject: Issue #678: added utility converting time passed and dynamically changing version timestamp --- web-ui/app/js/helpers/view_helper.js | 44 ++++++++++++++++++++++++++++++++++++ web-ui/app/js/page/version.js | 14 ++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'web-ui/app/js') diff --git a/web-ui/app/js/helpers/view_helper.js b/web-ui/app/js/helpers/view_helper.js index 7e07df75..f8152ff7 100644 --- a/web-ui/app/js/helpers/view_helper.js +++ b/web-ui/app/js/helpers/view_helper.js @@ -103,10 +103,53 @@ define( return fingerprint.replace(/(.{4})/g, '$1 ').trim(); } + function getSinceDate(sinceDate){ + var commitDate = new Date(sinceDate); + var number = Date.now(); + var millisecondsSince = number - commitDate; + + var SECONDS = 1000, + MIN = 60 * SECONDS, + HOUR = MIN * 60, + DAY = HOUR * 24, + WEEK = DAY * 7, + MONTH = WEEK * 4, + YEAR = DAY * 365; + + var years = Math.floor(millisecondsSince / YEAR); + if (years >= 1){ + return years + ' year(s)'; + } + + var months = Math.floor(millisecondsSince / MONTH); + if (months >= 1) { + return months + ' month(s)'; + } + + var weeks = Math.floor(millisecondsSince / WEEK); + if (weeks >= 1) { + return weeks + ' week(s)'; + } + + var days = Math.floor(millisecondsSince / DAY); + if (days >= 1) { + return days + ' day(s)'; + } + + var hours = Math.floor(millisecondsSince / HOUR); + if (hours >= 1) { + return hours + ' hour(s)'; + } + + var minutes = Math.floor(millisecondsSince / MIN); + return minutes + ' minute(s)'; + } + Handlebars.registerHelper('formatDate', formatDate); Handlebars.registerHelper('formatSize', formatSize); Handlebars.registerHelper('formatStatusClasses', formatStatusClasses); Handlebars.registerHelper('formatFingerPrint', formatFingerPrint); + Handlebars.registerHelper('sinceDate', getSinceDate); return { formatStatusClasses: formatStatusClasses, @@ -115,6 +158,7 @@ define( formatFingerPrint: formatFingerPrint, moveCaretToEndOfText: moveCaretToEndOfText, quoteMail: quoteMail, + sinceDate: getSinceDate, i18n: i18n }; }); diff --git a/web-ui/app/js/page/version.js b/web-ui/app/js/page/version.js index e5299f52..9fd5e629 100644 --- a/web-ui/app/js/page/version.js +++ b/web-ui/app/js/page/version.js @@ -14,13 +14,23 @@ * You should have received a copy of the GNU Affero General Public License * along with Pixelated. If not, see . */ -define(['flight/lib/component', 'views/templates'], function (defineComponent, templates) { +define(['flight/lib/component', 'views/templates', 'helpers/view_helper'], function (defineComponent, templates, viewHelper) { 'use strict'; return defineComponent(function () { - + this.defaultAttrs({ + 'sinceDate': '#version-date' + }); + this.render = function () { this.$node.html(templates.page.version()); + this.renderCommitDate(); + }; + + this.renderCommitDate = function(){ + var since = this.select('sinceDate').attr('data-since'), + commitDate = viewHelper.sinceDate(since); + this.select('sinceDate').html(commitDate + ' ago'); }; this.after('initialize', function () { -- cgit v1.2.3