Improved Javascript coding style
This commit is contained in:
parent
a5c89bc48e
commit
553c33b860
27 changed files with 180 additions and 140 deletions
|
@ -1,31 +1,31 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.API = function(promise, appState) {
|
App.API = function(jQuery, promise, appState) {
|
||||||
|
|
||||||
var baseUrl = '/api/';
|
var baseUrl = '/api/';
|
||||||
|
|
||||||
function get(url, data) {
|
function get(url, data) {
|
||||||
return request('GET', url, data);
|
return request('GET', url, data);
|
||||||
};
|
}
|
||||||
|
|
||||||
function post(url, data) {
|
function post(url, data) {
|
||||||
return request('POST', url, data);
|
return request('POST', url, data);
|
||||||
};
|
}
|
||||||
|
|
||||||
function put(url, data) {
|
function put(url, data) {
|
||||||
return request('PUT', url, data);
|
return request('PUT', url, data);
|
||||||
};
|
}
|
||||||
|
|
||||||
function _delete(url, data) {
|
function _delete(url, data) {
|
||||||
return request('DELETE', url, data);
|
return request('DELETE', url, data);
|
||||||
};
|
}
|
||||||
|
|
||||||
function request(method, url, data) {
|
function request(method, url, data) {
|
||||||
var fullUrl = baseUrl + '/' + url;
|
var fullUrl = baseUrl + '/' + url;
|
||||||
fullUrl = fullUrl.replace(/\/{2,}/, '/');
|
fullUrl = fullUrl.replace(/\/{2,}/, '/');
|
||||||
|
|
||||||
return promise.make(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
$.ajax({
|
jQuery.ajax({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Authorization-Token': appState.get('loginToken') || '',
|
'X-Authorization-Token': appState.get('loginToken') || '',
|
||||||
},
|
},
|
||||||
|
@ -37,16 +37,16 @@ App.API = function(promise, appState) {
|
||||||
error: function(xhr, textStatus, errorThrown) {
|
error: function(xhr, textStatus, errorThrown) {
|
||||||
reject({
|
reject({
|
||||||
status: xhr.status,
|
status: xhr.status,
|
||||||
json: xhr.responseJSON
|
json: xhr.responseJSON ?
|
||||||
? xhr.responseJSON
|
xhr.responseJSON :
|
||||||
: {error: errorThrown}});
|
{error: errorThrown}});
|
||||||
},
|
},
|
||||||
type: method,
|
type: method,
|
||||||
url: fullUrl,
|
url: fullUrl,
|
||||||
data: data,
|
data: data,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get: get,
|
get: get,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Auth = function(jQuery, util, api, appState, promise) {
|
App.Auth = function(_, jQuery, util, api, appState, promise) {
|
||||||
|
|
||||||
var privileges = {
|
var privileges = {
|
||||||
register: 'register',
|
register: 'register',
|
||||||
|
@ -105,12 +105,14 @@ App.Auth = function(jQuery, util, api, appState, promise) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isLoggedIn(userName) {
|
function isLoggedIn(userName) {
|
||||||
if (!appState.get('loggedIn'))
|
if (!appState.get('loggedIn')) {
|
||||||
return false;
|
return false;
|
||||||
if (typeof(userName) != 'undefined') {
|
}
|
||||||
if (getCurrentUser().name != userName)
|
if (typeof(userName) !== 'undefined') {
|
||||||
|
if (getCurrentUser().name !== userName) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ App.Bootstrap = function(auth, router, util, promise) {
|
||||||
.then(startRouting)
|
.then(startRouting)
|
||||||
.fail(function(response) {
|
.fail(function(response) {
|
||||||
console.log(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.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');
|
App.DI.get('bootstrap');
|
||||||
|
|
|
@ -10,8 +10,9 @@ App.BrowsingSettings = function(
|
||||||
auth.startObservingLoginChanges('browsing-settings', loginStateChanged);
|
auth.startObservingLoginChanges('browsing-settings', loginStateChanged);
|
||||||
|
|
||||||
readFromLocalStorage();
|
readFromLocalStorage();
|
||||||
if (auth.isLoggedIn())
|
if (auth.isLoggedIn()) {
|
||||||
loginStateChanged();
|
loginStateChanged();
|
||||||
|
}
|
||||||
|
|
||||||
function setSettings(newSettings) {
|
function setSettings(newSettings) {
|
||||||
settings = newSettings;
|
settings = newSettings;
|
||||||
|
@ -47,8 +48,10 @@ App.BrowsingSettings = function(
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFromString(string) {
|
function readFromString(string) {
|
||||||
if (!string)
|
if (!string) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
settings = JSON.parse(string);
|
settings = JSON.parse(string);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -81,6 +84,7 @@ App.BrowsingSettings = function(
|
||||||
getSettings: getSettings,
|
getSettings: getSettings,
|
||||||
setSettings: setSettings,
|
setSettings: setSettings,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
};
|
||||||
|
|
||||||
App.DI.registerSingleton('browsingSettings', App.BrowsingSettings);
|
App.DI.registerSingleton('browsingSettings', App.BrowsingSettings);
|
||||||
|
|
|
@ -31,18 +31,15 @@ App.Controls.FileDropper = function(
|
||||||
$dropDiv.addClass('active');
|
$dropDiv.addClass('active');
|
||||||
});
|
});
|
||||||
|
|
||||||
function getFiles() {
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
function addFiles(files) {
|
function addFiles(files) {
|
||||||
$dropDiv.removeClass('active');
|
$dropDiv.removeClass('active');
|
||||||
if (!allowMultiple && files.length > 1) {
|
if (!allowMultiple && files.length > 1) {
|
||||||
alert('Cannot select multiple files.');
|
window.alert('Cannot select multiple files.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onChange(files);
|
onChange(files);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
};
|
||||||
|
|
||||||
App.DI.register('fileDropper', App.Controls.FileDropper);
|
App.DI.register('fileDropper', App.Controls.FileDropper);
|
||||||
|
|
|
@ -12,16 +12,18 @@ App.DI = (function() {
|
||||||
var instance = instances[key];
|
var instance = instances[key];
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
var factory = factories[key];
|
var factory = factories[key];
|
||||||
if (!factory)
|
if (!factory) {
|
||||||
throw new Error('Unregistered key: ' + key);
|
throw new Error('Unregistered key: ' + key);
|
||||||
|
}
|
||||||
var objectInitializer = factory.initializer;
|
var objectInitializer = factory.initializer;
|
||||||
var singleton = factory.singleton;
|
var singleton = factory.singleton;
|
||||||
var deps = resolveDependencies(objectInitializer);
|
var deps = resolveDependencies(objectInitializer);
|
||||||
var instance = {};
|
instance = {};
|
||||||
instance = objectInitializer.apply(instance, deps);
|
instance = objectInitializer.apply(instance, deps);
|
||||||
if (singleton)
|
if (singleton) {
|
||||||
instances[key] = instance;
|
instances[key] = instance;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,15 +38,15 @@ App.DI = (function() {
|
||||||
|
|
||||||
function register(key, objectInitializer) {
|
function register(key, objectInitializer) {
|
||||||
factories[key] = {initializer: objectInitializer, singleton: false};
|
factories[key] = {initializer: objectInitializer, singleton: false};
|
||||||
};
|
}
|
||||||
|
|
||||||
function registerSingleton(key, objectInitializer) {
|
function registerSingleton(key, objectInitializer) {
|
||||||
factories[key] = {initializer: objectInitializer, singleton: true};
|
factories[key] = {initializer: objectInitializer, singleton: true};
|
||||||
};
|
}
|
||||||
|
|
||||||
function registerManual(key, objectInitializer) {
|
function registerManual(key, objectInitializer) {
|
||||||
instances[key] = objectInitializer();
|
instances[key] = objectInitializer();
|
||||||
};
|
}
|
||||||
|
|
||||||
function getFunctionParameterNames(func) {
|
function getFunctionParameterNames(func) {
|
||||||
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
|
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.CommentListPresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Comment list placeholder');
|
$el.html('Comment list placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.HelpPresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Help placeholder');
|
$el.html('Help placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.HomePresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Home placeholder');
|
$el.html('Home placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.LoginPresenter = function(
|
App.Presenters.LoginPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -18,10 +19,11 @@ App.Presenters.LoginPresenter = function(
|
||||||
topNavigationPresenter.select('login');
|
topNavigationPresenter.select('login');
|
||||||
promise.wait(util.promiseTemplate('login-form')).then(function(html) {
|
promise.wait(util.promiseTemplate('login-form')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
if (auth.isLoggedIn())
|
if (auth.isLoggedIn()) {
|
||||||
router.navigateToMainPage();
|
router.navigateToMainPage();
|
||||||
else
|
} else {
|
||||||
render();
|
render();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +32,7 @@ App.Presenters.LoginPresenter = function(
|
||||||
$el.find('form').submit(loginFormSubmitted);
|
$el.find('form').submit(loginFormSubmitted);
|
||||||
$messages = $el.find('.messages');
|
$messages = $el.find('.messages');
|
||||||
$messages.width($el.find('form').width());
|
$messages.width($el.find('form').width());
|
||||||
};
|
}
|
||||||
|
|
||||||
function loginFormSubmitted(e) {
|
function loginFormSubmitted(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
@ -40,12 +42,12 @@ App.Presenters.LoginPresenter = function(
|
||||||
var password = $el.find('[name=password]').val();
|
var password = $el.find('[name=password]').val();
|
||||||
var remember = $el.find('[name=remember]').val();
|
var remember = $el.find('[name=remember]').val();
|
||||||
|
|
||||||
if (userName.length == 0) {
|
if (userName.length === 0) {
|
||||||
messagePresenter.showError($messages, 'User name cannot be empty.');
|
messagePresenter.showError($messages, 'User name cannot be empty.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (password.length == 0) {
|
if (password.length === 0) {
|
||||||
messagePresenter.showError($messages, 'Password cannot be empty.');
|
messagePresenter.showError($messages, 'Password cannot be empty.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,22 +5,22 @@ App.Presenters.MessagePresenter = function(jQuery) {
|
||||||
|
|
||||||
function showInfo($el, message) {
|
function showInfo($el, message) {
|
||||||
return showMessage($el, 'info', message);
|
return showMessage($el, 'info', message);
|
||||||
};
|
}
|
||||||
|
|
||||||
function showError($el, message) {
|
function showError($el, message) {
|
||||||
return showMessage($el, 'error', message);
|
return showMessage($el, 'error', message);
|
||||||
};
|
}
|
||||||
|
|
||||||
function hideMessages($el) {
|
function hideMessages($el) {
|
||||||
$el.children('.message').each(function() {
|
$el.children('.message').each(function() {
|
||||||
$(this).slideUp('fast', function() {
|
jQuery(this).slideUp('fast', function() {
|
||||||
$(this).remove();
|
jQuery(this).remove();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
function showMessage($el, className, message) {
|
function showMessage($el, className, message) {
|
||||||
var $messageDiv = $('<div>');
|
var $messageDiv = jQuery('<div>');
|
||||||
$messageDiv.addClass('message');
|
$messageDiv.addClass('message');
|
||||||
$messageDiv.addClass(className);
|
$messageDiv.addClass(className);
|
||||||
$messageDiv.html(message);
|
$messageDiv.html(message);
|
||||||
|
@ -28,7 +28,7 @@ App.Presenters.MessagePresenter = function(jQuery) {
|
||||||
$el.append($messageDiv);
|
$el.append($messageDiv);
|
||||||
$messageDiv.slideDown('fast');
|
$messageDiv.slideDown('fast');
|
||||||
return $messageDiv;
|
return $messageDiv;
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
showInfo: showInfo,
|
showInfo: showInfo,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.PagedCollectionPresenter = function(util, promise, api) {
|
App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
|
||||||
|
|
||||||
var searchOrder;
|
var searchOrder;
|
||||||
var searchQuery;
|
var searchQuery;
|
||||||
|
@ -58,16 +58,20 @@ App.Presenters.PagedCollectionPresenter = function(util, promise, api) {
|
||||||
var totalPages = Math.ceil(totalRecords / pageSize);
|
var totalPages = Math.ceil(totalRecords / pageSize);
|
||||||
var pages = [1, totalPages];
|
var pages = [1, totalPages];
|
||||||
var pagesAroundCurrent = 2;
|
var pagesAroundCurrent = 2;
|
||||||
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++)
|
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++) {
|
||||||
if (pageNumber + i >= 1 && pageNumber + i <= totalPages)
|
if (pageNumber + i >= 1 && pageNumber + i <= totalPages) {
|
||||||
pages.push(pageNumber + i);
|
pages.push(pageNumber + i);
|
||||||
if (pageNumber - pagesAroundCurrent - 1 == 2)
|
}
|
||||||
|
}
|
||||||
|
if (pageNumber - pagesAroundCurrent - 1 === 2) {
|
||||||
pages.push(2);
|
pages.push(2);
|
||||||
if (pageNumber + pagesAroundCurrent + 1 == totalPages - 1)
|
}
|
||||||
|
if (pageNumber + pagesAroundCurrent + 1 === totalPages - 1) {
|
||||||
pages.push(totalPages - 1);
|
pages.push(totalPages - 1);
|
||||||
|
}
|
||||||
|
|
||||||
pages = pages.sort(function(a, b) { return a - b; }).filter(function(item, pos) {
|
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({
|
$target.html(template({
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.PostListPresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Post list placeholder');
|
$el.html('Post list placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.PostUploadPresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Post upload placeholder');
|
$el.html('Post upload placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.RegistrationPresenter = function(
|
App.Presenters.RegistrationPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -11,6 +12,7 @@ App.Presenters.RegistrationPresenter = function(
|
||||||
|
|
||||||
var $el = jQuery('#content');
|
var $el = jQuery('#content');
|
||||||
var template;
|
var template;
|
||||||
|
var $messages;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
topNavigationPresenter.select('register');
|
topNavigationPresenter.select('register');
|
||||||
|
@ -31,15 +33,16 @@ App.Presenters.RegistrationPresenter = function(
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
messagePresenter.hideMessages($messages);
|
messagePresenter.hideMessages($messages);
|
||||||
|
|
||||||
formData = {
|
var formData = {
|
||||||
userName: $el.find('[name=userName]').val(),
|
userName: $el.find('[name=userName]').val(),
|
||||||
password: $el.find('[name=password]').val(),
|
password: $el.find('[name=password]').val(),
|
||||||
passwordConfirmation: $el.find('[name=passwordConfirmation]').val(),
|
passwordConfirmation: $el.find('[name=passwordConfirmation]').val(),
|
||||||
email: $el.find('[name=email]').val(),
|
email: $el.find('[name=email]').val(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!validateRegistrationFormData(formData))
|
if (!validateRegistrationFormData(formData)) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
api.post('/users', formData)
|
api.post('/users', formData)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
|
@ -66,23 +69,23 @@ App.Presenters.RegistrationPresenter = function(
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateRegistrationFormData(formData) {
|
function validateRegistrationFormData(formData) {
|
||||||
if (formData.userName.length == 0) {
|
if (formData.userName.length === 0) {
|
||||||
messagePresenter.showError($messages, 'User name cannot be empty.');
|
messagePresenter.showError($messages, 'User name cannot be empty.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formData.password.length == 0) {
|
if (formData.password.length === 0) {
|
||||||
messagePresenter.showError($messages, 'Password cannot be empty.');
|
messagePresenter.showError($messages, 'Password cannot be empty.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formData.password != formData.passwordConfirmation) {
|
if (formData.password !== formData.passwordConfirmation) {
|
||||||
messagePresenter.showError($messages, 'Passwords must be the same.');
|
messagePresenter.showError($messages, 'Passwords must be the same.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -14,7 +14,7 @@ App.Presenters.TagListPresenter = function(
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$el.html('Tag list placeholder');
|
$el.html('Tag list placeholder');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.TopNavigationPresenter = function(
|
App.Presenters.TopNavigationPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -23,7 +24,7 @@ App.Presenters.TopNavigationPresenter = function(
|
||||||
selectedElement = newSelectedElement;
|
selectedElement = newSelectedElement;
|
||||||
$el.find('li').removeClass('active');
|
$el.find('li').removeClass('active');
|
||||||
$el.find('li.' + selectedElement).addClass('active');
|
$el.find('li.' + selectedElement).addClass('active');
|
||||||
};
|
}
|
||||||
|
|
||||||
function loginStateChanged() {
|
function loginStateChanged() {
|
||||||
render();
|
render();
|
||||||
|
@ -41,7 +42,7 @@ App.Presenters.TopNavigationPresenter = function(
|
||||||
canUploadPosts: auth.hasPrivilege(auth.privileges.uploadPosts),
|
canUploadPosts: auth.hasPrivilege(auth.privileges.uploadPosts),
|
||||||
}));
|
}));
|
||||||
$el.find('li.' + selectedElement).addClass('active');
|
$el.find('li.' + selectedElement).addClass('active');
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserAccountRemovalPresenter = function(
|
App.Presenters.UserAccountRemovalPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -48,7 +49,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
|
||||||
function accountRemovalFormSubmitted(e) {
|
function accountRemovalFormSubmitted(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var $el = jQuery(target);
|
var $el = jQuery(target);
|
||||||
$messages = $el.find('.messages');
|
var $messages = $el.find('.messages');
|
||||||
messagePresenter.hideMessages($messages);
|
messagePresenter.hideMessages($messages);
|
||||||
if (!$el.find('input[name=confirmation]:visible').prop('checked')) {
|
if (!$el.find('input[name=confirmation]:visible').prop('checked')) {
|
||||||
messagePresenter.showError($messages, 'Must confirm to proceed.');
|
messagePresenter.showError($messages, 'Must confirm to proceed.');
|
||||||
|
@ -74,6 +75,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
|
||||||
render: render,
|
render: render,
|
||||||
getPrivileges: getPrivileges
|
getPrivileges: getPrivileges
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
App.DI.register('userAccountRemovalPresenter', App.Presenters.UserAccountRemovalPresenter);
|
App.DI.register('userAccountRemovalPresenter', App.Presenters.UserAccountRemovalPresenter);
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserAccountSettingsPresenter = function(
|
App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -9,7 +10,6 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
auth,
|
auth,
|
||||||
messagePresenter) {
|
messagePresenter) {
|
||||||
|
|
||||||
var $messages;
|
|
||||||
var target;
|
var target;
|
||||||
var template;
|
var template;
|
||||||
var user;
|
var user;
|
||||||
|
@ -62,7 +62,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
function avatarStyleChanged(e) {
|
function avatarStyleChanged(e) {
|
||||||
var $el = jQuery(target);
|
var $el = jQuery(target);
|
||||||
var $target = $el.find('.avatar-content .file-handler');
|
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();
|
$target.show();
|
||||||
} else {
|
} else {
|
||||||
$target.hide();
|
$target.hide();
|
||||||
|
@ -70,14 +70,14 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
}
|
}
|
||||||
|
|
||||||
function avatarContentChanged(files) {
|
function avatarContentChanged(files) {
|
||||||
if (files.length == 1) {
|
if (files.length === 1) {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
avatarContent = reader.result;
|
avatarContent = reader.result;
|
||||||
var $el = jQuery(target);
|
var $el = jQuery(target);
|
||||||
var $target = $el.find('.avatar-content .file-handler');
|
var $target = $el.find('.avatar-content .file-handler');
|
||||||
$target.html(files[0].name);
|
$target.html(files[0].name);
|
||||||
}
|
};
|
||||||
reader.readAsDataURL(files[0]);
|
reader.readAsDataURL(files[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,9 +91,10 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
|
|
||||||
if (privileges.canChangeAvatarStyle) {
|
if (privileges.canChangeAvatarStyle) {
|
||||||
formData.avatarStyle = $el.find('[name=avatar-style]:checked').val();
|
formData.avatarStyle = $el.find('[name=avatar-style]:checked').val();
|
||||||
if (avatarContent)
|
if (avatarContent) {
|
||||||
formData.avatarContent = avatarContent;
|
formData.avatarContent = avatarContent;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (privileges.canChangeName) {
|
if (privileges.canChangeName) {
|
||||||
formData.userName = $el.find('[name=userName]').val();
|
formData.userName = $el.find('[name=userName]').val();
|
||||||
}
|
}
|
||||||
|
@ -136,7 +137,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateAccountSettingsFormData($messages, formData) {
|
function validateAccountSettingsFormData($messages, formData) {
|
||||||
if (formData.password != formData.passwordConfirmation) {
|
if (formData.password !== formData.passwordConfirmation) {
|
||||||
messagePresenter.showError($messages, 'Passwords must be the same.');
|
messagePresenter.showError($messages, 'Passwords must be the same.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -149,6 +150,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
|
||||||
render: render,
|
render: render,
|
||||||
getPrivileges: getPrivileges,
|
getPrivileges: getPrivileges,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
};
|
||||||
|
|
||||||
App.DI.register('userAccountSettingsPresenter', App.Presenters.UserAccountSettingsPresenter);
|
App.DI.register('userAccountSettingsPresenter', App.Presenters.UserAccountSettingsPresenter);
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserActivationPresenter = function(
|
App.Presenters.UserActivationPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
promise,
|
promise,
|
||||||
util,
|
util,
|
||||||
|
@ -45,8 +46,9 @@ App.Presenters.UserActivationPresenter = function(
|
||||||
function render() {
|
function render() {
|
||||||
$el.html(template());
|
$el.html(template());
|
||||||
$messages = $el.find('.messages');
|
$messages = $el.find('.messages');
|
||||||
if (formHidden)
|
if (formHidden) {
|
||||||
$el.find('form').hide();
|
$el.find('form').hide();
|
||||||
|
}
|
||||||
$el.find('form').submit(userQueryFormSubmitted);
|
$el.find('form').submit(userQueryFormSubmitted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,18 +65,18 @@ App.Presenters.UserActivationPresenter = function(
|
||||||
messagePresenter.hideMessages($messages);
|
messagePresenter.hideMessages($messages);
|
||||||
|
|
||||||
var userNameOrEmail = $el.find('form input[name=user]').val();
|
var userNameOrEmail = $el.find('form input[name=user]').val();
|
||||||
if (userNameOrEmail.length == 0) {
|
if (userNameOrEmail.length === 0) {
|
||||||
messagePresenter.showError($messages, 'Field cannot be blank.');
|
messagePresenter.showError($messages, 'Field cannot be blank.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var url = operation == 'passwordReset'
|
var url = operation === 'passwordReset' ?
|
||||||
? '/password-reset/' + userNameOrEmail
|
'/password-reset/' + userNameOrEmail :
|
||||||
: '/activation/' + userNameOrEmail;
|
'/activation/' + userNameOrEmail;
|
||||||
|
|
||||||
api.post(url).then(function(response) {
|
api.post(url).then(function(response) {
|
||||||
var message = operation == 'passwordReset'
|
var message = operation === 'passwordReset' ?
|
||||||
? 'Password reset request sent.'
|
'Password reset request sent.' :
|
||||||
: 'Activation e-mail resent.';
|
'Activation e-mail resent.';
|
||||||
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.';
|
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.';
|
||||||
|
|
||||||
$el.find('#user-query-form').slideUp(function() {
|
$el.find('#user-query-form').slideUp(function() {
|
||||||
|
@ -88,14 +90,14 @@ App.Presenters.UserActivationPresenter = function(
|
||||||
function confirmToken(token) {
|
function confirmToken(token) {
|
||||||
messagePresenter.hideMessages($messages);
|
messagePresenter.hideMessages($messages);
|
||||||
|
|
||||||
var url = operation == 'passwordReset'
|
var url = operation === 'passwordReset' ?
|
||||||
? '/finish-password-reset/' + token
|
'/finish-password-reset/' + token :
|
||||||
: '/finish-activation/' + token;
|
'/finish-activation/' + token;
|
||||||
|
|
||||||
api.post(url).then(function(response) {
|
api.post(url).then(function(response) {
|
||||||
var message = operation == 'passwordReset'
|
var message = operation === 'passwordReset' ?
|
||||||
? 'Your new password is <strong>' + response.json.newPassword + '</strong>.'
|
'Your new password is <strong>' + response.json.newPassword + '</strong>.' :
|
||||||
: 'E-mail activation successful.';
|
'E-mail activation successful.';
|
||||||
|
|
||||||
$el.find('#user-query-form').slideUp(function() {
|
$el.find('#user-query-form').slideUp(function() {
|
||||||
messagePresenter.showInfo($messages, message);
|
messagePresenter.showInfo($messages, message);
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserBrowsingSettingsPresenter = function(
|
App.Presenters.UserBrowsingSettingsPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -19,7 +20,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
|
||||||
user = args.user;
|
user = args.user;
|
||||||
target = args.target;
|
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) {
|
promise.wait(util.promiseTemplate('browsing-settings')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
|
@ -63,6 +64,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
|
||||||
render: render,
|
render: render,
|
||||||
getPrivileges: getPrivileges,
|
getPrivileges: getPrivileges,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
};
|
||||||
|
|
||||||
App.DI.register('userBrowsingSettingsPresenter', App.Presenters.UserBrowsingSettingsPresenter);
|
App.DI.register('userBrowsingSettingsPresenter', App.Presenters.UserBrowsingSettingsPresenter);
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserListPresenter = function(
|
App.Presenters.UserListPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -55,7 +56,7 @@ App.Presenters.UserListPresenter = function(
|
||||||
|
|
||||||
var $pager = $el.find('.pager');
|
var $pager = $el.find('.pager');
|
||||||
pagedCollectionPresenter.render($pager);
|
pagedCollectionPresenter.render($pager);
|
||||||
};
|
}
|
||||||
|
|
||||||
function orderLinkClicked(e) {
|
function orderLinkClicked(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
|
@ -2,6 +2,7 @@ var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.UserPresenter = function(
|
App.Presenters.UserPresenter = function(
|
||||||
|
_,
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
promise,
|
promise,
|
||||||
|
@ -42,7 +43,7 @@ App.Presenters.UserPresenter = function(
|
||||||
userAccountRemovalPresenter.init(_.extend(extendedContext, {target: '#account-removal-target'})))
|
userAccountRemovalPresenter.init(_.extend(extendedContext, {target: '#account-removal-target'})))
|
||||||
.then(function() {
|
.then(function() {
|
||||||
initTabs(args);
|
initTabs(args);
|
||||||
})
|
});
|
||||||
|
|
||||||
}).fail(function(response) {
|
}).fail(function(response) {
|
||||||
$el.empty();
|
$el.empty();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Promise = (function(jQuery) {
|
App.Promise = function(jQuery) {
|
||||||
|
|
||||||
function make(callback)
|
function make(callback)
|
||||||
{
|
{
|
||||||
|
@ -23,6 +23,6 @@ App.Promise = (function(jQuery) {
|
||||||
waitAll: waitAll,
|
waitAll: waitAll,
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
};
|
||||||
|
|
||||||
App.DI.registerSingleton('promise', App.Promise);
|
App.DI.registerSingleton('promise', App.Promise);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Router = function(jQuery, util, appState) {
|
App.Router = function(pathJs, _, jQuery, util, appState) {
|
||||||
|
|
||||||
var root = '#/';
|
var root = '#/';
|
||||||
|
|
||||||
|
@ -8,15 +8,15 @@ App.Router = function(jQuery, util, appState) {
|
||||||
|
|
||||||
function navigateToMainPage() {
|
function navigateToMainPage() {
|
||||||
window.location.href = root;
|
window.location.href = root;
|
||||||
};
|
}
|
||||||
|
|
||||||
function navigate(url) {
|
function navigate(url) {
|
||||||
window.location.href = url;
|
window.location.href = url;
|
||||||
};
|
}
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
Path.listen();
|
pathJs.listen();
|
||||||
};
|
}
|
||||||
|
|
||||||
function injectRoutes() {
|
function injectRoutes() {
|
||||||
inject('#/home', 'homePresenter');
|
inject('#/home', 'homePresenter');
|
||||||
|
@ -33,18 +33,18 @@ App.Router = function(jQuery, util, appState) {
|
||||||
inject('#/tags(/:searchArgs)', 'tagListPresenter');
|
inject('#/tags(/:searchArgs)', 'tagListPresenter');
|
||||||
inject('#/help', 'helpPresenter');
|
inject('#/help', 'helpPresenter');
|
||||||
setRoot('#/home');
|
setRoot('#/home');
|
||||||
};
|
}
|
||||||
|
|
||||||
function setRoot(newRoot) {
|
function setRoot(newRoot) {
|
||||||
root = newRoot;
|
root = newRoot;
|
||||||
Path.root(newRoot);
|
pathJs.root(newRoot);
|
||||||
};
|
}
|
||||||
|
|
||||||
function inject(path, presenterName, additionalParams) {
|
function inject(path, presenterName, additionalParams) {
|
||||||
Path.map(path).to(function() {
|
pathJs.map(path).to(function() {
|
||||||
util.initContentPresenter(presenterName, _.extend(this.params, additionalParams));
|
util.initContentPresenter(presenterName, _.extend(this.params, additionalParams));
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
start: start,
|
start: start,
|
||||||
|
|
|
@ -7,34 +7,38 @@ App.State = function() {
|
||||||
|
|
||||||
function get(key) {
|
function get(key) {
|
||||||
return properties[key];
|
return properties[key];
|
||||||
};
|
}
|
||||||
|
|
||||||
function set(key, value) {
|
function set(key, value) {
|
||||||
properties[key] = value;
|
properties[key] = value;
|
||||||
if (key in observers) {
|
if (key in observers) {
|
||||||
for (observerName in observers[key]) {
|
for (var observerName in observers[key]) {
|
||||||
if (observers[key].hasOwnProperty(observerName)) {
|
if (observers[key].hasOwnProperty(observerName)) {
|
||||||
observers[key][observerName](key, value);
|
observers[key][observerName](key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
function startObserving(key, observerName, callback) {
|
function startObserving(key, observerName, callback) {
|
||||||
if (!(key in observers))
|
if (!(key in observers)) {
|
||||||
observers[key] = {};
|
observers[key] = {};
|
||||||
if (!(observerName in observers[key]))
|
}
|
||||||
|
if (!(observerName in observers[key])) {
|
||||||
observers[key][observerName] = {};
|
observers[key][observerName] = {};
|
||||||
|
}
|
||||||
observers[key][observerName] = callback;
|
observers[key][observerName] = callback;
|
||||||
};
|
}
|
||||||
|
|
||||||
function stopObserving(key, observerName) {
|
function stopObserving(key, observerName) {
|
||||||
if (!(key in observers))
|
if (!(key in observers)) {
|
||||||
return;
|
return;
|
||||||
if (!(observerName in observers[key]))
|
}
|
||||||
|
if (!(observerName in observers[key])) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
delete observers[key][observerName];
|
delete observers[key][observerName];
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get: get,
|
get: get,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Util = (function(jQuery, promise) {
|
App.Util = function(_, jQuery, promise) {
|
||||||
|
|
||||||
var templateCache = {};
|
var templateCache = {};
|
||||||
var lastContentPresenterName;
|
var lastContentPresenterName;
|
||||||
|
@ -11,9 +11,10 @@ App.Util = (function(jQuery, promise) {
|
||||||
args = (args || '').split(/;/);
|
args = (args || '').split(/;/);
|
||||||
for (var i = 0; i < args.length; i ++) {
|
for (var i = 0; i < args.length; i ++) {
|
||||||
var arg = args[i];
|
var arg = args[i];
|
||||||
if (!arg)
|
if (!arg) {
|
||||||
continue;
|
continue;
|
||||||
kv = arg.split(/=/);
|
}
|
||||||
|
var kv = arg.split(/=/);
|
||||||
result[kv[0]] = kv[1];
|
result[kv[0]] = kv[1];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -22,9 +23,10 @@ App.Util = (function(jQuery, promise) {
|
||||||
function compileComplexRouteArgs(baseUri, args) {
|
function compileComplexRouteArgs(baseUri, args) {
|
||||||
var result = baseUri + '/';
|
var result = baseUri + '/';
|
||||||
_.each(args, function(v, k) {
|
_.each(args, function(v, k) {
|
||||||
if (typeof(v) == 'undefined')
|
if (typeof(v) === 'undefined') {
|
||||||
return;
|
return;
|
||||||
result += k + '=' + v + ';'
|
}
|
||||||
|
result += k + '=' + v + ';';
|
||||||
});
|
});
|
||||||
result = result.slice(0, -1);
|
result = result.slice(0, -1);
|
||||||
return result;
|
return result;
|
||||||
|
@ -36,20 +38,20 @@ App.Util = (function(jQuery, promise) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function initContentPresenter(presenterName, args) {
|
function initContentPresenter(presenterName, args) {
|
||||||
if (lastContentPresenterName != presenterName) {
|
if (lastContentPresenterName !== presenterName) {
|
||||||
var presenter = App.DI.get(presenterName);
|
var presenter = App.DI.get(presenterName);
|
||||||
var initResult = presenter.init.call(presenter, args);
|
presenter.init.call(presenter, args);
|
||||||
lastContentPresenterName = presenterName;
|
lastContentPresenterName = presenterName;
|
||||||
lastContentPresenter = presenter;
|
lastContentPresenter = presenter;
|
||||||
} else if (lastContentPresenter.reinit) {
|
} else if (lastContentPresenter.reinit) {
|
||||||
lastContentPresenter.reinit.call(presenter, args);
|
lastContentPresenter.reinit.call(lastContentPresenter, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function promiseTemplate(templateName) {
|
function promiseTemplate(templateName) {
|
||||||
return promiseTemplateFromCache(templateName)
|
return promiseTemplateFromCache(templateName) ||
|
||||||
|| promiseTemplateFromDOM(templateName)
|
promiseTemplateFromDOM(templateName) ||
|
||||||
|| promiseTemplateWithAJAX(templateName);
|
promiseTemplateWithAJAX(templateName);
|
||||||
}
|
}
|
||||||
|
|
||||||
function promiseTemplateFromCache(templateName) {
|
function promiseTemplateFromCache(templateName) {
|
||||||
|
@ -74,16 +76,15 @@ App.Util = (function(jQuery, promise) {
|
||||||
return promise.make(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
var templatesDir = '/templates';
|
var templatesDir = '/templates';
|
||||||
var templateUrl = templatesDir + '/' + templateName + '.tpl';
|
var templateUrl = templatesDir + '/' + templateName + '.tpl';
|
||||||
var templateString;
|
|
||||||
|
|
||||||
$.ajax({
|
jQuery.ajax({
|
||||||
url: templateUrl,
|
url: templateUrl,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
success: function(data, textStatus, xhr) {
|
success: function(data, textStatus, xhr) {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
},
|
},
|
||||||
error: function(xhr, textStatus, errorThrown) {
|
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();
|
reject();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -91,53 +92,60 @@ App.Util = (function(jQuery, promise) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatRelativeTime(timeString) {
|
function formatRelativeTime(timeString) {
|
||||||
if (!timeString)
|
if (!timeString) {
|
||||||
return 'never';
|
return 'never';
|
||||||
|
}
|
||||||
|
|
||||||
var time = Date.parse(timeString);
|
var then = Date.parse(timeString);
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
var difference = Math.abs(now - time);
|
var difference = Math.abs(now - then);
|
||||||
var future = now < time;
|
var future = now < then;
|
||||||
|
|
||||||
var text = (function(difference) {
|
var text = (function(difference) {
|
||||||
var mul = 1000;
|
var mul = 1000;
|
||||||
var prevMul;
|
var prevMul;
|
||||||
|
|
||||||
mul *= 60;
|
mul *= 60;
|
||||||
if (difference < mul)
|
if (difference < mul) {
|
||||||
return 'a few seconds';
|
return 'a few seconds';
|
||||||
if (difference < mul * 2)
|
} else if (difference < mul * 2) {
|
||||||
return 'a minute';
|
return 'a minute';
|
||||||
|
}
|
||||||
|
|
||||||
prevMul = mul; mul *= 60;
|
prevMul = mul; mul *= 60;
|
||||||
if (difference < mul)
|
if (difference < mul) {
|
||||||
return Math.round(difference / prevMul) + ' minutes';
|
return Math.round(difference / prevMul) + ' minutes';
|
||||||
if (difference < mul * 2)
|
} else if (difference < mul * 2) {
|
||||||
return 'an hour';
|
return 'an hour';
|
||||||
|
}
|
||||||
|
|
||||||
prevMul = mul; mul *= 24;
|
prevMul = mul; mul *= 24;
|
||||||
if (difference < mul)
|
if (difference < mul) {
|
||||||
return Math.round(difference / prevMul) + ' hours';
|
return Math.round(difference / prevMul) + ' hours';
|
||||||
if (difference < mul * 2)
|
} else if (difference < mul * 2) {
|
||||||
return 'a day';
|
return 'a day';
|
||||||
|
}
|
||||||
|
|
||||||
prevMul = mul; mul *= 30.42;
|
prevMul = mul; mul *= 30.42;
|
||||||
if (difference < mul)
|
if (difference < mul) {
|
||||||
return Math.round(difference / prevMul) + ' days';
|
return Math.round(difference / prevMul) + ' days';
|
||||||
if (difference < mul * 2)
|
} else if (difference < mul * 2) {
|
||||||
return 'a month';
|
return 'a month';
|
||||||
|
}
|
||||||
|
|
||||||
prevMul = mul; mul *= 12;
|
prevMul = mul; mul *= 12;
|
||||||
if (difference < mul)
|
if (difference < mul) {
|
||||||
return Math.round(difference / prevMul) + ' months';
|
return Math.round(difference / prevMul) + ' months';
|
||||||
if (difference < mul * 2)
|
} else if (difference < mul * 2) {
|
||||||
return 'a year';
|
return 'a year';
|
||||||
|
}
|
||||||
|
|
||||||
return Math.round(difference / mul) + ' years';
|
return Math.round(difference / mul) + ' years';
|
||||||
})(difference);
|
})(difference);
|
||||||
|
|
||||||
if (text == 'a day')
|
if (text === 'a day') {
|
||||||
return future ? 'tomorrow' : 'yesterday';
|
return future ? 'tomorrow' : 'yesterday';
|
||||||
|
}
|
||||||
return future ? 'in ' + text : text + ' ago';
|
return future ? 'in ' + text : text + ' ago';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +157,7 @@ App.Util = (function(jQuery, promise) {
|
||||||
compileComplexRouteArgs: compileComplexRouteArgs,
|
compileComplexRouteArgs: compileComplexRouteArgs,
|
||||||
formatRelativeTime: formatRelativeTime,
|
formatRelativeTime: formatRelativeTime,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
};
|
||||||
|
|
||||||
App.DI.registerSingleton('util', App.Util);
|
App.DI.registerSingleton('util', App.Util);
|
||||||
|
|
Loading…
Reference in a new issue