2013-06-28 23:06:16 +00:00
|
|
|
|
'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',
|
2013-09-18 00:54:57 +00:00
|
|
|
|
'AddSeries/NotFoundView',
|
|
|
|
|
'Shared/LoadingView',
|
|
|
|
|
], function (App, Marionette, AddSeriesCollection, SearchResultCollectionView, NotFoundView, LoadingView) {
|
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: {
|
2013-06-22 00:48:23 +00:00
|
|
|
|
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 () {
|
2013-08-09 03:58:44 +00:00
|
|
|
|
var showingAll = this.resultCollectionView.showMore();
|
2013-06-30 19:57:26 +00:00
|
|
|
|
|
2013-08-09 03:58:44 +00:00
|
|
|
|
if (showingAll) {
|
|
|
|
|
this.ui.loadMore.hide();
|
|
|
|
|
this.ui.searchBar.show();
|
|
|
|
|
}
|
|
|
|
|
},
|
2013-06-30 19:57:26 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
initialize: function (options) {
|
2013-06-30 19:57:26 +00:00
|
|
|
|
this.collection = new AddSeriesCollection({unmappedFolderModel: this.model});
|
2013-06-22 00:48:23 +00:00
|
|
|
|
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';
|
|
|
|
|
}
|
2013-09-18 00:54:57 +00:00
|
|
|
|
|
|
|
|
|
this.listenTo(this.collection, 'sync', this._showResults);
|
2013-06-30 19:57:26 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_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 () {
|
2013-02-14 02:28:56 +00:00
|
|
|
|
window.clearTimeout(self.$el.data('timeout'));
|
2013-06-22 00:48:23 +00:00
|
|
|
|
self.$el.data('timeout', window.setTimeout(function () {
|
|
|
|
|
self.search.call(self, {
|
|
|
|
|
term: self.ui.seriesSearch.val()
|
|
|
|
|
});
|
|
|
|
|
}, 500));
|
2013-02-14 02:28:56 +00:00
|
|
|
|
});
|
2013-01-23 05:48:22 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
if (this.isExisting) {
|
|
|
|
|
this.ui.searchBar.hide();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 19:57:26 +00:00
|
|
|
|
this.resultCollectionView = new SearchResultCollectionView({
|
|
|
|
|
collection: this.collection,
|
2013-06-22 00:48:23 +00:00
|
|
|
|
isExisting: this.isExisting
|
|
|
|
|
});
|
2013-06-21 01:43:58 +00:00
|
|
|
|
},
|
2013-01-27 02:14:42 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
search: function (options) {
|
|
|
|
|
|
2013-06-30 19:57:26 +00:00
|
|
|
|
this.abortExistingSearch();
|
2013-02-14 02:28:56 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
this.collection.reset();
|
2013-02-01 03:15:19 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
if (!options || options.term === '') {
|
|
|
|
|
this.searchResult.close();
|
2013-06-21 01:43:58 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2013-08-23 01:52:19 +00:00
|
|
|
|
this.searchResult.show(new LoadingView());
|
2013-09-18 00:54:57 +00:00
|
|
|
|
this.collection.term = options.term;
|
2013-06-30 19:57:26 +00:00
|
|
|
|
this.currentSearchPromise = this.collection.fetch({
|
2013-06-22 00:48:23 +00:00
|
|
|
|
data: { term: options.term }
|
2013-09-18 00:54:57 +00:00
|
|
|
|
});
|
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-02-14 02:28:56 +00:00
|
|
|
|
|
2013-09-18 00:54:57 +00: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-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-02-14 02:28:56 +00:00
|
|
|
|
}
|
2013-06-21 01:43:58 +00:00
|
|
|
|
});
|
2013-02-14 02:28:56 +00:00
|
|
|
|
});
|