Improved Javascript coding style

This commit is contained in:
Marcin Kurczewski 2014-09-08 22:02:28 +02:00
parent a5c89bc48e
commit 553c33b860
27 changed files with 180 additions and 140 deletions

View file

@ -1,31 +1,31 @@
var App = App || {};
App.API = function(promise, appState) {
App.API = function(jQuery, promise, appState) {
var baseUrl = '/api/';
function get(url, data) {
return request('GET', url, data);
};
}
function post(url, data) {
return request('POST', url, data);
};
}
function put(url, data) {
return request('PUT', url, data);
};
}
function _delete(url, data) {
return request('DELETE', url, data);
};
}
function request(method, url, data) {
var fullUrl = baseUrl + '/' + url;
fullUrl = fullUrl.replace(/\/{2,}/, '/');
return promise.make(function(resolve, reject) {
$.ajax({
jQuery.ajax({
headers: {
'X-Authorization-Token': appState.get('loginToken') || '',
},
@ -37,16 +37,16 @@ App.API = function(promise, appState) {
error: function(xhr, textStatus, errorThrown) {
reject({
status: xhr.status,
json: xhr.responseJSON
? xhr.responseJSON
: {error: errorThrown}});
json: xhr.responseJSON ?
xhr.responseJSON :
{error: errorThrown}});
},
type: method,
url: fullUrl,
data: data,
});
});
};
}
return {
get: get,

View file

@ -1,6 +1,6 @@
var App = App || {};
App.Auth = function(jQuery, util, api, appState, promise) {
App.Auth = function(_, jQuery, util, api, appState, promise) {
var privileges = {
register: 'register',
@ -105,12 +105,14 @@ App.Auth = function(jQuery, util, api, appState, promise) {
}
function isLoggedIn(userName) {
if (!appState.get('loggedIn'))
if (!appState.get('loggedIn')) {
return false;
if (typeof(userName) != 'undefined') {
if (getCurrentUser().name != userName)
}
if (typeof(userName) !== 'undefined') {
if (getCurrentUser().name !== userName) {
return false;
}
}
return true;
}

View file

@ -11,7 +11,7 @@ App.Bootstrap = function(auth, router, util, promise) {
.then(startRouting)
.fail(function(response) {
console.log(response);
alert('Fatal authentication error: ' + response.json.error);
window.alert('Fatal authentication error: ' + response.json.error);
});
});
@ -26,5 +26,7 @@ App.Bootstrap = function(auth, router, util, promise) {
};
App.DI.registerSingleton('bootstrap', App.Bootstrap);
App.DI.registerManual('jQuery', function() { return $; });
App.DI.registerManual('jQuery', function() { return window.$; });
App.DI.registerManual('pathJs', function() { return window.Path; });
App.DI.registerManual('_', function() { return window._; });
App.DI.get('bootstrap');

View file

@ -10,8 +10,9 @@ App.BrowsingSettings = function(
auth.startObservingLoginChanges('browsing-settings', loginStateChanged);
readFromLocalStorage();
if (auth.isLoggedIn())
if (auth.isLoggedIn()) {
loginStateChanged();
}
function setSettings(newSettings) {
settings = newSettings;
@ -47,8 +48,10 @@ App.BrowsingSettings = function(
}
function readFromString(string) {
if (!string)
if (!string) {
return;
}
try {
settings = JSON.parse(string);
} catch (e) {
@ -81,6 +84,7 @@ App.BrowsingSettings = function(
getSettings: getSettings,
setSettings: setSettings,
};
}
};
App.DI.registerSingleton('browsingSettings', App.BrowsingSettings);

View file

@ -31,18 +31,15 @@ App.Controls.FileDropper = function(
$dropDiv.addClass('active');
});
function getFiles() {
return files;
}
function addFiles(files) {
$dropDiv.removeClass('active');
if (!allowMultiple && files.length > 1) {
alert('Cannot select multiple files.');
window.alert('Cannot select multiple files.');
return;
}
onChange(files);
}
}
};
App.DI.register('fileDropper', App.Controls.FileDropper);

View file

@ -12,16 +12,18 @@ App.DI = (function() {
var instance = instances[key];
if (!instance) {
var factory = factories[key];
if (!factory)
if (!factory) {
throw new Error('Unregistered key: ' + key);
}
var objectInitializer = factory.initializer;
var singleton = factory.singleton;
var deps = resolveDependencies(objectInitializer);
var instance = {};
instance = {};
instance = objectInitializer.apply(instance, deps);
if (singleton)
if (singleton) {
instances[key] = instance;
}
}
return instance;
}
@ -36,15 +38,15 @@ App.DI = (function() {
function register(key, objectInitializer) {
factories[key] = {initializer: objectInitializer, singleton: false};
};
}
function registerSingleton(key, objectInitializer) {
factories[key] = {initializer: objectInitializer, singleton: true};
};
}
function registerManual(key, objectInitializer) {
instances[key] = objectInitializer();
};
}
function getFunctionParameterNames(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '');

View file

@ -14,7 +14,7 @@ App.Presenters.CommentListPresenter = function(
function render() {
$el.html('Comment list placeholder');
};
}
return {
init: init,

View file

@ -14,7 +14,7 @@ App.Presenters.HelpPresenter = function(
function render() {
$el.html('Help placeholder');
};
}
return {
init: init,

View file

@ -14,7 +14,7 @@ App.Presenters.HomePresenter = function(
function render() {
$el.html('Home placeholder');
};
}
return {
init: init,

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.LoginPresenter = function(
_,
jQuery,
util,
promise,
@ -18,10 +19,11 @@ App.Presenters.LoginPresenter = function(
topNavigationPresenter.select('login');
promise.wait(util.promiseTemplate('login-form')).then(function(html) {
template = _.template(html);
if (auth.isLoggedIn())
if (auth.isLoggedIn()) {
router.navigateToMainPage();
else
} else {
render();
}
});
}
@ -30,7 +32,7 @@ App.Presenters.LoginPresenter = function(
$el.find('form').submit(loginFormSubmitted);
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
};
}
function loginFormSubmitted(e) {
e.preventDefault();
@ -40,12 +42,12 @@ App.Presenters.LoginPresenter = function(
var password = $el.find('[name=password]').val();
var remember = $el.find('[name=remember]').val();
if (userName.length == 0) {
if (userName.length === 0) {
messagePresenter.showError($messages, 'User name cannot be empty.');
return false;
}
if (password.length == 0) {
if (password.length === 0) {
messagePresenter.showError($messages, 'Password cannot be empty.');
return false;
}

View file

@ -5,22 +5,22 @@ App.Presenters.MessagePresenter = function(jQuery) {
function showInfo($el, message) {
return showMessage($el, 'info', message);
};
}
function showError($el, message) {
return showMessage($el, 'error', message);
};
}
function hideMessages($el) {
$el.children('.message').each(function() {
$(this).slideUp('fast', function() {
$(this).remove();
jQuery(this).slideUp('fast', function() {
jQuery(this).remove();
});
});
};
}
function showMessage($el, className, message) {
var $messageDiv = $('<div>');
var $messageDiv = jQuery('<div>');
$messageDiv.addClass('message');
$messageDiv.addClass(className);
$messageDiv.html(message);
@ -28,7 +28,7 @@ App.Presenters.MessagePresenter = function(jQuery) {
$el.append($messageDiv);
$messageDiv.slideDown('fast');
return $messageDiv;
};
}
return {
showInfo: showInfo,

View file

@ -1,7 +1,7 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PagedCollectionPresenter = function(util, promise, api) {
App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
var searchOrder;
var searchQuery;
@ -58,16 +58,20 @@ App.Presenters.PagedCollectionPresenter = function(util, promise, api) {
var totalPages = Math.ceil(totalRecords / pageSize);
var pages = [1, totalPages];
var pagesAroundCurrent = 2;
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++)
if (pageNumber + i >= 1 && pageNumber + i <= totalPages)
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++) {
if (pageNumber + i >= 1 && pageNumber + i <= totalPages) {
pages.push(pageNumber + i);
if (pageNumber - pagesAroundCurrent - 1 == 2)
}
}
if (pageNumber - pagesAroundCurrent - 1 === 2) {
pages.push(2);
if (pageNumber + pagesAroundCurrent + 1 == totalPages - 1)
}
if (pageNumber + pagesAroundCurrent + 1 === totalPages - 1) {
pages.push(totalPages - 1);
}
pages = pages.sort(function(a, b) { return a - b; }).filter(function(item, pos) {
return !pos || item != pages[pos - 1];
return !pos || item !== pages[pos - 1];
});
$target.html(template({

View file

@ -14,7 +14,7 @@ App.Presenters.PostListPresenter = function(
function render() {
$el.html('Post list placeholder');
};
}
return {
init: init,

View file

@ -14,7 +14,7 @@ App.Presenters.PostUploadPresenter = function(
function render() {
$el.html('Post upload placeholder');
};
}
return {
init: init,

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.RegistrationPresenter = function(
_,
jQuery,
util,
promise,
@ -11,6 +12,7 @@ App.Presenters.RegistrationPresenter = function(
var $el = jQuery('#content');
var template;
var $messages;
function init() {
topNavigationPresenter.select('register');
@ -31,15 +33,16 @@ App.Presenters.RegistrationPresenter = function(
e.preventDefault();
messagePresenter.hideMessages($messages);
formData = {
var formData = {
userName: $el.find('[name=userName]').val(),
password: $el.find('[name=password]').val(),
passwordConfirmation: $el.find('[name=passwordConfirmation]').val(),
email: $el.find('[name=email]').val(),
};
if (!validateRegistrationFormData(formData))
if (!validateRegistrationFormData(formData)) {
return;
}
api.post('/users', formData)
.then(function(response) {
@ -66,23 +69,23 @@ App.Presenters.RegistrationPresenter = function(
}
function validateRegistrationFormData(formData) {
if (formData.userName.length == 0) {
if (formData.userName.length === 0) {
messagePresenter.showError($messages, 'User name cannot be empty.');
return false;
}
if (formData.password.length == 0) {
if (formData.password.length === 0) {
messagePresenter.showError($messages, 'Password cannot be empty.');
return false;
}
if (formData.password != formData.passwordConfirmation) {
if (formData.password !== formData.passwordConfirmation) {
messagePresenter.showError($messages, 'Passwords must be the same.');
return false;
}
return true;
};
}
return {
init: init,

View file

@ -14,7 +14,7 @@ App.Presenters.TagListPresenter = function(
function render() {
$el.html('Tag list placeholder');
};
}
return {
init: init,

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.TopNavigationPresenter = function(
_,
jQuery,
util,
promise,
@ -23,7 +24,7 @@ App.Presenters.TopNavigationPresenter = function(
selectedElement = newSelectedElement;
$el.find('li').removeClass('active');
$el.find('li.' + selectedElement).addClass('active');
};
}
function loginStateChanged() {
render();
@ -41,7 +42,7 @@ App.Presenters.TopNavigationPresenter = function(
canUploadPosts: auth.hasPrivilege(auth.privileges.uploadPosts),
}));
$el.find('li.' + selectedElement).addClass('active');
};
}
return {
init: init,

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserAccountRemovalPresenter = function(
_,
jQuery,
util,
promise,
@ -48,7 +49,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
function accountRemovalFormSubmitted(e) {
e.preventDefault();
var $el = jQuery(target);
$messages = $el.find('.messages');
var $messages = $el.find('.messages');
messagePresenter.hideMessages($messages);
if (!$el.find('input[name=confirmation]:visible').prop('checked')) {
messagePresenter.showError($messages, 'Must confirm to proceed.');
@ -74,6 +75,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
render: render,
getPrivileges: getPrivileges
};
};
App.DI.register('userAccountRemovalPresenter', App.Presenters.UserAccountRemovalPresenter);

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserAccountSettingsPresenter = function(
_,
jQuery,
util,
promise,
@ -9,7 +10,6 @@ App.Presenters.UserAccountSettingsPresenter = function(
auth,
messagePresenter) {
var $messages;
var target;
var template;
var user;
@ -62,7 +62,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
function avatarStyleChanged(e) {
var $el = jQuery(target);
var $target = $el.find('.avatar-content .file-handler');
if ($el.find('[name=avatar-style]:checked').val() == 'manual') {
if ($el.find('[name=avatar-style]:checked').val() === 'manual') {
$target.show();
} else {
$target.hide();
@ -70,14 +70,14 @@ App.Presenters.UserAccountSettingsPresenter = function(
}
function avatarContentChanged(files) {
if (files.length == 1) {
if (files.length === 1) {
var reader = new FileReader();
reader.onloadend = function() {
avatarContent = reader.result;
var $el = jQuery(target);
var $target = $el.find('.avatar-content .file-handler');
$target.html(files[0].name);
}
};
reader.readAsDataURL(files[0]);
}
}
@ -91,9 +91,10 @@ App.Presenters.UserAccountSettingsPresenter = function(
if (privileges.canChangeAvatarStyle) {
formData.avatarStyle = $el.find('[name=avatar-style]:checked').val();
if (avatarContent)
if (avatarContent) {
formData.avatarContent = avatarContent;
}
}
if (privileges.canChangeName) {
formData.userName = $el.find('[name=userName]').val();
}
@ -136,7 +137,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
}
function validateAccountSettingsFormData($messages, formData) {
if (formData.password != formData.passwordConfirmation) {
if (formData.password !== formData.passwordConfirmation) {
messagePresenter.showError($messages, 'Passwords must be the same.');
return false;
}
@ -149,6 +150,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
render: render,
getPrivileges: getPrivileges,
};
}
};
App.DI.register('userAccountSettingsPresenter', App.Presenters.UserAccountSettingsPresenter);

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserActivationPresenter = function(
_,
jQuery,
promise,
util,
@ -45,8 +46,9 @@ App.Presenters.UserActivationPresenter = function(
function render() {
$el.html(template());
$messages = $el.find('.messages');
if (formHidden)
if (formHidden) {
$el.find('form').hide();
}
$el.find('form').submit(userQueryFormSubmitted);
}
@ -63,18 +65,18 @@ App.Presenters.UserActivationPresenter = function(
messagePresenter.hideMessages($messages);
var userNameOrEmail = $el.find('form input[name=user]').val();
if (userNameOrEmail.length == 0) {
if (userNameOrEmail.length === 0) {
messagePresenter.showError($messages, 'Field cannot be blank.');
return;
}
var url = operation == 'passwordReset'
? '/password-reset/' + userNameOrEmail
: '/activation/' + userNameOrEmail;
var url = operation === 'passwordReset' ?
'/password-reset/' + userNameOrEmail :
'/activation/' + userNameOrEmail;
api.post(url).then(function(response) {
var message = operation == 'passwordReset'
? 'Password reset request sent.'
: 'Activation e-mail resent.';
var message = operation === 'passwordReset' ?
'Password reset request sent.' :
'Activation e-mail resent.';
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.';
$el.find('#user-query-form').slideUp(function() {
@ -88,14 +90,14 @@ App.Presenters.UserActivationPresenter = function(
function confirmToken(token) {
messagePresenter.hideMessages($messages);
var url = operation == 'passwordReset'
? '/finish-password-reset/' + token
: '/finish-activation/' + token;
var url = operation === 'passwordReset' ?
'/finish-password-reset/' + token :
'/finish-activation/' + token;
api.post(url).then(function(response) {
var message = operation == 'passwordReset'
? 'Your new password is <strong>' + response.json.newPassword + '</strong>.'
: 'E-mail activation successful.';
var message = operation === 'passwordReset' ?
'Your new password is <strong>' + response.json.newPassword + '</strong>.' :
'E-mail activation successful.';
$el.find('#user-query-form').slideUp(function() {
messagePresenter.showInfo($messages, message);

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserBrowsingSettingsPresenter = function(
_,
jQuery,
util,
promise,
@ -19,7 +20,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
user = args.user;
target = args.target;
privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name == auth.getCurrentUser().name;
privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name;
promise.wait(util.promiseTemplate('browsing-settings')).then(function(html) {
template = _.template(html);
@ -63,6 +64,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
render: render,
getPrivileges: getPrivileges,
};
}
};
App.DI.register('userBrowsingSettingsPresenter', App.Presenters.UserBrowsingSettingsPresenter);

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserListPresenter = function(
_,
jQuery,
util,
promise,
@ -55,7 +56,7 @@ App.Presenters.UserListPresenter = function(
var $pager = $el.find('.pager');
pagedCollectionPresenter.render($pager);
};
}
function orderLinkClicked(e) {
e.preventDefault();

View file

@ -2,6 +2,7 @@ var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserPresenter = function(
_,
jQuery,
util,
promise,
@ -42,7 +43,7 @@ App.Presenters.UserPresenter = function(
userAccountRemovalPresenter.init(_.extend(extendedContext, {target: '#account-removal-target'})))
.then(function() {
initTabs(args);
})
});
}).fail(function(response) {
$el.empty();

View file

@ -1,6 +1,6 @@
var App = App || {};
App.Promise = (function(jQuery) {
App.Promise = function(jQuery) {
function make(callback)
{
@ -23,6 +23,6 @@ App.Promise = (function(jQuery) {
waitAll: waitAll,
};
});
};
App.DI.registerSingleton('promise', App.Promise);

View file

@ -1,6 +1,6 @@
var App = App || {};
App.Router = function(jQuery, util, appState) {
App.Router = function(pathJs, _, jQuery, util, appState) {
var root = '#/';
@ -8,15 +8,15 @@ App.Router = function(jQuery, util, appState) {
function navigateToMainPage() {
window.location.href = root;
};
}
function navigate(url) {
window.location.href = url;
};
}
function start() {
Path.listen();
};
pathJs.listen();
}
function injectRoutes() {
inject('#/home', 'homePresenter');
@ -33,18 +33,18 @@ App.Router = function(jQuery, util, appState) {
inject('#/tags(/:searchArgs)', 'tagListPresenter');
inject('#/help', 'helpPresenter');
setRoot('#/home');
};
}
function setRoot(newRoot) {
root = newRoot;
Path.root(newRoot);
};
pathJs.root(newRoot);
}
function inject(path, presenterName, additionalParams) {
Path.map(path).to(function() {
pathJs.map(path).to(function() {
util.initContentPresenter(presenterName, _.extend(this.params, additionalParams));
});
};
}
return {
start: start,

View file

@ -7,34 +7,38 @@ App.State = function() {
function get(key) {
return properties[key];
};
}
function set(key, value) {
properties[key] = value;
if (key in observers) {
for (observerName in observers[key]) {
for (var observerName in observers[key]) {
if (observers[key].hasOwnProperty(observerName)) {
observers[key][observerName](key, value);
}
}
}
};
}
function startObserving(key, observerName, callback) {
if (!(key in observers))
if (!(key in observers)) {
observers[key] = {};
if (!(observerName in observers[key]))
}
if (!(observerName in observers[key])) {
observers[key][observerName] = {};
}
observers[key][observerName] = callback;
};
}
function stopObserving(key, observerName) {
if (!(key in observers))
if (!(key in observers)) {
return;
if (!(observerName in observers[key]))
}
if (!(observerName in observers[key])) {
return;
}
delete observers[key][observerName];
};
}
return {
get: get,

View file

@ -1,6 +1,6 @@
var App = App || {};
App.Util = (function(jQuery, promise) {
App.Util = function(_, jQuery, promise) {
var templateCache = {};
var lastContentPresenterName;
@ -11,9 +11,10 @@ App.Util = (function(jQuery, promise) {
args = (args || '').split(/;/);
for (var i = 0; i < args.length; i ++) {
var arg = args[i];
if (!arg)
if (!arg) {
continue;
kv = arg.split(/=/);
}
var kv = arg.split(/=/);
result[kv[0]] = kv[1];
}
return result;
@ -22,9 +23,10 @@ App.Util = (function(jQuery, promise) {
function compileComplexRouteArgs(baseUri, args) {
var result = baseUri + '/';
_.each(args, function(v, k) {
if (typeof(v) == 'undefined')
if (typeof(v) === 'undefined') {
return;
result += k + '=' + v + ';'
}
result += k + '=' + v + ';';
});
result = result.slice(0, -1);
return result;
@ -36,20 +38,20 @@ App.Util = (function(jQuery, promise) {
}
function initContentPresenter(presenterName, args) {
if (lastContentPresenterName != presenterName) {
if (lastContentPresenterName !== presenterName) {
var presenter = App.DI.get(presenterName);
var initResult = presenter.init.call(presenter, args);
presenter.init.call(presenter, args);
lastContentPresenterName = presenterName;
lastContentPresenter = presenter;
} else if (lastContentPresenter.reinit) {
lastContentPresenter.reinit.call(presenter, args);
lastContentPresenter.reinit.call(lastContentPresenter, args);
}
}
function promiseTemplate(templateName) {
return promiseTemplateFromCache(templateName)
|| promiseTemplateFromDOM(templateName)
|| promiseTemplateWithAJAX(templateName);
return promiseTemplateFromCache(templateName) ||
promiseTemplateFromDOM(templateName) ||
promiseTemplateWithAJAX(templateName);
}
function promiseTemplateFromCache(templateName) {
@ -74,16 +76,15 @@ App.Util = (function(jQuery, promise) {
return promise.make(function(resolve, reject) {
var templatesDir = '/templates';
var templateUrl = templatesDir + '/' + templateName + '.tpl';
var templateString;
$.ajax({
jQuery.ajax({
url: templateUrl,
method: 'GET',
success: function(data, textStatus, xhr) {
resolve(data);
},
error: function(xhr, textStatus, errorThrown) {
console.log(Error('Error while loading template ' + templateName + ': ' + errorThrown));
console.log(new Error('Error while loading template ' + templateName + ': ' + errorThrown));
reject();
},
});
@ -91,53 +92,60 @@ App.Util = (function(jQuery, promise) {
}
function formatRelativeTime(timeString) {
if (!timeString)
if (!timeString) {
return 'never';
}
var time = Date.parse(timeString);
var then = Date.parse(timeString);
var now = Date.now();
var difference = Math.abs(now - time);
var future = now < time;
var difference = Math.abs(now - then);
var future = now < then;
var text = (function(difference) {
var mul = 1000;
var prevMul;
mul *= 60;
if (difference < mul)
if (difference < mul) {
return 'a few seconds';
if (difference < mul * 2)
} else if (difference < mul * 2) {
return 'a minute';
}
prevMul = mul; mul *= 60;
if (difference < mul)
if (difference < mul) {
return Math.round(difference / prevMul) + ' minutes';
if (difference < mul * 2)
} else if (difference < mul * 2) {
return 'an hour';
}
prevMul = mul; mul *= 24;
if (difference < mul)
if (difference < mul) {
return Math.round(difference / prevMul) + ' hours';
if (difference < mul * 2)
} else if (difference < mul * 2) {
return 'a day';
}
prevMul = mul; mul *= 30.42;
if (difference < mul)
if (difference < mul) {
return Math.round(difference / prevMul) + ' days';
if (difference < mul * 2)
} else if (difference < mul * 2) {
return 'a month';
}
prevMul = mul; mul *= 12;
if (difference < mul)
if (difference < mul) {
return Math.round(difference / prevMul) + ' months';
if (difference < mul * 2)
} else if (difference < mul * 2) {
return 'a year';
}
return Math.round(difference / mul) + ' years';
})(difference);
if (text == 'a day')
if (text === 'a day') {
return future ? 'tomorrow' : 'yesterday';
}
return future ? 'in ' + text : text + ' ago';
}
@ -149,6 +157,7 @@ App.Util = (function(jQuery, promise) {
compileComplexRouteArgs: compileComplexRouteArgs,
formatRelativeTime: formatRelativeTime,
};
});
};
App.DI.registerSingleton('util', App.Util);