1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/UI/AddSeries/AddSeriesView.js

114 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
2013-06-21 04:43:58 +03:00
define(
[
2013-06-30 22:57:26 +03:00
'app',
2013-06-21 04:43:58 +03:00
'marionette',
'AddSeries/Collection',
'AddSeries/SearchResultCollectionView',
'Shared/SpinnerView'
2013-06-30 22:57:26 +03:00
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, SpinnerView) {
2013-06-21 04:43:58 +03:00
return Marionette.Layout.extend({
template: 'AddSeries/AddSeriesTemplate',
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
},
2013-06-30 22:57:26 +03:00
_onLoadMore: function () {
var showingAll = this.resultCollectionView.showMore();
2013-06-30 22:57:26 +03:00
if (showingAll) {
this.ui.loadMore.hide();
this.ui.searchBar.show();
}
},
2013-06-30 22:57:26 +03:00
initialize: function (options) {
2013-06-30 22:57:26 +03:00
this.collection = new AddSeriesCollection({unmappedFolderModel: this.model});
this.isExisting = options.isExisting;
2013-06-30 22:57:26 +03: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 04:43:58 +03:00
},
onRender: function () {
var self = this;
2013-06-30 22:57:26 +03:00
this.$el.addClass(this.className);
2013-06-21 04:43:58 +03: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 22:57:26 +03:00
this.resultCollectionView = new SearchResultCollectionView({
collection: this.collection,
isExisting: this.isExisting
});
2013-06-21 04:43:58 +03:00
},
2013-01-27 05:14:42 +03:00
search: function (options) {
var self = this;
2013-01-27 05:14:42 +03:00
2013-06-30 22:57:26 +03:00
this.abortExistingSearch();
this.collection.reset();
2013-02-01 06:15:19 +03:00
if (!options || options.term === '') {
this.searchResult.close();
2013-06-21 04:43:58 +03:00
}
else {
this.searchResult.show(new SpinnerView());
2013-06-30 22:57:26 +03:00
this.currentSearchPromise = this.collection.fetch({
data: { term: options.term }
}).done(function () {
2013-06-30 22:57:26 +03:00
if (!self.isClosed) {
self.searchResult.show(self.resultCollectionView);
if (!self.showingAll && self.isExisting) {
self.ui.loadMore.show();
}
}
});
2013-06-21 04:43:58 +03:00
}
2013-06-30 22:57:26 +03:00
return this.currentSearchPromise;
2013-06-21 04:43:58 +03:00
},
2013-06-30 22:57:26 +03:00
abortExistingSearch: function () {
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
});
});