-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathendpoints.js
104 lines (87 loc) · 2.83 KB
/
endpoints.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
// saves endpoints to local storage
cdbmanager.service('endpoints', ["$localStorage", function ($localStorage) {
let self = this;
$localStorage.endpoints = $localStorage.endpoints || [];
if ($localStorage.endpoints) {
this.current = $localStorage.endpoints[0];
} else {
this.current = null;
}
this.all = function () {
return $localStorage.endpoints;
};
this.add = function (endpoint) {
$localStorage.endpoints.push(angular.copy(endpoint));
self.current = $localStorage.endpoints[$localStorage.endpoints.length - 1];
};
this.update = function (endpoint, properties) {
for (let propertyName in properties) {
if (properties.hasOwnProperty(propertyName)) {
endpoint[propertyName] = properties[propertyName];
}
}
};
this.remove = function (endpoint) {
$localStorage.endpoints = $localStorage.endpoints.filter(function (_endpoint) {
return endpoint != _endpoint;
});
if (self.current == endpoint) {
if ($localStorage.endpoints) {
self.current = $localStorage.endpoints[0];
} else {
self.current = null;
}
}
};
this.setCurrent = function (endpoint) {
self.current = endpoint;
};
}]);
cdbmanager.controller('titleCtrl', ["$scope", "endpoints", function ($scope, endpoints) {
// keep the endpoint list in the title always updated
$scope.$watch(function () {
return endpoints.current;
}, function (currentEndpoint) {
$scope.currentEndpoint = currentEndpoint;
});
}]);
cdbmanager.controller('endpointsCtrl', ["$scope", "endpoints", "alerts", function ($scope, endpoints, alerts) {
$scope.updatedEndpoint = {};
// keep the endpoint list in the scope always updated
$scope.$watch(function () {
return endpoints.all();
}, function (endpoints) {
$scope.endpoints = endpoints;
});
// keep the current endpoint and form in the scope always updated
$scope.$watch(function () {
return endpoints.current;
}, function (endpoint) {
alerts.close();
$scope.current = endpoint;
$scope.updatedEndpoint = angular.copy($scope.current);
});
$scope.addEndpoint = function () {
endpoints.add({
name: "New"
});
};
$scope.$watch("updatedEndpoint.account", function (accountName) {
if (accountName) {
$scope.updatedEndpoint.sqlURL = "https://" + accountName + ".carto.com/api/v2/sql";
$scope.updatedEndpoint.mapsURL = "https://" + accountName + ".carto.com/api/v2/map";
} else {
$scope.updatedEndpoint.sqlURL = "";
$scope.updatedEndpoint.mapsURL = "";
}
});
$scope.removeCurrentEndpoint = function () {
endpoints.remove($scope.current);
};
$scope.updateCurrentEndpoint = function (updatedEndpoint) {
endpoints.update($scope.current, updatedEndpoint);
};
$scope.setCurrent = function () {
endpoints.setCurrent($scope.current);
};
}]);