2013-03-29 19:17:03 +00:00
|
|
|
|
"use strict";
|
2013-06-21 01:43:58 +00:00
|
|
|
|
define(
|
|
|
|
|
[
|
|
|
|
|
'marionette',
|
|
|
|
|
'AddSeries/Collection',
|
|
|
|
|
'AddSeries/SearchResultCollectionView',
|
|
|
|
|
'Shared/SpinnerView',
|
|
|
|
|
'app',
|
|
|
|
|
'AddSeries/RootFolders/Collection',
|
|
|
|
|
'AddSeries/SearchResultView',
|
|
|
|
|
'Shared/SpinnerView'
|
|
|
|
|
], function (Marionette, AddSeriesCollection, SearchResultCollectionView, SpinnerView) {
|
|
|
|
|
return Marionette.Layout.extend({
|
|
|
|
|
template: 'AddSeries/AddSeriesTemplate',
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
regions: {
|
|
|
|
|
searchResult: '#search-result'
|
|
|
|
|
},
|
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
initialize: function (options) {
|
2013-06-21 01:43:58 +00:00
|
|
|
|
this.collection = new AddSeriesCollection();
|
2013-06-22 00:48:23 +00:00
|
|
|
|
this.isExisting = options.isExisting;
|
2013-06-21 01:43:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onRender: function () {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.resultView = new SearchResultCollectionView({
|
|
|
|
|
fullResult: this.collection,
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
2013-01-27 02:14:42 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
this.abortExistingRequest();
|
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-06-22 00:48:23 +00:00
|
|
|
|
this.searchResult.show(new SpinnerView());
|
|
|
|
|
this.currentSearchRequest = this.collection.fetch({
|
|
|
|
|
data: { term: options.term }
|
|
|
|
|
}).done(function () {
|
|
|
|
|
self.searchResult.show(self.resultView);
|
2013-01-27 02:14:42 +00:00
|
|
|
|
|
2013-06-22 00:48:23 +00:00
|
|
|
|
if (!self.showingAll && self.isExisting) {
|
|
|
|
|
self.ui.loadMore.show();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-06-21 01:43:58 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2013-02-14 02:28:56 +00:00
|
|
|
|
|
2013-06-21 01:43:58 +00:00
|
|
|
|
abortExistingRequest: function () {
|
|
|
|
|
if (this.currentSearchRequest && this.currentSearchRequest.readyState > 0 && this.currentSearchRequest.readyState < 4) {
|
|
|
|
|
console.log('aborting previous pending search request.');
|
|
|
|
|
this.currentSearchRequest.abort();
|
|
|
|
|
}
|
2013-02-14 02:28:56 +00:00
|
|
|
|
}
|
2013-06-21 01:43:58 +00:00
|
|
|
|
});
|
2013-02-14 02:28:56 +00:00
|
|
|
|
});
|