From 553c33b860e65192e523a52a2240264cd9b400e0 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 8 Sep 2014 22:02:28 +0200 Subject: [PATCH] Improved Javascript coding style --- public_html/js/Api.js | 20 +++--- public_html/js/Auth.js | 10 +-- public_html/js/Bootstrap.js | 6 +- public_html/js/BrowsingSettings.js | 10 ++- public_html/js/Controls/FileDropper.js | 9 +-- public_html/js/DI.js | 14 ++-- .../js/Presenters/CommentListPresenter.js | 2 +- public_html/js/Presenters/HelpPresenter.js | 2 +- public_html/js/Presenters/HomePresenter.js | 2 +- public_html/js/Presenters/LoginPresenter.js | 12 ++-- public_html/js/Presenters/MessagePresenter.js | 14 ++-- .../js/Presenters/PagedCollectionPresenter.js | 16 +++-- .../js/Presenters/PostListPresenter.js | 2 +- .../js/Presenters/PostUploadPresenter.js | 2 +- .../js/Presenters/RegistrationPresenter.js | 15 ++-- public_html/js/Presenters/TagListPresenter.js | 2 +- .../js/Presenters/TopNavigationPresenter.js | 5 +- .../Presenters/UserAccountRemovalPresenter.js | 4 +- .../UserAccountSettingsPresenter.js | 16 +++-- .../js/Presenters/UserActivationPresenter.js | 30 ++++---- .../UserBrowsingSettingsPresenter.js | 6 +- .../js/Presenters/UserListPresenter.js | 3 +- public_html/js/Presenters/UserPresenter.js | 3 +- public_html/js/Promise.js | 4 +- public_html/js/Router.js | 20 +++--- public_html/js/State.js | 22 +++--- public_html/js/Util.js | 69 +++++++++++-------- 27 files changed, 180 insertions(+), 140 deletions(-) diff --git a/public_html/js/Api.js b/public_html/js/Api.js index e1f42257..a7e33d55 100644 --- a/public_html/js/Api.js +++ b/public_html/js/Api.js @@ -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, diff --git a/public_html/js/Auth.js b/public_html/js/Auth.js index f8301093..3b3f5044 100644 --- a/public_html/js/Auth.js +++ b/public_html/js/Auth.js @@ -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,11 +105,13 @@ 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; } diff --git a/public_html/js/Bootstrap.js b/public_html/js/Bootstrap.js index 643f9318..785f5b66 100644 --- a/public_html/js/Bootstrap.js +++ b/public_html/js/Bootstrap.js @@ -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'); diff --git a/public_html/js/BrowsingSettings.js b/public_html/js/BrowsingSettings.js index 13922b4d..048472aa 100644 --- a/public_html/js/BrowsingSettings.js +++ b/public_html/js/BrowsingSettings.js @@ -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); diff --git a/public_html/js/Controls/FileDropper.js b/public_html/js/Controls/FileDropper.js index 2899afeb..cf21d417 100644 --- a/public_html/js/Controls/FileDropper.js +++ b/public_html/js/Controls/FileDropper.js @@ -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); diff --git a/public_html/js/DI.js b/public_html/js/DI.js index b2b87f91..3ee32825 100644 --- a/public_html/js/DI.js +++ b/public_html/js/DI.js @@ -12,15 +12,17 @@ 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, ''); diff --git a/public_html/js/Presenters/CommentListPresenter.js b/public_html/js/Presenters/CommentListPresenter.js index d1df32ac..315ccec8 100644 --- a/public_html/js/Presenters/CommentListPresenter.js +++ b/public_html/js/Presenters/CommentListPresenter.js @@ -14,7 +14,7 @@ App.Presenters.CommentListPresenter = function( function render() { $el.html('Comment list placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/HelpPresenter.js b/public_html/js/Presenters/HelpPresenter.js index a2a8c8de..c83862a6 100644 --- a/public_html/js/Presenters/HelpPresenter.js +++ b/public_html/js/Presenters/HelpPresenter.js @@ -14,7 +14,7 @@ App.Presenters.HelpPresenter = function( function render() { $el.html('Help placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index 1e9c978d..c9fe07bc 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -14,7 +14,7 @@ App.Presenters.HomePresenter = function( function render() { $el.html('Home placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/LoginPresenter.js b/public_html/js/Presenters/LoginPresenter.js index 63f1a777..466189b1 100644 --- a/public_html/js/Presenters/LoginPresenter.js +++ b/public_html/js/Presenters/LoginPresenter.js @@ -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; } diff --git a/public_html/js/Presenters/MessagePresenter.js b/public_html/js/Presenters/MessagePresenter.js index 0e5b5802..cbdb4cee 100644 --- a/public_html/js/Presenters/MessagePresenter.js +++ b/public_html/js/Presenters/MessagePresenter.js @@ -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 = $('
'); + var $messageDiv = jQuery('
'); $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, diff --git a/public_html/js/Presenters/PagedCollectionPresenter.js b/public_html/js/Presenters/PagedCollectionPresenter.js index b9d3e382..c0e86916 100644 --- a/public_html/js/Presenters/PagedCollectionPresenter.js +++ b/public_html/js/Presenters/PagedCollectionPresenter.js @@ -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({ diff --git a/public_html/js/Presenters/PostListPresenter.js b/public_html/js/Presenters/PostListPresenter.js index b40d1354..4aa25ed9 100644 --- a/public_html/js/Presenters/PostListPresenter.js +++ b/public_html/js/Presenters/PostListPresenter.js @@ -14,7 +14,7 @@ App.Presenters.PostListPresenter = function( function render() { $el.html('Post list placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/PostUploadPresenter.js b/public_html/js/Presenters/PostUploadPresenter.js index fba01375..19da5f17 100644 --- a/public_html/js/Presenters/PostUploadPresenter.js +++ b/public_html/js/Presenters/PostUploadPresenter.js @@ -14,7 +14,7 @@ App.Presenters.PostUploadPresenter = function( function render() { $el.html('Post upload placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/RegistrationPresenter.js b/public_html/js/Presenters/RegistrationPresenter.js index 1baf51ef..a12ef8f1 100644 --- a/public_html/js/Presenters/RegistrationPresenter.js +++ b/public_html/js/Presenters/RegistrationPresenter.js @@ -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, diff --git a/public_html/js/Presenters/TagListPresenter.js b/public_html/js/Presenters/TagListPresenter.js index c36b728d..6706c24f 100644 --- a/public_html/js/Presenters/TagListPresenter.js +++ b/public_html/js/Presenters/TagListPresenter.js @@ -14,7 +14,7 @@ App.Presenters.TagListPresenter = function( function render() { $el.html('Tag list placeholder'); - }; + } return { init: init, diff --git a/public_html/js/Presenters/TopNavigationPresenter.js b/public_html/js/Presenters/TopNavigationPresenter.js index 6f91c791..c9db932b 100644 --- a/public_html/js/Presenters/TopNavigationPresenter.js +++ b/public_html/js/Presenters/TopNavigationPresenter.js @@ -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, diff --git a/public_html/js/Presenters/UserAccountRemovalPresenter.js b/public_html/js/Presenters/UserAccountRemovalPresenter.js index b2b10d28..c2716309 100644 --- a/public_html/js/Presenters/UserAccountRemovalPresenter.js +++ b/public_html/js/Presenters/UserAccountRemovalPresenter.js @@ -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); diff --git a/public_html/js/Presenters/UserAccountSettingsPresenter.js b/public_html/js/Presenters/UserAccountSettingsPresenter.js index 52922283..4b436071 100644 --- a/public_html/js/Presenters/UserAccountSettingsPresenter.js +++ b/public_html/js/Presenters/UserAccountSettingsPresenter.js @@ -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,8 +91,9 @@ 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); diff --git a/public_html/js/Presenters/UserActivationPresenter.js b/public_html/js/Presenters/UserActivationPresenter.js index 90688a80..e909e778 100644 --- a/public_html/js/Presenters/UserActivationPresenter.js +++ b/public_html/js/Presenters/UserActivationPresenter.js @@ -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.
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 ' + response.json.newPassword + '.' - : 'E-mail activation successful.'; + var message = operation === 'passwordReset' ? + 'Your new password is ' + response.json.newPassword + '.' : + 'E-mail activation successful.'; $el.find('#user-query-form').slideUp(function() { messagePresenter.showInfo($messages, message); diff --git a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js index e366af28..7592ec64 100644 --- a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js +++ b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js @@ -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); diff --git a/public_html/js/Presenters/UserListPresenter.js b/public_html/js/Presenters/UserListPresenter.js index a24446f0..742fc8e9 100644 --- a/public_html/js/Presenters/UserListPresenter.js +++ b/public_html/js/Presenters/UserListPresenter.js @@ -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(); diff --git a/public_html/js/Presenters/UserPresenter.js b/public_html/js/Presenters/UserPresenter.js index f4703244..1064ae4b 100644 --- a/public_html/js/Presenters/UserPresenter.js +++ b/public_html/js/Presenters/UserPresenter.js @@ -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(); diff --git a/public_html/js/Promise.js b/public_html/js/Promise.js index f51e4e00..c0bbb462 100644 --- a/public_html/js/Promise.js +++ b/public_html/js/Promise.js @@ -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); diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 0faa6203..a84a912b 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -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, diff --git a/public_html/js/State.js b/public_html/js/State.js index 22d4a2a2..94888205 100644 --- a/public_html/js/State.js +++ b/public_html/js/State.js @@ -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, diff --git a/public_html/js/Util.js b/public_html/js/Util.js index 6e8273e9..66389c24 100644 --- a/public_html/js/Util.js +++ b/public_html/js/Util.js @@ -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);