1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-18 23:48:35 +02:00
Sonarr/src/UI/AddSeries/AddSeriesView.js

140 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
2013-06-21 04:43:58 +03:00
define(
[
2013-10-09 04:43:41 +03:00
'vent',
2013-06-21 04:43:58 +03:00
'marionette',
'AddSeries/AddSeriesCollection',
2013-06-21 04:43:58 +03:00
'AddSeries/SearchResultCollectionView',
'AddSeries/NotFoundView',
'Shared/LoadingView',
2013-09-18 08:24:27 +03:00
'underscore'
2013-10-09 04:43:41 +03:00
], function (vent, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView, _) {
2013-06-21 04:43:58 +03:00
return Marionette.Layout.extend({
template: 'AddSeries/AddSeriesViewTemplate',
2013-06-21 04:43:58 +03:00
2013-06-30 22:57:26 +03:00
regions: {
searchResult: '#search-result'
},
2013-06-21 04:43:58 +03:00
ui: {
seriesSearch: '.x-series-search',
searchBar : '.x-search-bar',
loadMore : '.x-load-more'
2013-06-21 04:43:58 +03:00
},
2013-06-30 22:57:26 +03:00
events: {
'click .x-load-more': '_onLoadMore'
2013-06-21 04:43:58 +03:00
},
initialize: function (options) {
this.isExisting = options.isExisting;
2013-09-18 08:24:27 +03:00
this.collection = new AddSeriesCollection();
if (this.isExisting) {
this.collection.unmappedFolderModel = this.model;
}
2013-06-30 22:57:26 +03:00
if (this.isExisting) {
this.className = 'existing-series';
}
else {
this.className = 'new-series';
}
2013-10-09 04:43:41 +03:00
this.listenTo(vent, vent.Events.SeriesAdded, this._onSeriesAdded);
this.listenTo(this.collection, 'sync', this._showResults);
2013-09-18 08:24:27 +03:00
this.resultCollectionView = new SearchResultCollectionView({
collection: this.collection,
isExisting: this.isExisting
});
this.throttledSearch = _.debounce(this.search, 1000, {trailing: true}).bind(this);
2013-06-30 22:57:26 +03:00
},
2013-06-21 04:43:58 +03:00
onRender: function () {
var self = this;
2013-06-30 22:57:26 +03:00
this.$el.addClass(this.className);
2013-09-18 08:24:27 +03:00
this.ui.seriesSearch.keyup(function () {
self.searchResult.close();
self._abortExistingSearch();
self.throttledSearch({
term: self.ui.seriesSearch.val()
});
});
if (this.isExisting) {
this.ui.searchBar.hide();
}
2013-09-18 08:24:27 +03:00
},
2013-09-18 08:24:27 +03:00
onShow: function () {
this.searchResult.show(this.resultCollectionView);
this.ui.seriesSearch.focus();
2013-06-21 04:43:58 +03:00
},
2013-01-27 05:14:42 +03:00
search: function (options) {
this.collection.reset();
2013-02-01 06:15:19 +03:00
2013-09-18 08:24:27 +03:00
if (!options.term || options.term === this.collection.term) {
2013-09-18 22:43:37 +03:00
return $.Deferred().resolve();
2013-06-21 04:43:58 +03:00
}
2013-09-18 08:24:27 +03:00
this.searchResult.show(new LoadingView());
this.collection.term = options.term;
this.currentSearchPromise = this.collection.fetch({
data: { term: options.term }
});
2013-09-18 22:43:37 +03:00
return this.currentSearchPromise;
2013-06-21 04:43:58 +03:00
},
_onSeriesAdded: function (options) {
if (this.isExisting && options.series.get('path') === this.model.get('folder').path) {
this.close();
}
else if (!this.isExisting) {
this.collection.reset();
2013-10-02 18:16:41 +03:00
this.searchResult.close();
this.ui.seriesSearch.val('');
this.ui.seriesSearch.focus();
}
},
_onLoadMore: function () {
var showingAll = this.resultCollectionView.showMore();
this.ui.searchBar.show();
if (showingAll) {
this.ui.loadMore.hide();
}
},
_showResults: function () {
if (!this.isClosed) {
if (this.collection.length === 0) {
this.searchResult.show(new NotFoundView({term: this.collection.term}));
}
else {
this.searchResult.show(this.resultCollectionView);
if (!this.showingAll && this.isExisting) {
this.ui.loadMore.show();
}
}
}
},
2013-09-18 08:24:27 +03:00
_abortExistingSearch: function () {
2013-06-30 22:57:26 +03:00
if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) {
2013-06-21 04:43:58 +03:00
console.log('aborting previous pending search request.');
2013-06-30 22:57:26 +03:00
this.currentSearchPromise.abort();
2013-06-21 04:43:58 +03:00
}
}
2013-06-21 04:43:58 +03:00
});
});