-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsearch-controller.js
487 lines (424 loc) · 19.8 KB
/
search-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
angular
.module('nzbhydraApp')
.controller('SearchController', SearchController);
function SearchController($scope, $http, $stateParams, $state, $uibModal, $timeout, $sce, growl, SearchService, focus, ConfigService, HydraAuthService, CategoriesService, $element, SearchHistoryService) {
function getNumberOrUndefined(number) {
if (_.isUndefined(number) || _.isNaN(number) || number === "") {
return undefined;
}
number = parseInt(number);
if (_.isNumber(number)) {
return number;
} else {
return undefined;
}
}
var searchRequestId = 0;
var isSearchCancelled = false;
var epochEnter;
//Fill the form with the search values we got from the state params (so that their values are the same as in the current url)
$scope.mode = $stateParams.mode;
$scope.query = "";
$scope.selectedItem = null;
$scope.categories = _.filter(CategoriesService.getAllCategories(), function (c) {
return c.mayBeSelected && !(c.ignoreResultsFrom === "INTERNAL" || c.ignoreResultsFrom === "BOTH");
});
$scope.minsize = getNumberOrUndefined($stateParams.minsize);
$scope.maxsize = getNumberOrUndefined($stateParams.maxsize);
if (angular.isDefined($stateParams.category) && $stateParams.category) {
$scope.category = CategoriesService.getByName($stateParams.category);
} else {
$scope.category = CategoriesService.getDefault();
$scope.minsize = $scope.category.minSizePreset;
$scope.maxsize = $scope.category.maxSizePreset;
}
$scope.category = _.isNullOrEmpty($stateParams.category) ? CategoriesService.getDefault() : CategoriesService.getByName($stateParams.category);
$scope.season = $stateParams.season;
$scope.episode = $stateParams.episode;
$scope.query = $stateParams.query;
$scope.minage = getNumberOrUndefined($stateParams.minage);
$scope.maxage = getNumberOrUndefined($stateParams.maxage);
if (angular.isDefined($stateParams.indexers)) {
$scope.indexers = decodeURIComponent($stateParams.indexers).split(",");
}
if (angular.isDefined($stateParams.title) || (angular.isDefined($stateParams.tmdbId) || angular.isDefined($stateParams.imdbId) || angular.isDefined($stateParams.tvmazeId) || angular.isDefined($stateParams.rid) || angular.isDefined($stateParams.tvdbId))) {
var width = calculateWidth($stateParams.title) + 30;
$scope.selectedItemWidth = width + "px";
$scope.selectedItem = {
tmdbId: $stateParams.tmdbId,
imdbId: $stateParams.imdbId,
tvmazeId: $stateParams.tvmazeId,
rid: $stateParams.rid,
tvdbId: $stateParams.tvdbId,
title: $stateParams.title
}
}
$scope.showIndexers = {};
$scope.searchHistory = [];
var safeConfig = ConfigService.getSafe();
$scope.showIndexerSelection = HydraAuthService.getUserInfos().showIndexerSelection;
$scope.typeAheadWait = 300;
$scope.autocompleteLoading = false;
$scope.isAskById = $scope.category.searchType === "TVSEARCH" || $scope.category.searchType === "MOVIE";
$scope.isById = {value: $scope.selectedItem !== null || angular.isUndefined($scope.mode) || $scope.mode === null}; //If true the user wants to search by id so we enable autosearch. Was unable to achieve this using a simple boolean. Set to false if last search was not by ID
$scope.availableIndexers = [];
$scope.selectedIndexers = [];
$scope.autocompleteClass = "autocompletePosterMovies";
$scope.toggleCategory = function (searchCategory) {
var oldCategory = $scope.category;
$scope.category = searchCategory;
//Show checkbox to ask if the user wants to search by ID (using autocomplete)
if ($scope.category.searchType === "TVSEARCH" || $scope.category.searchType === "MOVIE") {
$scope.isAskById = true;
$scope.isById.value = true;
} else {
$scope.isAskById = false;
$scope.isById.value = false;
}
if (oldCategory.searchType !== searchCategory.searchType) {
$scope.selectedItem = null;
}
focus('searchfield');
//Hacky way of triggering the autocomplete loading
var searchModel = $element.find("#searchfield").controller("ngModel");
if (angular.isDefined(searchModel.$viewValue)) {
searchModel.$setViewValue(searchModel.$viewValue + " ");
}
if (safeConfig.categoriesConfig.enableCategorySizes) {
var min = searchCategory.minSizePreset;
var max = searchCategory.maxSizePreset;
if (_.isNumber(min)) {
$scope.minsize = min;
} else {
$scope.minsize = "";
}
if (_.isNumber(max)) {
$scope.maxsize = max;
} else {
$scope.maxsize = "";
}
}
$scope.availableIndexers = getAvailableIndexers();
};
// Any function returning a promise object can be used to load values asynchronously
$scope.getAutocomplete = function (val) {
$scope.autocompleteLoading = true;
//Expected model returned from API:
//label: What to show in the results
//title: Will be used for file search
//value: Will be used as extraInfo (ttid oder tvdb id)
//poster: url of poster to show
//Don't use autocomplete if checkbox is disabled
if (!$scope.isById.value || $scope.selectedItem) {
return {};
}
if ($scope.category.searchType === "MOVIE") {
return $http.get('internalapi/autocomplete/MOVIE', {params: {input: val}}).then(function (response) {
$scope.autocompleteLoading = false;
return response.data;
});
} else if ($scope.category.searchType === "TVSEARCH") {
return $http.get('internalapi/autocomplete/TV', {params: {input: val}}).then(function (response) {
$scope.autocompleteLoading = false;
return response.data;
});
} else {
return {};
}
};
$scope.onTypeAheadEnter = function () {
if (angular.isDefined(epochEnter)) {
//Very hacky way of preventing a press of "enter" to select an autocomplete item from triggering a search
//This is called *after* selectAutoComplete() is called
var epochEnterNow = (new Date).getTime();
var diff = epochEnterNow - epochEnter;
if (diff > 50) {
$scope.initiateSearch();
}
} else {
$scope.initiateSearch();
}
};
$scope.onTypeAheadKeyDown = function (event) {
if (event.keyCode === 8) {
if ($scope.query === "") {
$scope.clearAutocomplete();
}
}
};
$scope.onDropOnQueryInput = function (event) {
if ($scope.searchHistoryDragged === null || $scope.searchHistoryDragged === undefined) {
return;
}
$scope.category = CategoriesService.getByName($scope.searchHistoryDragged.categoryName);
$scope.season = $scope.searchHistoryDragged.season;
$scope.episode = $scope.searchHistoryDragged.episode;
$scope.query = $scope.searchHistoryDragged.query;
if ($scope.searchHistoryDragged.title != null) {
var width = calculateWidth($scope.searchHistoryDragged.title) + 30;
$scope.selectedItemWidth = width + "px";
}
var tvmaze = _.findWhere($scope.searchHistoryDragged.identifiers, {identifierKey: "TVMAZE"});
var tmdb = _.findWhere($scope.searchHistoryDragged.identifiers, {identifierKey: "TMDB"});
var imdb = _.findWhere($scope.searchHistoryDragged.identifiers, {identifierKey: "IMDB"});
var tvdb = _.findWhere($scope.searchHistoryDragged.identifiers, {identifierKey: "TVDB"});
$scope.selectedItem = {
tmdbId: tmdb === undefined ? null : tmdb.identifierValue,
imdbId: imdb === undefined ? null : imdb.identifierValue,
tvmazeId: tvmaze === undefined ? null : tvmaze.identifierValue,
tvdbId: tvdb === undefined ? null : tvdb.identifierValue,
title: $scope.searchHistoryDragged.title
}
event.preventDefault();
$scope.searchHistoryDragged = null;
focus('searchfield');
$scope.status.isopen = false;
}
$scope.$on("searchHistoryDrag", function (event, data) {
$scope.searchHistoryDragged = JSON.parse(data);
})
//Is called when the search page is opened with params, either because the user initiated the search (which triggered a goTo to this page) or because a search URL was entered
$scope.startSearch = function () {
isSearchCancelled = false;
searchRequestId = Math.round(Math.random() * 99999);
var modalInstance = $scope.openModal(searchRequestId);
var indexers = angular.isUndefined($scope.indexers) ? undefined : $scope.indexers.join(",");
SearchService.search(searchRequestId, $scope.category.name, $scope.query, $scope.selectedItem, $scope.season, $scope.episode, $scope.minsize, $scope.maxsize, $scope.minage, $scope.maxage, indexers, $scope.mode).then(function () {
//modalInstance.close();
SearchService.setModalInstance(modalInstance);
if (!isSearchCancelled) {
$state.go("root.search.results", {
minsize: $scope.minsize,
maxsize: $scope.maxsize,
minage: $scope.minage,
maxage: $scope.maxage
}, {
inherit: true
});
}
},
function () {
modalInstance.close();
});
};
$scope.openModal = function openModal(searchRequestId) {
return $uibModal.open({
templateUrl: 'static/html/search-state.html',
controller: SearchUpdateModalInstanceCtrl,
size: "md",
backdrop: "static",
backdropClass: "waiting-cursor",
resolve: {
searchRequestId: function () {
return searchRequestId;
},
onCancel: function () {
function cancel() {
isSearchCancelled = true;
}
return cancel;
}
}
});
};
$scope.goToSearchUrl = function () {
//State params (query parameters) should all be lowercase
var stateParams = {};
stateParams.mode = $scope.category.searchType.toLowerCase();
stateParams.imdbId = $scope.selectedItem === null ? null : $scope.selectedItem.imdbId;
stateParams.tmdbId = $scope.selectedItem === null ? null : $scope.selectedItem.tmdbId;
stateParams.tvdbId = $scope.selectedItem === null ? null : $scope.selectedItem.tvdbId;
stateParams.tvrageId = $scope.selectedItem === null ? null : $scope.selectedItem.tvrageId;
stateParams.tvmazeId = $scope.selectedItem === null ? null : $scope.selectedItem.tvmazeId;
stateParams.title = $scope.selectedItem === null ? null : $scope.selectedItem.title;
stateParams.season = $scope.season;
stateParams.episode = $scope.episode;
stateParams.query = $scope.query;
stateParams.minsize = $scope.minsize;
stateParams.maxsize = $scope.maxsize;
stateParams.minage = $scope.minage;
stateParams.maxage = $scope.maxage;
stateParams.category = $scope.category.name;
stateParams.indexers = encodeURIComponent($scope.selectedIndexers.join(","));
$state.go("root.search", stateParams, {inherit: false, notify: true, reload: true});
};
$scope.repeatSearch = function (request) {
var stateParams = SearchHistoryService.getStateParamsForRepeatedSearch(request);
stateParams.indexers = encodeURIComponent($scope.selectedIndexers.join(","));
$state.go("root.search", stateParams, {inherit: false, notify: true, reload: true});
};
$scope.searchBoxTooltip = "Prefix terms with -- to exclude'";
$scope.$watchGroup(['isAskById', 'selectedItem'], function () {
if (!$scope.isAskById) {
$scope.searchBoxTooltip = "Prefix terms with -- to exclude";
} else if ($scope.selectedItem === null) {
$scope.searchBoxTooltip = "Enter search terms for autocomplete";
} else {
$scope.searchBoxTooltip = "Enter additional search terms to limit the query";
}
});
$scope.clearAutocomplete = function () {
$scope.selectedItem = null;
$scope.query = ""; //Input is now for autocomplete and not for limiting the results
focus('searchfield');
};
$scope.clearQuery = function () {
$scope.selectedItem = null;
$scope.query = "";
focus('searchfield');
};
function calculateWidth(text) {
var canvas = calculateWidth.canvas || (calculateWidth.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = "13px Roboto";
return context.measureText(text).width;
}
$scope.selectAutocompleteItem = function ($item) {
$scope.selectedItem = $item;
$scope.query = "";
epochEnter = (new Date).getTime();
var width = calculateWidth($item.title) + 30;
$scope.selectedItemWidth = width + "px";
};
$scope.initiateSearch = function () {
if ($scope.selectedIndexers.length === 0) {
growl.error("You didn't select any indexers");
return;
}
if ($scope.selectedItem) {
//Movie or tv show was selected
$scope.goToSearchUrl();
} else {
//Simple query search
$scope.goToSearchUrl();
}
};
$scope.autocompleteActive = function () {
return $scope.isAskById;
};
$scope.seriesSelected = function () {
return $scope.category.searchType === "TVSEARCH";
};
$scope.toggleIndexer = function (indexer) {
$scope.availableIndexers[indexer.name].activated = !$scope.availableIndexers[indexer.name].activated;
};
function isIndexerPreselected(indexer) {
if (angular.isUndefined($scope.indexers)) {
return indexer.preselect;
} else {
return _.contains($scope.indexers, indexer.name);
}
}
function getAvailableIndexers() {
var alreadySelected = $scope.selectedIndexers;
var previouslyAvailable = _.pluck($scope.availableIndexers, "name");
$scope.selectedIndexers = [];
var availableIndexersList = _.chain(safeConfig.indexers).filter(function (indexer) {
if (!indexer.showOnSearch) {
return false;
}
var categorySelectedForIndexer = (angular.isUndefined(indexer.categories) || indexer.categories.length === 0 || $scope.category.name.toLowerCase() === "all" || indexer.categories.indexOf($scope.category.name) > -1);
return categorySelectedForIndexer;
}).sortBy(function (indexer) {
return indexer.name.toLowerCase();
})
.map(function (indexer) {
return {
name: indexer.name,
activated: isIndexerPreselected(indexer),
preselect: indexer.preselect,
categories: indexer.categories,
searchModuleType: indexer.searchModuleType
};
}).value();
_.forEach(availableIndexersList, function (x) {
var deselectedBefore = (_.indexOf(previouslyAvailable, x.name) > -1 && _.indexOf(alreadySelected, x.name) === -1);
var selectedBefore = (_.indexOf(previouslyAvailable, x.name) > -1 && _.indexOf(alreadySelected, x.name) > -1);
if ((x.activated && !deselectedBefore) || selectedBefore) {
$scope.selectedIndexers.push(x.name);
}
});
return availableIndexersList;
}
$scope.formatRequest = function (request) {
return $sce.trustAsHtml(SearchHistoryService.formatRequest(request, false, true, true, true));
};
$scope.availableIndexers = getAvailableIndexers();
function getAndSetSearchRequests() {
SearchHistoryService.getSearchHistoryForSearching().then(function (response) {
$scope.searchHistory = response.searchRequests;
});
}
if ($scope.mode) {
$scope.startSearch();
} else {
//Getting the search history only makes sense when we're not currently searching
_.defer(getAndSetSearchRequests);
}
$scope.$on("searchResultsShown", function () {
_.defer(getAndSetSearchRequests); //Defer because otherwise the results are only shown when this returns which may take a while with big databases
});
}
angular
.module('nzbhydraApp')
.controller('SearchUpdateModalInstanceCtrl', SearchUpdateModalInstanceCtrl);
function SearchUpdateModalInstanceCtrl($scope, $interval, SearchService, $uibModalInstance, searchRequestId, onCancel, bootstrapped) {
var loggedSearchFinished = false;
$scope.messages = [];
$scope.indexerSelectionFinished = false;
$scope.indexersSelected = 0;
$scope.indexersFinished = 0;
$scope.buttonText = "Cancel";
$scope.buttonTooltip = "Cancel search and return to search mask";
$scope.btnType = "btn-danger";
var socket = new SockJS(bootstrapped.baseUrl + 'websocket');
var stompClient = Stomp.over(socket);
stompClient.debug = null;
stompClient.connect({}, function (frame) {
stompClient.subscribe('/topic/searchState', function (message) {
var data = JSON.parse(message.body);
if (searchRequestId !== data.searchRequestId) {
return;
}
$scope.searchFinished = data.searchFinished;
$scope.indexersSelected = data.indexersSelected;
$scope.indexersFinished = data.indexersFinished;
$scope.progressMax = data.indexersSelected;
if ($scope.progressMax > data.indexersSelected) {
$scope.progressMax = ">=" + data.indexersSelected;
}
if ($scope.indexersFinished > 0) {
$scope.buttonText = "Show results";
$scope.buttonTooltip = "Show results that have already been loaded";
$scope.btnType = "btn-warning";
}
if (data.messages) {
$scope.messages = data.messages;
}
if ($scope.searchFinished && !loggedSearchFinished) {
$scope.messages.push("Finished searching. Preparing results...");
loggedSearchFinished = true;
}
});
});
$scope.shortcutSearch = function () {
SearchService.shortcutSearch(searchRequestId);
// onCancel();
// $uibModalInstance.dismiss();
};
$scope.hasResults = function (message) {
return /^[^0]\d+.*/.test(message);
};
}
angular
.module('nzbhydraApp').directive('draggable', ['$rootScope', function ($rootScope) {
return {
restrict: 'A',
link: function (scope, el, attrs, controller) {
el.bind("dragstart", function (e) {
$rootScope.$emit("searchHistoryDrag", el.attr("data-request"));
$rootScope.$broadcast("searchHistoryDrag", el.attr("data-request"));
});
}
}
}]);