From d76e3d2ed6260fe1ab1afcd49220683b57f9249e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 14 Aug 2014 23:06:17 -0700 Subject: [PATCH] Fixed: Cancelling editing a modal will reset to previous saved state --- src/UI/Content/theme.less | 6 + src/UI/Mixins/AsEditModalView.js | 107 ++++++++++++++++++ src/UI/Mixins/AsModelBoundView.js | 4 +- src/UI/Profile/ProfileModel.js | 2 +- src/UI/Series/Edit/EditSeriesView.js | 26 ++--- .../Series/Edit/EditSeriesViewTemplate.html | 5 +- .../Edit/DownloadClientEditView.js | 70 +++--------- .../Settings/Indexers/Edit/IndexerEditView.js | 85 +++----------- .../Edit/IndexerEditViewTemplate.html | 4 +- src/UI/Settings/Metadata/MetadataEditView.js | 31 +---- .../Metadata/MetadataEditViewTemplate.html | 2 +- .../Edit/NotificationEditView.js | 75 +++--------- .../Edit/NotificationEditViewTemplate.html | 4 +- .../Profile/Edit/EditProfileLayout.js | 63 ++++------- .../Edit/EditProfileLayoutTemplate.html | 5 +- src/UI/Shared/Modal/ModalRegion.js | 12 +- 16 files changed, 216 insertions(+), 285 deletions(-) create mode 100644 src/UI/Mixins/AsEditModalView.js diff --git a/src/UI/Content/theme.less b/src/UI/Content/theme.less index 7a45d851a..e71daf82e 100644 --- a/src/UI/Content/theme.less +++ b/src/UI/Content/theme.less @@ -247,3 +247,9 @@ body { margin-bottom : 0px; } } + +.twitter-typeahead { + .form-control[disabled] { + background-color: #ffffff; + } +} diff --git a/src/UI/Mixins/AsEditModalView.js b/src/UI/Mixins/AsEditModalView.js new file mode 100644 index 000000000..97b26be49 --- /dev/null +++ b/src/UI/Mixins/AsEditModalView.js @@ -0,0 +1,107 @@ +'use strict'; + +define( + ['AppLayout'], + function (AppLayout) { + + return function () { + + var originalInitialize = this.prototype.initialize; + var originalOnBeforeClose = this.prototype.onBeforeClose; + + var saveInternal = function () { + var self = this; + this.ui.indicator.show(); + + if (this._onBeforeSave) { + this._onBeforeSave.call(this); + } + + var promise = this.model.save(); + + promise.always(function () { + self.ui.indicator.hide(); + }); + + promise.done(function () { + self.originalModelData = self.model.toJSON(); + }); + + return promise; + }; + + this.prototype.initialize = function (options) { + + if (!this.model) { + throw 'View has no model'; + } + + this.originalModelData = this.model.toJSON(); + + this.events = this.events || {}; + this.events['click .x-save'] = '_save'; + this.events['click .x-save-and-add'] = '_saveAndAdd'; + this.events['click .x-test'] = '_test'; + this.events['click .x-delete'] = '_delete'; + + this.ui = this.ui || {}; + this.ui.indicator = '.x-indicator'; + + if (originalInitialize) { + originalInitialize.call(this, options); + } + }; + + this.prototype._save = function () { + + var self = this; + var promise = saveInternal.call(this); + + promise.done(function () { + if (self._onAfterSave) { + self._onAfterSave.call(self); + } + + self.originalModelData = self.model.toJSON(); + }); + }; + + this.prototype._saveAndAdd = function () { + + var self = this; + var promise = saveInternal.call(this); + + promise.done(function () { + if (self._onAfterSaveAndAdd) { + self._onAfterSaveAndAdd.call(self); + } + }); + }; + + this.prototype._test = function () { + var self = this; + + this.ui.indicator.show(); + + this.model.test().always(function () { + self.ui.indicator.hide(); + }); + }; + + this.prototype._delete = function () { + var view = new this._deleteView({ model: this.model }); + AppLayout.modalRegion.show(view); + }; + + this.prototype.onBeforeClose = function () { + this.model.set(this.originalModelData); + + if (originalOnBeforeClose) { + originalOnBeforeClose.call(this); + } + }; + + return this; + }; + } +); diff --git a/src/UI/Mixins/AsModelBoundView.js b/src/UI/Mixins/AsModelBoundView.js index 0095370e0..831f6b259 100644 --- a/src/UI/Mixins/AsModelBoundView.js +++ b/src/UI/Mixins/AsModelBoundView.js @@ -6,8 +6,8 @@ define( return function () { - var originalOnRender = this.prototype.onRender, - originalBeforeClose = this.prototype.onBeforeClose; + var originalOnRender = this.prototype.onRender; + var originalBeforeClose = this.prototype.onBeforeClose; this.prototype.onRender = function () { diff --git a/src/UI/Profile/ProfileModel.js b/src/UI/Profile/ProfileModel.js index 629512d52..63368fb74 100644 --- a/src/UI/Profile/ProfileModel.js +++ b/src/UI/Profile/ProfileModel.js @@ -3,7 +3,7 @@ define( [ 'backbone.deepmodel' ], function (DeepModel) { - return DeepModel.DeepModel.extend({ + return DeepModel.DeepModel.extend({ defaults: { id : null, name : '', diff --git a/src/UI/Series/Edit/EditSeriesView.js b/src/UI/Series/Edit/EditSeriesView.js index 18522f6bd..d4695c763 100644 --- a/src/UI/Series/Edit/EditSeriesView.js +++ b/src/UI/Series/Edit/EditSeriesView.js @@ -6,19 +6,19 @@ define( 'Profile/ProfileCollection', 'Mixins/AsModelBoundView', 'Mixins/AsValidatedView', + 'Mixins/AsEditModalView', 'Mixins/AutoComplete' - ], function (vent, Marionette, Profiles, AsModelBoundView, AsValidatedView) { + ], function (vent, Marionette, Profiles, AsModelBoundView, AsValidatedView, AsEditModalView) { var view = Marionette.ItemView.extend({ template: 'Series/Edit/EditSeriesViewTemplate', ui: { - profile: '.x-profile', - path : '.x-path' + profile : '.x-profile', + path : '.x-path' }, events: { - 'click .x-save' : '_saveSeries', 'click .x-remove': '_removeSeries' }, @@ -27,16 +27,14 @@ define( this.model.set('profiles', Profiles); }, - _saveSeries: function () { - - var self = this; + _onBeforeSave: function () { var profileId = this.ui.profile.val(); this.model.set({ profileId: profileId}); + }, - this.model.save().done(function () { - self.trigger('saved'); - vent.trigger(vent.Commands.CloseModalCommand); - }); + _onAfterSave: function () { + this.trigger('saved'); + vent.trigger(vent.Commands.CloseModalCommand); }, onRender: function () { @@ -48,7 +46,9 @@ define( } }); + AsModelBoundView.call(view); + AsValidatedView.call(view); + AsEditModalView.call(view); - AsModelBoundView.apply(view); - return AsValidatedView.apply(view); + return view; }); diff --git a/src/UI/Series/Edit/EditSeriesViewTemplate.html b/src/UI/Series/Edit/EditSeriesViewTemplate.html index 812206918..70933be3e 100644 --- a/src/UI/Series/Edit/EditSeriesViewTemplate.html +++ b/src/UI/Series/Edit/EditSeriesViewTemplate.html @@ -6,8 +6,7 @@