sonarr-repo-only/UI/AddSeries/AddSeriesView.js

114 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
2013-06-21 01:43:58 +00:00
define(
[
2013-06-30 19:57:26 +00:00
'app',
2013-06-21 01:43:58 +00:00
'marionette',
'AddSeries/Collection',
'AddSeries/SearchResultCollectionView',
'Shared/SpinnerView'
2013-06-30 19:57:26 +00:00
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, SpinnerView) {
2013-06-21 01:43:58 +00:00
return Marionette.Layout.extend({
template: 'AddSeries/AddSeriesTemplate',
2013-06-30 19:57:26 +00:00
regions: {
searchResult: '#search-result'
},
2013-06-21 01:43:58 +00:00
ui: {
seriesSearch: '.x-series-search',
searchBar : '.x-search-bar',
loadMore : '.x-load-more'
2013-06-21 01:43:58 +00:00
},
2013-06-30 19:57:26 +00:00
events: {
'click .x-load-more': '_onLoadMore'
2013-06-21 01:43:58 +00:00
},
2013-06-30 19:57:26 +00:00
_onLoadMore: function () {
var showingAll = this.resultCollectionView.showMore();
2013-06-30 19:57:26 +00:00
if (showingAll) {
this.ui.loadMore.hide();
this.ui.searchBar.show();
}
},
2013-06-30 19:57:26 +00:00
initialize: function (options) {
2013-06-30 19:57:26 +00:00
this.collection = new AddSeriesCollection({unmappedFolderModel: this.model});
this.isExisting = options.isExisting;
2013-06-30 19:57:26 +00:00
if (this.isExisting) {
this.className = 'existing-series';
this.listenTo(App.vent, App.Events.SeriesAdded, this._onSeriesAdded);
}
else {
this.className = 'new-series';
}
},
_onSeriesAdded: function (options) {
if (options.series.get('path') === this.model.get('folder').path) {
this.close();
}
2013-06-21 01:43:58 +00:00
},
onRender: function () {
var self = this;
2013-06-30 19:57:26 +00:00
this.$el.addClass(this.className);
2013-06-21 01:43:58 +00:00
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));
});
if (this.isExisting) {
this.ui.searchBar.hide();
}
2013-06-30 19:57:26 +00:00
this.resultCollectionView = new SearchResultCollectionView({
collection: this.collection,
isExisting: this.isExisting
});
2013-06-21 01:43:58 +00:00
},
2013-01-27 02:14:42 +00:00
search: function (options) {
var self = this;
2013-01-27 02:14:42 +00:00
2013-06-30 19:57:26 +00:00
this.abortExistingSearch();
this.collection.reset();
2013-02-01 03:15:19 +00:00
if (!options || options.term === '') {
this.searchResult.close();
2013-06-21 01:43:58 +00:00
}
else {
this.searchResult.show(new SpinnerView());
2013-06-30 19:57:26 +00:00
this.currentSearchPromise = this.collection.fetch({
data: { term: options.term }
}).done(function () {
2013-06-30 19:57:26 +00:00
if (!self.isClosed) {
self.searchResult.show(self.resultCollectionView);
if (!self.showingAll && self.isExisting) {
self.ui.loadMore.show();
}
}
});
2013-06-21 01:43:58 +00:00
}
2013-06-30 19:57:26 +00:00
return this.currentSearchPromise;
2013-06-21 01:43:58 +00:00
},
2013-06-30 19:57:26 +00:00
abortExistingSearch: function () {
if (this.currentSearchPromise && this.currentSearchPromise.readyState > 0 && this.currentSearchPromise.readyState < 4) {
2013-06-21 01:43:58 +00:00
console.log('aborting previous pending search request.');
2013-06-30 19:57:26 +00:00
this.currentSearchPromise.abort();
2013-06-21 01:43:58 +00:00
}
}
2013-06-21 01:43:58 +00:00
});
});