From 164a469e9dd38e2801721e527d2d0e28df856365 Mon Sep 17 00:00:00 2001 From: Peter Fogg Date: Mon, 24 Jun 2013 13:07:09 -0400 Subject: [PATCH] Factor `showNotificationBar` out into ValidatingView. When all settings require an explicit save, this functionality will be shared between each view (with slight changes, e.g. click handlers.) --- cms/static/js/views/settings/advanced_view.js | 42 ++++--------------- cms/static/js/views/validating_view.js | 34 +++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/cms/static/js/views/settings/advanced_view.js b/cms/static/js/views/settings/advanced_view.js index 302a918de1..7be45959a6 100644 --- a/cms/static/js/views/settings/advanced_view.js +++ b/cms/static/js/views/settings/advanced_view.js @@ -57,8 +57,14 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ mode: "application/json", lineNumbers: false, lineWrapping: false, onChange: function(instance, changeobj) { // this event's being called even when there's no change :-( - if (instance.getValue() !== oldValue && !self.notificationBarShowing) { - self.showNotificationBar(); + if (instance.getValue() !== oldValue) { + var message = gettext("Your changes will not take effect until you save your progress. Take care with key and value formatting, as validation is not implemented."); + self.showNotificationBar(message, + _.bind(self.saveView, self), + _.bind(self.revertView, self)); + if(self.saved) { + self.saved.hide(); + } } }, onFocus : function(mirror) { @@ -97,38 +103,6 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ } }); }, - showNotificationBar: function() { - var self = this; - var message = gettext("Your changes will not take effect until you save your progress. Take care with key and value formatting, as validation is not implemented.") - var confirm = new CMS.Views.Notification.Warning({ - title: gettext("You've Made Some Changes"), - message: message, - actions: { - primary: { - "text": gettext("Save Changes"), - "class": "action-save", - "click": function() { - self.saveView(); - confirm.hide(); - self.notificationBarShowing = false; - } - }, - secondary: [{ - "text": gettext("Cancel"), - "class": "action-cancel", - "click": function() { - self.revertView(); - confirm.hide(); - self.notificationBarShowing = false; - } - }] - }}); - this.notificationBarShowing = true; - confirm.show(); - if(this.saved) { - this.saved.hide(); - } - }, saveView : function() { // TODO one last verification scan: // call validateKey on each to ensure proper format diff --git a/cms/static/js/views/validating_view.js b/cms/static/js/views/validating_view.js index afb355ad4a..b3f78c2a87 100644 --- a/cms/static/js/views/validating_view.js +++ b/cms/static/js/views/validating_view.js @@ -67,5 +67,39 @@ CMS.Views.ValidatingView = Backbone.View.extend({ // put error on the contained inputs return $(ele).find(inputElements); } + }, + + showNotificationBar: function(message, primaryClick, secondaryClick) { + if(this.notificationBarShowing) { + return; + } + var self = this; + this.confirmation = new CMS.Views.Notification.Warning({ + title: gettext("You've made some changes"), + message: message, + actions: { + primary: { + "text": gettext("Save Changes"), + "class": "action-save", + "click": function() { + primaryClick(); + self.confirmation.hide(); + self.notificationBarShowing = false; + } + }, + secondary: [{ + "text": gettext("Cancel"), + "class": "action-cancel", + "click": function() { + if(secondaryClick) { + secondaryClick(); + } + self.confirmation.hide(); + self.notificationBarShowing = false; + } + }] + }}); + this.notificationBarShowing = true; + this.confirmation.show(); } });