-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathconnection-test.js
83 lines (73 loc) · 3.13 KB
/
connection-test.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
angular
.module('nzbhydraApp')
.directive('connectionTest', connectionTest);
function connectionTest() {
return {
templateUrl: 'static/html/directives/connection-test.html',
require: ['^type', '^data'],
scope: {
type: "=",
id: "=",
data: "=",
downloader: "="
},
controller: controller
};
function controller($scope) {
$scope.message = "";
var testButton = "#button-test-connection";
var testMessage = "#message-test-connection";
function showSuccess() {
angular.element(testButton).removeClass("btn-default");
angular.element(testButton).removeClass("btn-danger");
angular.element(testButton).addClass("btn-success");
}
function showError() {
angular.element(testButton).removeClass("btn-default");
angular.element(testButton).removeClass("btn-success");
angular.element(testButton).addClass("btn-danger");
}
$scope.testConnection = function () {
angular.element(testButton).addClass("glyphicon-refresh-animate");
var myInjector = angular.injector(["ng"]);
var $http = myInjector.get("$http");
var url;
var params;
if ($scope.type === "downloader") {
url = "internalapi/test_downloader";
params = {name: $scope.downloader, username: $scope.data.username, password: $scope.data.password};
if ($scope.downloader === "NZBGET") {
params.host = $scope.data.host;
params.port = $scope.data.port;
params.ssl = $scope.data.ssl;
} else {
params.apiKey = $scope.data.apiKey;
params.url = $scope.data.url;
}
} else if ($scope.data.type === "newznab") {
url = "internalapi/test_newznab";
params = {host: $scope.data.host, apiKey: $scope.data.apiKey};
if (angular.isDefined($scope.data.username)) {
params["username"] = $scope.data.username;
params["password"] = $scope.data.password;
}
}
$http.get(url, {params: params}).then(function (result) {
//Using ng-class and a scope variable doesn't work for some reason, is only updated at second click
if (result.successful) {
angular.element(testMessage).text("");
showSuccess();
} else {
angular.element(testMessage).text(result.message);
showError();
}
}, function () {
angular.element(testMessage).text(result.message);
showError();
}
).finally(function () {
angular.element(testButton).removeClass("glyphicon-refresh-animate");
})
}
}
}