2013-06-29 02:06:16 +03:00
|
|
|
'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',
|
2013-09-18 03:54:57 +03:00
|
|
|
'AddSeries/NotFoundView',
|
|
|
|
'Shared/LoadingView',
|
2013-09-18 08:24:27 +03:00
|
|
|
'underscore'
|
|
|
|
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView, _) {
|
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: {
|
2013-06-22 03:48:23 +03:00
|
|
|
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-22 03:48:23 +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';
|
|
|
|
this.listenTo(App.vent, App.Events.SeriesAdded, this._onSeriesAdded);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.className = 'new-series';
|
|
|
|
}
|
2013-09-18 03:54:57 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2013-09-18 09:03:09 +03:00
|
|
|
this.throttledSearch = _.debounce(this.search, 1000, {trailing: true}).bind(this);
|
2013-06-30 22:57:26 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onSeriesAdded: function (options) {
|
2013-09-18 08:24:27 +03:00
|
|
|
if (this.isExisting && options.series.get('path') === this.model.get('folder').path) {
|
2013-06-30 22:57:26 +03:00
|
|
|
this.close();
|
|
|
|
}
|
2013-06-21 04:43:58 +03:00
|
|
|
},
|
|
|
|
|
2013-09-18 08:24:27 +03:00
|
|
|
_onLoadMore: function () {
|
|
|
|
var showingAll = this.resultCollectionView.showMore();
|
2013-09-18 22:43:37 +03:00
|
|
|
this.ui.searchBar.show();
|
2013-09-18 08:24:27 +03:00
|
|
|
|
|
|
|
if (showingAll) {
|
|
|
|
this.ui.loadMore.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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()
|
|
|
|
})
|
2013-02-14 05:28:56 +03:00
|
|
|
});
|
2013-01-23 08:48:22 +03:00
|
|
|
|
2013-06-22 03:48:23 +03:00
|
|
|
if (this.isExisting) {
|
|
|
|
this.ui.searchBar.hide();
|
|
|
|
}
|
2013-09-18 08:24:27 +03:00
|
|
|
},
|
2013-06-22 03:48:23 +03:00
|
|
|
|
2013-09-18 08:24:27 +03:00
|
|
|
onShow: function () {
|
|
|
|
this.searchResult.show(this.resultCollectionView);
|
2013-06-21 04:43:58 +03:00
|
|
|
},
|
2013-01-27 05:14:42 +03:00
|
|
|
|
2013-06-22 03:48:23 +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
|
|
|
},
|
2013-02-14 05:28:56 +03:00
|
|
|
|
2013-09-18 03:54:57 +03:00
|
|
|
_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-02-14 05:28:56 +03:00
|
|
|
}
|
2013-06-21 04:43:58 +03:00
|
|
|
});
|
2013-02-14 05:28:56 +03:00
|
|
|
});
|