From 381af184cec77b9bfae42907a48dc39978d33ac1 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Fri, 3 May 2013 16:50:22 -0700 Subject: [PATCH] replaced bootstrap notifications for errors with messenger.js --- UI/Content/Messenger/messenger.css | 3 +- UI/Instrumentation/ErrorHandler.js | 74 +++++++++++++++++++++++++++++ UI/Shared/NotificationCollection.js | 69 +-------------------------- UI/app.js | 6 +-- 4 files changed, 80 insertions(+), 72 deletions(-) create mode 100644 UI/Instrumentation/ErrorHandler.js diff --git a/UI/Content/Messenger/messenger.css b/UI/Content/Messenger/messenger.css index b5d3dbd7a..e413d636f 100644 --- a/UI/Content/Messenger/messenger.css +++ b/UI/Content/Messenger/messenger.css @@ -81,7 +81,8 @@ ul.messenger.messenger-fixed.messenger-on-top.messenger-on-left, ul.messenger.me } /* line 66, ../../src/sass/messenger.sass */ ul.messenger.messenger-fixed.messenger-on-right, ul.messenger.messenger-fixed.messenger-on-left { - width: 350px; + width: auto; + min-width: 350px; } /* line 69, ../../src/sass/messenger.sass */ ul.messenger.messenger-fixed.messenger-on-right .messenger-actions, ul.messenger.messenger-fixed.messenger-on-left .messenger-actions { diff --git a/UI/Instrumentation/ErrorHandler.js b/UI/Instrumentation/ErrorHandler.js new file mode 100644 index 000000000..7e5da2e05 --- /dev/null +++ b/UI/Instrumentation/ErrorHandler.js @@ -0,0 +1,74 @@ +"use strict"; +define(function () { + + /* var model = new NzbDrone.Shared.NotificationModel(); + model.set('title','test notification'); + model.set('message','test message'); + model.set('level', 'error'); + this.push(model); + */ + + window.alert = function (message) { + window.Messenger().post(message); + }; + + + var self = this; + + window.onerror = function (msg, url, line) { + + try { + + var a = document.createElement('a'); + a.href = url; + var messageText = a.pathname.split('/').pop() + ' : ' + line + "
" + msg; + + var message = { + message : messageText, + type : 'error', + hideAfter: 1000, + showCloseButton: true + }; + + window.Messenger().post(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. + }; + + $(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) { + message.message = 'NzbDrone Server Not Reachable. make sure NzbDrone is running.'; + } else { + message.message = ajaxOptions.type + " " + ajaxOptions.url + " : " + xmlHttpRequest.statusText; + } + + window.Messenger().post(message); + alert('test'); + return false; + }); + +}); + diff --git a/UI/Shared/NotificationCollection.js b/UI/Shared/NotificationCollection.js index 6ceb5c035..95e6ffdeb 100644 --- a/UI/Shared/NotificationCollection.js +++ b/UI/Shared/NotificationCollection.js @@ -2,74 +2,7 @@ define(['app', 'Shared/NotificationModel'], function () { var notificationCollection = Backbone.Collection.extend({ - - model: NzbDrone.Shared.NotificationModel, - - initialize: function () { - - /* var model = new NzbDrone.Shared.NotificationModel(); - model.set('title','test notification'); - model.set('message','test message'); - model.set('level', 'error'); - this.push(model); - */ - - window.alert = function (message) { - window.Messenger().post(message); - }; - - - var self = this; - - window.onerror = function (msg, url, line) { - - try { - var model = new NzbDrone.Shared.NotificationModel(); - - var a = document.createElement('a'); - a.href = url; - - model.set('title', a.pathname.split('/').pop() + ' : ' + line); - model.set('message', msg); - model.set('level', 'error'); - self.push(model); - } 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. - }; - - $(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 model = new NzbDrone.Shared.NotificationModel(); - model.set('level', 'error'); - - if (xmlHttpRequest.status === 0 && xmlHttpRequest.readyState === 0) { - model.set('title', "Connection Failed"); - model.set('message', "NzbDrone Server Not Reachable. make sure NzbDrone is running."); - } else { - model.set('title', ajaxOptions.type + " " + ajaxOptions.url + " : " + xmlHttpRequest.statusText); - model.set('message', xmlHttpRequest.responseText); - } - - self.push(model); - return false; - }); - } + model: NzbDrone.Shared.NotificationModel }); return new notificationCollection(); diff --git a/UI/app.js b/UI/app.js index 6f82a28e4..f8f806534 100644 --- a/UI/app.js +++ b/UI/app.js @@ -26,7 +26,7 @@ require.config({ } }); -define('app', function () { +define('app', ['Instrumentation/ErrorHandler'], function () { window.NzbDrone = new Backbone.Marionette.Application(); window.NzbDrone.Config = {}; @@ -63,11 +63,11 @@ define('app', function () { //TODO: Move to commands OpenModalDialog : 'openModal', CloseModalDialog: 'closeModal', - SeriesAdded: 'seriesAdded' + SeriesAdded : 'seriesAdded' }; window.NzbDrone.Commands = { - SaveSettings : 'saveSettings' + SaveSettings: 'saveSettings' }; window.NzbDrone.Constants = {