You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/UI/AddSeries/AddSeriesView.js

175 lines
5.7 KiB

9 years ago
'use strict';
11 years ago
define(
[
'underscore',
'vent',
11 years ago
'marionette',
'AddSeries/AddSeriesCollection',
11 years ago
'AddSeries/SearchResultCollectionView',
'AddSeries/EmptyView',
'AddSeries/NotFoundView',
'AddSeries/ErrorView',
'Shared/LoadingView'
], function (_, vent, Marionette, AddSeriesCollection, SearchResultCollectionView, EmptyView, NotFoundView, ErrorView, LoadingView) {
11 years ago
return Marionette.Layout.extend({
template: 'AddSeries/AddSeriesViewTemplate',
11 years ago
regions: {
searchResult: '#search-result'
},
11 years ago
ui: {
seriesSearch: '.x-series-search',
searchBar : '.x-search-bar',
loadMore : '.x-load-more'
11 years ago
},
events: {
'click .x-load-more': '_onLoadMore'
11 years ago
},
initialize: function (options) {
this.isExisting = options.isExisting;
this.collection = new AddSeriesCollection();
if (this.isExisting) {
this.collection.unmappedFolderModel = this.model;
}
if (this.isExisting) {
this.className = 'existing-series';
}
else {
this.className = 'new-series';
}
this.listenTo(vent, vent.Events.SeriesAdded, this._onSeriesAdded);
this.listenTo(this.collection, 'sync', this._showResults);
this.resultCollectionView = new SearchResultCollectionView({
collection: this.collection,
isExisting: this.isExisting
});
this.throttledSearch = _.debounce(this.search, 1000, {trailing: true}).bind(this);
},
11 years ago
onRender: function () {
var self = this;
this.$el.addClass(this.className);
this.ui.seriesSearch.keyup(function (e) {
//Ignore special keys: http://www.javascripter.net/faq/keycodes.htm
if (_.contains([9, 16, 17, 18, 19, 20, 33, 34, 35, 36, 37, 38, 39, 40, 91, 92, 93 ], e.keyCode)) {
return;
}
self._abortExistingSearch();
self.throttledSearch({
term: self.ui.seriesSearch.val()
});
});
this._clearResults();
if (this.isExisting) {
this.ui.searchBar.hide();
}
},
onShow: function () {
this.ui.seriesSearch.focus();
11 years ago
},
search: function (options) {
var self = this;
this.collection.reset();
if (!options.term || options.term === this.collection.term) {
11 years ago
return Marionette.$.Deferred().resolve();
11 years ago
}
this.searchResult.show(new LoadingView());
this.collection.term = options.term;
this.currentSearchPromise = this.collection.fetch({
data: { term: options.term }
});
this.currentSearchPromise.fail(function () {
self._showError();
});
return this.currentSearchPromise;
11 years ago
},
_onSeriesAdded: function (options) {
if (this.isExisting && options.series.get('path') === this.model.get('folder').path) {
this.close();
}
else if (!this.isExisting) {
this.collection.term = '';
this.collection.reset();
this._clearResults();
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();
}
},
_clearResults: function () {
if (!this.isExisting) {
this.searchResult.show(new EmptyView());
}
else {
this.searchResult.close();
}
},
_showResults: function () {
if (!this.isClosed) {
if (this.collection.length === 0) {
this.ui.searchBar.show();
this.searchResult.show(new NotFoundView({term: this.collection.term}));
}
else {
this.searchResult.show(this.resultCollectionView);
if (!this.showingAll && this.isExisting) {
this.ui.loadMore.show();
}
}
}
},
_abortExistingSearch: function () {
if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) {
11 years ago
console.log('aborting previous pending search request.');
this.currentSearchPromise.abort();
11 years ago
}
else {
this._clearResults();
}
},
_showError: function () {
if (!this.isClosed) {
this.ui.searchBar.show();
this.searchResult.show(new ErrorView({term: this.collection.term}));
this.collection.term = '';
}
}
11 years ago
});
});