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',
|
|
|
|
'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: {
|
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-30 22:57:26 +03:00
|
|
|
_onLoadMore: function () {
|
|
|
|
this.ui.loadMore.hide();
|
|
|
|
this.ui.searchBar.show();
|
|
|
|
this.resultCollectionView.showAll();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2013-06-22 03:48:23 +03:00
|
|
|
initialize: function (options) {
|
2013-06-30 22:57:26 +03:00
|
|
|
this.collection = new AddSeriesCollection({unmappedFolderModel: this.model});
|
2013-06-22 03:48:23 +03:00
|
|
|
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 () {
|
2013-02-14 05:28:56 +03:00
|
|
|
window.clearTimeout(self.$el.data('timeout'));
|
2013-06-22 03:48:23 +03:00
|
|
|
self.$el.data('timeout', window.setTimeout(function () {
|
|
|
|
self.search.call(self, {
|
|
|
|
term: self.ui.seriesSearch.val()
|
|
|
|
});
|
|
|
|
}, 500));
|
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-06-30 22:57:26 +03:00
|
|
|
this.resultCollectionView = new SearchResultCollectionView({
|
|
|
|
collection: this.collection,
|
2013-06-22 03:48:23 +03:00
|
|
|
isExisting: this.isExisting
|
|
|
|
});
|
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) {
|
|
|
|
|
|
|
|
var self = this;
|
2013-01-27 05:14:42 +03:00
|
|
|
|
2013-06-30 22:57:26 +03:00
|
|
|
this.abortExistingSearch();
|
2013-02-14 05:28:56 +03:00
|
|
|
|
2013-06-22 03:48:23 +03:00
|
|
|
this.collection.reset();
|
2013-02-01 06:15:19 +03:00
|
|
|
|
2013-06-22 03:48:23 +03:00
|
|
|
if (!options || options.term === '') {
|
|
|
|
this.searchResult.close();
|
2013-06-21 04:43:58 +03:00
|
|
|
}
|
|
|
|
else {
|
2013-06-22 03:48:23 +03:00
|
|
|
this.searchResult.show(new SpinnerView());
|
2013-06-30 22:57:26 +03:00
|
|
|
this.currentSearchPromise = this.collection.fetch({
|
2013-06-22 03:48:23 +03:00
|
|
|
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-22 03:48:23 +03:00
|
|
|
}
|
|
|
|
});
|
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-02-14 05:28:56 +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-02-14 05:28:56 +03:00
|
|
|
}
|
2013-06-21 04:43:58 +03:00
|
|
|
});
|
2013-02-14 05:28:56 +03:00
|
|
|
});
|