From 7a4d91d2a5c28b32d6883221b7271ef4b375df1d Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Mon, 9 Jan 2012 14:56:21 +0000 Subject: [PATCH] Moving the relativeDate from manage_account to helpers. --- resources/static/pages/manage_account.js | 85 +-------------------- resources/static/shared/helpers.js | 96 +++++++++++++++++++++++- 2 files changed, 95 insertions(+), 86 deletions(-) diff --git a/resources/static/pages/manage_account.js b/resources/static/pages/manage_account.js index 44e38a9e7..4ab3d292e 100644 --- a/resources/static/pages/manage_account.js +++ b/resources/static/pages/manage_account.js @@ -42,94 +42,13 @@ BrowserID.manageAccount = (function() { errors = bid.Errors, dom = bid.DOM, storage = bid.Storage, + helpers = bid.Helpers, pageHelpers = bid.PageHelpers, cancelEvent = pageHelpers.cancelEvent, confirmAction = confirm, doc = document, tooltip = bid.Tooltip; - function relativeDate(date) { - var diff = (((new Date()).getTime() - date.getTime()) / 1000), - day_diff = Math.floor(diff / 86400), - dObj = { "friendly" : date.toLocaleDateString(), - "additional" : date.toLocaleTimeString(), - "utc" : date.toUTCString(), - "locale" : date.toLocaleString() }; - - /* some kind of error */ - if (day_diff < 0) { - dObj.friendly = "in the future!?!"; - return dObj; - } else if (isNaN(day_diff)) { - dObj.friendly = dObj.additional = "unknown"; - return dObj; - } - - if (day_diff === 0) { - if (diff < 60) { - dObj.friendly = "just now"; - return dObj; - } - if (diff < 120 + 30) { /* 1 minute plus some fuzz */ - dObj.friendly = "a minute ago"; - return dObj; - } - if (diff < 3600) { - dObj.friendly = Math.floor(diff / 60) + " minutes ago"; - return dObj; - } - if (diff < (60 * 60) * 2) { - dObj.friendly = "1 hour ago"; - return dObj; - } - if (diff < 24 * 60 * 60) { - dObj.friendly = Math.floor(diff / 3600) + " hours ago"; - return dObj; - } - } - if (day_diff === 1) { - dObj.friendly = "yesterday"; - return dObj; - } - if (day_diff < 7) { - dObj.friendly = day_diff + " days ago"; - return dObj; - } - if (day_diff < 8) { - dObj.friendly = "last week"; - return dObj; - } - /* for this scope: we want day of week and the date - plus the month (if different) */ - if (day_diff < 31) { - dObj.friendly = Math.ceil(day_diff / 7) + " weeks ago"; - return dObj; - } - - /* for this scope: we want month + date */ - if (day_diff < 62) { - dObj.friendly = "a month ago"; - return dObj; - } - if (day_diff < 365) { - dObj.friendly = Math.ceil(day_diff / 31) + " months ago"; - return dObj; - } - - /* for this scope: we want month + year */ - if (day_diff >= 365 && day_diff < 730) { - dObj.additional = date.toLocaleDateString(); - dObj.friendly = "a year ago"; - return dObj; - } - if (day_diff >= 365) { - dObj.additional = date.toLocaleDateString(); - dObj.friendly = Math.ceil(day_diff / 365) + " years ago"; - return dObj; - } - return dObj; - } - function syncAndDisplayEmails(oncomplete) { user.syncEmails(function() { displayStoredEmails(oncomplete); @@ -189,7 +108,7 @@ BrowserID.manageAccount = (function() { var template = $("#templateUser").html(); _(emails).each(function(data, e) { - var date = relativeDate(new Date(data.created)); + var date = helpers.relativeDate(new Date(data.created)); var identity = _.template(template, { email: e, diff --git a/resources/static/shared/helpers.js b/resources/static/shared/helpers.js index 2105a8d3d..647361062 100644 --- a/resources/static/shared/helpers.js +++ b/resources/static/shared/helpers.js @@ -34,7 +34,7 @@ * * ***** END LICENSE BLOCK ***** */ (function() { - "use strict" + "use strict"; var bid = BrowserID, dom = bid.DOM, @@ -48,7 +48,7 @@ for(var key in source) { target[key] = source[key]; } - }; + } function getAndValidateEmail(target) { var email = (dom.getInner(target) || "").trim(); @@ -81,6 +81,88 @@ return url; } + function relativeDate(date) { + var diff = (((new Date()).getTime() - date.getTime()) / 1000), + day_diff = Math.floor(diff / 86400), + dObj = { "friendly" : date.toLocaleDateString(), + "additional" : date.toLocaleTimeString(), + "utc" : date.toUTCString(), + "locale" : date.toLocaleString() }; + + /* some kind of error */ + if (day_diff < 0) { + dObj.friendly = "in the future!?!"; + return dObj; + } else if (isNaN(day_diff)) { + dObj.friendly = dObj.additional = "unknown"; + return dObj; + } + + if (day_diff === 0) { + if (diff < 60) { + dObj.friendly = "just now"; + return dObj; + } + if (diff < 120 + 30) { /* 1 minute plus some fuzz */ + dObj.friendly = "a minute ago"; + return dObj; + } + if (diff < 3600) { + dObj.friendly = Math.floor(diff / 60) + " minutes ago"; + return dObj; + } + if (diff < (60 * 60) * 2) { + dObj.friendly = "1 hour ago"; + return dObj; + } + if (diff < 24 * 60 * 60) { + dObj.friendly = Math.floor(diff / 3600) + " hours ago"; + return dObj; + } + } + if (day_diff === 1) { + dObj.friendly = "yesterday"; + return dObj; + } + if (day_diff < 7) { + dObj.friendly = day_diff + " days ago"; + return dObj; + } + if (day_diff < 8) { + dObj.friendly = "last week"; + return dObj; + } + /* for this scope: we want day of week and the date + plus the month (if different) */ + if (day_diff < 31) { + dObj.friendly = Math.ceil(day_diff / 7) + " weeks ago"; + return dObj; + } + + /* for this scope: we want month + date */ + if (day_diff < 62) { + dObj.friendly = "a month ago"; + return dObj; + } + if (day_diff < 365) { + dObj.friendly = Math.ceil(day_diff / 31) + " months ago"; + return dObj; + } + + /* for this scope: we want month + year */ + if (day_diff >= 365 && day_diff < 730) { + dObj.additional = date.toLocaleDateString(); + dObj.friendly = "a year ago"; + return dObj; + } + if (day_diff >= 365) { + dObj.additional = date.toLocaleDateString(); + dObj.friendly = Math.ceil(day_diff / 365) + " years ago"; + return dObj; + } + return dObj; + } + extend(helpers, { /** @@ -116,7 +198,15 @@ * @param {object} [params] - object to convert to GET parameters. * @returns {string} */ - toURL: toURL + toURL: toURL, + + /** + * Get the date relative to now. + * @method relativeDate + * @param {Date} date + * @returns {string} date relative to now. + */ + relativeDate: relativeDate }); -- GitLab