From 3311357df024e430c27178e93c8d8b5b18b4b272 Mon Sep 17 00:00:00 2001 From: kayone Date: Wed, 20 Nov 2013 22:04:15 -0800 Subject: [PATCH] rjs optimize the app. --- Gruntfile.js | 2 +- .../Frontend/IsCacheableSpecification.cs | 2 +- .../Frontend/Mappers/IndexHtmlMapper.cs | 1 + .../Notifications/NotificationModule.cs | 4 +- .../Notifications/Notification.cs | 17 --- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 - src/UI/Instrumentation/ErrorHandler.js | 140 +++++++++--------- src/UI/Require/require.api.js | 7 +- src/UI/app.js | 66 ++++++--- src/UI/index.html | 6 +- src/UI/jQuery/jquery.shim.js | 7 - src/UI/main.js | 11 ++ 12 files changed, 135 insertions(+), 129 deletions(-) delete mode 100644 src/NzbDrone.Core/Notifications/Notification.cs delete mode 100644 src/UI/jQuery/jquery.shim.js create mode 100644 src/UI/main.js diff --git a/Gruntfile.js b/Gruntfile.js index e7ce977e4..5fbd7d5ae 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -163,7 +163,7 @@ module.exports = function (grunt) { keepBuildDir : true, modules: [{ name: 'app', - exclude: ['JsLibraries/jquery', 'templates.js'] + exclude: ['templates.js'] }], } diff --git a/src/NzbDrone.Api/Frontend/IsCacheableSpecification.cs b/src/NzbDrone.Api/Frontend/IsCacheableSpecification.cs index aa8dc8e65..58307b4a6 100644 --- a/src/NzbDrone.Api/Frontend/IsCacheableSpecification.cs +++ b/src/NzbDrone.Api/Frontend/IsCacheableSpecification.cs @@ -22,7 +22,7 @@ namespace NzbDrone.Api.Frontend if (context.Request.Path.StartsWith("/api", StringComparison.CurrentCultureIgnoreCase)) return false; if (context.Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase)) return false; - if (context.Request.Path.EndsWith("app.js")) return false; + if (context.Request.Path.EndsWith("main.js")) return false; if (context.Request.Path.StartsWith("/log", StringComparison.CurrentCultureIgnoreCase) && context.Request.Path.EndsWith(".txt", StringComparison.CurrentCultureIgnoreCase)) diff --git a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs index 87a46c462..6baebd32c 100644 --- a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs +++ b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs @@ -55,6 +55,7 @@ namespace NzbDrone.Api.Frontend.Mappers text = text.Replace(".css", ".css?v=" + BuildInfo.Version); text = text.Replace(".js", ".js?v=" + BuildInfo.Version); text = text.Replace("API_KEY", _configFileProvider.ApiKey); + text = text.Replace("APP_VERSION", BuildInfo.Version.ToString()); return text; } diff --git a/src/NzbDrone.Api/Notifications/NotificationModule.cs b/src/NzbDrone.Api/Notifications/NotificationModule.cs index d80d91e87..62fedc40a 100644 --- a/src/NzbDrone.Api/Notifications/NotificationModule.cs +++ b/src/NzbDrone.Api/Notifications/NotificationModule.cs @@ -4,8 +4,8 @@ namespace NzbDrone.Api.Notifications { public class IndexerModule : ProviderModuleBase { - public IndexerModule(NotificationFactory notificationrFactory) - : base(notificationrFactory, "notification") + public IndexerModule(NotificationFactory notificationFactory) + : base(notificationFactory, "notification") { } diff --git a/src/NzbDrone.Core/Notifications/Notification.cs b/src/NzbDrone.Core/Notifications/Notification.cs deleted file mode 100644 index 40275ecf9..000000000 --- a/src/NzbDrone.Core/Notifications/Notification.cs +++ /dev/null @@ -1,17 +0,0 @@ -using NzbDrone.Core.ThingiProvider; - -namespace NzbDrone.Core.Notifications -{ - public class Notification - { - public int Id { get; set; } - public string Name { get; set; } - public string ImplementationName { get; set; } - public string Link { get; set; } - public bool OnGrab { get; set; } - public bool OnDownload { get; set; } - public IProviderConfig Settings { get; set; } - public INotification Instance { get; set; } - public string Implementation { get; set; } - } -} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 1664adc77..b4916da94 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -354,7 +354,6 @@ - diff --git a/src/UI/Instrumentation/ErrorHandler.js b/src/UI/Instrumentation/ErrorHandler.js index d55c10735..0de3744dd 100644 --- a/src/UI/Instrumentation/ErrorHandler.js +++ b/src/UI/Instrumentation/ErrorHandler.js @@ -1,88 +1,92 @@ -'use strict'; -(function () { - window.alert = function (message) { - window.Messenger().post(message); - }; +define( + [ + 'jquery', + 'messenger' + ], function ($, Messenger) { + 'use strict'; - var addError = function(message){ - window.$('#errors').append('
' + message + '
'); - }; + window.alert = function (message) { + new Messenger().post(message); + }; - window.onerror = function (msg, url, line) { + var addError = function (message) { + $('#errors').append('
' + message + '
'); + }; - try { + window.onerror = function (msg, url, line) { - var a = document.createElement('a'); - a.href = url; - var filename = a.pathname.split('/').pop(); + try { - //Suppress Firefox debug errors when console window is closed - if (filename.toLowerCase() === 'markupview.jsm' || filename.toLowerCase() === 'markup-view.js') { - return false; + var a = document.createElement('a'); + a.href = url; + var filename = a.pathname.split('/').pop(); + + //Suppress Firefox debug errors when console window is closed + if (filename.toLowerCase() === 'markupview.jsm' || filename.toLowerCase() === 'markup-view.js') { + return false; + } + + var messageText = filename + ' : ' + line + '
' + msg; + + var message = { + message : messageText, + type : 'error', + hideAfter : 1000, + showCloseButton: true + }; + + new Messenger().post(message); + + addError(message.message); + + } + catch (error) { + console.log('An error occurred while reporting error. ' + error); + console.log(msg); + new Messenger().post('Couldn\'t report JS error. ' + msg); } - var messageText = filename + ' : ' + line + '
' + msg; + return false; //don't suppress default alerts and logs. + }; + + $(document).ajaxError(function (event, xmlHttpRequest, ajaxOptions) { + + //don't report 200 error codes + if (xmlHttpRequest.status >= 200 && xmlHttpRequest.status <= 300) { + return undefined; + } + + //don't report aborted requests + if (xmlHttpRequest.statusText === 'abort') { + return undefined; + } var message = { - message : messageText, type : 'error', hideAfter : 1000, showCloseButton: true }; - window.Messenger().post(message); + if (xmlHttpRequest.status === 0 && xmlHttpRequest.readyState === 0) { + return false; + } + if (xmlHttpRequest.status === 400 && ajaxOptions.isValidatedCall) { + return false; + } + + if (xmlHttpRequest.status === 503) { + message.message = xmlHttpRequest.responseJSON.message; + } + + else { + message.message = '[{0}] {1} : {2}'.format(ajaxOptions.type, xmlHttpRequest.statusText, ajaxOptions.url); + } + + new Messenger().post(message); addError(message.message); - } - catch (error) { - console.log('An error occurred while reporting error. ' + error); - console.log(msg); - window.alert('Couldn\'t report JS error. ' + msg); - } - - return false; //don't suppress default alerts and logs. - }; - - window.$(document).ajaxError(function (event, xmlHttpRequest, ajaxOptions) { - - //don't report 200 error codes - if (xmlHttpRequest.status >= 200 && xmlHttpRequest.status <= 300) { - return undefined; - } - - //don't report aborted requests - if (xmlHttpRequest.statusText === 'abort') { - return undefined; - } - - var message = { - type : 'error', - hideAfter : 1000, - showCloseButton: true - }; - - if (xmlHttpRequest.status === 0 && xmlHttpRequest.readyState === 0) { return false; - } - - if (xmlHttpRequest.status === 400 && ajaxOptions.isValidatedCall) { - return false; - } - - if (xmlHttpRequest.status === 503) { - message.message = xmlHttpRequest.responseJSON.message; - } - - else { - message.message = '[{0}] {1} : {2}'.format(ajaxOptions.type, xmlHttpRequest.statusText, ajaxOptions.url); - } - - window.Messenger().post(message); - addError(message.message); - - return false; + }); }); -})(); - diff --git a/src/UI/Require/require.api.js b/src/UI/Require/require.api.js index 920cb76b0..24884b26e 100644 --- a/src/UI/Require/require.api.js +++ b/src/UI/Require/require.api.js @@ -1,7 +1,4 @@ -define( - [ - 'jquery' - ], function ($) { +define([], function () { 'use strict'; return { load: function (name, parentRequire, onload, config) { @@ -13,7 +10,7 @@ define( var resource = name.split('!')[0]; var url = window.NzbDrone.ApiRoot + '/' + resource; - $.ajax({ + window.$.ajax({ url: url }).done(function (data) { onload(data); diff --git a/src/UI/app.js b/src/UI/app.js index 3fc2e355b..5bc24b4da 100644 --- a/src/UI/app.js +++ b/src/UI/app.js @@ -1,6 +1,6 @@ 'use strict'; require.config({ - urlArgs: 'v=' + window.NzbDrone.Version, + paths: { 'backbone' : 'JsLibraries/backbone', 'moment' : 'JsLibraries/moment', @@ -22,23 +22,39 @@ require.config({ 'signalR' : 'JsLibraries/jquery.signalR', 'jquery.knob' : 'JsLibraries/jquery.knob', 'jquery.dotdotdot' : 'JsLibraries/jquery.dotdotdot', - 'jquery' : 'jQuery/jquery.shim', + 'messenger' : 'JsLibraries/messenger', + 'jquery' : 'JsLibraries/jquery', 'libs' : 'JsLibraries/', 'api': 'Require/require.api' }, shim: { - jquery :{ - exports: '$' - }, - signalR: { + + api: { deps: [ 'jquery' ] }, - bootstrap: { + + jquery : { + exports: '$' + }, + messenger : { + deps : + [ + 'jquery' + ], + exports: 'Messenger' + }, + signalR : { + deps: + [ + 'jquery' + ] + }, + bootstrap : { deps: [ 'jquery' @@ -49,23 +65,24 @@ require.config({ }); } }, - backstrech: { + backstrech : { deps: [ 'jquery' ] }, - underscore: { + underscore : { deps : [ 'jquery' ], exports: '_' }, - backbone: { + backbone : { deps: [ 'jquery', + 'Instrumentation/ErrorHandler', 'underscore', 'Mixins/jquery.ajax', 'jQuery/ToTheTop' @@ -73,7 +90,7 @@ require.config({ exports: 'Backbone' }, - marionette: { + marionette : { deps: [ 'backbone', @@ -88,45 +105,45 @@ require.config({ } }, - 'jquery.knob': { + 'jquery.knob' : { deps: [ 'jquery' ] }, - 'jquery.dotdotdot': { + 'jquery.dotdotdot' : { deps: [ 'jquery' ] }, - 'backbone.pageable': { + 'backbone.pageable' : { deps: [ 'backbone' ] }, - 'backbone.deepmodel': { + 'backbone.deepmodel' : { deps: [ 'backbone', 'underscore' ] }, - 'backbone.validation': { + 'backbone.validation' : { deps : [ 'backbone' ], exports: 'Backbone.Validation' }, - 'backbone.modelbinder':{ - deps : + 'backbone.modelbinder': { + deps: [ 'backbone' ] }, - backgrid : { + backgrid : { deps: [ 'backbone' @@ -154,7 +171,7 @@ require.config({ }); } }, - 'backgrid.paginator': { + 'backgrid.paginator' : { exports: 'Backgrid.Extension.Paginator', @@ -163,7 +180,7 @@ require.config({ 'backgrid' ] }, - 'backgrid.selectall': { + 'backgrid.selectall' : { exports: 'Backgrid.Extension.SelectAll', @@ -175,6 +192,11 @@ require.config({ } }); + +require.config({ + urlArgs: 'v=' + window.NzbDrone.Version +}); + define( [ 'jquery', @@ -224,5 +246,5 @@ define( $('#footer-region .version').html(footerText); }); - app.start(); + return app; }); \ No newline at end of file diff --git a/src/UI/index.html b/src/UI/index.html index 14a071113..78a7e4394 100644 --- a/src/UI/index.html +++ b/src/UI/index.html @@ -71,10 +71,6 @@ - - - - - + diff --git a/src/UI/jQuery/jquery.shim.js b/src/UI/jQuery/jquery.shim.js deleted file mode 100644 index 8afee0490..000000000 --- a/src/UI/jQuery/jquery.shim.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; -define( - [ - - ], function () { - return window.$; - }); diff --git a/src/UI/main.js b/src/UI/main.js new file mode 100644 index 000000000..8b7531955 --- /dev/null +++ b/src/UI/main.js @@ -0,0 +1,11 @@ +require.config({ + urlArgs: 'v=' + window.NzbDrone.Version +}); + +define( + [ + 'app' + ], function (app) { + 'use strict'; + app.start(); + }); \ No newline at end of file