From 927dbba945cc2d06832c1141646d053731c6fa92 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 17 Sep 2013 22:24:27 -0700 Subject: [PATCH] add series cleanup --- UI/AddSeries/AddSeriesView.js | 78 +++++++++++++++++++---------------- UI/AddSeries/Collection.js | 5 --- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/UI/AddSeries/AddSeriesView.js b/UI/AddSeries/AddSeriesView.js index 324ac0f90..6e3b832f9 100644 --- a/UI/AddSeries/AddSeriesView.js +++ b/UI/AddSeries/AddSeriesView.js @@ -7,7 +7,8 @@ define( 'AddSeries/SearchResultCollectionView', 'AddSeries/NotFoundView', 'Shared/LoadingView', - ], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView) { + 'underscore' + ], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView, _) { return Marionette.Layout.extend({ template: 'AddSeries/AddSeriesTemplate', @@ -25,18 +26,13 @@ define( 'click .x-load-more': '_onLoadMore' }, - _onLoadMore: function () { - var showingAll = this.resultCollectionView.showMore(); - - if (showingAll) { - this.ui.loadMore.hide(); - this.ui.searchBar.show(); - } - }, - initialize: function (options) { - this.collection = new AddSeriesCollection({unmappedFolderModel: this.model}); this.isExisting = options.isExisting; + this.collection = new AddSeriesCollection(); + + if (this.isExisting) { + this.collection.unmappedFolderModel = this.model; + } if (this.isExisting) { this.className = 'existing-series'; @@ -47,55 +43,65 @@ define( } this.listenTo(this.collection, 'sync', this._showResults); + + this.resultCollectionView = new SearchResultCollectionView({ + collection: this.collection, + isExisting: this.isExisting + }); + + this.throttledSearch = _.throttle(this.search, 1000, {trailing: true}).bind(this); }, _onSeriesAdded: function (options) { - if (options.series.get('path') === this.model.get('folder').path) { + if (this.isExisting && options.series.get('path') === this.model.get('folder').path) { this.close(); } }, + _onLoadMore: function () { + var showingAll = this.resultCollectionView.showMore(); + + if (showingAll) { + this.ui.loadMore.hide(); + this.ui.searchBar.show(); + } + }, + onRender: function () { var self = this; this.$el.addClass(this.className); - this.ui.seriesSearch.data('timeout', null).keyup(function () { - window.clearTimeout(self.$el.data('timeout')); - self.$el.data('timeout', window.setTimeout(function () { - self.search.call(self, { - term: self.ui.seriesSearch.val() - }); - }, 500)); + this.ui.seriesSearch.keyup(function () { + self.searchResult.close(); + self._abortExistingSearch(); + self.throttledSearch({ + term: self.ui.seriesSearch.val() + }) }); if (this.isExisting) { this.ui.searchBar.hide(); } + }, - this.resultCollectionView = new SearchResultCollectionView({ - collection: this.collection, - isExisting: this.isExisting - }); + onShow: function () { + this.searchResult.show(this.resultCollectionView); }, search: function (options) { - this.abortExistingSearch(); - this.collection.reset(); - if (!options || options.term === '') { - this.searchResult.close(); + if (!options.term || options.term === this.collection.term) { + return; } - else { - this.searchResult.show(new LoadingView()); - this.collection.term = options.term; - this.currentSearchPromise = this.collection.fetch({ - data: { term: options.term } - }); - } - return this.currentSearchPromise; + + this.searchResult.show(new LoadingView()); + this.collection.term = options.term; + this.currentSearchPromise = this.collection.fetch({ + data: { term: options.term } + }); }, _showResults: function () { @@ -113,7 +119,7 @@ define( } }, - abortExistingSearch: function () { + _abortExistingSearch: function () { if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) { console.log('aborting previous pending search request.'); this.currentSearchPromise.abort(); diff --git a/UI/AddSeries/Collection.js b/UI/AddSeries/Collection.js index 0dce7bc1e..c44acf9a2 100644 --- a/UI/AddSeries/Collection.js +++ b/UI/AddSeries/Collection.js @@ -8,11 +8,6 @@ define( url : window.NzbDrone.ApiRoot + '/series/lookup', model: SeriesModel, - initialize: function (options) { - this.unmappedFolderModel = options.unmappedFolderModel; - }, - - parse: function (response) { var self = this;